谭浩强《C程序设计》例题练习+补充练习 您所在的位置:网站首页 c程序设计试题汇编第三版谭浩强 谭浩强《C程序设计》例题练习+补充练习

谭浩强《C程序设计》例题练习+补充练习

2023-07-16 18:04| 来源: 网络整理| 查看: 265

C语言学习笔记——谭浩强《C程序设计》例题练习+补充练习

1.1要求在屏幕上输出“This is a C program.”

#include int main() //int main后面忘记加() { printf("This is a C program.\n"); //换行是\n,在(“ “)里面,忘了加”;” return 0; //忘写return 0; }

1.2求两个整数之和,求123和456的和

#include int a,b,sum; int main() { a=123; b=456; sum=a+b; printf("the sum is:%d\n",sum); //%d不能少,否则结果出不来,见1.2.1。输出,(“”,sum)中,sum前面不能加% return 0; }

1.3求两个整数中的较大者 方法一:

#include int a,b,c; int main() { scanf("%d,%d",&a,&b); if(a>=b) c=a; else c=b; printf("%d",c); return c; } //不知道为什么没有用

方法二:

#include int main() { int max(int x,int y); int a,b,c; scanf("%d.%d",&a,&b); c=max(a,b); printf("max=%d\n",c); return 0; //main函数中return语句指定的返回值一般为0 } int max(int x,int y) { int z; if(x>y)z=x; else z=y; return(z); //只有通过return语句才能把求出的z值作为函数的值并返回调用他的main函数中(第8行)。不要以为在max函数中求出最大值z后就会自动的作为函数值返回调用处 } //不知道为啥,还是没有用

2.1求12345 方法一

#include int main() { int s,i; s=1; for(i=2;i=0) {x1=((-b)+sqrt(b*b-4*a*c))/(2*a); x2=((-b)-sqrt(b*b-4*a*c))/(2*a); printf("x1=%f\nx2=%f\n",x1,x2);} else printf("no answer\n"); } ---228

例题示范:

#include #include int main() { double a,b,c,disc,x1,x2,p,q; scanf("%1f%1f%1f",&a,&b,&c); disc=b*b-4*a*c; p=-b/(2.0*a); q=sqrt(disc)/(2.0*a); x1=p+q; x2=p-q; printf("x1=%7.2f\nx2=%7.2f\n",x1,x2); //7.2表示指定数据占七列,其中小数占两列 return 0; } //按照书上敲出来运行有问题 ---242

3.6用%f输出实数,只能得到6位小数。

#include int main() { double a=1.0; //a是双精度型 printf("%f\n",a/3); //a/3也是双精度性,但是用%f格式声明只能输出6位小数 } ---248

补充:用%f输出实数,得到15位小数。

#include int main() { double a=1.0; printf("%20.15f\n",a/3); } #include int main() { double a=1.0; printf("%16.15f\n",a/3); }

3.7float型数据的有效位数

#include int main() { float a; //fliat型数据只能保证6-7位有效数字 a=10000/3.0; //因此从左面开始的第7位数字(即第三位小数)以后的数字不保证正确 printf("%f\n",a); return 0; }

补充:

#include int main() { double a; //将float改成double a=10000/3.0; printf("%f\n",a); //由于%f,得到6位小数 return 0; }

3.8先后输出BOY三个字符

#include int main() { char a='B',b='O',c='Y'; putchar(a); putchar(b); putchar(c); putchar('\n'); return 0; }

补充,用ASCII码表示BOY

#include int main() { int a=66,b=79,c=89; putchar(a); putchar(b); putchar(c); putchar('\n'); return 0; } ---296

补充练手

输入三角形的三边长,求三角形面积。 #include #include void main() { float a,b,c,p; double s; printf("please input a,b and c:"); scanf("%f%f%f",&a,&b,&c); if(a+b>c && a+c>b && b+c>a) { p=(a+b+c)/2; s=sqrt(p*(p-a)*(p-b)*(p-c)); printf("the areas is:%f",s); } else printf ("the line is error\n"); }

从键盘输入一个大写字母/小写字母,要求改用小写/大写字母输入。

#include void main() { char a; printf("please input a word:"); scanf ("%c",&a); if(a>='A' && a='a' && a=0) {x1=((-b)+sqrt(b*b-4*a*c))/(2*a); x2=((-b)-sqrt(b*b-4*a*c))/(2*a); printf("x1=%f\nx2=%f\n",x1,x2);} else printf("no answer\n"); }

根据输入年、月,判断该月有多少天

#include #include int main() { int year,month; printf("please input year,month:"); scanf("%i%i",&year,&month); if((year%4 == 0 && year%100!=0)||year%400==0) { switch(month) { case 2:printf("29\n");break; case 1: case 3: case 5: case 7: case 8: case 10: case 12:printf("31\n");break; case 4: case 6: case 9: case 11:printf("30\n");break; } } else { switch(month) { case 2:printf("28\n");break; case 1: case 3: case 5: case 7: case 8: case 10: case 12:printf("31\n");break; case 4: case 6: case 9: case 11:printf("30\n");break; } } } ---390

输出所有“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于数的本身。

#include #include int main() { int s; for(s=100;s{1, 2, 3}, {4, 5, 6}}; int mb[2][3] = {{6, 3, 4}, {5, 1, 2}}; int mc[2][3] ={0}; for (i = 0; i < 2; i++) for (j = 0; j < 3; j++) mc[i][j] = ma[i][j] + mb[i][j]; for (i = 0; i < 2; i++) { for (j = 0; j < 3; j++) printf("%3d", mc[i][j]); putchar('\n'); } return (0); }

计算出1000以内的质数(第1版)

#include int main(void) { int i, no; unsigned long counter = 0; for (no = 2; no


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有