Chapter 8 Programming Exercises
1.
/*
PE 8-1
设计一个程序,统计在读到文件结尾之前读取的字符数
*/
#include
int main(void)
{
int count = 0;
while (getchar() != EOF)
count++;
printf("%d", count); //换行符也算在内
return 0;
}
这题到底是什么意思啊 = = 至今看不懂,看懂的大佬麻烦评论下
/*
PE 8-2
编写一个程序,在遇到EOF之前,把输入作为字符流读取。
程序要打印每个输入的字符及其相应的ASCII十进制值。
注意,在ASCII序列中,空格字符前面的字符都是非打印字符,要特殊处理这些字符。
如果非打印字符时换行符或制表符,则分别打印\n或\t。否则,使用控制字符表示法。
例如ASCII的1是Ctrl+A,可显示为^A。注意A的ASCII码是Ctrl+A的值加上64。
其他非打印字符也有类似的关系。除每次遇到换行符打印新的一行之外,每行打印10对值。
(注意:不同的操作系统其控制字符可能不同。)
*/
/*
PE 8-3
编写一个程序,在遇到EOF之前,把输入作为字符流读取。
该程序要报告输入的大写字母和小写字母的个数。假设大小写字母数是连续的。
或者使用ctype.c库中的分类函数更方便。
*/
#include
int main(void)
{
int count_upper = 0;
int count_lower = 0;
int ch;
while ((ch = getchar()) != EOF)
if (isupper(ch))
count_upper++;
else if (islower(ch))
count_lower++;
printf("The number of uppercase letter %d, Lower case letters%d. \n ",
count_upper, count_lower);
return 0;
}
/*
PE 8-5
修改LIsting 8.4 的猜数程序,使用更智能的猜测策略。
例如,程序最初猜 50,询问用户是猜大了、猜小了还是猜对了。
如果猜小了,那么下一次猜测的值是 50和 100的中值,也就是 25,等等。
使用二分查找(binary search)策略,如果用户没有欺骗程序,
那么程序程序很快就会猜到正确的答案。
*/
#include
#include
char get_first(void);
char get_choice(void);
int main(void)
{
int guess = 50;
int start = 1;
int end = 100;
char choice;
printf("Pick an integer form 1 to 100. I will try to guess it. \n");
printf("Uh...is your number 50?\n");
while ((choice = get_choice()) != 'y')
{
char ch;
printf("Is big or small? \n");
ch = get_first();
if (ch == 's')
{
start = guess;
guess = (start + end) / 2;
}
if (ch == 'b')
{
end = guess;
guess = (start + end) / 2;
}
printf("Uh...is your number %d?\n", guess);
}
printf("I knew i could do it! \n");
return 0;
}
char get_first(void) //进化版get_first()读取首个非空白字符
{
char ch;
ch = getchar();
while (isblank(ch))
ch = getchar();
while (getchar() != '\n')
continue;
return ch;
}
char get_choice(void)
{
char ch;
printf("Respond with a y if my guess is right and with an n if it is wrong. \n");
ch = get_first();
while (ch != 'y' && ch != 'n')
{
printf("Please respond with y or n. \n");
ch = get_first();
}
return ch;
}
写复杂了,参考答案写的简单一点,有时间再写一遍简洁版
6.
/*
PE 8-6
修改程序清单8.8中的get_first()函数,让改函数返回读取的第一个非空白字符,
并在一个简单的程序中测试。
*/
#include
#include
char get_first(void);
int main(void)
{
char ch;
ch = get_first();
printf("%c", ch);
return 0;
}
char get_first(void)
{
char ch;
ch = getchar();
while (isblank(ch))
ch = getchar();
while (getchar() != '\n')
continue;
return ch;
}
/*
修改第7章的编程练习8,用字符代替数字标记菜单的选项。
用q代替 5 作为结束输入的标记
*/
#include
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
#define TIME_BREAK 40
#define BREAK1 300
#define BREAK2 450
#define BASE1 BREAK1 * RATE1
#define BASE2 (BASE1 + (BREAK2 - BREAK1) * RATE2)
int main(void)
{
float time; //工作时间
float sum_money; //薪水
float tax; //税
float get_money; //最终得到的钱
char tag = 'b'; //初始化为2来控制第一次不打印 ERROR :Please enter a number 1~5
float BASE; //每小时的工资
do
{
printf("******************************************************************************* \n");
printf("Enter the number corresponding to the desired pay rate or action: \n");
printf("a) $8.75/hr b) $9.33/hr \n");
printf("c) $10.00/hr d) $11.20/hr \n");
printf("q) quit \n");
printf("******************************************************************************* \n");
if (tag < 'a' || tag > 'd' && tag != 'q') //主要是修改这里的条件
{
printf("ERROR :Please respond with a,b,c,d or q: ");//修改了这里的打印
}
scanf("%c", &tag);
while (getchar() != '\n')
continue;
} while (tag < 'a' || tag > 'd' && tag != 'q'); //主要是修改这里的条件
while (tag != 'q')
{
switch (tag)
{
case 'a' : BASE = 8.75; //不要忘记修改标签....
break;
case 'b' : BASE = 9.33;
break;
case 'c' : BASE = 10.00;
break;
case 'd' : BASE = 11.2;
}
printf("What is your working time? \n");
scanf("%f", &time);
printf("time is %f\n", time );//测试语句
while (time <= 0)
{
printf("invalid value please enter a positive number: \n");
scanf("%f", &time);
}
if (time > 0 && time <= TIME_BREAK)
{
sum_money = time * BASE;
}
else
{
sum_money = 40 * BASE + (time - TIME_BREAK) * 1.5 * BASE;
}
if (sum_money <= BREAK1)
{
tax = sum_money * RATE1;
}
else if (sum_money <= BREAK2)
{
tax = BASE1 + (sum_money - BREAK1) * RATE2;
}
else if (sum_money > 450)
tax = BASE2 + (sum_money - BREAK2) *RATE3;
get_money = sum_money - tax;
printf("Your salary is %.3f, and your tax is %.3f, finally you can get %.3f ", sum_money, tax, get_money);
printf("Choose another par rate tag now. \n");
scanf("%c", &tag);
}
printf("Done! \n");
return 0;
}
同样写的太乱,有时间用模块化函数在重写一遍
/*
PE 8-8
编写一个程序,显示一个提供加法、减法、乘法、除法的菜单。
获得用户选择的选项后,程序提示用户输入两个数字,然后执行用户刚才选择的操作。
该程序只接受菜单提供的选项。
程序使用float类型的变量储存用户输入的数字,如果用户输入失败,则允许再次输入。
进行除法运算时,如果用户输入 0 作为第 2 个数(除数),程序应提示用户重新输入一个新值。
该程序的一个运行示例如下:
Enter the operation of your choice:
a. add s. subtract
m. multiply d. divide
q. quit
a //输入
Enter first number: 22.4 //输入
Enter second number: one //输入
one is not a number.
Please enter a number, such as 2.5, -1.78E8, or 3: 1 //输入
22.4 + 1 = 23.4
Enter the operation of your choice:
a. add s. subtract
m. multiply d. divide
q. quit
q //输入
Bye.
*/
#include
char get_first(void);
char get_choice(void);
float get_float(void);
float add(float a, float b);
float subtract(float a, float b);
float multiply(float a, float b);
float divide(float a, float * b);
int main(void)
{
char choice;
while ((choice = get_choice()) != 'q')
{
float f_op;
float s_op;
float answer;
switch(choice)
{
case 'a' :
printf("Add: \n");
printf("Enter first number: ");
f_op = get_float();
printf("Enter second number: ");
s_op = get_float();
printf("%.2f + %.2f = %.2f \n", f_op, s_op,add(f_op, s_op));
break;
case 's' :
printf("Subtract: \n");
printf("Enter first number: ");
f_op = get_float();
printf("Enter second number: ");
s_op = get_float();
printf("%.2f - %.2f = %.2f \n", f_op, s_op, subtract(f_op, s_op));
break;
case 'm' :
printf("Multiply: \n");
printf("Enter first number: ");
f_op = get_float();
printf("Enter second number: ");
s_op = get_float();
printf("%.2f * %.2f = %.2f \n", f_op, s_op, multiply(f_op, s_op));
break;
case 'd' :
printf("Divide: \n");
printf("Enter first number: ");
f_op = get_float();
printf("Enter second number: ");
s_op = get_float();
printf("%.2f / %.2f = %.2f \n", f_op, s_op, divide(f_op, &s_op));
break;
default : printf("Program Error!");
break;
}
getchar();
}
printf("Bye. \n");
return 0;
}
char get_first(void)
{
char ch;
ch = getchar();
while (getchar() != '\n')
continue;
return ch;
}
char get_choice(void)
{
char ch;
printf("Enter the operation of your choice: \n");
printf("a. add s. subtract \n");
printf("m. multiply d. divide \n");
printf("q. quit\n");
ch = get_first();
while (ch != 'a' && ch != 's' && ch != 'm' && ch != 'd' && ch != 'q')
{
printf("Please respond with a,s,m,d, or q :");
ch = get_first();
}
return ch;
}
float get_float(void)
{
float input;
char ch;
while (scanf("%f", &input) != 1)
{
while ((ch = getchar()) != '\n')
putchar(ch);
printf(" is not a integer. \n");
printf("Please enter a number such as 2.5, -1.78E8 or 3:");
}
return input;
}
float add(float a, float b)
{
return (a + b);
}
float subtract(float a, float b)
{
return (a - b);
}
float multiply(float a, float b)
{
return (a * b);
}
float divide(float a, float * b)
{
if (*b == 0)
{
printf("Enter a number other than 0: ");
*b = get_float();
}
return a / *b;
}