《C语言必背个经典程序》由会员分享,可在线阅读,更多相关《C语言必背个经典程序(11页珍藏版)》请在人人文库网上搜索。

1、C语言必背18个经典程序1、 /*输出9*9 口诀。共9行9列,i控制行,j控制列。*/#include stdio.hmain()int i,j,result;for (i=1;i#include main() float a45,sum1,sum2;int i,j;for(i=0;imain() char c200,c1;int i,j,k;printf(Enter a string: );scanf(%s,c);k=strlen(c);for (i=0,j=k-1;imain() char s80,c;int j,k; printf(nEnter a string: );gets(s);。

2、 printf(nEnter a character: );c=getchar( ); for(j=k=0;sj!= 0;j+) if(sj!=c)sk+=sj;sk= 0;printf(n%s,s);10、/* 编写一个 void sort(int *x,int n)实现将 x 数组中的 n 个数据从大到小排序。 n 及数组元素在主函数中输入。将结果显示在屏幕上并输出到文件p9_1.out 中 */#includevoid sort(int *x,int n)int i,j,k,t;for(i=0;ixk) k=j;if(k!=i)t=xi;xi=xk;xk=t;void main()FIL。

3、E *fp;int *p,i,a10;fp=fopen(p9_1.out,w);p=a;printf(Input 10 numbers:);for(i=0;ix) ai+1=ai; i-; /* a+i=x;j+; /* for(i=1;ireplace(char *s,char c1,char c2) while(*s!=0) if (*s=c1)*s=c2;s+;main() FILE *fp;char str100,a,b;if(fp=fopen(p10_2.out,w)=NULL) printf(cannot open the filen); exit(0); printf(Enter。

4、 a string:n);gets(str);printf(Enter a&b:n);scanf(%c,%c,&a,&b);printf(%sn,str);fprintf(fp,%sn,str);replace(str,a,b);printf(The new string is%sn,str);fprintf(fp,The new string is%sn,str);fclose(fp);13、 /*在一个字串si中查找一子串s2,若存在则返回子串在主串中的起始位置 ,不存在则返回 -1 。 */main()char s16=thisis;char s25=is;printf(%dn,sear。

5、ch(s1,s2);system(pause);int search(char s1,char s2)int i=0,j,len=strlen(s2);while(s1i)for(j=0;j=len)return i;else i+;return -1;14、/* 用指针变量输出结构体数组元素。 */struct studentint num;char *name;char sex;int age;stu5=1001,lihua,F,18,1002,liuxing,M,19,1003,huangke,F ,19,1004,fengshou,F,19,1005,Wangming,M,18;mai。

6、n()int i;struct student *ps;printf(Num tNametttSextAgetn);/* 用指针变量输出结构体数组元素。 */ for(ps=stu;psnum,ps-name,ps-sex,ps-age);/* 用数组下标法输出结构体数组元素学号和年龄。for(i=0;inum,p-name,p-age); p=p-next;while(p!=NULL);16、/* 输入一个字符串,判断其是否为回文。回文字符串是指从左到右读和从右到左 读完全相同的字符串。 */#include #include #includemain() char s100;int i,j。

7、,n;printf( 输入字符串: n);gets(s);n=strlen(s);for(i=0,j=n-1;i=j) printf(是回文串 n);else printf(不是回文串 n);17、/*冒泡排序,从小到大,排序后结果输出到屏幕及文件myf2.out*/#in cludevoid fun (i nt a,i nt n)int i,j,t;for(i=0;iaj+1) t=aj;aj=aj+1;aj+1=t;main ()int a10=12,45,7,8,96,4,10,48,2,46,n=10,i;FILE *f;if(f=fope n(myf2.out,w)=NULL)pri。

8、n tf(open file myf2.out failed! n);fun( a,10);for(i=0;iA+4 - 9X3 - 7认为达到精度要求,请完善函数。将结果double coun tpi(double eps)/*epsint m=1;double temp=1.0,s=0; while(temp=eps) s+=temp;temp=temp*m/(2*m+1);m+;return(2*s);main ()FILE *fp;double eps=1e-5,pi;if(fp=fope n(p7_3.out,w)=NULL) prin tf(ca nnot ope n the file n); exit(0);为允许误差*/pi= countpi(eps); printf(pi=%lfn,pi); fprintf(fp,pi=%lfn,pi); fclose(fp。

更多推荐

C语言,5个经典程序,C语言必背个经典程序