189 8069 5689

c语言怎么定义筛选函数 c语言中的筛选法

c语言中数组的筛选、排序

第二个要求需要用另外的数组去存储所存数标记。

10年积累的成都网站制作、网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有八宿免费网站建设让你可以放心的选择与我们合作。

如定义一个数组A[11],然后判定上面要求筛选的数据的奇偶性,将标记存在新的数组中,需要时再判定数组听标记,然后输出所代表的标记的数据即可。

但是这样的话空间耗费比较大。

用c语言对文章进行筛选

主要是CalcWord和WriteWord 函数,仔细研究吧,别浪费我的心血:

#include stdio.h

#include conio.h

#include string.h

#include ctype.h

#include math.h

struct WORD_FOUND

{

char szWord[256];

int nCount;

};

WORD_FOUND g_WordFound[1000]={{"",0}};

int g_nMatch=0;

int CalcWord(char *szFile)

{

char szText[1024];

char szWord[512];

FILE * fp;

int i;

if ((fp=fopen(szFile,"r"))==NULL)

{

printf("file not found");

return -1;

}

while(fgets(szText,sizeof(szText)-1,fp))

{

char *tmp=szText,*end=szText+strlen(szText);

char *endChr;

while (tmpend)

{

while ((*tmp==' '||*tmp=='\n'||*tmp=='\t'||!isalpha(*tmp))*tmp) tmp++;

endChr=tmp;

while (*endChr*endChr!='\n'*endChr!='\r'*endChr!=' ')

{

if (!isalpha(abs(*endChr)))

break;

endChr++;

}

memset(szWord,0,sizeof(szWord));

strncpy(szWord,tmp,endChr-tmp);

bool bFound=false;

for (i=0;ig_nMatch;i++)

{

if (strcmp(g_WordFound[i].szWord,szWord)==0)

{

g_WordFound[i].nCount++;

bFound=true;

break;

}

}

if (!bFoundstrlen(szWord)0)

{

strcpy(g_WordFound[g_nMatch].szWord,szWord);

g_WordFound[g_nMatch].nCount++;

g_nMatch++;

}

tmp=endChr;

if (tmp=end) break;

}

}

return 0;

}

int WriteWord(char *szFile,int nAppearance)

{

int i;

FILE *file=fopen(szFile,"w");

if (!file) return -1;

for (i=0;ig_nMatch;i++)

{

if (g_WordFound[i].nCount=nAppearance)

{

fwrite(g_WordFound[i].szWord,1,strlen(g_WordFound[i].szWord),file);

fwrite("\n",1,1,file);

printf("%s = %d\n",g_WordFound[i].szWord,g_WordFound[i].nCount);

}

}

if (file) fclose(file);

return 0;

}

int main()

{

int i;

if (CalcWord("in.txt")!=0)

{

printf("Calculate word failed\n");

return 0;

}

WriteWord("out.txt",10);

for (i=0;ig_nMatch;i++)

{

printf("%s = %d\n",g_WordFound[i].szWord,g_WordFound[i].nCount);

}

getch();

return 0;

}

C语言中筛选法是怎么一回事,请知道的详细讲一下

问题1. 可以定义为a[100] 那么改为for(i=0;i=100;i++) //要给数组赋值先 目的是为了得到a[100]问题2. for(i=0;i101;i++) a[i]=i; 这是给数组a[100]赋值 在下面程序中有作用的问题3. 帮你改了一下 加了个语句 就解决了 你看看 #includestdio.h#includemath.hvoid main() { int i,j,k,count=0; int a[101]; for(i=0;i101;i++) a[i]=i; for(j=2;j101;j++) { for(k=2;k=sqrt(j);k++) { if(a[j]%k==0j!=2) { a[j]=0; } } if(a[j]!=0) { printf("%d ",j); count++; } if(count%10==0) printf("\n"); } }

筛选法的C语言实现筛选法程序步骤

先解释一下筛选法的步骤:

1 先将1挖掉(因为1不是素数)。

2 用2去除它后面的各个数,把能被2整除的数挖掉,即把2的倍数挖掉。

3 用3去除它后面的各数,把3的倍数挖掉。

4 分别用5…各数作为除数去除这些数以后的各数。

上述操作需要一个很大的容器去装载所有数的集合,只要满足上述条件,即2的倍数大于1的全部置0,3的倍数大于1的全部置0,4的倍数大于1的全部置0.。。。一直到这个数据集合的末尾,这样一来不为0的数就是素数了,然后按下标在里面进行查找就好了


文章题目:c语言怎么定义筛选函数 c语言中的筛选法
本文路径:http://cdxtjz.cn/article/ddjesde.html

其他资讯