王峰_C语言程序设计02
- 格式:ppt
- 大小:2.27 MB
- 文档页数:71
c程序设计语言(第2版新版)习题解答C程序设计语言(第2版新版)是计算机编程领域的经典教材,由Brian W. Kernighan和Dennis M. Ritchie共同编写,后者也是C语言的共同发明者。
这本书不仅介绍了C语言的基本语法和编程技巧,还包括了丰富的习题,帮助读者更好地理解和掌握C语言。
以下是对书中部分习题的解答。
第1章引言习题1-1:编写一个C程序,打印出“Hello, World!”。
```c#include <stdio.h>int main() {printf("Hello, World!\n");return 0;}```习题1-2:修改上述程序,使其打印出不同的字符串。
```c#include <stdio.h>int main() {printf("Welcome to C Programming!\n");return 0;}```第2章数据类型及其运算习题2-1:编写一个程序,计算两个整数的和,并打印结果。
```c#include <stdio.h>int main() {int a, b, sum;a = 5;b = 10;sum = a + b;printf("The sum of %d and %d is %d\n", a, b, sum);return 0;}```习题2-2:编写一个程序,计算两个浮点数的乘积。
```c#include <stdio.h>int main() {float x, y, product;x = 3.14159;y = 2.718;product = x * y;printf("The product of %.5f and %.5f is %.5f\n", x, y,product);return 0;}```第3章控制语句习题3-1:编写一个程序,打印从1到10的整数。
《C语言程序设计》课后习题参考答案第1章C语言及程序设计概述1.单选题(1)A (2)B (3)A (4)B (5)C2.填空题(1)//,/*…*/(2)scanf()(3)printf()3.判断题(1)对(2)错(3)错(4)对第二章:数据类型运算符与表达式1.单选题(1)C (2)D (3)C (4)B (5)C (6)A (7)B (8)D (9)A(8)D(9)A(10)B(11)C(12)C(13)D(15)B(16)C(17)A(18)A(19)B(20)C(21)C(22)D(23)C(24)A(25)D(26)B(27)C(28)B(29)B(30)A2.填空题(1)sqrt(pow(y,x)+log10(y)) (2)36(3)6(4)3 3(6)36(7)int x=8,y=8;(8)1(9)E(10)6,7,8,9(11)6(12)66,96(13)240(14)1,1,-1,-1(15)5,2,6(16)1(17)!(18)2(19)(a>0&&a<101)&&(a%3==0||a%7==0) (20)A3.程序分析题(1)2 72 74 94 412 12116(3)100 d 68 D97 a 49 1 (4)0 1 0 1 (5)2 4 6 7-0.58 7046.587.5(6)33 12 113 13 082 32(7)618 30181814.改错题(1)①无初始赋值,不能输出②数据精度丢失③少“;”号④单字符变量不能保存字符串常量⑤不能连续初始化⑥非法标识符(2)short i=38000溢出PI=3.1416 常量不能修改值Printf(“%d”,x%y) %必须是整数a*=(b+c)/=d符合赋值左边不能是表达式第三章:算法概念与顺序结构程序设计1.选择题(1)D(2)B、D(3)D(4)B(5)C(6)A(8)C(9)B(10)D2.填空题.(1)一条语句;(2)小于左右(3)%%(4)输出项列表输出控制符(5)取地址取a的地址(6)从盘获取一个字符(7)大括号(8)f=68.00(9)n1=%d\n n2=%d(10)7,5,c=33.程序分析题.(1)i=100,c=a,f=1.234000(2)65535,65536(10)1234,123.5,12345.5第四章:选择结构程序设计(1)C (2)B (3)B (4)B (5)D (6)D (7)D (8)A (9)B (10)A (11)B (12)B (13)D (14)A (15)C (16)A (17)C (18)D (19)B (20)D (21)A(23)A(24)A2.填空题.(1)1(2)5 8 8(3)4 5 99(4)2(5)10 20 0(6)2 1(7)-4(8)3(9)No(10)25(11)45 45(12)0(13)5.5(14)13(15)3第五章:循环结构程序设计(1)C(2)A(3)D(4)B(5)D(6)B(7)A A或B(8)输出4444(9)B(10)A(11)B(12)C(13)A(14)B2.填空题.(1)r=m;m=n;n=rm%n(2)3(3)-5(4)i%3==2&&i%5==3&&i%7==2 j==5j!=k(6)int s=8,j=i+2k+i+j==8(7)8(8)k&&i<=500 k/10 continue第六章:数组1.选择题(1)B(2)C(3)D(4)D(5)B(6)C(7)B(8)A(9)A(10)D2.填空题.(1)按行序优先的原则(2)0 9(4)1 2(5)6(6)前者以回车后者以空格或回车作为间隔符(7)gets()(8)&a[i](9)char k -1(10)9 83.程序分析题.(1)1 3 7 15(2)0 0 0(3)读取输入字符串中数字字符(4)①if(str1==str2) (strcmp(str1,str2)==0)②&c1[0]③m[i][k-1]第七章:函数的调用1.选择题(1)B(2)C(3)C(4)A(6)D(7)D(8)D(9)B(10)D(11)A(12)A(13)B(14)B2.填空题.(1)有参无参(2)顺序类型(3)定义调用(4)①fmax(a,N) ②s[k]=s[p](5)①age(n-1)+2 ②age(5)(6)①prt(c,n-1) ②prt(…‟,n-i) ③prt(…*‟,2*i-1) 3.程序分析题.(1)h l o(2)3(3)2 6(4)5 25。
c程序设计语言(第2版)C程序设计语言(第2版)是一本经典的计算机编程教材,由著名的计算机科学家Brian W. Kernighan和Dennis M. Ritchie共同撰写。
这本书首次出版于1978年,第2版于1988年出版。
它通常被简称为K&R,是C语言编程的权威指南,对初学者和有经验的程序员都具有极高的参考价值。
书籍概述这本书全面介绍了C语言的语法、语义和编程技巧。
它不仅涵盖了C语言的基本元素,如变量、运算符、控制结构、函数和数组,还深入探讨了更高级的主题,包括指针、结构、联合、枚举、位操作和预处理器。
第一部分:基础第一部分主要介绍了C语言的基础知识。
它解释了C语言的基本数据类型和运算符,以及如何使用它们来执行基本的算术和逻辑运算。
此外,它还介绍了控制流语句,如if语句、switch语句、while循环和for循环,这些是任何编程语言中实现条件和循环逻辑的基础。
第二部分:函数第二部分专注于函数的定义和使用。
函数是C语言中实现代码重用和模块化的关键。
这部分详细讨论了函数的定义、声明、调用以及如何传递参数。
它还介绍了递归函数的概念,这是一种特殊的函数,它在执行过程中调用自身。
第三部分:指针第三部分深入探讨了指针的概念。
指针是C语言中非常强大的特性,允许程序员直接操作内存地址。
这部分解释了指针的基本概念,包括如何声明指针、指针的算术运算以及指针与数组的关系。
此外,它还介绍了指针在函数参数传递中的应用,以及如何使用指针实现动态内存分配。
第四部分:结构、联合和枚举第四部分介绍了C语言中的复合数据类型,包括结构、联合和枚举。
结构允许将不同类型的数据组合成一个单一的数据类型,联合允许在同一内存位置存储不同的数据类型,而枚举提供了一种定义命名常量的方法。
这部分还讨论了如何使用这些复合类型来创建复杂的数据结构。
第五部分:预处理器和宏第五部分介绍了C语言的预处理器和宏。
预处理器提供了一种在编译之前处理源代码的方法,包括文件包含、条件编译和宏定义。
国家开放大学《C语言程序设计》章节测试参考答案第一章C语言概述自测练习一:C语言字符集自测练习1.在C语言字符集中,包含有全部26个英文大写字母和对应的小写字母。
(√)2.在C语言字符集中,一个大写英文字母和它的小写英文字母被视为不同的字符。
(√)3.在C语言程序中,ABC和abc被作为同一标识符使用。
(×)自测练习二:C语言字符集自测练习1.在C语言中,保留字是有专门含义和作用的,不能作为一般标识符使用。
(√)2.在C语言中,作为标识符的第一个字符只能是英文字母或下划线,不能是数字字符。
(√)3.C语言中使用的字符常量,其起止标记符是()。
a. 双引号b. 尖括号c. 单引号d. 中括号4.C语言中使用的字符串常量,其起止标记符是()。
a. 双引号b. 中括号c. 尖括号d. 单引号自测练习三:C语句分类自测练习1.在C语言中,一条简单语句的结束符是()。
a. 分号b. 空格c. 冒号d. 逗号2.在C语言中,每条复合语句的开始标记字符为()。
a. <b. [c. (d. {3.不符合C语言规定的复合语句是()。
a. {x=0;}b. {}c. {;}d. {y=10}4.C语言中的选择类语句有两条,它们是()。
a. else和caseb. if和switchc. if和elsed. switch和case自测练习四:函数分类与使用自测练习1.在每个C语言程序中都必须包含有这样一个函数,该函数的函数名为()。
a. nameb. mainc. MAINd. function2.C语言程序中的基本功能模块为()。
a. 标识符b. 函数c. 表达式d. 语句3.一个函数定义所包含的两个部分是()。
a. 函数头和函数体b. 函数原型和函数体c. 函数名和参数表d. 函数头和函数尾4.一个程序文件开始使用的每条预处理命令,其首字符必须是()。
a. #b. @c. %d. $5.在一个程序文件中,若要使用#include命令包含一个系统头文件,则此头文件所使用的起止定界符为一对()。
《全国计算机等级考试二级教程--C语言程序设计》课后习题答案第一章1.1 EXE1.2 C OBJ EXE1.3 顺序选择循环第二章一. 选择题2.1 B 2.2 D 2.3 B 2.4 A 2.5 C 2.6 A 2.7 B2.8 B 2.9 D 2.10 C 2.11 B 2.12 B 2.13 A二. 填空题2.14 11 122.15 4.2 4.22.16 { } 定义执行语句2.17 关键字用户标识符2.18 int float double2.19 float a1=1; float a2=1;2.20 存储单元2.213.52.22 (a*b)/c a*b/c a/c*b2.23 把常量10赋给变量s2.24 位 1或02.25 8 127 0111111 -128 100000002.26 32767 -32768 10000000000000002.27 10 8 16三. 上机改错题2.28#include "stdio.h"; 删除行尾的";"main(); / * main function * / 删除")"后的";",注释中的*要紧靠“/”,即应为“/*”和“*/”函数开始处遗失了一个“{”float r,s ; /*/*r is radius*/,/* s is area of circuilar*/*/ 注释符号不可嵌套使用r = 5.0 ;s = 3.14159 * r * r ;printf("%f\n",s) 行尾遗失了“;”函数结束处遗失了一个“}”2.29#include "stdio.h"main /* main function */ main后遗失了“()”{float a,b,c,v; /*a,b,c are sides, v is volume of cube */a=2.0; b=3.0; c=4.0 行尾遗失了“;”v=a*b*c;printf("%f\n", v) 行尾遗失了“;”}第三章一. 选择题3.1 C 3.2 C 3.3 D 3.4 C 3.5 D 3.6 B 3.7 C 3.8 D 3.9 A 3.10 B3.11 C 3.12 D 3.13 D 3.14 A 3.15 C 3.16 C 3.17 C 3.18 无答案 3.19 C 3.20 B二. 填空题3.21 (1)-2002500(2)i=-200,j=2500(3)i=-200j=25003.22 12 0 03.23 一条语句 ;3.24 ;3.25 100,25.81,1.89234 100 25.81 1.89234 100 25.81 1.892343.26 x=127,x= 127,x= 177,x= 7f,x= 1273.27 x=127,x=127 ,x=$127 ,x=$000127,x=%06d3.28 a=513.789215,a= 513.79,a= 513.78921500,a= 513.78921500三. 编程题和改错题3.29 修改后的程序如下:main(){double a,b,c,s,v;printf("input a,b,c:");scanf("%lf%lf%lf",&a,&b,&c);s =a*b;v=a*b*c;printf("a=%f,b=%f,c=%f\n", a,b,c);printf("s=%f,v=%f\n",s,v);}3.30#includemain(){int a=560,b=60;printf("560 minute is %d hour and %d minute.\n",a/b,a%b);}3.31#include{int a,b;a=1500;b=350;printf("a div b is : %d\n",a/b);printf("a mod b is : %d\n",a%b);}3.32#includemain(){double a,b,c,ave;printf ("input 3 double number : \n");scanf ("%lf%lf%lf",&a,&b,&c);printf ("%.1f\n",(a+b+c)/3);}3.33#includevoid main(){int a,b,c,t;printf("请依次输入整数a,b,c:");scanf("%d%d%d",&a,&b,&c);printf("\n你输入的值是: a=%d,b=%d,c=%d\n",a,b,c);t=b;b=a;a=c;c=t;printf("交换之后的值是:a=%d,b=%d,c=%d\n",a,b,c);}第四章一. 选择题4.1 A 4.2 A 4.3 A 4.4 D 4.5 C 4.6 A 4.7 B 4.8 C 4.9 D 4.10 C二. 填空题4.11 非0 04.12 < > >= <=同级 == !=同级4.13 ! && ||4.15 !4.16 a == b || a < c x > 4 || x < -44.17 14.18 x <= 0 1 > 04.19 3 2 24.20 *#三. 编程题4.22#include/* 检查日期的合法性 */int checkdate(int year, int month, int day){if(year < 1900 || year > 2005){printf("输入的年份无效!\n");return 0;}else if(month < 0 && month > 12){printf("输入的月份无效!\n");return 0;}else if(day <= 0 && day > 31){printf("输入的日期无效!\n");return 0;}else{switch(month){case 4:case 6:case 9:case 11:if(day > 30){printf("输入的日期无效!\n");return 0;}break;case 2:if((year%4 == 0 && year%100 != 0) || year%400 == 0) {if(day > 29){printf("输入的日期无效!\n");return 0;}else{if(day > 28){printf("输入的出生日期无效!\n"); return 0;}}break;}/* end of switch(m0)*/}return 1;}void main(){int y0, m0, d0; /* 生日 */int y1, m1, d1; /* 当前日期 */int years, months, days; /* 实足年龄*/printf("请输入学生的生日:");scanf("%d%d%d", &y0,&m0,&d0);if(checkdate(y0, m0, d0)){printf("请输入当前日期:");scanf("%d%d%d", &y1,&m1,&d1);/*当前日期合法性检查*/if(!checkdate(y1, m1, d1)){return;}else if(y0 > y1){printf("出生年份比当前年份晚!\n"); return;}else if(y0 == y1){if(m0 > m1){printf("出生年月比当前年月晚!\n"); return;else if(m0 == m1){if(d0 > d1){printf("出生年月日比当前年月日晚!\n"); return;}}}}/* 计算实足年龄 */years = y1 - y0;months = m1 - m0;days = d1 - d0;/* 修正实足年龄天数*/if(days < 0){months--;switch(m1){case 1:case 5:case 7:case 10:case 12:days += 30;break;case 2:case 4:case 6:case 8:case 9:case 11:days += 31;break;case 3:if((y1%4 == 0 && y1%100 != 0) || y1%400 == 0) {days += 29;}elsedays += 28;}break;}/* end of switch(m1) */}/* end of if(days < 0) *//* 修正实足年龄月数*/if(months < 0){months += 12;years--;}/* end of if(months < 0) */printf("出生日期: %d年%d月%d日\n", y0, m0, d0);printf("当前日期: %d年%d月%d日\n", y1, m1, d1);printf("实足年龄: %d年%d月%d日\n", years, months, days);return;}4.23#includevoid main(){int a;printf ("请输入一个整数 :");scanf ("%d",&a);if (a%2==0){printf ("%d 是偶数\n", a);}else{printf ("%d 是奇数\n", a);}}4.24#includevoid main()int a,b,c,temp,max;printf ("请输入三个整数 :");scanf ("%d %d %d",&a,&b,&c);temp=(a>b)? a:b;max=(temp>c)? temp:c;printf ("\n");printf ("你输入的数中最大的是 %d.\n",max); }4.25(1)不嵌套的if语句#includevoid main(){int x,y;printf("input x :");scanf("%d",&x);if ( x>-5 && x<0 ){printf("y is %d\n",y=x);}if ( x==0 ){printf("y is %d\n",y=x-1);}if ( x>0 && x<10 ){printf("y is %d\n",y=x+1);}if ( x>=10 || x<=-5){printf("error\n");}}(2)嵌套的if语句#includevoid main(){int x,y;printf("input x :");scanf("%d",&x);printf("\n");if(x < 0){if(x > -5){printf("y is %d.\n",y=x); }else{printf("error!\n");}}if(0 == x){printf("y is %d.\n",y=x-1); }if(x > 0){if(x < 10){printf("y is %d.\n",y=x+1); }else{printf("error!\n");}}}(3)if_else语句#includevoid main(){int x,y;printf("input x :");scanf("%d",&x);if( x>-5 && x<0 ){printf("y is %d.\n",y=x); }else if( x==0 ){printf("y is %d.\n",y=x-1); }else if( x>0 && x<10 ){printf("y is %d.\n",y=x+1); }else{printf("error!\n");}}(4)switch语句#includevoid main(){int x,y;printf("input x : ");scanf("%d",&x);switch (x){case -4:case -3:case -2:case -1:printf("y is %d.\n",y=x); break;case 0:printf("y is %d.\n",y=x-1); break;case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:printf("y is %d.\n",y=x+1); break;default:printf("error!\n");}}第五章一. 选择题5.1 D 5.2 C 5.3 B 5.4 C 5.5 C 5.6 B 5.7 D 5.8 A 5.9 D 5.10 D二. 填空题5.11 5 4 65.12 死循环5.13 -15.14 115.15 d=1.0 k++ k<=n5.16 x>=0 x三. 编程题5.17#includevoid main(){int i;int sig = 1;int sum = 0;for(i=1; i<=101; i++,i++){sum += sig*i;sig *= -1;}printf("sum=%d\n", sum);}5.18(1)#includevoid main(){int i;double m=1.0;double e = 1.0;for(i=1; i<50; i++){m *= i;e += 1/m;}printf("e=%f\n",e);(2)#includevoid main(){int i=1;double m=1.0;double e = 1.0;while(1/m >= 0.0004){m *= i;e += 1/m;i++;}printf("e=%f\n",e);}5.19#includevoid main(){int year;int col = 0;for(year=1600; year<=2000; year++){if((year%4 == 0 && year%100 != 0) || year%400 == 0) {printf("%d\t", year);col++;if(col%5 == 0){printf("\n");}}}printf("\n");}5.20#include#define N 7void main(){int i;int j;int m;int k = N/2;for(i=0; i {m = i-k;if(m < 0){m *= -1;}for(j=0; j {printf(" ");}for(j=0; j<2*(k-m)+1; j++){printf("*");}printf("\n");}}第六章一. 选择题6.1 B 6.2 D 6.3 A 6.4 A 6.5 B 6.6 D 6.7 D 6.8 B 6.9 A 6.10 A 6.11 C二. 填空题6.12 -16.13 16.14 ctype.h6.15 16.16 10A 20B 30C 40D6.177.29 101.298AB6.18 A7.29B101.2986.19 A B C (每个字符后有三个空格)三. 编程题6.20#include#define N 80void main(){char str[N];int iLoop = 0;gets(str);while(str[iLoop]){printf("%c-%d\t", str[iLoop],str[iLoop]); iLoop++;if(iLoop%3 == 0){printf("\n");}}printf("\n");}6.21#include#define N 80void main(){char str[N];int num = 0;int iLoop = 0;gets(str);while(str[iLoop]){if(str[iLoop] >= '0' && str[iLoop] <= '9') {num = 10*num + (str[iLoop] - '0');}iLoop++;}printf("%d\n",num);}6.22#include#include#define N 80void main()char str[N];int num = -1;do{gets(str);num++;}while(strcmp(str, "EOF"));printf("您输入了%d行字符!\n",num);}6.23#include#define N 80void main(){char str[N];int iLoop = 0;int num = 0;gets(str);while(str[iLoop] && iLoop < N){if(str[iLoop] >= 'a' && str[iLoop] <= 'z'){num++;}iLoop++;}printf("您输入了字符中有%d个小写字母!\n",num); }6.24#includevoid main(){int line;int iLoop1;int iLoop2;printf("请输入图案的行数(不大于26):");scanf("%d", &line);for(iLoop1 = 0; iLoop1 < line; iLoop1++)for(iLoop2 = 0; iLoop2 < line - iLoop1; iLoop2++) {printf(" ");}for(iLoop2 = 0; iLoop2 < 2*iLoop1+1; iLoop2++) {printf("%c",iLoop1 + 'A');}printf("\n");}}第七章一. 选择题7.1 C 7.2 C 7.3 B 7.4 C 7.5 A 7.6 D 7.7 A二. 填空题7.8 127.9 9.0000007.10 47.11 n=1 s7.12 <=y z*x7.13 1 s*i 0 f(k)三. 程序调试和编程题7.14fun(int n){ int k,yes;for(k=2; k<=n/2; k++){if(n%k == 0) { yes = 0; break;}else yes = 1;}return yes;}7.15int mymod(int a, int b){return a%b;}7.16double fun(int n){double sum = 0;int iLoop;int sig = -1;for(iLoop=1; iLoop<=n; iLoop++){sig *= -1;sum += sig*1.0/iLoop;}return sum;}7.17double fun(int n){double t = 1.0;int iLoop;long tmp;for(iLoop=2; iLoop<=n; iLoop++){tmp = iLoop*iLoop;t -= 1.0/tmp;}return t;}7.18#include#includedouble fun(double x){return x*x + 5*x + 4;}void main(){int x = 2;printf("y1=%f\n", fun(x));printf("y2=%f\n", fun(x+15));printf("y3=%f\n", fun(sin(x)));}第八章一. 选择题8.1 A 8.2 B 8.3 B 8.4 C 8.5 B 8.6 B 8.7 C 8.8 D 8.9 B 8.10 C 8.11 C 8.12 C二. 填空题8.14 7 18.15 (1)char *p=&ch; (2) p=&ch; (3)scanf("%c",p); (4)*p='A'; (5)printf("%c",*p);8.16 (1)s=p+3; (2)s=s-2 (3)50 (4)*(s+1) (5)2 (6)10 20 30 40 50三. 编程题8.17void fun(double x, double y, double *sum, double *div){*sum = x + y;*div = x - y;return;}8.18void fun(double x, double y, double z, double *max, double *min){*max = x;*min = x;if(*max < y){*max = y;}if(*max < z){*max = z;}if(*min > y){*min = y;}if(*min > z){*min = z;}return;}第九章一. 选择题9.1 D 9.2 A 9.3 A 9.4 C 9.5 C 9.6 A 9.7 B 9.8 D 9.9 C 9.10 C9.11 C 9.12 D 9.13 D 9.14 A 9.15 A 9.16 A 9.17 C 9.18 C二. 填空题9.19 9 09.21 129.22 39.23 27219.24 -850,2,09.25 k=p k9.26 (c=getchar()) c-'A'三. 编程题9.27#include#define N 81int main(){int counter[10] = {0};int iLoop = 0;char str[N];gets(str);while(str[iLoop]){if(str[iLoop] >= '0' && str[iLoop] <= '9'){counter[str[iLoop] - '0']++;}iLoop++;}for(iLoop=0; iLoop < 10; iLoop++){printf("%d - %d\n", iLoop, counter[iLoop]);}return 0;}9.28void fun(int array[], int arraysize, int start) {int iLoop;if(start < arraysize-1){if(start <=0){start = 1;}for(iLoop = start; iLoop < arraysize; iLoop++){array[iLoop-1] = array[iLoop];}}for(iLoop = 0; iLoop < arraysize; iLoop++){printf("No.%d = %d\n", iLoop, array[iLoop]);}}9.29int fun(int arry1[], int arry2[], int arrysize){int iLoop;int counter = 0;for(iLoop = 0; iLoop < arrysize; iLoop++){if(arry1[iLoop] % 2){arry2[counter++] = arry1[iLoop];}}return counter;}9.30void fun(char array[], int arraysize){int iLoop1;int iLoop2;char temp;/* 冒泡排序 */for(iLoop1 = 0; iLoop1 < arraysize - 1; iLoop1++){for(iLoop2 = 0; iLoop2 < arraysize - 1 - iLoop1; iLoop2++) {if(array[iLoop2] < array[iLoop2 + 1]){temp = array[iLoop2];array[iLoop2] = array[iLoop2 + 1];array[iLoop2 + 1] = temp;}}}}9.31#includevoid fun(int array[], int arraysize, int inertNumber) {int iLoop;int iLoop2;if(array[0] < array[arraysize-1]){for(iLoop = 0; iLoop< arraysize; iLoop++){if(array[iLoop] > inertNumber){for(iLoop2 = arraysize - 1; iLoop2 >= iLoop; iLoop2--) {array[iLoop2 + 1] = array[iLoop2];}array[iLoop] = inertNumber;break;}}if(iLoop >= arraysize){array[arraysize] = inertNumber;}}else{for(iLoop = 0; iLoop< arraysize; iLoop++){if(array[iLoop] < inertNumber){for(iLoop2 = arraysize - 1; iLoop2 >= iLoop; iLoop2--) {array[iLoop2 + 1] = array[iLoop2];}array[iLoop] = inertNumber;break;}}if(iLoop >= arraysize){array[arraysize] = inertNumber;}}}int main(){int iLoop;int a[20] = {7,6,5,3,2,1};for(iLoop = 0; iLoop < 6; iLoop++) {printf("%d ", a[iLoop]);}printf("\n");fun(a, 6, 0);for(iLoop = 0; iLoop < 7; iLoop++) {printf("%d ", a[iLoop]);}printf("\n");fun(a, 7, 4);for(iLoop = 0; iLoop < 8; iLoop++) {printf("%d ", a[iLoop]);}printf("\n");fun(a, 8, 8);for(iLoop = 0; iLoop < 9; iLoop++) {printf("%d ", a[iLoop]);}printf("\n");return 0;}9.32int fun(int number, int array[]) {int iLoop = 0;int iLoop2;int binLen;int midNumber;int div;int remain;midNumber = number;do{div = midNumber/2;remain = midNumber%2;midNumber = div;array[iLoop++] = remain;}while(midNumber);binLen = iLoop;for(iLoop2 = 0, iLoop = binLen - 1; iLoop2 < iLoop; iLoop2++, iLoop--) {midNumber = array[iLoop2];array[iLoop2] = array[iLoop];array[iLoop] = midNumber;}return binLen;}9.33#include#include#define N 15void fun(int array[], int arraysize){int x;int iLoop;int iLoop2;for(iLoop = 0; iLoop < arraysize; iLoop++){iLoop2 = 0;x = rand()%20;do{if(x == array[iLoop2] && iLoop > 0){x = rand()%20;iLoop2 = 0;}iLoop2++;}while(iLoop2 < iLoop);array[iLoop] = x;}}int main(){int a[N];int iLoop;fun(a, N);for(iLoop = 0; iLoop < N; iLoop++){printf("%d\n", a[iLoop]);}return 0;}第十章一. 选择题10.1 C 10.2 B 10.3 C 10.4 B 10.5 C 10.6 A 10.7 C 10.8 A 10.9 C 10.10 C二. 填空题10.11 GFEDCB10.12 XYZ10.13 SO10.14 1010.15 Itis10.16 strlen(str)-1 j--10.17 310.18 goodgood!三. 编程题10.19char* mygets(char *str){int iLoop = 0;char ch;while((ch=getchar()) != '\n'){str[iLoop++] = ch;}str[iLoop] = '\0';return str;}char * myputs(char *str){int iLoop = 0;while(str[iLoop]){putchar(str[iLoop++]);}putchar('\n');return str;}10.20#include#includeint fun(char *str){int len;int iLoop1;int iLoop2;int result = 1;len = strlen(str);for(iLoop1 = 0, iLoop2 = len - 1; iLoop1 < iLoop2; iLoop1++, iLoop2--) {if(str[iLoop1] != str[iLoop2]){result = 0;break;}}return result;}int main(){char a[20] = "ABCDCBA";char b[20] = "ABCDEBA";printf("%d\n", fun(a));printf("%d\n", fun(b));return 0;}10.21char fun(char *str, int pos){int len;int iLoop;char ch;len = strlen(str);if(pos > len){return NULL;}ch = str[pos];for(iLoop = pos; iLoop < len - 1; iLoop++) {str[iLoop] = str[iLoop + 1];}str[len-1] = '\0';return ch;}。
c语言程序设计基础教程课后答案C语言程序设计基础教程课后答案第一章:C语言概述1. 简述C语言的特点。
- C语言是一种结构化编程语言,具有高效、灵活、功能强大等特点。
- 它支持多种编程范式,如过程式编程、结构化编程、面向对象编程等。
- C语言具有较低的抽象级别,接近硬件,因此执行效率较高。
2. C语言的发展历史。
- C语言在1972年由丹尼斯·里奇(Dennis Ritchie)在贝尔实验室开发。
- 它最初是作为UNIX操作系统的系统编程语言而设计的。
第二章:C语言环境和基本语法1. 如何设置C语言的开发环境?- 选择一个C语言编译器,如GCC、MSVC等。
- 安装编译器并配置环境变量,以便在命令行中直接调用编译器。
2. C语言的基本语法结构是什么?- C语言的基本结构包括预处理指令、函数定义、变量声明和语句。
第三章:数据类型、运算符和表达式1. C语言中的基本数据类型有哪些?- 整型(int)、字符型(char)、浮点型(float和double)等。
2. 运算符的分类及其用法。
- 算术运算符:+、-、*、/、%等。
- 赋值运算符:=、+=、-=等。
- 比较运算符:==、!=、>、<等。
- 逻辑运算符:&&、||、!等。
第四章:控制语句1. 条件语句的用法。
- if语句用于基于条件执行代码块。
- switch语句用于多条件选择。
2. 循环语句的用法。
- while循环用于在满足条件时重复执行代码块。
- for循环用于已知迭代次数的循环。
- do-while循环先执行代码块再判断条件。
第五章:数组和字符串1. 数组的基本概念和使用。
- 数组是相同数据类型的元素集合,存储在连续的内存位置。
- 声明数组:int arr[10]; 表示一个包含10个整数的数组。
2. 字符串的表示和操作。
- 在C语言中,字符串是以空字符('\0')结尾的字符数组。
- 使用标准库函数如strcpy、strlen等操作字符串。
《C语言程序设计能力教程(第二版)》课后作业及实训题参考答案第1章进入C语言程序世界二、1. I love China!printf("we are students.\n")2. 6项目实训题参考答案1.编写一个C程序,输出以下信息:* * * * * * * * * * * * * * * * * * * *I am a student!* * * * * * * * * * * * * * * * * * * *main(){ printf("********************\n");printf(" I am a student!\n ");printf("********************\n");}2.已知立方体的长、宽、高分别是10cm、20cm、15cm,编写程序,求立方体体积。
解:main(){int a,b,c,v;a=10;b=20;c=15;v=a*b*c;printf("v=%d",v);}本程序运行结果为:v=3000第2章编制C程序的基础知识一选择题C B A B A C C二操作题,2,-8,23.000000,2.500000,-8.0000002. ABC DEFGHwhy is 21+35 equal 523.3 14 32 31 24. aa bb cc abcA N项目实训题1.定义一个符号常量M为5和一个变量n值为2,把它们的乘积输出。
#define M 5main(){ int n,c;n=2; c=M*n;printf("%d\n",c); }2.编程求下面算术表达式的值。
(1)x+a%3*(int)(x+y)%2/4,设x=2.5,a=7,y=4.7;(2)(float)(a+b)/2+(int)x%(int)y,设a=2,b=3,x=3.5,y=2.5。