经典C语言程序设计100例

上传人:汽*** 文档编号:567985328 上传时间:2024-07-22 格式:PDF 页数:64 大小:240.26KB
返回 下载 相关 举报
经典C语言程序设计100例_第1页
第1页 / 共64页
经典C语言程序设计100例_第2页
第2页 / 共64页
经典C语言程序设计100例_第3页
第3页 / 共64页
经典C语言程序设计100例_第4页
第4页 / 共64页
经典C语言程序设计100例_第5页
第5页 / 共64页
点击查看更多>>
资源描述

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

1、经典 C 语言程序设计 100 例 1-10【程序 1】题目:有 1、2、3、4 个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?1.程序分析:可填在百位、十位、个位的数字都是 1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。 2.程序源代码:main()int i,j,k;printf(n);for(i=1;i5;i+)*以下为三重循环*/ for(j=1;j5;j+) for (k=1;k5;k+) if (i!=k&i!=j&j!=k) /*确保 i、j、k 三位互不相同*/ printf(%d,%d,%dn,i,j,k); 【程序 2】题目:企业发放的奖金根据利润

2、提成。利润(I)低于或等于 10 万元时,奖金可提 10%;利润高 于 10 万元,低于 20 万元时,低于 10 万元的部分按 10%提成,高于 10万元的部分,可可提 成 7.5%;20 万到 40 万之间时,高于 20 万元的部分,可提成 5%;40 万到 60 万之间时高于 40 万元的部分,可提成 3%;60 万到 100 万之间时,高于 60 万元的部分,可提成 1.5%,高于 100 万元时,超过 100 万元的部分按 1%提成,从键盘输入当月利润 I,求应发放奖金总数?1.程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。2.程序源代码:main()long i

3、nt i;int bonus1,bonus2,bonus4,bonus6,bonus10,bonus;scanf(%ld,&i);bonus1=100000*0.1;bonus2=bonus1+100000*0.75;bonus4=bonus2+200000*0.5;bonus6=bonus4+200000*0.3;bonus10=bonus6+400000*0.15; if(i=100000) bonus=i*0.1; else if(i=200000) bonus=bonus1+(i-100000)*0.075; else if(i=400000) bonus=bonus2+(i-2000

4、00)*0.05; else if(i=600000) bonus=bonus4+(i-400000)*0.03; else if(i=1000000) bonus=bonus6+(i-600000)*0.015; else bonus=bonus10+(i-1000000)*0.01;printf(bonus=%d,bonus); -【程序 3】题目:一个整数,它加上 100 后是一个完全平方数,再加上 168 又是一个完全平方数,请问该数是多少?1.程序分析:在 10 万以内判断,先将该数加上 100 后再开方,再将该数加上268 后再开方,如果开方后 的结果满足如下条件,即是结果。请看具

5、体分析:2.程序源代码:#include math.hmain()long int i,x,y,z;for (i=1;i2)/*如果是闰年且月份大于 2,总天数应该加一天*/sum+;printf(It is the %dth day.,sum);-【程序 5】题目:输入三个整数 x,y,z,请把这三个数由小到大输出。1.程序分析:我们想办法把最小的数放到 x 上,先将 x 与 y 进行比较,如果xy 则将 x 与 y 的值进行交换, 然后再用 x 与 z 进行比较,如果 xz 则将 x 与 z 的值进行交换,这样能使 x 最小。2.程序源代码:main()int x,y,z,t;scanf(

6、%d%d%d,&x,&y,&z);if (xy)t=x;x=y;y=t; /*交换 x,y 的值*/if(xz)t=z;z=x;x=t;/*交换 x,z 的值*/if(yz)t=y;y=z;z=t;/*交换 z,y 的值*/printf(small to big: %d %d %dn,x,y,z);-【程序 6】题目:用*号输出字母 C 的图案。1.程序分析:可先用*号在纸上写出字母 C,再分行输出。2.程序源代码:#include stdio.hmain()printf(Hello C-world!n);printf( *n);printf( *n);printf( * n);printf(

7、 *n);-【程序 7】题目:输出特殊图案,请在 c 环境中运行,看一看,Very Beautiful!1.程序分析:字符共有 256 个。不同字符,图形不一样。2.程序源代码:#include stdio.hmain()char a=176,b=219;printf(%c%c%c%c%cn,b,a,a,a,b);printf(%c%c%c%c%cn,a,b,a,b,a);printf(%c%c%c%c%cn,a,a,b,a,a);printf(%c%c%c%c%cn,a,b,a,b,a);printf(%c%c%c%c%cn,b,a,a,a,b);-【程序 8】题目:输出 9*9 口诀。1.

8、程序分析:分行与列考虑,共 9 行 9 列,i 控制行,j 控制列。2.程序源代码:#include stdio.hmain() int i,j,result; printf(n); for (i=1;i10;i+) for(j=1;j10;j+) result=i*j; printf(%d*%d=%-3d,i,j,result);/*-3d 表示左对齐,占 3 位*/ printf(n);/*每一行后换行*/ -【程序 9】题目:要求输出国际象棋棋盘。1.程序分析:用 i 控制行,j 来控制列,根据 i+j 的和的变化来控制输出黑方格,还是白方格。2.程序源代码:#include stdio

9、.hmain()int i,j;for(i=0;i8;i+) for(j=0;j8;j+) if(i+j)%2=0) printf(%c%c,219,219); else printf( ); printf(n); -【程序 10】题目:打印楼梯,同时在楼梯上方打印两个笑脸。 1.程序分析:用 i 控制行,j 来控制列,j 根据 i 的变化来控制输出黑方格的个数。2.程序源代码:#include stdio.hmain()int i,j;printf(11n);/*输出两个笑脸*/for(i=1;i11;i+) for(j=1;j=i;j+) printf(%c%c,219,219); pri

