复习题1、再次运行程序清单4.1,但是在要求您输入名字时,请输入您的名字和姓氏。发生了什么?为什么?
答:
1 #include <stdio.h>
2 #include <string.h>
3 #define DENSITY 62.4
4 int main(void)
5 {
6 float weight, volume;
7 int size, letters;
8 char name[40]; // name是一个有40个字符的数组
9
10 printf("Hi! What's your first name?\n");
11 scanf("%s", name);
12 printf("%s, what's your weight in pounds?\n", name);
13 scanf("%f", &weight);
14 size = sizeof(name);
15 letters = strlen(name);
16 volume = weight / DENSITY;
17 printf("Well, %s, your volume is %2.2f cubic feet.\n", name, volume);
18 printf("Also, your first name has %d letters, \n", letters);
19 printf("and we have %d bytes to store it in.\n", size);
20 return 0;
21 }
如果输入名字和姓氏,会输出如下结果:
Hi! What's your first name?
Li Ayun
Li, what's your weight in pounds?
Well, Li, your volume is 0.00 cubic feet.
Also, your first name has 2 letters,
and we have 40 bytes to store it in.
原因:(参考课后答案)程序不能正常工作。第一个scanf()语句只是读入您的名而没有读入您的姓,您的姓依然存储在输入“缓冲区”(缓冲区只是一块用来存放输入的临时存储区域)中。当下一个scanf()语句想要读入您的体重时,它从上次读入结束的地方开始,这样就试图把您的姓作为体重来读取。这会使scanf()失败。一方面,如果您对姓名请求做出像Li 123这样的响应,程序会使用123作为您的体重,虽然您是在程序请求体重之前输入123的。
2、假定下列每个示例都是某个完整程序的一部分。它们的打印结果分别是什么?
a.printf("He sold the painting for $%2.2f.\n", 2.345e2);
b.printf("%c%c%c\n", 'H', 105, '\41');
c.#define Q "His Hamlet was funny without being vulgar. "
printf("%s\nhas %d characters.\n", Q, strlen(Q));
d.printf("Is %2.2e the same as %2.2f?\n", 1201.0, 1201.0);
答:
a.He sold the painting for $234.50.
b.Hi!
c.His Hamlet was funny without being vulgar. (注意,与课后答案不一样,是因为细看题目的话,此句末尾有一个空格,
strlen()函数输出字符串中字符(包括空格和标点符号)的准确数目) has 43 characters.
d.Is 1.20e+003 the same as 1201.00?
3、在问题2c中,应进行哪些修改以使字符串Q引在双引号中输出?
答:
使用\"。示例如下:
printf("\"%s\"\nhas %d characters.\n", Q, strlen(Q));
4、找出下列程序中的错误。
1 define B booboo
2 define X 10
3 main(int)
4 {
5 int age;
6 char name;
7
8 printf("Please enter your first name. ");
9 scanf("%s", name);
10 printf("All right, %c, what's your age?\n", name);
11 scanf("%f", age);
12 xp = age + X;
13 printf("That's a %s! You must be at least %d.\n", B, xp);
14 rerun 0;
15 }
答:
下面是一个正确的版本:
1 #include <stdio.h>
2 #include <string.h>
3 #define B "booboo"
4 #define X 10
5 int main(void)
6 {
7 int age;
8 int xp;
9
10 char name[40];
11
12 printf("Please enter your first name. \n");
13 scanf("%s", name);
14 printf("All right, %s, what's your age?\n", name);
15 scanf("%d", &age);
16 xp = age + X;
17 printf("That's a %s! You must be at least %d.\n", B, xp);
18 return 0;
19 }
5、假设一个程序这样开始:
1 #define BOOK "War and Peace"
2 int main(void)
3 {
4 float cost = 12.99;
5 float precent = 80.0;
请构造一个printf()语句,使用BOOK、cost和percent打印下列内容:
This copy of "War and Peace" sells for $12.99.
That is 80% of list.
答:
printf("This copy of \"%s\" sells for $%.2f.\nThat is %.0f%% of list.", BOOK, cost, percent);
6、您会使用什么转换说明来打印下列各项内容?
a.一个字段宽度等于数字位数的十进制整数。
b.一个形如8A、字段宽度为4的十六进制整数。
c.一个形如232.346、字段宽度为10的浮点数。
d.一个形如2.33e+002、字段宽度为12的浮点数。
e.一个字段宽度为30、左对齐的字符串。
答:
a.%d
b.%4X
c.%10.3f
d.%12.2e
e.%-30s
对于浮点数来说,字段宽度包含了小数点右边的数字的数目7、您会使用哪个转换说明来打印下列各项内容?
a.一个字段宽度为15的unsigned long整数
b.一个形如0x8a、字段宽度为4的十六进制整数
c.一个形如2.33E+02、字段宽度为12、左对齐的浮点数
d.一个形如+232.346、字段宽度为10的浮点数
e.一个字符串的前8个字符,字段宽度为8字符
答:
a.%15lu
b.%#4x(
字段宽度应放在#和x之间)c.%-12.2E("-"修饰符使浮点数左对齐输出)
d.%+10.3f
e.%-8.8s("-"修饰符使文本左对齐输出)
8、您会使用什么转换说明来打印下列各项内容?
a.一个字段宽度为6、最少有4位数字的十进制整数
b.一个字段宽度在参数列表中给定的八进制整数
c.一个字段宽度为2的字符
d.一个形如+3.13、字段宽度等于数字中字符个数的浮点数
e.一个字符串的前5个字符,字段宽度为7、左对齐
答:
a.%6.4d
b.%*o(
此处为小写字母o,而不是数字0)c.%2c
d.%+.2f
e.%-7.5s
9、为下列每个输入行提供一个对其进行读取的scanf()语句,并声明语句中用到的所有变量或数组。
a.101
b.22.32 8.34E-09
c.linguini
d.catch 22
e.catch 22(但是跳过catch)
答:
a.
1 int num;
2 scanf("%d", &num);
b.
1 float kgs, share;
2 scanf("%f%f", &kgs, &share);
c.
1 char str[40];
2 scanf("%s", str);
d.
1 char str[40];
2 int number;
3 scanf("%s %d", str,&number);
e.
1 char str[40];
2 int number;
3 scanf("%s %d", str, &number);
10、什么是空白字符?
答:
空白字符包括空格、制表符和换行符。C使用空白字符分隔各个语言符号;scanf()使用空白字符分隔相邻的输入项。
11、假设您想在程序中使用圆括号代替花括号。以下方法可以吗?
#define ( {
#define ) }
答:
会发生替换。但不幸的是,预处理器不能区别哪些圆括号应该被替换成花括号,哪些圆括号不应该被替换成花括号。因此:
1 #include <stdio.h>
2 #define ( {
3 #define ) }
4 int main(void)
5 (
6 printf("Hello World!\n");
7 return 0;
8 )
会变为:
1 #include <stdio.h>
2 int main{void}
3 {
4 printf{"Hello World!\n"};
5 return 0;
6 }
所有圆括号都要替换为花括号。
编程练习1、
1 #include <stdio.h>
2 int main(void)
3 {
4 char name[40]; // 名字
5 char surname[40]; // 姓氏
6
7 printf("Please enter your name and surname: \n");
8 scanf("%s%s", name, surname);
9 printf("%s,%s", surname, name);
10 return 0;
11 }
2、
1 #include <stdio.h>
2 #include <string.h>
3 int main(void)
4 {
5 char name[40]; // 名字
6
7 printf("Please enter your name: \n");
8 scanf("%s", name);
9 printf("\"%s\"\n", name);
10 printf("\"%20s\"\n", name);
11 printf("\"%-20s\"\n", name);
12 printf("%*s\n", strlen(name)+3, name);
13 return 0;
14 }
3、
1 #include <stdio.h>
2 int main(void)
3 {
4 float number;
5
6 printf("Please enter a float number: \n");
7 scanf("%f", &number);
8 printf("The input is %.1f or %.1e\n", number, number);
9 printf("The input is %+.3f or %.3E\n", number, number);
10 return 0;
11 }
4、
1 #include <stdio.h>
2 int main(void)
3 {
4 /* 以厘米为单位输入身高,并以米为单位进行显示 */
5 float weight;
6 char name[40];
7
8 printf("Please enter your weight(cm): \n");
9 scanf("%f", &weight);
10 printf("Please enter your name: \n");
11 scanf("%s", name);
12 printf("%s, you are %.2f meter tall\n", name, weight/100);
13 return 0;
14 }
5、
1 #include <stdio.h>
2 #include <string.h>
3 int main(void)
4 {
5 char name[40]; // 名字
6 char surname[40]; // 姓氏
7
8 printf("Please enter your name: \n");
9 scanf("%s", name);
10 printf("Please enter your surname: \n");
11 scanf("%s", surname);
12 printf("%10s %10s\n", surname, name);
13 printf("%10d %10d\n", strlen(surname), strlen(name));
14 printf("%-10s %-10s\n", surname, name);
15 printf("%-10d %-10d\n", strlen(surname), strlen(name));
16 return 0;
17 }
结果为:(看起来有一点怪啊!!!)
Please enter your name:
Ayun
Please enter your surname:
li
li Ayun
2 4
li Ayun
2 4
6、
1 #include <stdio.h>
2 #include <float.h>
3 int main(void)
4 {
5 double dblnum = 1.0/3.0;
6 float fltnum = 1.0/3.0;
7
8 printf("%.4f\n", dblnum);
9 printf("%.12f\n", dblnum);
10 printf("%.16f\n", dblnum);
11 printf("%.4f\n", fltnum);
12 printf("%.12f\n", fltnum);
13 printf("%.16f\n", fltnum);
14 printf("double precision = %d digits\n", DBL_DIG);
15 printf("float precision = %d digits\n", FLT_DIG);
16 return 0;
17 }
7、(
定义浮点类型的时候是使用float,还是double好???) 1 #include <stdio.h>
2 #define LITRE 3.785
3 #define KM 1.609
4 int main(void)
5 {
6 float mile; // 英里数
7 float gallon; // 加仑数
8
9 printf("Please enter your mile: \n");
10 scanf("%f", &mile);
11 printf("Please enter your gallon: \n");
12 scanf("%f", &gallon);
13 printf("Miles per gallon of gasoline: %.1f\n", mile/gallon);
14 printf("Liters per 100 kilometers: %.1f\n", gallon*LITRE*100/(mile*KM));
15 return 0;
16 }