C语言程序设计教程 课后习题参考答案

更新时间:2023-12-05 21:05:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

《C语言程序设计教程》

课后习题参考答案

习题1

1. (1)编译、链接 .exe

(2)函数 主函数(或main函数) (3)编辑 编译 链接 2.

(1)-(5):DDBBC (6)-(10):ABBBC 3.

(1)答:C语言简洁、紧凑,使用方便、灵活;C语言是高级语言,同时具备了低级语言的特征;C语言是结构化程序设计语言,具有结构化的程序控制语句;C语言有各种各样的数据类型;C语言可移植性好;生成目标代码质量高,程序执行效率高。

(2)编辑、编译、链接、执行

(3)一个C程序由一或多个函数组成,一函数若干条语句构成,每条语句的末尾必须以分号结束。

(4)标识符,关键字,运算符,分隔符,常量,注释符等 4. 从键盘输入一个双精度小数,打印出它的余弦值。 #include #include main( ) {

double x;

scanf(“%lf”, &x);

printf(“%lf\\n”, cos(x) ); }

第2章 1.

(1)BDE、ACFG (2)D (3) C (4) C 2.

(1)错(2)错(3)错(4)对(5)错 3.

(1)a=3,b=-27 (2)a=11,b=6,c=6 (3)3 (4)1 0 1 0 1 1 0 (5)-9 9 8 (6)1)20 2)8 3)70 4)0 5)0 6)0 4. (1)

#include main( ) {

double r, h ,v; r = 2.5; h = 3.5;

v = 3.14*r*r*h;

printf(“v=%lf\\n”, v); } (2)

#include main( ) {

char ch;

ch = getchar( );

printf(“%c\\n”, ch + 32); } (3)

#include main( ) {

printf(“ *\\n”); printf(“ ***\\n”); printf(“ *****\\n”); printf(“*******\\n”); } (4)

#include main( ) {

double x;

scanf(“%lf”, &x);

printf(“%d , %lf\\n”, (int)x, x – (int)x ); } (5)

#include main( ) { double a=3, b=5; double result = (-2 * a + ( 4*a – b )/( 2*a + b ) )/( (a - 4*b)/(a + b) ); printf(“%lf\\n”, result); }

习题3 1.

(1)D(2)AD(3)C(4)B(5)A (6)-(10):BDACB 2.

(1)3.141593,3.1416,3.142 (2)c=K

(3)| 123.46|,|123 | (4)x= 1.23,y= 50.00 (5)0 3.

(1)scanf(%f”, c); 改为:scanf(“%f”, &c);

f = (9/5)*c+32; 改为:f = (9.0/5)*c + 32;

printf(“摄氏温度%f度相当于华氏温度%f度”, &c, &f); 改为: printf(“摄氏温度%f度相当于华氏温度%f度”, c, f); (2)