10、ntf(n); 【程序 11】题目:古典问题:有一对兔子,从出生后第 3 个月起每个月都生一对兔子,小兔子长到第三个月 后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?1.程序分析:兔子的规律为数列 1,1,2,3,5,8,13,21.2.程序源代码:main()long f1,f2;int i;f1=f2=1;for(i=1;i=20;i+) printf(%12ld %12ld,f1,f2); if(i%2=0) printf(n);/*控制输出,每行四个*/ f1=f1+f2; /*前两个月加起来赋值给第三个月*/ f2=f1+f2; /*前两个月加起来赋值给第三个月*/

11、 -【程序 12】题目:判断 101-200 之间有多少个素数,并输出所有素数。1.程序分析:判断素数的方法:用一个数分别去除 2 到 sqrt(这个数),如果能被整除, 则表明此数不是素数,反之是素数。 2.程序源代码:#include math.hmain() int m,i,k,h=0,leap=1; printf(n); for(m=101;m=200;m+) k=sqrt(m+1); for(i=2;i=k;i+) if(m%i=0) leap=0;break; if(leap) printf(%-4d,m);h+; if(h%10=0) printf(n); leap=1; pri

12、ntf(nThe total is %d,h);-【程序 13】题目:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数 本身。例如:153 是一个“水仙花数”,因为 153=1 的三次方5 的三次方3 的三次方。1.程序分析:利用 for 循环控制 100-999 个数,每个数分解出个位,十位,百位。2.程序源代码:main()int i,j,k,n;printf(water flowernumber is:); for(n=100;n1000;n+) i=n/100;/*分解出百位*/ j=n/10%10;/*分解出十位*/ k=n%10;/*分解出个位*/

13、 if(i*100+j*10+k=i*i*i+j*j*j+k*k*k) printf(%-5d,n); printf(n);-【程序 14】题目:将一个正整数分解质因数。例如:输入 90,打印出 90=2*3*3*5。程序分析:对 n 进行分解质因数,应先找到一个最小的质数 k,然后按下述步骤完成: (1)如果这个质数恰等于 n,则说明分解质因数的过程已经结束,打印出即可。(2)如果 nk,但 n 能被 k 整除,则应打印出 k 的值,并用 n 除以 k 的商,作为新的正整数你 n, 重复执行第一步。(3)如果 n 不能被 k 整除,则用 k+1 作为 k 的值,重复执行第一步。2.程序源代码

14、:/* zheng int is divided yinshu*/main()int n,i;printf(nplease input a number:n);scanf(%d,&n);printf(%d=,n);for(i=2;i=90 分的同学用 A 表示,60-89 分之间的用 B 表示, 60 分以下的用 C 表示。1.程序分析:(ab)?a:b 这是条件运算符的基本例子。2.程序源代码:main() int score; char grade; printf(please input a scoren); scanf(%d,&score); grade=score=90?A:(sco

15、re=60?B:C); printf(%d belongs to %c,score,grade);-【程序 16】题目:输入两个正整数 m 和 n,求其最大公约数和最小公倍数。1.程序分析:利用辗除法。2.程序源代码:main() int a,b,num1,num2,temp; printf(please input two numbers:n); scanf(%d,%d,&num1,&num2); if(num1=a&c=A&c=0&c=9) digit+; else others+;printf(all in all:char=%d space=%d digit=%d others=%dn

16、,letters,space,digit,others);-【程序 18】题目:求 s=a+aa+aaa+aaaa+aa.a 的值,其中 a 是一个数字。例如2+22+222+2222+22222(此时 共有 5 个数相加),几个数相加有键盘控制。1.程序分析:关键是计算出每一项的值。2.程序源代码:main() int a,n,count=1; long int sn=0,tn=0; printf(please input a and nn); scanf(%d,%d,&a,&n); printf(a=%d,n=%dn,a,n); while(count=n) tn=tn+a; sn=sn+

17、tn; a=a*10; +count; printf(a+aa+.=%ldn,sn);-【程序 19】题目:一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=123.编程 找出 1000 以内的所有完数。1. 程序分析:请参照程序-上页程序 14. 2.程序源代码:main()static int k10;int i,j,n,s;for(j=2;j1000;j+) n=-1; s=j; for(i=1;ij;i+) if(j%i)=0) n+; s=s-i; kn=i; if(s=0) printf(%d is a wanshu,j); for(i=0;in;i+) printf(

18、%d,ki); printf(%dn,kn); -【程序 20】题目:一球从 100 米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第 10 次落地时,共经过多少米?第 10 次反弹多高?1.程序分析:见下面注释2.程序源代码:main()float sn=100.0,hn=sn/2;int n;for(n=2;n0) x1=(x2+1)*2;/*第一天的桃子数是第 2 天桃子数加 1 后的 2 倍*/ x2=x1; day-; printf(the total is %dn,x1);-【程序 22】题目:两个乒乓球队进行比赛,各出三人。甲队为 a,b,c 三人,乙队为 x,y

19、,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=z;i+) for(j=x;j=z;j+) if(i!=j) for(k=x;k=z;k+) if(i!=k&j!=k) if(i!=x&k!=x&k!=z) printf(order is a-%ct

20、b-%ctc-%cn,i,j,k); -【程序 23】 题目:打印出如下图案(菱形)*1.程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重 for 循环,第一层控制行,第二层控制列。 2.程序源代码:main()int i,j,k;for(i=0;i=3;i+) for(j=0;j=2-i;j+) printf( ); for(k=0;k=2*i;k+) printf(*); printf(n); for(i=0;i=2;i+) for(j=0;j=i;j+) printf( ); for(k=0;k=4-2*i;k+) printf(*); printf(n);

21、-【程序 24】 题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13.求出这个数列的前 20 项之和。1.程序分析:请抓住分子与分母的变化规律。 2.程序源代码:main()int n,t,number=20;float a=2,b=1,s=0;for(n=1;n=number;n+) s=s+a/b; t=a;a=a+b;b=t;/*这部分是程序的关键,请读者猜猜 t 的作用*/ printf(sum is %9.6fn,s);-【程序 25】 题目:求 1+2!+3!+.+20!的和1.程序分析:此程序只是把累加变成了累乘。 2.程序源代码:main()float n

