C语言经典程序100例_第1页
C语言经典程序100例_第2页
C语言经典程序100例_第3页
C语言经典程序100例_第4页
C语言经典程序100例_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、c语言经典程序100例【程序31】题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个 字母一样,则继续判断第二个字母。1 程序分析:用情况语句比较好,如果第一个字母一样,则判断用情 况语句或if语句判断第二个字母。2.程序源代码:#inelude <stdio.h>void main ()char letter;printf("please input the first letter of somedayn); while (letter=getch() !二'y' )/*当所按字母为 y 时才结束*/ switch (letter)case

2、' s' :printf ("please input second lettern); if (letter二getch ()二二's')printf("saturday、n); else if (letter=getch ()二二'u')printf (sundayn); else printf (,zdata errorn); break;case ' f' :printf ("fridayn):break:case ' m' :printf (z,mondayn);break;

3、case ' t' :printf ("please input second lettern,/): if (letter=getch () =,u')printf("tuesday、n); else if (letter=getch ()二二'h')printf (,zthursdayn); else printf (,zdata errorn); break;case ' w' :printf(wednesdayxn);break: default: printf (''data errorn,z

4、);【程序32】题 目:press any key to change color, do you want to try it. please hurry up!1 程序分析:2.程序源代码:ttinclude <conio. h>void main(void)int color;for (color 二 0; color < 8; color+)textbackground (color) ; /*设置文木的背景颜色*/ cprintf (z,this is color %drn,/, color);cprintf (''press any key to

5、continuern,z);getcho ;/*输入字符看不见*/【程序33题目:学习gotoxy ()与clrscr ()函数1 程序分析:2.程序源代码:ttinclude <conio. h>void main(void) clrscr() ;/*清屏函数*/ textbackground(2);gotoxy(1, 5) ;/*定位函数*/cprintf ("'output at row 5 column ln"); textbackground(3);gotoxy (20, 10);cprintf ("'output at ro

6、w 10 column 20n);【程序34题冃:练习函数调用1. 程序分析:2. 程序源代码:ttinclude <stdio.h> void hello_world(void)printf (z,hello, world!nz,);void three_hellos(void) int counter;for (counter 二 1; counter 二 3; counter+) hello_world() ;/*调用此函数*/void main(void) three_hellos () ;/*调用此函数*/【程序35】题目:文本颜色设置1 程序分析:2.程序源代码: #i

7、nclude <conio. h> void main(void) int color;for (color 二 1; color < 16; color+)textcolor (color) ;/*设置文本颜色*/ cprintf (z,this is color %drn,/, color); textcolor (128 + 15);cprintf ("'this is blinkingrrt);【程序36题目:求iooz内的素数1 程序分析:2 程序源代码:ftinclude <stdio.h> ttinclude "mathh

8、ttdefine n 101 main ()int i, j, line, a.n;for(i=2;i<n; i+) ai=i;for(i=2;i<sqrt (n);i+)for(j=i+l;j<n;j+)if (a.i !=0&&aj !=0) if (aj%aei=o) aj二0 汀printf(n);for (i=2, line二0;i<n;i+)if (ai!=0)printf ("%5d", a.i); line+;if (line二二10)printf("n"); line=0;【程序37题目:对10个

9、数进行排序1程序分析:可以利用选择法,即从后9个比较过程中,选择一个最 小的与第一个元素交换,下次类推,即用第二个元素与后8个进行比较,并进行 交换。2.程序源代码:ttdefine n 10main ()int i, j, min, tem, an;/*input data*/printf ("please input ten num:nz,);for(i=0;i<n;i+)printf (z,a%d=/,, i); scanf (d, &ai) ; printf(n);for(i=0;i<n;i+)printf(%5d, ai);printf (rt);/*s

10、ort ten num*/for (i=0; int ; i+)min二i;for(j二i+1;jn;j+) if (amin>aj) min二j; tem=a.i;ai=amin;amin=tem;/output data*/printf (''after sorted n); for(i=0;i<n;i+)printf(5d,ai);【程序38题目:求一个3*3矩阵对角线元素之和1 程序分析:利用双重for循环控制输入二维数组,再将aii累 加后输出。2.程序源代码:ma.in() float a33, sum二0;int i, j;printf (,zplea

11、se input rectangle element: n,z): for(i=0;i<3;i+)for(j=0;j<3;j+)scanf&aij);for (i=0;i<3;i+)sum二sum+a.i i;printf (z,dui jiaoxian he is %6 2f,sum);【程序39题目:有个已经排好序的数组。现输入一个数,要求按原來的规律 将它插入数组中。1. 程序分析:首先判断此数是否大于最后一个数,然后再考虑插入 中间的数的情况,插入后此元素之后的数,依次后移一个位置。2. 程序源代码:ma.in()int all = l, 4, 6, 9, 1

12、3, 16, 19, 28, 40, 100;int tempi, temp2, number, end, i, j ;printf(''original array is:n);for(i=0;i<10;i+)printf("%5d, ai);printf(n);printf(,zinsert a new number:");scanf(d", &number);end=a.9;if(number>end)a10=number;elsefor (i=0;i<10;i+) if(ai>number) tempi二ai;ai=number; for(j二i+1;jll;j+) temp2二a.j;aj二tempi; templ=temp2;break;for(i=0;i<ll;i+) printf(6d,ai);【程序40】题目:将一个数组逆序输出。1 程序分析:用第一个与最后一个交换。2 程序源代码:ttdefine n 5 ma.in() int an = 9, 6, 5, 4, 1, i,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论