在C code无重复 [英] no duplication in a C code
本文介绍了在C code无重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道为什么C code工作没有任何重复的 P
,因为它也将被更新,但如果我把W¯¯
在 ind_w [I]
它需要重复?
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;
#定义MAX 132
#定义MAXW 30
INT Len_w [MAXW]
INT Max_V(INT VF [],INT LEN);
主要()
{
个char [MAX + 1],W [MAXW] * Ind_w [MAXW],* P,出[MAXW]
INT K = 0,I = 0,最大值,G = 0;
P = S;
的printf(\\ n插入行.... \\ n);
与fgets(S,MAX,标准输入); 而(sscanf的(P,%s%N,W&安培; K)== 1){
Len_w [I] = strlen的(重量);
Ind_w [I] = P;
P + = K;
我++;
}
最大= Max_V(Len_w,1 + 1); 对于(G = 0; G< = I; G ++){
如果(Len_w并[g] ==最大){
sscanf的(Ind_w [G],%S,出来);
的printf(\\ N%的,出来);
}
}
返回0;
}
/ * ------------------------------------------------ ---------------------------- * /
INT Max_V(INT VF [],INT LEN)
{
诠释J; INT最大= 0;
MAX = VF [0];
为(J = 0; J< LEN; J ++)
{
如果(VF [J]>最大)
{
MAX = VF [J]。
}
}
返回最大;
}
/ * ------------------------------------------------ ---------------------------- * /
解决方案
P = S;而(sscanf的(P,%s%N,W&安培; K)== 1){
Len_w [I] = strlen的(重量);
Ind_w [I] = P;
P + = K;
我++;
}
指针到起始位置,其中的sscanf
写扫描的字在每个呼叫的变化。这样的话都存储一前一后在是W
,并且其中字开始的位置(在阵列中取值
)存储在 Ind_w
。
如果您存储
Ind_w [I] = W;
您存储始终是相同的地址,即第一个字符
在 W的
。
I want to know why this C code works without any duplication for p
as it also gets updated, but if i put w
in ind_w[i]
it needs duplication?
#include<stdio.h>
#include<string.h>
#define MAX 132
#define MAXW 30
int Len_w[MAXW];
int Max_V(int vf[], int len);
main()
{
char s[MAX+1], w[MAXW], *Ind_w[MAXW],*p,out[MAXW];
int k=0, i=0, Maximum, g=0;
p=s;
printf("\nInsert the line....\n");
fgets(s,MAX,stdin);
while(sscanf(p,"%s%n",w,&k)==1){
Len_w[i] = strlen(w);
Ind_w[i] = p;
p+=k;
i++;
}
Maximum = Max_V(Len_w,i+1);
for(g=0;g<=i;g++){
if(Len_w[g] == Maximum){
sscanf(Ind_w[g],"%s",out);
printf("\n%s",out);
}
}
return 0;
}
/*----------------------------------------------------------------------------*/
int Max_V(int vf[], int len)
{
int j; int Max=0;
Max=vf[0];
for(j=0;j<len;j++)
{
if(vf[j]>Max)
{
Max=vf[j];
}
}
return Max;
}
/*----------------------------------------------------------------------------*/
解决方案
p=s;
while(sscanf(p,"%s%n",w,&k)==1){
Len_w[i] = strlen(w);
Ind_w[i] = p;
p+=k;
i++;
}
The pointer to the starting position where sscanf
writes the scanned word to changes in each call. so the words are stored one after the other in w
, and the positions where the words start (in the array s
) are stored in Ind_w
.
If you store
Ind_w[i] = w;
you store always the same address, that of the first char
in w
.
这篇关于在C code无重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文