22、,s=0,t=1;for(n=1;n=20;n+) t*=n; s+=t; printf(1+2!+3!.+20!=%en,s);-【程序 26】 题目:利用递归方法求 5!。1.程序分析:递归公式:fn=fn_1*4!2.程序源代码:#include stdio.hmain()int i;int fact();for(i=0;i5;i+) printf(40:%d!=%dn,i,fact(i);int fact(j)int j;int sum;if(j=0) sum=1;else sum=j*fact(j-1);return sum;-【程序 27】 题目:利用递归函数调用方式,将所输入的

23、5 个字符,以相反顺序打印出来。1.程序分析:2.程序源代码:#include stdio.hmain()int i=5;void palin(int n);printf(40:);palin(i);printf(n);void palin(n)int n;char next;if(n=1) next=getchar(); printf(n0:); putchar(next); else next=getchar(); palin(n-1); putchar(next); -【程序 28】 题目:有 5 个人坐在一起,问第五个人多少岁?他说比第 4 个人大 2 岁。问第4 个人岁数,他说比第

24、3 个人大 2 岁。问第三个人,又说比第 2 人大两岁。问第 2 个人,说比第一个人大两岁。最后 问第一个人,他说是 10 岁。请问第五个人多大?1.程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道 第四人的岁数,依次类推,推到第一人(10 岁),再往回推。2.程序源代码:age(n)int n;int c;if(n=1) c=10;else c=age(n-1)+2;return(c);main() printf(%d,age(5);-【程序 29】 题目:给一个不多于 5 位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。1. 程序分析:学会分解

25、出每一位数,如下解释:(这里是一种简单的算法,师专数 002 班赵鑫提供) 2.程序源代码:main( )long a,b,c,d,e,x;scanf(%ld,&x);a=x/10000;/*分解出万位*/b=x%10000/1000;/*分解出千位*/c=x%1000/100;/*分解出百位*/d=x%100/10;/*分解出十位*/e=x%10;/*分解出个位*/if (a!=0) printf(there are 5, %ld %ld %ld %ld %ldn,e,d,c,b,a);else if (b!=0) printf(there are 4, %ld %ld %ld %ldn,e

26、,d,c,b); else if (c!=0) printf( there are 3,%ld %ld %ldn,e,d,c); else if (d!=0) printf(there are 2, %ld %ldn,e,d); else if (e!=0) printf( there are 1,%ldn,e);-【程序 30】 题目:一个 5 位数,判断它是不是回文数。即 12321 是回文数,个位与万位相同,十位与千位相同。1.程序分析:同 29 例2.程序源代码:main( )long ge,shi,qian,wan,x;scanf(%ld,&x);wan=x/10000;qian=x

27、%10000/1000;shi=x%100/10;ge=x%10;if (ge=wan&shi=qian)/*个位等于万位并且十位等于千位*/ printf(this number is a huiwenn);else printf(this number is not a huiwenn);【程序 31】题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 判断第二个字母。1.程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if 语句判断第二个字母。2.程序源代码:#include void main()char letter;printf(plea

28、se 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);break;case M:printf(

29、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 color, do you want to try i

30、t. Please hurry up!1.程序分析:2.程序源代码:#include void main(void)int color;for (color = 0; color 8; color+) textbackground(color);/*设置文本的背景颜色*/ cprintf(This is color %drn, color); cprintf(Press any key to continuern); getch();/*输入字符看不见*/ -【程序 33】题目:学习 gotoxy()与 clrscr()函数1.程序分析:2.程序源代码:#include void main(v

31、oid)clrscr();/*清屏函数*/textbackground(2);gotoxy(1, 5);/*定位函数*/cprintf(Output at row 5 column 1n);textbackground(3);gotoxy(20, 10);cprintf(Output at row 10 column 20n);-【程序 34】题目:练习函数调用1. 程序分析: 2.程序源代码:#include void hello_world(void)printf(Hello, world!n);void three_hellos(void)int counter;for (counter

32、 = 1; counter = 3; counter+)hello_world();/*调用此函数*/void main(void)three_hellos();/*调用此函数*/-【程序 35】题目:文本颜色设置1.程序分析:2.程序源代码:#include void main(void)int color;for (color = 1; color 16; color+) textcolor(color);/*设置文本颜色*/ cprintf(This is color %drn, color); textcolor(128 + 15);cprintf(This is blinkingrn

33、);-【程序 36】题目:求 100 之内的素数1.程序分析:2.程序源代码:#include #include math.h#define N 101main()int i,j,line,aN;for(i=2;iN;i+) ai=i;for(i=2;isqrt(N);i+) for(j=i+1;jN;j+) if(ai!=0&aj!=0) if(aj%ai=0) aj=0;printf(n);for(i=2,line=0;iN;i+) if(ai!=0) printf(%5d,ai); line+; if(line=10) printf(n);line=0;-【程序 37】题目:对 10 个

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

35、for(j=i+1;jaj) min=j;tem=ai;ai=amin;amin=tem;/*output data*/printf(After sorted n);for(i=0;iN;i+)printf(%5d,ai);-【程序 38】题目:求一个 3*3 矩阵对角线元素之和 1.程序分析:利用双重 for 循环控制输入二维数组,再将 aii累加后输出。2.程序源代码:main()float a33,sum=0;int i,j;printf(please input rectangle element:n);for(i=0;i3;i+) for(j=0;j3;j+) scanf(%f,&a

36、ij);for(i=0;i3;i+) sum=sum+aii;printf(duijiaoxian he is %6.2f,sum);-【程序 39】题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。1. 程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后 此元素之后的数,依次后移一个位置。 2.程序源代码:main()int a11=1,4,6,9,13,16,19,28,40,100;int temp1,temp2,number,end,i,j;printf(original array is:n);for(i=0;iend) a10=

