C语言程序设计 现代设计方法_第7章代码、练习题及编程题答案

代码

7.1 sum2.c
程序  数列求和(改进版)
6.1节编写了一个程序对用户输入的整数数列求和。该程序的一个问题就是所求出的和(或其中某个输入数)可能会超出int 型变量允许的最大值。如果程序运行在整数长度为16位的机器上,可能会发生下面的情况:
This program sums a series of integers.
Enter integers (0 to terminate): 10000 20000 30000 0
The sum is: -5536
求和的结果应该为60 000,但这个值不在int 型变量表示的范围内,所以出现了溢出。当有符号整数发生溢出时,结果是未定义的,在本例中我们得到了一个毫无意义的结果。为了改进这个程序,可以把变量改换成long 型。

sum2.c
/* Sums a series of numbers (using long int variables) */
#include <stdio.h>
int main(void)
{
   
	long n, sum = 0;
	printf("This program sums a series of integers.\n");
	printf("Enter integers (0 to terminate): ");
	
	scanf("%ld", &n);
	while (n != 0) {
   
		sum += n;
		scanf("%ld", &n);
	}
	
	printf("The sum is: %ld\n", sum);
	return 0;
}

这种改变非常简单:将n 和sum 声明为long 型变量而不是int 型变量,然后把scanf 和printf 函数中的转换说明由%d 改为%ld。

7.3 length.c
程序  确定消息的长度
为了说明字符的读取方式,下面编写一个程序来计算消息的长度。在用户输入消息后,程序显示长度:
Enter a message: Brevity is the soul of wit.
Your message was 27 character(s) long.
消息的长度包括空格和标点符号,但是不包含消息结尾的换行符。程序需要采用循环结构来实现读入字符和计数器自增操作,循环在遇到换行符时立刻终止。我们既可以采用scanf 函数也可以采用getchar 函数读取字符,但大多数C程序员愿意采用getchar 函数。采用简明的while 循环书写的程序如下:

/* Determines the length of a message */
#include <stdio.h>
int main(void)
{
   
	char ch;
	int len = 0;
	printf("Enter a message: ");
	ch = getchar();
	while (ch != '\n') {
   
		len++;
		ch = getchar();
	}
	printf("Your message was %d character(s) long.\n", len);
	return 0;
}

回顾有关while 循环和getchar 函数惯用法的讨论,我们发现程序可以缩短成如下形式:

length2.c
/* Determines the length of a message */
#include <stdio.h>
int main(void)
{
   
	int len = 0;
	printf("Enter a message: ");
	while (getchar() != '\n')
		len++;
	printf("Your message was %d character(s) long.\n", len);
	return 0;
}

练习题

  1. 给出下列整数常量的十进制值。
  (a) 077
  (b) 0x77
  (c) 0XABC
(a) 63  
(b) 119  
(c) 2748
  1. 下列哪些常量在C语言中不是合法的?区分每一个合法的常量是整数还是浮点数。
  (a) 010E2
  (b) 32.1E+5
  (c) 0790
  (d) 100_000
  (e) 3.978e-2
(c) and (d) are illegal constants, because of undefined numeral `9` in octal
number `0790`, and underscore in `100_000`.
(a), (b) and (e) are legal, floating point constants.
  1. 下列哪些类型在C语言中不是合法的?
  (a) short unsigned int
  (b) short float
  (c) long double
  (d) unsigned long
(b) `short float` is not a legal type. The only available floating point types
are `float`, `double` and `long double`.
  1. 如果变量c 是char 类型,那么下列哪条语句是非法的?
  (a) i += c; /* i has type int */
  (b) c = 2 * c - 1;
  (c) putchar(c);
  (d) printf(c);
(d) `printf(c);` is illegal, because the `printf` function prints strings, not
individual (numerical representations of) characters.
  1. 下列哪条不是书写数65的合法方式?(假设字符集是ASCII。)
  (a) 'A'
  (b) 0b1000001
  (c) 0101
  (d) 0x41
(b) `0b1000001` is illegal, as C does not allow binary represented numbers.
  1. 对于下面的数据项,指明char 、short 、int 、long 类型中哪种类型是足以存储数据的最小类型。
  (a) 一个月的天数
  (b) 一年的天数
  (c) 一天的分钟数
  (d) 一天的秒数
(a) Max. 31: `char`  
(b) Max. 366: `short`  
(c) Max. 1500: `short`  
(d) Max. 86760: `long`
  1. 对于下面的字符转义,给出等价的八进制转义。(假定字符集是ASCII。)可以参考附录E,其中列出了ASCII字符的数值码。
  (a) \b
  (b) \n
  (c) \r
  (d) \t
(a) `\10`  
(b) `\12`  
(c) `\15`  
(d) `\11`
  1. 重复练习题7,给出等价的十六进制转义。
(a) `\x08`  
(b) `\x0a`  
(c) `\x0d`  
(d) `\x09`

  1. 假设变量i 和变量j 都是int 类型,那么表达式i / j + ‘a’ 是什么类型?
`int`. The expression automatically promotes the `char` value `'a'` to an `int`.
  1. 假设变量i 是int 类型,变量j 是long int 类型,并且变量k 是unsigned int 类型,那么表达式i + (int)j * k 是什么类型?
`unsigned int`. `j` casts to `int`, and `k` promotes the expression to `unsigned
int`.
  1. 假设变量i 是int 类型,变量f 是float 类型,变量d 是double 类型,那么表达式i * f / d 是什么类型?
`double`, as it will promote the other types automatically to `double`.
  1. 假设变量i 是int 类型,变量f 是float 类型,变量d 是double 类型,请解释在执行下列语句时发生了什么转换?
    d = i + f;
The addition operation will require conversion of the value of `i` to `float`,
and the assignment operation will require conversion of the summed value to
`double`.

  1. 假设程序包含下列声明:
char c = '\1';
short s = 2;
int i = -3;
long m = 5;
float f = 6.5f;
double d = 7.5;

给出下列每个表达式的值和类型。

  (a) c * i   (c) f / c   (e) f - d
  (b) s + m   (d) d / s   (f) (int) f
<
  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值