C语言前驱和后继字母,C语言程序设计练习题目

《C语言程序设计练习题目》由会员分享,可在线阅读,更多相关《C语言程序设计练习题目(14页珍藏版)》请在人人文库网上搜索。

1、C语言程序设计练习题目1、求两数之和#include “stdio.h”void main( )int a, b, sum;/* 定义变量 */scanf (“%d %d”,&a, &b);/* 给变量赋值 */sum = a+b;/* 求a与b的和 */printf(“sum is %dn”, sum);/* 输出结果 sum is 579 */2、求两数中的大数# include “stdio.h” /* 包含头文件 */void main()/* 主函数 */int a, b, c;/* 定义变量 */scanf(“%d %d”, &a, &b);/* 输入变量a,b的值 */c = m。

2、ax (a, b);/* 调用函数,将得到的值赋给c */printf(“max = %dn”, c);/* 输出c的值 */int max(int x, int y) /* 子函数,定义max函数,函数值为整型,x、y为形参,整型*/int z;/* 函数中要用到的内部变量z,也要加以定义。*/if (xy)z = x;/* 比较x、y的大小,如果x大于y,则执行z = x */else z = y;/* 否则执行z = y; */return (z);/* 将z的值返回,通过max带回调用处 */3、输入一个小写字母,打印其大写字母及其前导字母与后续字母分析:小写字母的ASCII码比对应的。

3、大写字母大32(例A、a的ASCII码分别是65、97)# include “stdio.h”void main ()char c;printf (“请输入任意一个小写字母:”);c = getchar();printf(“%c, %c, %c n”, c, c-1, c+1);printf(“%c, %c, %c n”, c-32, c-33, c-31);4、求方程ax2 +bx + c = 0的根,设b2 -4ac 0。(其中a,b,c的值由键盘输入)# include “ stdio.h” /*头文件中包含输入输出函数printf, scanf*/# include “math.h”v。

4、oid main ()float a, b, c, x1, x2, dt, p, q;printf (“输入a, b, c: ”);scanf (“%f %f %f”, &a, &b, &c);dt = sqrt (b*b 4*a*c);p = -b / (2*a);q = dt / (2 * a);x1 = p+q;x2 = p-q;printf(“方程 %6.2fX*X + %6.2fX + %6.2f = 0的根为:n”, a, b, c);printf(“x1 = %f t x2 = %fn”, x1, x2);5、鸡兔同笼。已知鸡兔总头数为h,总脚数为f,求鸡兔各多少只?# incl。

5、ude void main( )int x, y, h, f;printf (“输入h, f : ”);scanf (“%d %d”, &h, &f);x = 2*h f/2;y = f/2 h;printf(“头 = %d, 脚 = %d n”, h, f);printf(“鸡 = %d, 兔 = %d n”, x, y);6、输入一个整数a,判断它是偶数还是奇数?# include void main() int aprintf (“t Input a number: ”);scanf (“%d”, &a);if (a%2 = 0)printf(“nt%d is evenn”, a);el。

6、seprintf (“nt%d is oddn”, a);7、任给a,b,c三个数,按从大到小的顺序输出# include void main ()int a, b, c, t;printf (“input a, b, c: ”);scanf (“%d, %d, %d”, &a, &b, &c);if ( avoid main () int score;printf (“input a score: ”);scanf (“%d”, &score);if ( score = 90) printf (“优秀n”);else if (score = 80) printf (“良好n”);else i。

7、f (score = 70) printf (“中n”);else if (score = 60) printf (“及格n”);else printf (“不及格n”);9、设平面上一点M,其坐标为(x,y),若M落在圆心在坐标原点的单位圆上,则置key = 1,落在圆外置key = 2,落在圆内置key = 0。yrM (x, y)01x分析:1) 求点M到原点的距离r2) 比较r和单位圆半径的大小,从而得到相应的key值。输入x、yr 1 ?是 否key = 2r = 1 ?是 否key = 1key = 0输出key# include # include void main()floa。