37、number;else for(i=0;inumber) temp1=ai; ai=number; for(j=i+1;j11;j+) temp2=aj; aj=temp1; temp1=temp2; break; for(i=0;i11;i+) printf(%6d,ai);-【程序 40】题目:将一个数组逆序输出。1.程序分析:用第一个与最后一个交换。2.程序源代码:#define N 5main() int aN=9,6,5,4,1,i,temp; printf(n original array:n); for(i=0;iN;i+) printf(%4d,ai); for(i=0;iN/

38、2;i+) temp=ai; ai=aN-i-1; aN-i-1=temp; printf(n sorted array:n);for(i=0;iN;i+) printf(%4d,ai);【程序 41】题目:学习 static 定义静态变量的用法1.程序分析:2.程序源代码:#include stdio.hvarfunc()int var=0;static int static_var=0;printf(40:var equal %d n,var);printf(40:static var equal %d n,static_var);printf(n);var+;static_var+;vo

39、id main()int i; for(i=0;i3;i+) varfunc();-【程序 42】 题目:学习使用 auto 定义变量的用法1.程序分析:2.程序源代码:#include stdio.hmain()int i,num;num=2; for (i=0;i3;i+) printf(40: The num equal %d n,num); num+; auto int num=1; printf(40: The internal block num equal %d n,num); num+; -【程序 43】题目:学习使用 static 的另一用法。1.程序分析:2.程序源代码:#

40、include stdio.hmain()int i,num;num=2;for(i=0;i3;i+)printf(40: The num equal %d n,num);num+;static int num=1;printf(40:The internal block num equal %dn,num);num+;-【程序 44】题目:学习使用 external 的用法。1.程序分析:2.程序源代码:#include stdio.hint a,b,c;void add() int a;a=3;c=a+b;void main() a=b=4;add();printf(The value o

41、f c is equal to %dn,c);-【程序 45】题目:学习使用 register 定义变量的方法。1.程序分析:2.程序源代码:void main()register int i;int tmp=0;for(i=1;i);scanf(%d,&num);printf(40:The square for this number is %d n,SQ(num);if(num=50) again=TRUE;else again=FALSE;-【程序 47】题目:宏#define 命令练习(2)1.程序分析:2.程序源代码:#include stdio.h#define exchange(

42、a,b) /*宏定义中允许包含两道衣裳命令的情形,此时必须在最右边加上*/ int t; t=a; a=b; b=t; void main(void)int x=10;int y=20;printf(x=%d; y=%dn,x,y);exchange(x,y);printf(x=%d; y=%dn,x,y);-【程序 48】题目:宏#define 命令练习(3)1.程序分析:2.程序源代码:#define LAG #define SMA y)?x:y#define MINIMUM(x,y) (xy)?y:xvoid main() int a=10,b=20;#ifdef MAXprintf(4

43、0: The larger one is %dn,MAXIMUM(a,b);#elseprintf(40: The lower one is %dn,MINIMUM(a,b);#endif#ifndef MINprintf(40: The lower one is %dn,MINIMUM(a,b);#elseprintf(40: The larger one is %dn,MAXIMUM(a,b);#endif#undef MAX#ifdef MAXprintf(40: The larger one is %dn,MAXIMUM(a,b);#elseprintf(40: The lower o

44、ne is %dn,MINIMUM(a,b);#endif#define MIN#ifndef MINprintf(40: The lower one is %dn,MINIMUM(a,b);#elseprintf(40: The larger one is %dn,MAXIMUM(a,b);#endif-【程序 50】题目:#include 的应用练习1.程序分析:2.程序源代码:test.h 文件如下:#define LAG #define SMA #define EQ =#include test.h /*一个新文件 50.c,包含 test.h*/#include stdio.hvoi

45、d main() int i=10;int j=20;if(i LAG j)printf(40: %d larger than %d n,i,j);else if(i EQ j)printf(40: %d equal to %d n,i,j);else if(i SMA j)printf(40:%d smaller than %d n,i,j);elseprintf(40: No such value.n);题目:学习使用按位与 & 。1.程序分析:0&0=0; 0&1=0; 1&0=0; 1&1=12.程序源代码:#include stdio.hmain()int a,b;a=077;b=a

46、&3;printf(40: The a & b(decimal) is %d n,b);b&=7;printf(40: The a & b(decimal) is %d n,b);-【程序 52】题目:学习使用按位或 | 。1.程序分析:0|0=0; 0|1=1; 1|0=1; 1|1=12.程序源代码:#include stdio.hmain()int a,b;a=077;b=a|3;printf(40: The a & b(decimal) is %d n,b);b|=7;printf(40: The a & b(decimal) is %d n,b);-【程序 53】题目:学习使用按位异

47、或 。1.程序分析:00=0; 01=1; 10=1; 11=02.程序源代码:#include stdio.hmain()int a,b;a=077;b=a3;printf(40: The a & b(decimal) is %d n,b);b=7;printf(40: The a & b(decimal) is %d n,b);-【程序 54】题目:取一个整数 a 从右端开始的 47 位。程序分析:可以这样考虑: (1)先使 a 右移 4 位。(2)设置一个低 4 位全为 1,其余全为 0 的数。可用(04;c=(04);d=b&c;printf(%on%on,a,d);-【程序 55】题

48、目:学习使用按位取反。1.程序分析:0=1; 1=0;2.程序源代码:#include stdio.hmain()int a,b;a=234;b=a;printf(40: The as 1 complement(decimal) is %d n,b);a=a;printf(40: The as 1 complement(hexidecimal) is %x n,a); -【程序 56】题目:画图,学用 circle 画圆形。1.程序分析:2.程序源代码:/*circle*/#include graphics.hmain()int driver,mode,i;float j=1,k=1;driv

