复习题1、确定哪个表达式为true,哪个为false。
a.100 > 3 && 'a' > 'c'
b.100 > 3 || 'a' > 'c'
c.!(100>3)
答:
a.false
b.true
c.false
2、构造一个表达式来表示下列条件:
a.number等于或大于90,但是小于100
b.ch不是字符q也不是字符k
c.number界于1到9之间(包括1和9),但是不等于5
d.number不在1到9之间
答:
a.number >= 90 && number < 100
b.ch != 'q' && ch != 'k'
c.number >= 1 && number <= 9 && number != 5
d.number < 1 || number > 9
3、下面程序中的关系表达式过于复杂,并有些错误,请简化并改正它。
#include <stdio.h>
1 int main(void)
2 {
3 int weight, height; /* weight以磅为单位,height以英寸为单位 */
4
5 scanf("%d, weight, height);
6 if(weight < 100 && height > 64)
7 if(height >= 72)
8 printf("You are very tall for your weight.\n");
9 else if(height < 72 && > 64)
10 printf("You are tall for your weight.\n");
11 else if(weight > 300 && !(weight <= 300)
12 && height < 48)
13 if(!(height >= 48))
14 printf(" You are quite short for your weight.\n");
15 else
16 printf("Your weight is ideal.\n");
17
18 return 0;
19 }
答:
第5行:应该是scanf("%d %d", &weight, &height);。在scanf()中不要忘记使用&运算符。这一行前面也应该有提示输入的语句。但第6行已经保证height>64,因此,不需要任何测试,并且
else if应该是else。
第9行:它的意思是(height<72&&height>64)。但是表达式的第一部分是不必要的,因为既然程序已经到达了这个else if,那么height必然小于72。因此一个简单的(height>64)就可以了。
第11行:条件冗余;第二个表达式(weight不是小于或等于300的)与第一个子表达式意义相同。所需要的只是一个简单的(weight>300)。但是这里还有更多的问题。第11行属于一个不正确的if!很明显,这个else是与第6行中的if相匹配的。但是根据if的“最就近原则”,它会与第7行的if相匹配。(
因此会在weight小于100并且height小于或等于64时到达第11行。这就使得在到达该语句时weight不可能超过300。)第7到10行:应该用花括号括起来。这样第11行就会是第6行而不是第7行的可选情况。而如果到第9行的else if由一个简单的else替代了,就不再需要花括号了。
第13行:应该简化为if(height<48)。其实这一行完全可以忽略,因为第12行已经作了这种测试。
第15行:这个else与第13行的if相匹配。把第13行和第14行括在花括号中可以强制使这个else与第6行的if相匹配。或者,按照建议,简单地删除第13行。
下面是一个正确的版本:
#include <stdio.h>
int main(void)
{
int weight, height; /* weight以磅为单位,height以英寸为单位 */
printf("Enter your weight in pounds and ");
printf("your height in inches.\n");
scanf("%d %d", &weight, &height);
if(weight < 100 && height > 64)
if(height >= 72)
printf("You are very tall for your weight.\n");
else
printf("You are tall for your weight.\n");
else if(weight > 300 && height < 48)
printf(" You are quite short for your weight.\n");
else
printf("Your weight is ideal.\n");
return 0;
}
4、下列每个表达式的数值是多少?
a.5 > 2
b.3 + 4 > 2 && 3 < 2
c.x >= y || y > x
d.d = 5 + (6 > 2)
e.'X' > 'T' ? 10 : 5
f. x > y ? y > x : x > y
答:
a.1
b.0
c.1(如果第一个表达式为假则第二个为真,反之亦然;只需要一个为真的表达式,结果就为真。)
d.6
e.10
f.0
5、下列程序将打印出什么?
#include <stdio.h>
int main(void)
{
int num;
for(num = 1; num <= 11; num++)
{
if(num % 3 == 0)
putchar('$');
else
putchar('*');
putchar('#');
putchar('%');
}
putchar('\n');
return 0;
}
答:
*#%*#%$#%*#%*#%$#%*#%*#%$#%*#%*#%
6、下列程序将打印出什么?
#include <stdio.h>
int main(void)
{
int i = 0;
while(i < 3)
{
switch(i++)
{
case 0: printf("fat");
case 1: printf("hat");
case 2: printf("cat");
default: printf("Oh no!");
}
putchar('\n');
}
return 0;
}
答:
fathatcatOh no!
hatcatOh no!
catOh no!
7、下列程序有什么错误?
1 #include <stdio.h>
2 int main(void)
3 {
4 char ch;
5 int lc = 0; /*统计小写字符
6 int uc = 0; /*统计大写字符
7 int oc = 0; /*统计其他字符
8
9 while((ch = getchar()) != '#')
10 {
11 if('a' <= ch >= 'z')
12 lc++;
13 else if(!(ch < 'A') || !(ch > 'Z')
14 uc++;
15 oc++;
16 }
17 printf("%d lowercase, %d uppercase, %d other, lc, uc, oc");
18 return 0;
19 }
答:
第5行到第7行的注释应该以*/结尾,或者用//来代替/*。表达式'a' <= ch >= 'z'应该被写成这样:ch >= 'a' && ch <= 'z'。或者用一种更简单也更通用的方法:包含ctype.h文件并使用islower()。顺便提一下,'a' <= ch >= 'z'在C中是合法的,只是不具有正确的意义。因为关系运算符是从左到右结合的,所以这个表达式被解释为('a' <= ch) >= 'z'。圆括号中表达式的值为1或0(真或假),然后检查这个值来看它是否大于或等于'z'的数值编码。0和1都不能满足这个条件,所以整个表达式的值总是为0(假)。在第二个判断表达式中,在第二个表达式中,||应该为&&,尽管!(ch < 'A')是合法的,而且意义也正确,但ch >= 'A'更为简单。'Z'后面需要有两个结束圆括号,而不是一个。再一次更简单的方法是使用isuper()。应该在oc++;语句前面放置一个else,否则。每输入一个字符,它都会加1。在printf()调用中的控制表达式应该用双引号引起来。
下面是一个正确的版本:
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char ch;
int lc = 0; //统计小写字符
int uc = 0; //统计大写字符
int oc = 0; //统计其他字符
while((ch = getchar()) != '#')
{
if(islower(ch))
lc++;
else if(isupper(ch))
uc++;
else
oc++;
}
printf("%d lowercase, %d uppercase, %d other", lc, uc, oc);
return 0;
}
8、下列程序将打印出什么?
/* retire.c*/
#include <stdio.h>
int main(void)
{
int age = 20;
while(age++ <= 65)
{
if((age % 20) == 0) /* age能被20整除吗?*/
printf("You are %d. Here is a raise.\n", age);
if(age = 65)
printf("You are %d. Here is your gold watch.\n", age);
}
return 0;
}
答:
无休止地打印同一行:
You are 65. Here is your gold watch.
9、当给定下述输入时,下列程序将打印出什么?
q
c
g
b
#include <stdio.h>
int main(
void)
{
char ch;
while((ch = getchar()) != '#')
{
if(ch == '\n')
continue;
printf("Step 1\n");
if(ch == 'c')
continue;
else if(ch == 'b')
break;
else if(ch == 'g')
goto laststep;
printf("Step 2\n")
;
laststep: printf("Step 3\n");
}
printf("Done!\n");
return 0;
}
答:
q
Step 1
Step 2
Step 3 (
当ch == 'q'时,laststep: printf("Step 3\n");语句也要打印出来)
c
Step 1
g
Step 1
Step 3 (
当ch == 'g'时,通过goto语句跳转到laststep: printf("Step 3\n");,前面的语句printf("Step 2\n")
;就不要打印出来了)
b
Step 1
Done!
10、重写题目9的程序,以使它表现相同的行为但不使用continue或goto。
#include <stdio.h>
int main(void)
{
char ch;
while((ch = getchar()) != '#')
{
if(ch != '\n')
{
printf("Step 1\n");
if(ch != 'c')
{
if(ch == 'b')
break;
if(ch != 'g')
printf("Step 2\n");
printf("Step 3\n");
}
}
}
printf("Done!\n");
return 0;
}
编程练习1、
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int s_ct = 0; //统计空格数
int l_ct = 0; //统计换行数
int o_ct = 0; //统计除空格和换行符之外的其他字符
char ch;
printf("Please enter text to be analyzed(# to terminate): \n");
while((ch = getchar()) != '#')
{
if(ch == ' ')
s_ct++;
if(ch == '\n')
l_ct++;
if(ch != ' ' && ch != '\n')
o_ct++;
}
printf("The number of spaces,lines,others: %d, %d, %d", s_ct, l_ct, o_ct);
return 0;
}
2、
#include <stdio.h>
int main(void)
{
char ch;
int count = 0;
printf("Please enter text to be analyzed(# to terminate): \n");
while((ch = getchar()) != '#') // 当读取\n时,又是一种什么情况,比如说要从几行来键入???
{
count++;
printf("%c/%d ", ch, ch);
if(count % 8 == 0)
printf("\n");
}
//printf("Each character and its ASCII code value:");
return 0;
}
3、
#include <stdio.h>
int main(void)
{
int even_count = 0;
float even_sum = 0;
int odd_count = 0;
float odd_sum = 0;
int number;
printf("Please enter text to be analyzed(# to terminate): \n");
while(scanf("%d", &number) == 1)
{
if(number == 0)
break;
if(number % 2 == 0)
{
even_count++;
even_sum += number;
}
else
{
odd_count++;
odd_sum += number;
}
}
printf("The number of odd: %d; The average of odd: %.2f\n", odd_count, odd_sum / odd_count);
printf("The number of even: %d; The average of even: %.2f\n", even_count, even_sum / even_count);
return 0;
}
4、
#include <stdio.h>
int main(void)
{
char ch;
int count = 0;
printf("Please enter text to be analyzed(# to terminate): \n");
while((ch = getchar()) != '#')
{
if(ch == '!')
{
count++;
printf("!!");
}
else if(ch == '.')
{
count++;
putchar('!');
}
else
putchar(ch);
}
printf("\nNumber of alternatives: %d", count);
return 0;
}
5、
#include <stdio.h>
int main(void)
{
int even_count = 0;
float even_sum = 0;
int odd_count = 0;
float odd_sum = 0;
int number;
printf("Please enter text to be analyzed(# to terminate): \n");
while(scanf("%d", &number) == 1)
{
// 此判断如何才能弄到switch里去呢???
if(number == 0)
break;
switch(number % 2)
{
case 0: even_count++;
even_sum += number;
break;
case 1: odd_count++;
odd_sum += number;
break;
}
}
printf("The number of odd: %d; The average of odd: %.2f\n", odd_count, odd_sum / odd_count);
printf("The number of even: %d; The average of even: %.2f\n", even_count, even_sum / even_count);
return 0;
}
6、
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
char prev = 0; // 前一个字符
char current = 0, ch; // 当前字符
int count = 0;
printf("Please enter text to be analyzed(# to terminate): \n");
while((ch = getchar()) != '#')
{
prev = current; // 每次都要更新前一个字符,我怎么就没想到呢???
current = ch;
if(prev == 'e' && current == 'i')
count++;
}
printf("The number of ei: %d\n", count);
return 0;
}
7、
#include <stdio.h>
#define BASE_PAY 10.00
#define WORK_OVERTIME 40
#define MULTIPLE 1.5
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
#define BREAK1 300
#define BREAK2 450
#define BASE1 (BREAK1 * RATE1)
#define BASE2 (BASE1 + (BREAK2 - BREAK1) * RATE2)
int main(void)
{
int hour;
double total, tax, net_pay;
printf("Please enter the hour used.\n");
scanf("%d", &hour);
if(hour <= WORK_OVERTIME)
{
total = hour * BASE_PAY;
if (total <= BREAK1)
{
tax = total * RATE1;
net_pay = total - tax;
}
else
{
tax = BASE1 + (total - BREAK1) * RATE2;
net_pay = total - tax;
}
}
else
{
total = BASE_PAY * WORK_OVERTIME + (hour - WORK_OVERTIME) * MULTIPLE * BASE_PAY;
if(total <= BREAK2)
{
tax = BASE1 + (total - BREAK1) * RATE2;
net_pay = total - tax;
}
else
{
tax = BASE2 + (total - BREAK2) * RATE3;
net_pay = total - tax;
}
}
printf("The total pay: %.2f; tax: %.2f; net pay: %.2f\n", total, tax, net_pay);
return 0;
}
8、
#include <stdio.h>
#define WORK_OVERTIME 40
#define MULTIPLE 1.5
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
#define BREAK1 300
#define BREAK2 450
#define BASE1 (BREAK1 * RATE1)
#define BASE2 (BASE1 + (BREAK2 - BREAK1) * RATE2)
int main(void)
{
int hour, choise;
double total, tax, net_pay;
double base_pay; // 基本工资等级不能用#define来定义了,因为它要随着程序而改变了,书上真是胡说八道
printf("*****************************************************************\n");
printf("Enter number corresponding to the desired pay rate or action:\n");
printf("1) $8.75/hr\t2) $9.33/hr\n");
printf("3) $10.00/hr\t4) $11.20/hr\n");
printf("5) quit\n");
printf("*****************************************************************\n");
printf("Please enter your choise: ");
while(scanf("%d", &choise) == 1)
{
if(choise == 5)
break;
else
switch(choise)
{
case 1:
base_pay = 8.15;
break; // break只是导致程序脱离switch语句,跳到switch之后的下一条语句!!!
case 2:
base_pay = 9.33;
break;
case 3:
base_pay = 10.00;
break;
case 4:
base_pay = 11.20;
break;
default:
printf("default choices to the user: 1 2 3 4 5\n");
printf("Please enter your choise: ");
continue; // continue导致程序跳过该循环的其余部分,其中包括switch的其余部分!!!
}
printf("Please enter the hour used: ");
scanf("%d", &hour);
if(hour <= WORK_OVERTIME)
{
total = hour * base_pay;
if (total <= BREAK1)
{
tax = total * RATE1;
net_pay = total - tax;
}
else
{
tax = BASE1 + (total - BREAK1) * RATE2;
net_pay = total - tax;
}
}
else
{
total = base_pay * WORK_OVERTIME + (hour - WORK_OVERTIME) * MULTIPLE * base_pay;
if(total <= BREAK2)
{
tax = BASE1 + (total - BREAK1) * RATE2;
net_pay = total - tax;
}
else
{
tax = BASE2 + (total - BREAK2) * RATE3;
net_pay = total - tax;
}
}
printf("The total pay: %.2f; tax: %.2f; net pay: %.2f\n", total, tax, net_pay);
printf("Please enter your choise: ");
}
return 0;
}
9、(
接受一个整数输入,然后显示所有大于或等于该数的素数,我觉得这是第7章编程练习里最难的了!!!借鉴创十三的做法终于做出来了!!!)#include <stdio.h>
#include <stdbool.h>
#include <math.h>
bool isPrime(int a); // 判断一个数a是否为素数
void printPrime(int a); // 输出所有小于或等于a的素数
int main(void)
{
int num;
printf("Please input a number: ");
scanf("%d", &num);
printPrime(num);
return 0;
}
bool isPrime(int a)
{
int i;
int j = sqrt(a);
for(i = 2; i <= j; i++)
{
if(a % i == 0)
return false;
}
return true;
}
void printPrime(int a)
{
if(a == 1)
printf("1是一个无效的输入数!!!");
else
while(a != 1)
{
if(isPrime(a))
printf("%d ", a);
a--;
}
}
10、(与练习8类似)
#include <stdio.h>
#define RATE1 0.15
#define RATE2 0.28
int main(void)
{
int choise, break_point;
double income, tax;
printf("*****************************************************************\n");
printf("Enter number corresponding to the desired tax type or action:\n");
printf("1) 单身\t2) 户主\n");
printf("3) 已婚,共有\t4) 已婚,离异\n");
printf("5) 退出\n");
printf("*****************************************************************\n");
printf("Please enter your choise: ");
while(scanf("%d", &choise) == 1)
{
if(choise == 5)
break;
else
switch(choise)
{
case 1:
break_point = 17850;
break;
case 2:
break_point = 23900;
break;
case 3:
break_point = 29750;
break;
case 4:
break_point = 14875;
break;
default:
printf("default choices to the user: 1 2 3 4 5\n");
printf("Please enter your choise: ");
continue;
}
printf("Please enter your income: ");
scanf("%lf", &income);
if(income <= break_point)
tax = income * RATE1;
else
tax = break_point * RATE1 + (income - break_point) * RATE2;
printf("your tax is %.2f\n", tax);
printf("Please enter your choise: ");
}
return 0;
}
11、
#include <stdio.h>
#define SALE1 1.25
#define SALE2 0.65
#define SALE3 0.89
#define DISCOUNT_MONEY 100
#define DISCOUNT 0.05
#define WEIGHT1 5
#define WEIGHT2 20
#define TRANSPORTATION_COST1 3.50
#define TRANSPORTATION_COST2 10.00
#define BASE 8
#define COST3 0.1
int main(void)
{
int a = 0, b = 0, c = 0;
int total_pounds;
char choise;
double total, discount, transportation_cost;
printf("*****************************************************************\n");
printf("Enter number corresponding to the desired action:\n");
printf("a) 输入所需朝鲜蓟的磅数\n");
printf("b) 输入所需甜菜的磅数\n");
printf("c) 输入所需胡萝卜的磅数\n");
printf("q) 退出订购过程\n");
printf("*****************************************************************\n");
printf("Please enter your choise: ");
while((choise = getchar()) != 'q')
{
switch(choise)
{
case 'a':
printf("输入所需朝鲜蓟的磅数: ");
scanf("%d", &a);
break;
case 'b':
printf("输入所需甜菜的磅数: ");
scanf("%d", &b);
break;
case 'c':
printf("输入所需胡萝卜的磅数: ");
scanf("%d", &c);
break;
default:
printf("输入有误,您有合适的选项:a b c q\n");
break;
}
while(getchar() != '\n')
continue;
printf("Please enter your choise: ");
}
total = SALE1 * a + SALE2 * b + SALE1 * c;
total_pounds = a + b + c;
if(total < DISCOUNT_MONEY)
discount = 0;
else
discount = total * DISCOUNT;
if(total_pounds <= WEIGHT1)
transportation_cost = TRANSPORTATION_COST1;
else if(total_pounds > 5 && total_pounds < 20)
transportation_cost = TRANSPORTATION_COST2;
else
transportation_cost = BASE + COST3 * total_pounds;
printf("所需朝鲜蓟的磅数: %d\n", a);
printf("所需甜菜的磅数: %d\n", b);
printf("所需胡萝卜的磅数: %d\n", c);
printf("订购的总磅数:%d\n", total_pounds);
printf("订购的朝鲜蓟蔬菜的费用:%.2f\n", a * SALE1);
printf("订购的甜菜蔬菜的费用:%.2f\n", b * SALE2);
printf("订购的胡萝卜蔬菜的费用:%.2f\n", c * SALE3);
printf("订单的运输和装卸费用:%.2f\n", transportation_cost);
printf("订单的折扣:%.2f\n", discount);
printf("订单的总费用(包括运输费用,而且还要减去折扣):%.2f\n", total - discount + transportation_cost);
return 0;
}