8、t x, y, r;int key;printf (“input x, y: ”);scanf (“%f, %f”, &x, &y);r = sqrt ( x*x + y*y );if ( r1 ) key = 2else if ( r =1) key = 1;else key = 0;printf (“(%.1f, %.1f ) : %d n”, x, y, key);执行结果: input x, y: 1.5, 0.3(1.5, 0.3) : 210、计算函数:# include void main ()float x, y;printf (“t input x: ”);scanf (“%。

9、f”, &x);if (x=-3.0 & xvoid main()int year, leap;printf ( “Which year ? ”);scanf (“%d”, &year);if (year % 400 = 0 ) leap = 1;elseif (year % 4 = 0)if (year % 100 = 0)leap = 0;else leap = 1;else leap = 0;if (leap = 1)printf (“%d 年是闰年n”, year);elseprintf ( “%d 年不是闰年n”, year);12、任意输入一个字符,若是大写字母将其转换成小写字母。。

10、# include void main()char ch;ch = getchar();ch = (ch=A & ch void main () float s, f, p, w, d;printf (“输入单价、距离和货物重量:”);scanf (“%f, %f, %f”, &p, &s, &w);switch (int) s/250)/* 强制类型转换 */case 0:d = 0; break;case 1:d = 0.02; break;case 2: case 3:d = 0.05; break;case 4: case 5: case 6: case 7:d = 0.08; bre。

11、ak;case 8: case 9: case 10: case 11:d = 0.1; break;default:d = 0.15;f = p * s * w * (1-d);printf (“总运费 %.2fn”, f );14、设计一个简单的计算器,完成两个数的加减乘除分析:l 输入二个数及运算符l switch表达式:运算符l 常量:+, -, *, /# include void main()float x, y;char op;printf (“enter x, opration, y: “);scanf(“%f%c%f”, &x, &op, &y); /* 输入数据时不能有空格。

12、 */switch (op);case +:printf (“%.2f%c%.2f = %.2fn”, x, op, y, x+y);break;ca se -:printf (“%.2f%c%.2f = %.2fn”, x, op, y, x-y);break;ca se *:printf (“%.2f%c%.2f = %.2fn”, x, op, y, x*y);break;ca se /:if (y = 0.0)printf(“errorn”);elseprintf (“%.2f%c%.2f = %.2fn”, x, op, y, x/y);break;15、输入三角形的三边长,求三角形。

13、面积。假设:三个边长a,b,c能构成三角形。#include#includevoid main()float a,b,c,s,area;scanf(“%f,%f,%f,&a,&b,&c);s=1.0/2*(a+b+c);area=sqrt(s*(s-a)*(s-b)*(s-c);printf(“a=%7.2f, b=%7.2f, c=%7.2f, s=%7.2fn”,a,b,c,s);printf(“area=%7.2fn”,area);16、从键盘输入一个大写字母,要求改用小写字母输出。 void () ,;();(,);(,);17、输入两个实数,按代数值由小到大的顺序输出这两个数。#in。

14、cludevoid main()float a,b,t;scanf(%f,%f,&a,&b);if(ab)t=a;a=b;b=t;printf(%5.2f,%5.2fn,a,b);18、有一个函数 ,编一程序,输入一个x值,输出y值。 #include void main ()int x,y;scanf(%d,&x);if(x#include void main ( ) float a,b,c,disc,x1,x2,realpart,imagpart;scanf(%f,%f,%f,&a,&b,&c);printf(the equation );if(fabs(a)1e-6)x1=(-b+sqr。

15、t(disc)/(2*a);x2=(-b-sqrt(disc)/(2*a);printf(has distinct real roots:%8.4f and %8.4fn,x1,x2);elserealpart=-b/(2*a);imagpart=sqrt(-disc)/(2*a);printf(has complex rootsn);printf(%8.4f+%8.4fin,realpart,imagpart);printf(%8.4f-%8.4fin,realpart,imagpart);20、键盘输入半径,求圆周长。21、键盘输入半径,求圆面积。22、输入4个整数,要求按照由小到大的顺序。

16、输出。23、使用中间变量实现两个变量的交换。#includevoid main()int a,b,t;printf(input two datas:);scanf(%d,%d,&a,&b);/*输入两个数*/t=a;/*实现a和b的互换,用t作中间变量*/a=b;b=t;printf(output the two datas:%d,%dn,a,b);/*输出交换后的两个数*/24、输出字符的前驱和后继#includevoid main()char c1,c,c2;printf(plese input character :);c=getchar();/*getchar函数得到的字符赋给变量c*。

17、/c1=c-1;/*前驱字符*/c2=c+1;/*后继字符*/printf(%c %d ,%c %d ,%c %d n,c1,c1,c,c,c2,c2);25、摄氏度和华氏度之间的转换#includevoid main()float c,f;/*定义两个浮点型变量*/printf(please input Celsius temperature:);scanf(%f,&c); /*输入摄氏温度*/f=(9.0/5.0)*c+32;/*根据公式,求出华氏温度*/printf(Fahrenheit is %5.2fn,f);26、分解三位整数的各位数字#includevoid main()int 。

18、num;int hundred,ten,indiv;printf(plese input num(100999):);scanf(%d,&num);/*输入三位的整数*/hundred=num/100;/*百位的数字*/ten=num/10%10;/*十位的数字*/indiv=num%10; /*个位的数字*/printf(hundred is %d,ten is %d,indiv is %dn,hundred,ten,indiv);27、输入3个学生的成绩,求这三个学生的总成绩和平均成绩。#include void main() int a,b,c,sum;float ave;printf(。

19、input three students score:);scanf(%d%d%d,&a,&b,&c); /*输入三个学生成绩*/sum=a+b+c;/*求总成绩*/ave=sum/3.0;/*求平均成绩*/printf(sum=%4dt Average=%5.2fn,sum,ave);28、实现小数点后第三位的四舍五入,也就是实数后面有2个有效数字#includestdio.hvoid main()double x; /*变量声明*/printf(inputx(double):);/*提示信息*/scanf(%lf,&x);printf(n);x=(int)(x*100+0.5); /*实现。

20、第三位的四舍五入*/x/=100;/*复合的赋值语句*/printf(x=%.2fn,x);29、(P28)输入一个不多于5位的正整数,要求:1)求它是几位数;2)分别输出每一位数字;3)按逆序输出各位数字。3044、教材第六章P129,习题6.16.154549、教材第六章P124,例题6.66.105064、教材第七章P152,习题7.17.156571、教材第七章P124,例题7.37.97289、教材第七章P202,习题8.18.1890、打印九九乘法表#include main( ) int i,j;for(i=1;i#include void main()double y;int 。