49、er=VGA;mode=VGAHI;initgraph(&driver,&mode,);setbkcolor(YELLOW);for(i=0;i=25;i+)setcolor(8);circle(310,250,k);k=k+j;j=j+0.3; -【程序 57】题目:画图,学用 line 画直线。1.程序分析:2.程序源代码:#include graphics.hmain()int driver,mode,i;float x0,y0,y1,x1;float j=12,k;driver=VGA;mode=VGAHI;initgraph(&driver,&mode,);setbkcolor(GR

50、EEN);x0=263;y0=263;y1=275;x1=275;for(i=0;i=18;i+)setcolor(5);line(x0,y0,x0,y1);x0=x0-5;y0=y0-5;x1=x1+5;y1=y1+5;j=j+10;x0=263;y1=275;y0=263;for(i=0;i=20;i+)setcolor(5);line(x0,y0,x0,y1);x0=x0+5;y0=y0+5;y1=y1-5;-【程序 58】题目:画图,学用 rectangle 画方形。1.程序分析:利用 for 循环控制 100-999 个数,每个数分解出个位,十位,百位。2.程序源代码:#includ

51、e graphics.hmain()int x0,y0,y1,x1,driver,mode,i;driver=VGA;mode=VGAHI;initgraph(&driver,&mode,);setbkcolor(YELLOW);x0=263;y0=263;y1=275;x1=275;for(i=0;i=18;i+)setcolor(1);rectangle(x0,y0,x1,y1);x0=x0-5;y0=y0-5;x1=x1+5;y1=y1+5;settextstyle(DEFAULT_FONT,HORIZ_DIR,2);outtextxy(150,40,How beautiful it i

52、s!);line(130,60,480,60);setcolor(2);circle(269,269,137);-【程序 59】题目:画图,综合例子。1.程序分析:2.程序源代码:# define PAI 3.1415926# define B 0.809# include graphics.h#include math.hmain()int i,j,k,x0,y0,x,y,driver,mode;float a;driver=CGA;mode=CGAC0;initgraph(&driver,&mode,);setcolor(3);setbkcolor(GREEN);x0=150;y0=100

