学完C语言之后,我就去阅读《C Primer Plus》这本经典的C语言书籍,对每一章的编程练习题都做了相关的解答,仅仅代表着我个人的解答思路,如有错误,请各位大佬帮忙点出!
1.设计一个程序,统计在读到文件结尾之前读取的字符数。
#include <stdio.h>
int main(void)
{
int ch;
int count = 0;
printf("请输入一些字符:");
while ((ch = getchar()) != EOF)
{
count++;
}
printf("Characters: %d\n", count);
return 0;
}
2.编写一个程序,在遇到 EOF 之前,把输入作为字符流读取。程序要 打印每个输入的字符及其相应的ASCII十进制值。注意,在ASCII序列中,空 格字符前面的字符都是非打印字符,要特殊处理这些字符。如果非打印字符 是换行符或制表符,则分别打印\n或\t。否则,使用控制字符表示法。例 如,ASCII的1是Ctrl+A,可显示为^A。注意,A的ASCII值是Ctrl+A的值加上 64。其他非打印字符也有类似的关系。除每次遇到换行符打印新的一行之 外,每行打印10对值。(注意:不同的操作系统其控制字符可能不同。)
#include <stdio.h>
int main(void)
{
int ch;
int i = 0;
printf("请输入一些字符:");
while ((ch = getchar()) != EOF)
{
if (i++ == 10)
{
printf("\n");
i = 1;
}
if (ch >= 32) //空格的十进制ASCII码;
{
printf("%c-%d ", ch, ch);
}
else if (ch == '\n')
{
printf("\\n-\\n\n");
i = 0;
}
else if (ch == '\t')
{
printf("\\t-\\t ");
}
else
{
printf("%c-%c ", ch, ch + 64);
}
}
printf("Done.\n");
return 0;
}
3.编写一个程序,在遇到 EOF 之前,把输入作为字符流读取。该程序 要报告输入中的大写字母和小写字母的个数。假设大小写字母数值是连续 的。或者使用ctype.h库中合适的分类函数更方便。
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int ch;
int lowernum = 0;
int uppernum = 0;
printf("请输入一些字符:");
while ((ch = getchar()) != EOF)
{
if(islower(ch))
{
lowernum++;
}
if (isupper(ch))
{
uppernum++;
}
}
printf("大写字母为%d,小写字母个数为%d\n",uppernum,lowernum);
return 0;
}
4.编写一个程序,在遇到EOF之前,把输入作为字符流读取。该程序要 报告平均每个单词的字母数。不要把空白统计为单词的字母。实际上,标点 符号也不应该统计,但是现在暂时不同考虑这么多(如果你比较在意这点, 考虑使用ctype.h系列中的ispunct()函数)。
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
int main(void)
{
bool inword = false;
int ch, words = 0, letter = 0;
float ave = 0.0;
printf("请输入一些字符:");
while ((ch = getchar()) != EOF)
{
if (ispunct(ch))
{
continue;
}
if (isalpha(ch))
{
letter++;
}
if (!isspace(ch) && !inword)
{
inword = true;
words++;
}
if (isspace(ch) && inword)
{
inword = false;
}
}
ave = 1.0 * letter / words;
printf("总单词数: %d\n", words);
printf("总字母数: %d\n", letter);
printf("平均每个单词的字母数为:%f\n",ave);
return 0;
}
5.修改程序清单8.4的猜数字程序,使用更智能的猜测策略。例如,程序 最初猜50,询问用户是猜大了、猜小了还是猜对了。如果猜小了,那么下一 次猜测的值应是50和100中值,也就是75。如果这次猜大了,那么下一次猜 测的值应是50和75的中值,等等。使用二分查找(binary search)策略,如 果用户没有欺骗程序,那么程序很快就会猜到正确的答案。
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int high = 100;
int low = 1;
int guess = (high + low) / 2;
int response;
printf("Pick an integer from 1 to 100. I will try to guess ");
printf("it.\nRespond with a y if my guess is right, with");
printf("\na h if it is high, and with an l if it is low.\n");
printf("Uh...is your number %d?\n", guess);
while ((response = getchar()) != 'y')
{
if (response == '\n')
{
continue;
}
if (tolower(response) != 'h' && tolower(response) != 'l')
{
printf("I don't understand that response. Please enter h for\n");
printf("high, l for low, or y for correct.\n");
continue;
}
if (tolower(response) == 'h')
{
high = guess - 1;
}
else if (tolower(response) == 'l')
{
low = guess + 1;
}
guess = (high + low) / 2;
printf("Well, then, is it %d?\n", guess);
}
printf("I knew I could do it!\n");
return 0;
}
6.修改程序清单8.8中的get_first()函数,让该函数返回读取的第1个非空 白字符,并在一个简单的程序中测试。
#include <stdio.h>
#include <ctype.h>
int get_first(void)
{
int ch;
do
{
ch = getchar();
} while (isspace(ch));
while (getchar() != '\n')
continue;
return ch;
}
int main(void)
{
int ch;
printf("Please enter some characters ('#' to quit):");
while ((ch = get_first()) != '#')
{
printf("Result: %c\n", ch);
printf("You can enter again ('#' to quit):\n");
}
printf("Done.\n");
return 0;
}
7.修改第7章的编程练习8,用字符代替数字标记菜单的选项。用q代替5 作为结束输入的标记。
#include <stdio.h>
#include <stdlib.h>
#define EXTRA_HOUR 1.5
#define BASE_TAX 0.15
#define EXTRA_TAX 0.2
#define EXCEED_TAX 0.25
void menu(void)
{
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");
}
void cal(float base_salary, int hours)
{
float salary = 0.0, tax = 0.0, taxed_salary = 0.0;
if (hours <= 30)
{
salary = hours * base_salary;
tax = salary * BASE_TAX;
taxed_salary = salary - tax;
}
else if (hours <= 40)
{
salary = hours * base_salary;
tax = 300 * BASE_TAX + (salary - 300) * EXTRA_TAX;
taxed_salary = salary - tax;
}
else
{
salary = (40 + (hours - 40) * EXTRA_HOUR) * base_salary;
tax = 300 * BASE_TAX + (salary - 300) * EXTRA_TAX + (salary - 450) * EXCEED_TAX;
taxed_salary = salary - tax;
}
printf("salary:%.2lf,tax:%.2lf,taxed_salary:%.2lf\n", salary, tax, taxed_salary);
}
int main(void)
{
int hours = 0;
char input = 0;
menu();
while (1)
{
scanf("%c`", &input);
switch (input)
{
case 'a':
printf("请输入工作小时:");
getchar();
scanf("%d", &hours);
cal(8.75, hours);
break;
case 'b':
printf("请输入工作小时:");
getchar();
scanf("%d", &hours);
cal(9.33, hours);
break;
case 'c':
printf("请输入工作小时:");
getchar();
scanf("%d", &hours);
cal(10.00, hours);
break;
case 'd':
printf("请输入工作小时:");
getchar();
scanf("%d", &hours);
cal(11.20, hours);
break;
case 'q':
exit(0);
break;
}
}
return 0;
}
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 an 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
d
Enter first number: 18.4
Enter second number: 0
Enter a number other than 0: 0.2
18.4 / 0.2 = 92
Enter the operation of your choice:
a. add s. subtract
m. multiply d. divide
q. quit q
Bye.
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
int get_first(void)
{
int ch;
do
{
ch = getchar();
} while (isspace(ch));
while (getchar() != '\n')
continue;
return ch;
}
int get_choice(void)
{
int 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 enter with a,s,m,d or q :");
ch = get_first();
}
return ch;
}
float get_float(void)
{
int ch;
float input;
while (scanf("%f", &input) != 1)
{
while ((ch = getchar()) != '\n')
{
putchar(ch);
}
printf(" is not an number.\n");
printf("Please enter a number such as 2.5, -1.78E8 or 3: ");
}
return input;
}
int main(void)
{
int ct;
float i, j;
while ((ct = get_choice()) != 'q')
{
printf("Enter first number: ");
i = get_float();
printf("Enter second number: ");
j = get_float();
switch (ct)
{
case 'a':
{
printf("%g + %g = %g\n", i, j, i + j);
break;
}
case 's':
{
printf("%g - %g = %g\n", i, j, i - j);
break;
}
case 'm':
{
printf("%g * %g = %g\n", i, j, i * j);
break;
}
case 'd':
{
while (fabs(j) <= 1e-6)
{
//↑判断float型浮点数是否为0;
printf("Enter a number other than 0: ");
j = get_float();
}
printf("%g / %g = %g\n", i, j, i / j);
break;
}
}
}
printf("Bye.\n");
return 0;
}