第二章 数据类型 课后习题
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        double
3. 下列哪些是合法的变量定义?
    (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_transactionsage_in_years
int number_of_transactions, age_in_years;
(b) 单精度浮点型变量total_pay,tax_payment,distanceaverage
    float total_pay, tax_payment, distance, average;
(c) 字符型变量account_type
    char account_type;
(d) 双精度浮点型变量gross_pay
    double 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,852int 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                41
B            66                42
Y            89                59
Z            90                5a
a            97                61
b            98                62
y            121                79
z            122                7a
0            48                30
1            49                31
,            44                2c
空格        32                20
11.在程序P2C中,将第14行的%d改为%c,第16行的%c改为%d。编译并运行修改后的程序。你能解释运行结果吗?(提示:请参看附录C的ASCII码表)
第三章 简单算术运算符与表达式 课后习题
1. 将下列数学方程转化为合法的C语句:
(a)   
(b)
(c)
(d)
(e)
    (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=0
3. 变量定义如第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;
switch语句必须使用break吗(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=存单元的随机值

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。