21、x,m;for(y=1;y=-1;y-=0.1) /*y为列方向,值从1到-1,步长为0.1*/m=acos(y)*10;/*计算出y对应的弧度m,乘以10为图形放大倍数*/for(x=1;xvoid main()int i;long sum=0;/*定义一个长整型变量sum用来存放和*/for(i=1;i=i)/*整数i是素数:输出,计数器加1*/printf(%6d,i); counter+;97、实数数列求和,数列为(正整数n从键盘输入)#include#includevoid main()int i,n;double s=0;/*变量s用来存放累加的和*/printf(Enter n:。

22、);/*输入提示*/scanf(%d,&n);for(i=1;ib)/*如果第一个数字大于第二个数字*/pmax=&a;/*指针变量赋值*/pmin=&b;/*指针变量赋值*/elsepmax=&b;/*指针变量赋值*/pmin=&a;/*指针变量赋值*/100、编写程序打开一个文件,并把输入的字符写入该文件。#includemain()FILE *fp;char a=A,b=B,c=C;if (fp=fopen (c:file1.txt,w)=NULL) printf (cannot open filen);exit(0);fputc (a,fp);/*一次只可以写入一个字符*/fputc (b,fp);/*写入完毕以后,文件指针自动后移到下一个写入位置*/fputc (c,fp);fputc (0xff,fp);/*输入字符的末尾加上文件结束标志EOF*/fclose (fp);/*关闭文件*。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值