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

1、c 语言入门经典必背 18 个程序1 、 /* 输出 9*9 口诀。共 9 行 9 列, i 控制行, j 控制列。 */#include stdio.h main()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;i main() char s80,c; int j,k;printf(nEn。

2、ter a string: ); gets(s);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。

3、)t=xi; xi=xk;xk=t;void main()FILE *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-; /* 将比 x 大的数往后移动一个位置 */a+i=x;j+; /* 插入 x 后元素总个数增加 */ 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=fope。

4、n(p10_2.out,w)=NULL) printf(cannot open the filen); exit(0); printf(Enter 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 、 /* 在一个字串 s1 中查找一子串 s2 。

5、,若存在则返回子串在主串中的起始位置,不存在则返回 -1 。 */ main()char s16=thisis;char s25=is;printf(%dn,search(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=100。

6、1,lihua,F,18,1002,liuxing,M,19,1003,huangke,F,19,1004,fengshou,F,19,1005,Wangming,M, 18;main()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 、 /* 输入。

7、一个字符串,判断其是否为回文。回文字符串是指从左到右读和从右到左读完全相同的字符串。 */#include #include #include main() char s100;int i,j,n;printf( 输入字符串: n);gets(s); n=strlen(s);for(i=0,j=n-1;i=j) printf( 是回文串 n); else printf( 不是回文串 n);17 、 /* 冒泡排序,从小到大,排序后结果输出到屏幕及文件 myf2 .out*/#includevoid fun(int a,int n)int i,j,t;for(i= 0 ;iaj+1) t=aj;。

8、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=fopen(myf2 .out ,w)=NULL) printf(open file myf2.out failed!n); fun(a,10);for(i=0;idouble countpi(double eps) /*eps 为允许误差 */int 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 。

9、*fp;double eps=1e-5,pi; if(fp=fopen(p7_3.out,w)=NULL) printf(cannot open the filen); exit(0);pi= countpi(eps); printf(pi=%lfn,pi); fprintf(fp,pi=%lfn,pi); fclose(fp);“”“”At the end, Xiao Bian gives you a passage. Minand once said, people who learn to learn are very happy people. In every wonderful l。

10、ife, learning is an eternal theme. As a professional clerical and teaching position, I understand the importance of continuous learning, life is diligent, nothing can be gained, only continuous learning can achieve better self. Only by constantly learning and mastering the latest relevant knowledge, can employees from all walks of life keep up with the pace of enterprise development and innovate to meet the needs of the market. This document is also edited by my studio professionals, there may be errors in the document, if there are errors, please correct, thank you。

更多推荐

c语言必背18个程序,C++必背18个经典程序(最新整理)