补充定义:int h; h = 500/60 改为: h = 500 / 60; m = 500% 60 改为: m = 500`;

printf(“500分钟是%d小时%d分钟,”&h, &m); 改为: printf(“500分钟是%d小时%d分钟” , h, m); 4. (1)

#include main( ) {

char x,y;

scanf(“%c%c”, &x, &y);

printf(“%d\\n”, (x-?0?) + (y-?0?) ); }

(2)

#include main( ) {

char x, y; char tmp;

printf(“Input two characters:”); scanf(“%c%c”, &x, &y);

printf(“Before swap: x=%c, y=%c\\n”, x, y);

tmp = x; x = y; y = tmp; printf(“After swap: x=%c, y=%c\\n”, x, y); } (3)

#include main( ) {

char ch;

ch = getchar( );

printf(“%c\\n”, ch - 32); }

第4章 1.

(1)-(5):CAACA 2.

(1)BBB

(2)AAABBBCCC (3)end (4)d=20 (5)s=2,t=3 (6)first third (7)y=0 y=5 y=10 y=5 3.

(1)y=?A? && ch<=?Z? ch>=?a?&&ch<=?z? ch = ch-32 (3)x>2&&x<=10 x>-1&&x<=2 (4)t=x; x=y; y=t; 4. (1)

#include main( ) {

int x, y , z, t;

scanf(“%d%d%d”, &x, &y, &z); if ( x>y )

{ t=x; x=y; y=t; } if( x > z )

{ t = x; x = z; z= t; } if( y > z )

{ t = y; y= z; z = t; }

printf(“%d %d %d\\n”, x, y ,z); } (2)

#include main( ) {

int score;

scanf(“%d”, &score);

if ( score < 0 || score > 100 ) printf(“成绩不合理\\n”); else if( score>=90 ) printf(“优秀\\n”); else if( score>=80 ) printf(“良好\\n”); else if( score >= 70 ) printf(“中等\\n”); else if( score >= 60 ) printf(“及格\\n”); else printf(“不及格\\n”); } (3)

#include main( ) {

int n;

int g,s,b,q;//各位上的数值 scanf(“%d”, &n); g = n; //个位 s = n/10; //十位 b = n/100; //百位 q = n/1000; //千位 if( n < 10 ) //一位数 { printf(“%d\\n”, 1);//位数 printf(“%d\\n”, g); //各位上的数值 }

else if ( n < 100 ) //两位数 { printf(“%d\\n”, 2);//位数 printf(“%d %d\\n”, g,s);

}

else if ( n < 1000 ) //三位数 { printf(“%d\\n”, 3);//位数 printf(“%d %d %d\\n”, g, s, b); }

else if ( n < 10000 ) //四位数 { printf(“%d\\n”, 4);//位数 printf(“%d %d %d %d\\n”, g, s, b, q); } } (4)

#include main( ) {

int n;

scanf(“%d”, &n);

if( n % 3==0 && n%5==0 && n%7==0 ) printf(“能同时被3、5、7整除\\n”); else if( n%3==0 && n%5==0) printf(“能被3和5整除\\n”); else if( n%3==0 && n%7==0 ) printf(“能被3和7整除\\n”); else if( n%5==0 && n%7==0 ) printf(“能被5和7整除\\n”);

else if( n%3==0 || n%5==0 || n%7==0 ) { if( n%3==0 ) printf(“能被3整除\\n”); else if( n%5==0 ) printf(“能被5整除\\n”); else printf(“能被7整除\\n”); } else printf(“不能被3、5、7中任一个数整除\\n”); } (5)

#include main( ) {

int carType;//车型。1代表夏利;2代表富康;3代表桑塔纳 double xiali = 2.1; //每公里价格

double fukang = 2.4; double sangtana = 2.7; double distance; //距离

double totalMoney;//总的收费

printf(\请输入您乘坐的车型:1代表夏利;2代表富康;3代表桑塔纳:\

scanf(\

printf(\请输入您乘车的总路程:\ scanf(\ if( carType == 1)//夏利 { if( distance < 3 ) totalMoney = 7.0; else totalMoney = 7 + xiali * (distance – 3); }

else if( carType == 2 ) //富康 { if( distance < 3 ) totalMoney = 8.0; else totalMoney = 8 + fukang * (distance – 3); }

else if( carType == 3 ) //富康 { if( distance < 3 ) totalMoney = 9.0; else totalMoney = 9 + sangtana * (distance – 3); }

printf(\(四舍五入)您的车费为:%.0lf\\n\

} (6)

#include main( ) {

double a, b, c;

scanf(“%lf%lf%lf”, &a, &b, &c); if( a+b>c && b+c>a && c+a>b ) { if( a==b && b==c ) printf(“等边三角形\\n”); else if( a==b || b== c || c==a ) printf(“等腰三角形\\n”);

else printf(“一般三角形\\n”); } else printf(“不能构成三角形\\n”); }

第5章

1. (1)C(2)C(3)K=36(4)C(5)B 2.

(1) 3次

(2) x>=1 && x<=10 || x>=200&&x<210 (3) e == 0 (4) 6次 (5) 10 3.

(1) 20,10 (2) 16,0

(3) 7BAB4BAB1BC (4) ABABABC (5) ******

****** ****** ****** 4.

(1) a != b (2) n / 10

(3) scanf(“%d”, &a);

5. (3) 行 int fac = 1, sum = 0; 6. (1)

#include main( ) {

char ch;

int alpha=0, space=0, digit=0, other=0; while( (ch=getchar( ) ) != ?\\n? ) { if( ch>=?A?&&ch<=?Z? || ch>=?a?&&ch<=?z?) alpha++; else if( ch>=?0? && ch<=?9?) digit++; else if( ? ? == ch ) space++; else other++; }

printf(“%d %d %d %d\\n”, alpha, digit, space, other ); } (2)

#include main( ) {

int m20, m10;

for(m20=1; m20<5; m20++) { for(m10 = 1; m10<10; m10++) if( 20*m20+10*m10 == 100 ) printf(“%d, %d\\n”, m20, m10 ); } } (3)

#include main( ) {

int x, y, z;

for(x=0; x<10; x++) for(y=0; y<10; y++) for(z=0; z<10; z++) if( x*100+y*10+z + y*100+z*10+z == 532 ) printf(“%d %d %d\\n”, x, y, z); } (4)

#include main( ) {

int row, spaceCount,starCount; int n;

scanf(\

本文来源:https://www.bwwdw.com/article/7p8t.html

Top