53、;circle(x0,y0,10);circle(x0,y0,20);circle(x0,y0,50);for(i=0;i16;i+) a=(2*PAI/16)*i; x=ceil(x0+48*cos(a); y=ceil(y0+48*sin(a)*B); setcolor(2); line(x0,y0,x,y);setcolor(3);circle(x0,y0,60);/* Make 0 time normal size letters */settextstyle(DEFAULT_FONT,HORIZ_DIR,0);outtextxy(10,170,press a key);getch()

54、;setfillstyle(HATCH_FILL,YELLOW);floodfill(202,100,WHITE);getch();for(k=0;k=500;k+) setcolor(3); for(i=0;i=16;i+) a=(2*PAI/16)*i+(2*PAI/180)*k; x=ceil(x0+48*cos(a); y=ceil(y0+48+sin(a)*B); setcolor(2); line(x0,y0,x,y); for(j=1;j=50;j+) a=(2*PAI/16)*i+(2*PAI/180)*k-1; x=ceil(x0+48*cos(a); y=ceil(y0+4

55、8*sin(a)*B); line(x0,y0,x,y); restorecrtmode();-【程序 60】题目:画图,综合例子。1.程序分析:2.程序源代码:#include graphics.h#define LEFT 0#define TOP 0#define RIGHT 639#define BOTTOM 479#define LINES 400#define MAXCOLOR 15main()int driver,mode,error;int x1,y1;int x2,y2;int dx1,dy1,dx2,dy2,i=1;int count=0;int color=0;driver

56、=VGA;mode=VGAHI;initgraph(&driver,&mode,);x1=x2=y1=y2=10;dx1=dy1=2;dx2=dy2=3;while(!kbhit() line(x1,y1,x2,y2); x1+=dx1;y1+=dy1; x2+=dx2;y2+dy2; if(x1=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:+col

57、or; closegraph();【程序 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;i10;i+) ai0=1; aii=1;for(i=2;i10;i+) for(j=1;ji;j+) aij=ai-1j-1+ai-1j;for(i=0;i10;i+) for(j=0;j=i;j+) printf(%5d,aij); printf(n); -【程序 62】题目:学习 putpix

58、el 画点。1.程序分析:2.程序源代码:#include stdio.h#include graphics.hmain()int i,j,driver=VGA,mode=VGAHI;initgraph(&driver,&mode,);setbkcolor(YELLOW);for(i=50;i=230;i+=20) for(j=50;j=230;j+) putpixel(i,j,1);for(j=50;j=230;j+=20) for(i=50;i=230;i+) putpixel(i,j,1);-【程序 63】题目:画椭圆 ellipse1.程序分析:2.程序源代码:#include std

59、io.h#include graphics.h#include conio.hmain()int x=360,y=160,driver=VGA,mode=VGAHI;int num=20,i;int top,bottom;initgraph(&driver,&mode,);top=y-30;bottom=y-30;for(i=0;inum;i+)ellipse(250,250,0,360,top,bottom);top-=5;bottom+=5;getch();-【程序 64】题目:利用 ellipse and rectangle 画图。1.程序分析:2.程序源代码:#include stdi

60、o.h#include graphics.h#include conio.hmain()int driver=VGA,mode=VGAHI;int i,num=15,top=50;int left=20,right=50;initgraph(&driver,&mode,);for(i=0;inum;i+)ellipse(250,250,0,360,right,left);ellipse(250,250,0,360,20,top);rectangle(20-2*i,20-2*i,10*(i+2),10*(i+2);right+=5;left+=5;top+=10;getch();-【程序 65】

61、题目:一个最优美的图案。1.程序分析:2.程序源代码:#include graphics.h#include math.h#include dos.h#include conio.h#include stdlib.h#include stdio.h#include stdarg.h#define MAXPTS 15#define PI 3.1415926struct PTS int x,y;double AspectRatio=0.85;void LineToDemo(void)struct viewporttype vp;struct PTS pointsMAXPTS;int i, j, h

62、, w, xcenter, ycenter;int radius, angle, step;double rads;printf( MoveTo / LineTo Demonstration );getviewsettings( &vp );h = vp.bottom - vp.top;w = vp.right - vp.left;xcenter = w / 2; /* Determine the center of circle */ycenter = h / 2;radius = (h - 30) / (AspectRatio * 2);step = 360 / MAXPTS; /* De

63、termine # of increments */angle = 0; /* Begin at zero degrees */for( i=0 ; iMAXPTS ; +i ) /* Determine circle intercepts */rads = (double)angle * PI / 180.0; /* Convert angle to radians */pointsi.x = xcenter + (int)( cos(rads) * radius );pointsi.y = ycenter - (int)( sin(rads) * radius * AspectRatio

64、);angle += step; /* Move to next increment */circle( xcenter, ycenter, radius ); /* Draw bounding circle */for( i=0 ; iMAXPTS ; +i ) /* Draw the cords to the circle */for( j=i ; jn2) swap(pointer1,pointer2);if(n1n3) swap(pointer1,pointer3);if(n2n3) swap(pointer2,pointer3);printf(the sorted numbers a

65、re:%d,%d,%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;i9;i+) scanf(%d,&numberi); scanf(%d,&number9);max_m

66、in(array)int array10;int *max,*min,k,l;int *p,*arr_end;arr_end=array+10;max=min=array;for(p=array+1;p*max) max=p; else if(*p*min) min=p; k=*max; l=*min; *p=array0;array0=l;l=*p; *p=array9;array9=k;k=*p; return;output(array)int array10; int *p;for(p=array;parray+9;p+) printf(%d,*p);printf(%dn,array9)

67、;-【程序 68】题目:有 n 个整数,使其前面各数顺序向后移 m 个位置,最后 m 个数变成最前面的 m 个数1.程序分析:2.程序源代码:main()int number20,n,m,i;printf(the total numbers is:);scanf(%d,&n);printf(back m:);scanf(%d,&m);for(i=0;in-1;i+) scanf(%d,&numberi);scanf(%d,&numbern-1);move(number,n,m);for(i=0;iarray;p-) *p=*(p-1); *array=array_end; m-; if(m0)

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

69、p+i)=0; k=0; m+; i+;if(i=n) i=0;while(*p=0) p+;printf(%d is leftn,*p);-【程序 70】题目:写一个函数,求一个字符串的长度,在 main 函数中输入字符串,并输出其长度。1.程序分析:2.程序源代码:main()int len;char *str20;printf(please input a string:n);scanf(%s,str);len=length(str);printf(the string has %d characters.,len);length(p)char *p;int n;n=0;while(*p

70、!=0) n+; p+;return n; 【程序 71】题目:编写 input()和 output()函数输入,输出 5 个学生的数据记录。1.程序分析:2.程序源代码:#define N 5struct student char num6; char name8; int score4; stuN;input(stu)struct student stu; int i,j; for(i=0;iN;i+) printf(n please input %d of %dn,i+1,N); printf(num: ); scanf(%s,stui.num); printf(name: ); sca

71、nf(%s,stui.name); for(j=0;j3;j+) printf(score %d.,j+1); scanf(%d,&stui.scorej); printf(n); print(stu)struct student stu; int i,j;printf(nNo. Name Sco1 Sco2 Sco3n);for(i=0;iN;i+) printf(%-6s%-10s,stui.num,stui.name); for(j=0;jn);for(i=0;idata=num; ptr-next=(link)malloc(sizeof(node); if(i=4) ptr-next=

72、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.hstruct list int data; struct list *next;typedef struct list node;typedef node *link;void main() link ptr,hea

73、d,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,ptr-data); ptr=ptr-next;-【程序 74】题目:连接两个链表。1.程序分析:2.程序源代码:#in

74、clude stdlib.h#include stdio.hstruct 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*

75、/ 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(mintmp-next-data) min=tmp-next-data; btmp=tmp; tmp=tmp-next; printf(40: %dn,min);pointer=delete_node(pointer,btmp);link create_list(int array,int

76、num) link tmp1,tmp2,pointer;int i;pointer=(link)malloc(sizeof(node);pointer-data=array0;tmp1=pointer;for(i=1;inext=NULL; tmp2-data=arrayi; tmp1-next=tmp2; tmp1=tmp1-next;return pointer;link concatenate(link pointer1,link pointer2) link tmp;tmp=pointer1;while(tmp-next) tmp=tmp-next;tmp-next=pointer2;

77、return pointer1;void main(void) int arr1=3,12,8,9,11; link ptr; ptr=create_list(arr1,5); selection_sort(ptr,5);-【程序 75】题目:放松一下,算一道简单的题目。1.程序分析:2.程序源代码:main()int i,n;for(i=1;i1) break;if(n%2=0) printf(Even=); sum=dcall(peven,n);else printf(Odd=); sum=dcall(podd,n);printf(%f,sum);float peven(int n)flo

78、at s;int i;s=1;for(i=2;i=n;i+=2) s+=1/(float)i;return(s);float podd(n)int n;float s;int i;s=0;for(i=1;i=n;i+=2) s+=1/(float)i;return(s);float dcall(fp,n)float (*fp)();int n;float s;s=(*fp)(n);return(s);-【程序 77】题目:填空练习(指向指针的指针)1.程序分析:2.程序源代码:main() char *s=man,woman,girl,boy,sister;char *q;int k;for(

79、k=0;k5;k+);/*这里填写什么语句*/ printf(%sn,*q);-【程序 78】题目:找到年龄最大的人,并输出。请找出程序中有什么问题。1.程序分析:2.程序源代码:#define N 4#include stdio.hstatic struct man char name20;int age; personN=li,18,wang,19,zhang,20,sun,22;main()struct man *q,*p;int i,m=0;p=person;for (i=0;iN;i+)if(mage) q=p+; m=q-age;printf(%s,%d,(*q).name,(*q

80、).age);-【程序 79】题目:字符串排序。1.程序分析:2.程序源代码:main()char *str120,*str220,*str320;char swap();printf(please input three stringsn);scanf(%s,str1);scanf(%s,str2);scanf(%s,str3);if(strcmp(str1,str2)0) swap(str1,str2);if(strcmp(str1,str3)0) swap(str1,str3);if(strcmp(str2,str3)0) swap(str2,str3);printf(after bei

81、ng sortedn);printf(%sn%sn%sn,str1,str2,str3);char swap(p1,p2)char *p1,*p2;char *p20;strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p);-【程序 80】题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只 猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了 一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的, 问海滩上原来最少有多少个桃子?1.程序分析:2.程序源代码:main()int i,

82、m,j,k,count;for(i=4;i10000;i+=4) count=0;m=i;for(k=0;k5;k+) j=i/4*5+1; i=j; if(j%4=0) count+; else break; i=m; if(count=4) printf(%dn,count); break; 题目:809*?=800*?+9*?+1 其中?代表的两位数,8*?的结果为两位数,9*?的结果为 3 位数。求?代表的两位数,及 809*?后的结果。1.程序分析:2.程序源代码:output(long b,long i) printf(n%ld/%ld=809*%ld+%ld,b,i,i,b%i)

83、;main()long int a,b,i;a=809;for(i=10;i=1000&b=10000&8*i=100)output(b,i); -【程序 82】题目:八进制转换为十进制1.程序分析:2.程序源代码:main() char *p,s6;int n;p=s;gets(p);n=0;while(*(p)!=0)n=n*8+*p-0;p+;printf(%d,n);-【程序 83】题目:求 07 所能组成的奇数个数。1.程序分析:2.程序源代码:main()long sum=4,s=4;int j;for(j=2;j=8;j+)/*j is place of number*/ pri

84、ntf(n%ld,sum);if(j=2)s*=7;elses*=8;sum+=s;printf(nsum=%ld,sum);-【程序 84】题目:一个偶数总能表示为两个素数之和。1.程序分析:2.程序源代码:#include stdio.h#include math.hmain() int a,b,c,d;scanf(%d,&a);for(b=3;b=a/2;b+=2) for(c=2;csqrt(b)d=a-b;elsebreak;for(c=2;csqrt(d)printf(%d=%d+%dn,a,b,d);-【程序 85】题目:判断一个素数能被几个 9 整除1.程序分析:2.程序源代码

85、:main() long int m9=9,sum=9;int zi,n1=1,c9=1;scanf(%d,&zi);while(n1!=0) if(!(sum%zi)n1=0;elsem9=m9*10;sum=sum+m9;c9+;printf(%ld,can be divided by %d 9,sum,c9);-【程序 86】题目:两个字符串连接程序1.程序分析:2.程序源代码:#include stdio.hmain()char a=acegikm;char b=bdfhjlnpq;char c80,*p;int i=0,j=0,k=0;while(ai!=0&bj!=0)if (ai

86、bj) ck=ai;i+;elseck=bj+;k+;ck=0;if(ai=0)p=b+j;elsep=a+i;strcat(c,p);puts(c);-【程序 87】题目:回答结果(结构体变量传递)1.程序分析:2.程序源代码:#include stdio.hstruct student int x;char c; a;main()a.x=3;a.c=a;f(a);printf(%d,%c,a.x,a.c);f(struct student b)b.x=20;b.c=y;-【程序 88】题目:读取 7 个数(150)的整数值,每读取一个值,程序打印出该值个数的。1.程序分析:2.程序源代码:

87、main()int i,a,n=1;while(n=7) do scanf(%d,&a); while(a50);for(i=1;i=a;i+) printf(*);printf(n);n+;getch();-【程序 89】题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下: 每位数字都加上 5,然后用和除以 10 的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。1.程序分析:2.程序源代码:main()int a,i,aa4,t;scanf(%d,&a);aa0=a%10;aa1=a%100/10;aa2=a%1000/100;aa3=a/

88、1000;for(i=0;i=3;i+) aai+=5; aai%=10; for(i=0;i=0;i-)printf(%d,aai);-【程序 90】题目:专升本一题,读结果。1.程序分析:2.程序源代码:#include stdio.h#define M 5main()int aM=1,2,3,4,5;int i,j,t;i=0;j=M-1;while(ij) t=*(a+i);*(a+i)=*(a+j);*(a+j)=t;i+;j-;for(i=0;im;i+) printf(%d,*(a+i); 【程序 91】题目:时间函数举例 11.程序分析:2.程序源代码:#include std

89、io.h#include time.hvoid main() time_t lt; /*define a longint time varible*/lt=time(NULL);/*system time and date*/printf(ctime(); /*english format output*/printf(asctime(localtime();/*tranfer to tm*/printf(asctime(gmtime(); /*tranfer to Greenwich time*/-【程序 92】题目:时间函数举例 21.程序分析:2.程序源代码:/*calculate ti

90、me*/#include time.h#include stdio.hmain() time_t start,end;int i;start=time(NULL);for(i=0;i3000;i+) printf(1111111111n);end=time(NULL);printf(1: The different is %6.3fn,difftime(end,start);-【程序 93】题目:时间函数举例 31.程序分析:2.程序源代码:/*calculate time*/#include time.h#include stdio.hmain() clock_t start,end;int

91、 i;double var;start=clock();for(i=0;ii)printf(please input a little smaller.n);scanf(%d,&guess);elseprintf(please input a little bigger.n);scanf(%d,&guess);end=clock();b=time(NULL);printf(1: It took you %6.3f secondsn,var=(double)(end-start)/18.2);printf(1: it took you %6.3f secondsnn,difftime(b,a);

92、if(var15)printf(11 You are very clever! 11nn);else if(var25)printf(11 you are normal! 11nn);elseprintf(11 you are stupid! 11nn);printf(11 Congradulations 11nn);printf(The number you guess is %d,i);printf(ndo you want to try it again?(yy.or.n)n);if(c=getch()=y)goto loop;-【程序 95】题目:家庭财务管理小程序1.程序分析:2.程

93、序源代码:/*money management system*/#include stdio.h#include dos.hmain()FILE *fp;struct date d;float sum,chm=0.0;int len,i,j=0;int c;char ch4=,ch116=,chtime12=,chshop16,chmoney8;pp: clrscr();sum=0.0;gotoxy(1,1);printf(|-|);gotoxy(1,2);printf(| money management system(C1.0) 2000.03 |);gotoxy(1,3);printf(

94、|-|);gotoxy(1,4);printf(| - money records - | - today cost list - |);gotoxy(1,5);printf(| - |-|);gotoxy(1,6);printf(| date: - | |);gotoxy(1,7);printf(| | | | |);gotoxy(1,8);printf(| - | |);gotoxy(1,9);printf(| thgs: - | |);gotoxy(1,10);printf(| | | | |);gotoxy(1,11);printf(| - | |);gotoxy(1,12);prin

95、tf(| cost: - | |);gotoxy(1,13);printf(| | | | |);gotoxy(1,14);printf(| - | |);gotoxy(1,15);printf(| | |);gotoxy(1,16);printf(| | |);gotoxy(1,17);printf(| | |);gotoxy(1,18);printf(| | |);gotoxy(1,19);printf(| | |);gotoxy(1,20);printf(| | |);gotoxy(1,21);printf(| | |);gotoxy(1,22);printf(| | |);gotoxy

96、(1,23);printf(|-|);i=0;getdate(&d);sprintf(chtime,%4d.%02d.%02d,d.da_year,d.da_mon,d.da_day);for(;)gotoxy(3,24);printf( Tab _browse cost list Esc _quit);gotoxy(13,10);printf( );gotoxy(13,13);printf( );gotoxy(13,7);printf(%s,chtime);j=18;ch0=getch();if(ch0=27)break;strcpy(chshop,);strcpy(chmoney,);if

97、(ch0=9)mm:i=0;fp=fopen(home.dat,r+);gotoxy(3,24);printf( );gotoxy(6,4);printf( list records );gotoxy(1,5);printf(|-|);gotoxy(41,4);printf( );gotoxy(41,5);printf( |);while(fscanf(fp,%10s%14s%fn,chtime,chshop,&chm)!=EOF) if(i=36) getch();i=0;if (i%36)16) gotoxy(41,4+i-17);printf( );gotoxy(42,4+i-17);i

98、+;sum=sum+chm;printf(%10s %-14s %6.1fn,chtime,chshop,chm);gotoxy(1,23);printf(|-|);gotoxy(1,24);printf(| |);gotoxy(1,25);printf(|-|);gotoxy(10,24);printf(total is %8.1f$,sum);fclose(fp);gotoxy(49,24);printf(press any key to.);getch();goto pp;elsewhile(ch0!=r) if(j15) len=len+1; j=11;strcpy(ch1,);j=j

99、-2;strncat(ch1,chtime,len);strcpy(chtime,);strncat(chtime,ch1,len-1);gotoxy(13,7);printf( );gotoxy(13,7);printf(%s,chtime);ch0=getch();if(ch0=9)goto mm;if(ch0=27)exit(1);gotoxy(3,24);printf( );gotoxy(13,10);j=0;ch0=getch();while(ch0!=r) if (j14) strncat(chshop,ch,1);j+;if(ch0=8) len=strlen(chshop)-1

100、;strcpy(ch1,);j=j-2;strncat(ch1,chshop,len);strcpy(chshop,);strncat(chshop,ch1,len-1);gotoxy(13,10);printf( );gotoxy(13,10);printf(%s,chshop);ch0=getch();gotoxy(13,13);j=0;ch0=getch();while(ch0!=r) if (j=a&stri=z)stri=stri-32;fputc(stri,fp);i+;fclose(fp);fp=fopen(test,r);fgets(str,strlen(str)+1,fp);

101、printf(%sn,str);fclose(fp);-【程序 99】题目:有两个磁盘文件 A 和 B,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列), 输出到一个新文件 C 中。1.程序分析:2.程序源代码:#include stdio.hmain() FILE *fp;int i,j,n,ni;char c160,t,ch;if(fp=fopen(A,r)=NULL)printf(file A cannot be openedn);exit(0);printf(n A contents are :n);for(i=0;(ch=fgetc(fp)!=EOF;i+)ci=ch;

102、putchar(ci);fclose(fp);ni=i;if(fp=fopen(B,r)=NULL)printf(file B cannot be openedn);exit(0);printf(n B contents are :n);for(i=0;(ch=fgetc(fp)!=EOF;i+)ci=ch;putchar(ci);fclose(fp);n=i;for(i=0;in;i+)for(j=i+1;jcj)t=ci;ci=cj;cj=t;printf(n C file is:n);fp=fopen(C,w);for(i=0;in;i+) putc(ci,fp);putchar(ci)

103、;fclose(fp);-【程序 100】题目:有五个学生,每个学生有 3 门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出 平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件stud中。1.程序分析:2.程序源代码:#include stdio.hstruct student char num6;char name8;int score3;float avr; stu5;main()int i,j,sum;FILE *fp;/*input*/for(i=0;i5;i+) printf(n please input No. %d score:n,i);printf(stuNo:);scanf(%s,stui.num);printf(name:);scanf(%s,stui.name);sum=0;for(j=0;j3;j+) printf(score %d.,j+1);scanf(%d,&stui.scorej);sum+=stui.scorej;stui.avr=sum/3.0;fp=fopen(stud,w);for(i=0;i5;i+)if(fwrite(&stui,sizeof(struct student),1,fp)!=1)printf(file write errorn);fclose(fp);

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 施工组织

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