C语言必背18个经典程序(可直接打印).doc

上传人:11****ws 文档编号:3737211 上传时间:2019-07-10 格式:DOC 页数:9 大小:53KB
下载 相关 举报
C语言必背18个经典程序(可直接打印).doc_第1页
第1页 / 共9页
C语言必背18个经典程序(可直接打印).doc_第2页
第2页 / 共9页
C语言必背18个经典程序(可直接打印).doc_第3页
第3页 / 共9页
C语言必背18个经典程序(可直接打印).doc_第4页
第4页 / 共9页
C语言必背18个经典程序(可直接打印).doc_第5页
第5页 / 共9页
点击查看更多>>
资源描述

1、C 语言必背 18 个经典程序11、/*输出 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;imain() char s80,c;int j,k;printf(“nEnter a st

2、ring: “);gets(s);C 语言必背 18 个经典程序5printf(“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

3、!=i) 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=fo

4、pen(“p10_2.out“,“w“)=NULL) printf(“cannot open the filen“);exit(0); printf(“Enter a string:n“);gets(str);printf(“Enter ascanf(“%c,%c“,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 中查找一子串 s

5、2,若存在则返回子串在主串中的起始位置,不存在则返回-1。*/C 语言必背 18 个经典程序7main() 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 student int num;char *name;char sex;

6、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 tNametttSextAgetn“); /*用指针变量输出结构体数组元素。 */for(ps=stu;psnum,ps-name,ps-sex,ps-age);/*用数组下标法输出结构体数组元素学号和年龄。 */for(i=0;inum,p-name,p-age);p=p-next;

7、while(p!=NULL);16、/*输入一个字符串,判断其是否为回文。回文字符串是指从左到右读和从右到左读完全相同的字符串。*/#include #include #includemain() 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=

8、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(“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 *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);

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

当前位置:首页 > 实用文档资料库 > 策划方案

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。