经典C语言100例

上传人:龙*** 文档编号:32615031 上传时间:2018-02-12 格式:DOC 页数:48 大小:165.50KB
返回 下载 相关 举报
经典C语言100例_第1页
第1页 / 共48页
经典C语言100例_第2页
第2页 / 共48页
经典C语言100例_第3页
第3页 / 共48页
经典C语言100例_第4页
第4页 / 共48页
经典C语言100例_第5页
第5页 / 共48页
点击查看更多>>
资源描述

《经典C语言100例》由会员分享,可在线阅读,更多相关《经典C语言100例(48页珍藏版)》请在金锄头文库上搜索。

1、= 【程序 17】 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 1.程序分析:利用 while 语句,条件为输入的字符不为n. 2.程序源代码: #include stdio.h main() char c; int letters=0,space=0,digit=0,others=0; printf(please input some charactersn); while(c=getchar()!=n) if(c=a&c=A&c=0&c0) x1=(x2+1)*2;/*第一天的桃子数是第 2 天桃子数加 1 后的 2 倍*/ x2=x1; day-; prin

2、tf(the total is %dn,x1); = 【程序 22】 题目:两个乒乓球队进行比赛,各出三人。甲队为 a,b,c 三人,乙队为 x,y,z 三人。已抽签决定 比赛名单。有人向队员打听比赛的名单。a 说他不和 x 比,c 说他不和 x,z 比,请编程序找出 三队赛手的名单。 1.程序分析:判断素数的方法:用一个数分别去除 2 到 sqrt(这个数) ,如果能被整除, 则表明此数不是素数,反之是素数。 2.程序源代码: main() char i,j,k;/*i 是 a 的对手,j 是 b 的对手,k 是 c 的对手*/ for(i=x;i void main() char lett

3、er; printf(please input the first letter of somedayn); while (letter=getch()!=Y)/*当所按字母为 Y 时才结束*/ switch (letter) case S:printf(please input second lettern); if(letter=getch()=a) printf(saturdayn); else if (letter=getch()=u) printf(sundayn); else printf(data errorn); break; case F:printf(fridayn);br

4、eak; case M:printf(mondayn);break; case T:printf(please input second lettern); if(letter=getch()=u) printf(tuesdayn); else if (letter=getch()=h) printf(thursdayn); else printf(data errorn); break; case W:printf(wednesdayn);break; default: printf(data errorn); = 【程序 32】 题目:Press any key to change col

5、or, do you want to try it. Please hurry up! 1.程序分析: 2.程序源代码: #include void main(void) int color; for (color = 0; color void main(void) clrscr();/*清屏函数 */ textbackground(2); gotoxy(1, 5);/*定位函数 */ cprintf(Output at row 5 column 1n); textbackground(3); gotoxy(20, 10); cprintf(Output at row 10 column 2

6、0n); = 【程序 34】 题目:练习函数调用 1. 程序分析: 2.程序源代码: #include void hello_world(void) printf(Hello, world!n); void three_hellos(void) int counter; for (counter = 1; counter void main(void) int color; for (color = 1; color #include math.h #define N 101 main() int i,j,line,aN; for(i=2;iaj) min=j; tem=ai; ai=amin

7、; amin=tem; /*output data*/ printf(After sorted n); for(i=0;iend) a10=number; else for(i=0;inumber) temp1=ai; ai=number; for(j=i+1;j4; c=(0=RIGHT) dx1=-dx1; if(y1=BOTTOM) dy1=-dy1; if(x2=RIGHT) dx2=-dx2; if(y2=BOTTOM) dy2=-dy2; if(+countLINES) setcolor(color); color=(color=MAXCOLOR)?0:+color; closeg

8、raph(); 12 回复:经典源程序 100 例 【程序 61】 题目:打印出杨辉三角形(要求打印出 10 行如下图) 1.程序分析: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10105 1 2.程序源代码: main() int i,j; int a1010; printf(n); for(i=0;in2) swap(pointer1,pointer2); if(n1n3) swap(pointer1,pointer3); if(n2n3) swap(pointer2,pointer3); printf(the sorted numbers are:%d,%d,

9、%dn,n1,n2,n3); swap(p1,p2) int *p1,*p2; int p; p=*p1;*p1=*p2;*p2=p; = 【程序 67】 题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。 1.程序分析:谭浩强的书中答案有问题。 2.程序源代码: main() int number10; input(number); max_min(number); output(number); input(number) int number10; int i; for(i=0;i*max) max=p; else if(*parray;p-) *p=*(p-1

10、); *array=array_end; m-; if(m0) move(array,n,m); = 【程序 69】 题目:有 n 个人围成一圈,顺序排号。从第一个人开始报数(从 1 到 3 报数) ,凡报到 3 的人退出 圈子,问最后留下的是原来第几号的那位。 1. 程序分析: 2.程序源代码: #define nmax 50 main() int i,k,m,n,numnmax,*p; printf(please input the total of numbers:); scanf(%d, p=num; for(i=0;in); for(i=0;idata=num; ptr-next=(

11、link)malloc(sizeof(node); if(i=4) ptr-next=NULL; else ptr=ptr-next; ptr=head; while(ptr!=NULL) printf(The value is =%dn,ptr-data); ptr=ptr-next; = 【程序 73】 题目:反向输出一个链表。 1.程序分析: 2.程序源代码: /*reverse output a list*/ #include stdlib.h #include stdio.h struct list int data; struct list *next; ; typedef str

12、uct list node; typedef node *link; void main() link ptr,head,tail; int num,i; tail=(link)malloc(sizeof(node); tail-next=NULL; ptr=tail; printf(nplease input 5 data=n); for(i=0;idata=num; head=(link)malloc(sizeof(node); head-next=ptr; ptr=head; ptr=ptr-next; while(ptr!=NULL) printf(The value is =%dn,

13、ptr-data); ptr=ptr-next; = 【程序 74】 题目:连接两个链表。 1.程序分析: 2.程序源代码: #include stdlib.h #include stdio.h struct list int data; struct list *next; ; typedef struct list node; typedef node *link; link delete_node(link pointer,link tmp) if (tmp=NULL) /*delete first node*/ return pointer-next; else if(tmp-next-next=NULL)/*delete last node*/ tmp-next=NULL; else /*delete the other node*/ tmp-next=tmp-next-next; return pointer; void selection_sort(link pointer,int num) link tmp,btmp; int i,min; for(i=0;idata; btmp=NULL; while(tmp-next) if(min

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

当前位置:首页 > 高等教育 > 大学课件

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