第二章数据类型课后习题1.下列哪些是合法的变量名?如果合法,你认为它是一个好的助记符(能提醒你它的用途)吗?(a) stock_code 合法、好的助记符(b) money$ 非法,$为非法字符(c) Jan_Sales 合法、好的助记符(d) X-RAY 非法,–为非法字符(e) int 非法,int为关键字(f) xyz 合法、不是好的助记符(g) 1a 非法,变量名必须以字母或下划线打头(h) invoice_total合法、好的助记符(i) john's_exam_mark非法,’为非法字符(j) default 非法,default为关键字2.请确定下列常量的数据类型:(a) 'x' char(b) -39 int(c) 39.99 double(d) -39.0 double3.下列哪些是合法的变量定义?(a) integer account_code ; 非法,无integer类型(b) float balance ; 合法(c) decimal total ; 非法,无decimal类型(d) int age ; 合法(e) double int ; 非法,int为关键字,不能作为变量名(f) char c ; 合法4.写出下列各小题中的变量定义:(a) 整型变量number_of_transactions和age_in_yearsint number_of_transactions, age_in_years;(b) 单精度浮点型变量total_pay,tax_payment,distance和averagefloat total_pay, tax_payment, distance, average;(c) 字符型变量account_typechar account_type;(d) 双精度浮点型变量gross_paydouble gross_pay;5. 为下列各小题写出最合适的变量定义:(a) 班级中的学生人数int number_of_students;(b) 平均价格float average_price;(c) 自1900年1月1日以来的天数int days_since_1900;(d) 利率百分比float interest_rate;(e) 本页中最常出现的字符char most_common_char;(f) 中国的人口总数(在2010年11月大约为1,339,724,852)int population_of_china;6. 假定有如下定义:int i ;char c ;下面哪些是合法的C语句?c = 'A' ; 合法i = "1" ; 非法,字符串不能赋值给整型i = 1 ; 合法c = "A" ; 非法,”A”为字符串,存储为’A’和’\0’两个字符c = '1'; 合法7. 写一个C程序,给第4题中的变量各赋一个值,然后以每行一个变量的形式显示这些变量的值。
#include <stdio.h>int main(void){int number_of_transactions, age_in_years;float total_pay, tax_payment, distance, average;char account_type;double gross_pay;number_of_transactions = 211;age_in_years = 66;total_pay = 3128.0f;tax_payment = 214.5f;distance = 2431.5f;average = 83.5f;account_type = 'c';gross_pay = 9313.5;printf("%d\n%d\n%.1f\n%.1f\n%.1f\n%.1f\n%c\n%.1f",number_of_transactions, age_in_years, total_pay, tax_payment, distance, average, account_type, gross_pay);return 0;}8.写一个C程序显示如下信息:**************** Hello World ****************#include <stdio.h>int main(void){printf("***************\n");printf("* Hello World *\n");printf("***************\n");return 0;}9.写一个C程序在不同的行分别显示你的姓名和家庭住址。
#include <stdio.h>int main(void){printf("张三\n");printf("黑龙江省哈尔滨市南岗区\n");return 0;}10.ASCII码用于表示计算机内存中的字母、数字和其它符号。
使用附录C中的ASCII码表查找下面每个字符的ASCII编码:'A' 'B' 'Y' 'Z' 'a' 'b' 'y' 'z' '0' '1' ',' ' ' (空格)字符十进制ASCII码十六进制ASCII码A 65 41B 66 42Y 89 59Z 90 5aa 97 61b 98 62y 121 79z 122 7a0 48 301 49 31, 44 2c空格32 2011.在程序P2C中,将第14行的%d改为%c,第16行的%c改为%d。
编译并运行修改后的程序。
你能解释运行结果吗?(提示:请参看附录C的ASCII码表)第三章 简单算术运算符与表达式 课后习题1. 将下列数学方程转化为合法的C 语句:(a)2121x x y y m --=(b)c mx y +=(c)e d c b a -=(d)9)32(5-=F C(e)221at ut s +=(a) m = (y1 – y2) / (x1 – x2); (b) y = m * x + c; (c) a = b / c – d / e; (d) C = 5 * (F – 32) / 9.0; (e) s = u * t + a * t * t / 2.0;2. 有如下变量定义:int a = 1, b = 10, c = 5 ; int d ;下面每条语句执行后d 的值为?(a) d = b / c + 1 ; d=3 (b) d = b % 3 ;d=1(c) d = b - 3 * c / 5 ;d=7(d) d = b * 10 + c - a * 5 ; d=100(e) d = ( a + b - 1 ) / c ; d=2(f) d = ( ( -a % c ) + b ) * c ; d=45(g) d = --a ; d=03.变量定义如第2题,请改正下列C语句中的错误:(a) d = 2(b + c) ; d = 2 * (b + c)(b) d = 5b + 9c ; d = 5 * b + 9 * c;(c) d = b - 3 X 19 ; d = b – 3 * 19;(d) d = b.c + 10 ; d = b * c + 10;(e) d = ( a + b ) / c ; 无错误4.为下列任务写出合适的C语句:(a) 将num1加1,并将结果放回到num1中num1 = num1 + 1;或num1++;(b) 将num1加2,并将结果放回到num2中num2 = num1 + 2;(c) 将num2加2,并将结果放回到num2中num2 = num2 + 2;或num2 += 2;(d) 将num1减1,并将结果放回到num1中num1 = num1 – 1;或num1--;(e) 将num2减2,并将结果放回到num2中num2 = num2 – 2;或num2 -= 2;5.有如下定义:int a = 12, b = 0, c = 3 ;int d ;下列每条语句执行后a、b、c和d的值各是什么?(a)a++ ; a=13 b=0 c=3 d=内存单元的随机值(b)b-- ; a=12 b=-1 c=3 d=内存单元的随机值(c) d = ++c ; a=12 b=0 c=4 d=4(d) d = c-- ; a=12 b=0 c=2 d=3(e) d = a++ - 2 ; a=13 b=0 c=3 d=10(f) d = a++ + b++ - c-- ; a=13 b=1 c=2 d=96.有如下定义:int a = 1, b = 2, c = 3 ;下列每条语句执行后a、b、c的值各是什么?(a) a += b ; a=3 b=2 c=3(b) a /= 3 ; a=0 b=2 c=3(c) a *= c ; a=3 b=2 c=3(d) a %= 2 ; a=1 b=2 c=3(e) a += b+1 ; a=4 b=2 c=3(f) a += ++b ; a=4 b=3 c=37.有如下定义:char ch_val ; int int_val ; short short_val ;float float_val ; double double_val ;unsigned int unsigned_int_val ;下面哪些可能因为赋值类型自动转换而损失数据?(a) short_val = int_val ; 可能损失精度(b) int_val = ch_val ; 不能损失精度(c) double_val = float_val ; 不能损失精度(d) int_val = float_val ; 可能损失精度(e) int_val = unsigned_int_val ; 可能损失精度8.和第7题的变量定义一样,下列各表达式的数据类型各是什么?(a) int_val * float_val ; double(b) float_val + int_val / 100 ; double(c) ch_val + short_val + int_val ; int(d) (double)int_val + double_val + float_val ; double(e) (int)float_val * float_val / int_val ; double(f) int_val + 3.0 ; double9.有如下变量定义:int a = 5, b = 4 ;float c = 3.0, d ;下列每小题中的d的值为?(a) d = a / b ; d=1.0(b) d = (float)a / b ; d=1.25(c) d = c / b ; d=0.75(d) d = (int)c / b ; d=0.0(e) d = a / 2 ; d=2.0(f) d = a / 2.0 ; d=2.5(g) d = (float)a / 2 ; d=2.5(h) d = (int)c % 2 ; d=1.010.写一个程序计算长为11.5厘米,宽为2.5厘米,高为10厘米的盒子的体积和表面积。