c语言入门经典必背18个程序

上传人:suns****4568 文档编号:60837707 上传时间:2018-11-19 格式:PDF 页数:9 大小:139.60KB
返回 下载 相关 举报
c语言入门经典必背18个程序_第1页
第1页 / 共9页
c语言入门经典必背18个程序_第2页
第2页 / 共9页
c语言入门经典必背18个程序_第3页
第3页 / 共9页
c语言入门经典必背18个程序_第4页
第4页 / 共9页
c语言入门经典必背18个程序_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《c语言入门经典必背18个程序》由会员分享,可在线阅读,更多相关《c语言入门经典必背18个程序(9页珍藏版)》请在金锄头文库上搜索。

1、c语言入门经典必背18个程序.txt 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;i main() 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()

2、char s80,c; int j,k; printf(“nEnter 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 中 */ #include void sort(int *x

3、,int n) int i,j,k,t; for(i=0;ixk) k=j; if(k!=i) t=xi; xi=xk; xk=t; 第 4 页 c语言入门经典必背18个程序.txt 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;i replace(char *s,char c1

4、,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 a string:n“); 第 5 页 c语言入门经典必背18个程序.txt gets(str); printf(“Enter a scanf(“%c,%c“, printf(“%sn“,str);fprintf(fp,“%sn“,str); replace(

5、str,a,b); printf(“The new string is-%sn“,str); fprintf(fp,“The new string is-%sn“,str); fclose(fp); 13 、 /* 在一个字串 s1 中查找一子串 s2 ,若存在则返回子串在主串中的起始位置 ,不存在则返回 -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);

6、 while(s1i) for(j=0;j=len)return i; else i+; return -1; 14 、 /* 用指针变量输出结构体数组元素。 */ struct student int 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; main() int i; struct student *ps; printf(“Num tNamettt

7、SextAgetn“); /* 用指针变量输出结构体数组元素。 */ 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 #include main() char s100; int i,j,n; printf(“ 输入字符串: n“); gets(s);

8、 n=strlen(s); for(i=0,j=n-1;i=j) printf(“ 是回文串 n“); else printf(“ 不是回文串 n“); 17 、 /* 冒泡排序,从小到大,排序后结果输出到屏幕及文件 myf2 .out*/ #include void fun(int a,int 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=fopen(“myf2 .out “,“w“)=NULL) printf

9、(“open file myf2.out failed!n“); fun(a,10); for(i=0;i double 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 *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); 第 8 页 c语言入门经典必背18个程序.txt 第 9 页

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 其它文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号