复习题
1、假定所有的变量都是int类型。找出下面每一个变量的值:
a.x = (2 + 3) * 6;
b.x = (12 + 6) / 2 * 3;
c.y = x = (2 + 3) / 4;
d.y = 3 + 2 * (x = 7 / 2);
答:
a.
x = 30
b.
x = 27
c.
x = 1
y = 1
d.
x = 3
y = 9
2、假定所有的变量都是int类型。找出下面每一个变量的值:
a.x = (int) 3.8 + 3.3;
b.x = (2 + 3) * 10.5;
c.x = 3 / 5 * 22.0;
d.x = 22.0 * 3 /5;
答:
a.
x = 6
b.
x = 52
c.
x = 0
d.
x = 13
3、您怀疑下面的程序里有一些错误。您能找出这些错误吗?
1 int main(void)
2 {
3 int i = 1,
4 float n;
5 printf("Watch out! Here come a bunch of fractions!\n");
6 while(i < 30)
7 n = 1/i;
8 printf("%f", n);
9 printf("That's all, folks!\n");
10 return;
11 }
答:
第0行:应该有#include <stdio.h>。
第3行:应该以分号而不是逗号结尾。
第6行:while语句建立一个无限循环。因为i的值保持为1,所以它总是小于30。推测一下它的意思大概是要写成while(i++ < 30)。
第6到8行:这样的缩排说明我们想要使第7行和8行组成一个代码块,但是缺少了花括号会使while循环只包括第7行。应该添加花括号。
第7行:因为1和i都是整数,所以当i为1时除法运算的结果会是1,而当i为更大的数时结果为0。使用n = 1.0/i;会使i在进行除法运算之前先转换为浮点数,这样就会产生非0的答案。
第8行:我们在控制语句中漏掉了换行符(\n),这会使数字只要可能就在一行中打印。
第10行:应该是return 0;。
下面是一个正确的版本:
1 #include <stdio.h>
2 int main(void)
3 {
4 int i = 1;
5 float n;
6 printf("Watch out! Here come a bunch of fractions!\n");
7 while(i++ < 30)
8 {
9 n = 1.0/i;
10 printf("%f\n", n);
11 }
12 printf("That's all, folks!\n");
13 return 0;
14 }
4、这是程序清单5.9的其他一种设计方法。表面上看,它使用了一个scanf()函数替代了程序清单5.9中的两个scanf()。但是该程序不令人满意。和程序清单5.9相比,它有什么缺点?
1 #include <stdio.h>
2 #define S_TO_M 60
3 int main(void)
4 {
5 int sec, min, left;
6 printf("This program converts seconds to minutes and ");
7 printf("seconds.\n");
8 printf("Just enter the number of seconds.\n");
9 printf("Enter 0 to end the program.\n");
10 while(sec > 0)
11 {
12 scanf("%d", &sec);
13 min = sec / S_TO_M;
14 left = sec % S_TO_M;
15 printf("%d sec is %d min, %d sec.\n", sec, min, left);
16 printf("Next input?\n");
17 }
18 printf("Bye!\n");
19 return 0;
20 }
答:(参考课后答案)
主要问题在于判断语句(sec是否大于0?)和获取sec值的scanf()语句之间的关系。具体地说,第一次进行判断时,程序还没有机会来获得sec的值,这样就会对碰巧处在那个内存位置的一个垃圾值进行比较。一个比较笨拙的解决方法是对sec进行初始化,比如把它初始化为1,这样就可以通过第一次判断。但是还有一个问题,当最后输入0来停止程序时,在循环结束之前不会检查sec,因而0秒的结果也被打印出来。更好的方法是使scanf()语句在进行while判断之前执行。可以通过像下面这样改变程序的读取部分来做到这一点:
1 scanf("%d", &sec);
2 while(sec > 0)
3 {
4 min = sec / S_TO_M;
5 left = sec % S_TO_M;
6 printf("%d sec is %d min, %d sec.\n", sec, min, left);
7 printf("Next input?\n");
8 scanf("%d", &sec);
9 }
第一次获取输入使用循环外部的scanf(),以后就使用在循环结尾处(也即在循环再次执行之前)的scanf()语句。这是处理这类问题的一个常用方法。
5、下面的程序将打印什么?
1 #include <stdio.h>
2 #define FORMAT "%s! C is cool!\n"
3 int main(void)
4 {
5 int num = 10;
6
7 printf(FORMAT, FORMAT);
8 printf("%d\n", ++num);
9 printf("%d\n", num++);
10 printf("%d\n", num--);
11 printf("%d\n", num);
12 return 0;
13 }
答:
%s! C is cool!
! C is cool!
11
11
12
11
6、下面的程序将打印什么?
1 #include <stdio.h>
2 int main(void)
3 {
4 char c1, c2;
5 int diff;
6 float num;
7
8 c1 = 'S';
9 c2 = 'O';
10 diff = c1 - c2;
11 num = diff;
12 printf("%c%c%c: %d %3.2f\n", c1, c2,c1, diff, num);
13 return 0;
14 }
答:
SOS: 4 4.00
7、下面的程序将打印出什么?
1 #include <stdio.h>
2 #define TEN 10
3 int main(void)
4 {
5 int n = 0;
6 while(n++ < TEN)
7 printf("%5d", n);
8 printf("\n");
9 return 0;
10 }
答:
1 2 3 4 5 6 7 8 9 10(
注意:每个数字占据5列的宽度)
8、修改上一个程序,让它打印从a到g的字母。
答:
1 #include <stdio.h>
2 #define CHARACTER 'g'
3 int main(void)
4 {
5 char ch = 'a' - 1;
6 while(ch++ < CHARACTER)
7 printf("%3c", ch);
8 printf("\n");
9 return 0;
10 }
9、如果下面的片段是一个完整程序的一部分,它们将打印出什么?
a.
1 int x = 0;
2 while(++x < 3)
3 printf("%4d", x);
b.(
注意:使第二个printf()语句缩进并不能使它成为while循环的一部分。因此它只是在while循环结束之后被调用一次,我看成一个代码块了)
1 int x = 100;
2
3 while(x++ < 103)
4 printf("%4d\n", x);
5 printf("%4d\n", x);
c.
1 char ch = 's';
2
3 while(ch < 'w')
4 {
5 printf("%c", ch);
6 ch++;
7 }
8 printf("%c\n", ch);
答:
a.
1 2
b.
101
102
103
104
c.
stuvw
10、下面的程序将打印什么?
1 #define MESG "COMPUTER BYTES DOG"
2 #include <stdio.h>
3 int main(void)
4 {
5 int n = 0;
6
7 while(n < 5)
8 printf("%s\n", MESG);
9 n++;
10 printf("That's all.\n");
11 return 0;
12 }
答:
这是一个构造有缺陷的程序。因为while语句没有使用花括号,只有printf()语句作为循环的一部分,所以程序无休止地打印消息COMPUTER BYTES DOG直到您强行关闭程序为止。
11、构造完成下面功能(或者用一个术语来说,有下面的副作用)的语句:
a.把变量x的值增加10
b.把变量x的值增加1
c.将
a与b之和的两倍赋给c
d.将a与两倍的b之和赋给c
答:
a.x = x + 10;
b.x++; or ++x; or x = x + 1;
c.c = (a + b) * 2;
d.c = a + 2 * b;
12、构造具有下面功能的语句:
a.把变量x的值减1
b.把n除以k所得的余数赋给m
c.用b减去a的差去除q,并将结果赋给p
d.用a与b的和除以c与d的乘积,并将结果赋给x
答:
a.x--; or --x; or x = x - 1;
b.m = n % k;
c.p = q / (b - a);
d.x = (a + b) / (c * d);
编程练习
1、
1 #include <stdio.h>
2 const int PARAM = 60;
3 int main(void)
4 {
5 int min, hour, left;
6
7 printf("Convert minutes to hours and minutes!\n");
8 printf("Enter the number of minutes (<=0 to quit):\n");
9 scanf("%d", &min);
10 while(min > 0)
11 {
12 hour = min / PARAM;
13 left = min % PARAM;
14 printf("%d minutes is %d hours, %d minutes.\n", min, hour, left);
15 printf("Enter next value (<=0 to quit): \n");
16 scanf("%d", &min);
17 }
18 printf("Done!\n");
19 return 0;
20 }
2、
1 #include <stdio.h>
2 int main(void)
3 {
4 int number, maxnum;
5 printf("Please enter a int number:\n");
6 scanf("%d", &number);
7 maxnum = number + 10;
8 while(number <= maxnum)
9 {
10 printf("%5d", number++);
11 }
12 printf("\n");
13 return 0;
14 }
3、(
与题目1类似) 1 #include <stdio.h>
2 const int PARAM = 7;
3 int main(void)
4 {
5 int day, week, left;
6
7 printf("Convert days to weeks and days!\n");
8 printf("Enter the number of days (<=0 to quit):\n");
9 scanf("%d", &day);
10 while(day > 0)
11 {
12 week = day / PARAM;
13 left = day % PARAM;
14 printf("%d days are %d weeks, %d days.\n", day, week, left);
15 printf("Enter next value (<=0 to quit): \n");
16 scanf("%d", &day);
17 }
18 printf("Done!\n");
19 return 0;
20 }
4、
1 #include <stdio.h>
2 #define CM_PER_INCH 0.3937
3 #define FEET_PER_INCH 12
4 int main(void)
5 {
6 float cm, inch, left;
7 int feet;
8
9 printf("Enter a height in centimeters: ");
10 scanf("%f", &cm);
11 while(cm > 0)
12 {
13 inch = cm * CM_PER_INCH;
14 feet = inch / FEET_PER_INCH;
15 left = (inch / FEET_PER_INCH - feet) * FEET_PER_INCH ;
16 printf("%.1f cm = %d feet, %.1f inches.\n", cm, feet, left);
17 printf("Enter a height in centimeters (<=0 to quit): ");
18 scanf("%f", &cm);
19 }
20 printf("bye\n");
21 return 0;
22 }
5、
1 #include <stdio.h>
2 int main(void)
3 {
4 int count, sum, limit;
5 count = 0;
6 sum = 0;
7
8 printf("Please enter a limit number: ");
9 scanf("%d", &limit);
10 while(count++ < limit)
11 {
12 sum = sum + count;
13 }
14 printf("sum = %d\n", sum);
15 return 0;
16 }
6、
1 #include <stdio.h>
2 int main(void)
3 {
4 int count, sum, limit;
5 count = 0;
6 sum = 0;
7
8 printf("Please enter a limit number: ");
9 scanf("%d", &limit);
10 while(count++ < limit)
11 {
12 sum = sum + count * count;
13 }
14 printf("sum = %d\n", sum);
15 return 0;
16 }
7、
1 #include <stdio.h>
2 float cube(float num);
3 int main(void)
4 {
5 float number;
6 printf("Please enter a number: ");
7 scanf("%f", &number);
8 printf("The cube of the %.2f is %.2f", number, cube(number));
9 return 0;
10 }
11 float cube(float num)
12 {
13 return num * num * num;
14 }
8、(
注意:我用到了<string.h>头文件中的getchar()函数,还是用目前的知识弄不出来,啊!)
1 #include <stdio.h>
2 #include <
string.h>
3 const float ONE_PARAM = 1.8;
4 const float TWO_PARAM = 32.0;
5 const float THREE_PARAM = 273.16;
6 void temperatures(
double fahrenheit);
7 int main(
void)
8 {
9 float number;
10 while(1==1)
11 {
12 printf("Please again enter a fahrenheit's temperature: ");
13 scanf("%f", &number);
14 if(
getchar() == 'q')
15 {
16 break;
17 }
18 temperatures(number);
19 }
20 printf("Done!\n");
21 return 0;
22 }
23 void temperatures(
double fahrenheit)
24 {
25 float celsius, kelvin;
26 celsius = ONE_PARAM * fahrenheit + 32.0;
27 kelvin = celsius + 273.16;
28 printf("fahrenheit is %.2f, celsius is %.2f, kelvin is %.2f.\n", fahrenheit, celsius, kelvin);
29 }
今天看到第6章 循环部分,原来可以这样做,是我读书太不用心了!
1 #include <stdio.h>
2 #include <string.h>
3 const float ONE_PARAM = 1.8;
4 const float TWO_PARAM = 32.0;
5 const float THREE_PARAM = 273.16;
6 void temperatures(double fahrenheit);
7 int main(void)
8 {
9 float number;
10
11 printf("Please again enter a fahrenheit's temperature: ");
12 while(scanf("%f", &number) == 1)
13 {
14 temperatures(number);
15 printf("Please again enter a fahrenheit's temperature: ");
16 }
17 printf("Done!\n");
18 return 0;
19 }
20 void temperatures(double fahrenheit)
21 {
22 float celsius, kelvin;
23 celsius = ONE_PARAM * fahrenheit + 32.0;
24 kelvin = celsius + 273.16;
25 printf("fahrenheit is %.2f, celsius is %.2f, kelvin is %.2f.\n", fahrenheit, celsius, kelvin);
26 }