復習題
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、您懷疑下面的程序里有一些錯誤。您能找出這些錯誤嗎?
第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;。
下面是一個正確的版本:
主要問題在于判斷語句(sec是否大于0?)和獲取sec值的scanf()語句之間的關系。具體地說,第一次進行判斷時,程序還沒有機會來獲得sec的值,這樣就會對碰巧處在那個內存位置的一個垃圾值進行比較。一個比較笨拙的解決方法是對sec進行初始化,比如把它初始化為1,這樣就可以通過第一次判斷。但是還有一個問題,當最后輸入0來停止程序時,在循環結束之前不會檢查sec,因而0秒的結果也被打印出來。更好的方法是使scanf()語句在進行while判斷之前執行。可以通過像下面這樣改變程序的讀取部分來做到這一點:
5、下面的程序將打印什么?
%s! C is cool!
! C is cool!
11
11
12
11
6、下面的程序將打印什么?
SOS: 4 4.00
7、下面的程序將打印出什么?
1 2 3 4 5 6 7 8 9 10(注意:每個數字占據5列的寬度)
8、修改上一個程序,讓它打印從a到g的字母。
答:
a.
a.
1 2
b.
101
102
103
104
c.
stuvw
10、下面的程序將打印什么?
這是一個構造有缺陷的程序。因為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、假定所有的變量都是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 }
答: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相比,它有什么缺點?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 }
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 }
答:(參考課后答案)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()語句。這是處理這類問題的一個常用方法。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 }
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 }
答: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 }
答: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 }
答: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、如果下面的片段是一個完整程序的一部分,它們將打印出什么?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 }
a.
1 int x = 0;
2 while(++x < 3)
3 printf("%4d", x);
b.(注意:使第二個printf()語句縮進并不能使它成為while循環的一部分。因此它只是在while循環結束之后被調用一次,我看成一個代碼塊了)2 while(++x < 3)
3 printf("%4d", x);
1 int x = 100;
2
3 while(x++ < 103)
4 printf("%4d\n", x);
5 printf("%4d\n", x);
c.2
3 while(x++ < 103)
4 printf("%4d\n", x);
5 printf("%4d\n", x);
1 char ch = 's';
2
3 while(ch < 'w')
4 {
5 printf("%c", ch);
6 ch++;
7 }
8 printf("%c\n", ch);
答: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 }
答: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、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 }
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類似)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 }
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、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 }
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、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 }
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、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 }
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、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 }
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()函數,還是用目前的知識弄不出來,啊!)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 }
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章 循環部分,原來可以這樣做,是我讀書太不用心了!
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 }
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 }