下面的一些程序要求输入以EOF终止。如果你的操作系统很难或根本无法使用重定向,请使用一些其他的测试来终止输入,如读到&字符时停止。
1.设计一个程序,统计在读到文件结尾之前读取的字符数。
#include <stdio.h>
int main(void)
{
int ch;
int ct = 0;
printf("请输入一些字符:\n");
while ((ch = getchar()) != EOF)
{
ct++;
}
printf("读到EOF前字符数有%d个(包括空白符).\n", ct);
return 0;
}
2.编写一个程序,在遇到EOF之前,把输入作为字符流读取。程序要打印每个输入的字符及其相应的ASCII十进制值。注意,在ASCII序列中,空格字符前面的字符都是非打印字符,要特殊处理这些字符。如果非打印字符是换行符或制表符,则分别打印\n或\t。否则,使用控制字符表示法。例如,ASCI工的l是 Ctrl+A,可显示为^A。注意,A的ASCII 值是Ctrl+A的值加上.64。其他非打印字符也有类似的关系。除每次遇到换行符打印新的一行之外,每行打印10对值。(注意:不同的操作系统其控制字符可能不同。)
#include <stdio.h>
int main(void)
{
int ch;
int i = 0;
printf("请输入一些字符(按照\"字符\"-\"十进制ASCII码\"打印):\n");
while ((ch = getchar()) != EOF)
{
if (i++ == 10)
{
putchar('\n');
i = 1;
}
if (ch >= 32) //空格的十进制ASCII码;
{
printf(" \'%c\'-%3d ", 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);
}
}
return 0;
}
3.编写一个程序,在遇到EOF之前,把输入作为字符流读取。该程序要报告输入中的大写字母和小写字母的个数。假设大小写字母数值是连续的。或者使用ctype.h 库中合适的分类函数更方便。
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int ch;
unsigned int uct = 0;
unsigned int lct = 0;
unsigned int oct = 0;
printf("请输入一些字符:\n");
while ((ch = getchar()) != EOF)
{
if (isupper(ch))
{
uct++;
}
else if (islower(ch))
{
lct++;
}
else
{
oct++;
}
}
printf("大写字母有%u个.\n", uct);
printf("小写字母有%u个.\n", lct);
printf("其它字符(包括空白符)有%u个.\n", oct);
return 0;
}
4.编写一个程序,在遇到EOF之前,把输入作为字符流读取。该程序要报告平均每个单词的字母数。
不要把空白统计为单词的字母。实际上,标点符号也不应该统计,但是现在暂时不同考虑这么多(如果你比较在意这点,考虑使用ctype.h系列中的ispunct ()函数)。
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
int main(void)
{
float count;
bool inword = false;
int ch, words, letter;
words = letter = 0;
printf("请您输入一些单词(遇到文件结尾结束):\n");
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;
}
}
count = (float)letter / words;
printf("总共有%d个单词,%d个字母.\n", words, letter);
printf("平均每个单词的字母数是%g个.\n", count);
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>
#define STOP '#'
int get_first(void);
int main(void)
{
int ch;
printf("(本程序返回读取的第1个非空白字符)\n");
printf("请您输入一些字符(单独按#退出):\n");
while ((ch = get_first()) != STOP)
{
printf("结果是:%c\n", ch);
printf("请您再次输入(单独按#退出):\n");
}
printf("本程序完成!\n");
return 0;
}
int get_first(void)
{
int ch;
do
{
ch = getchar();
} while (isspace(ch));
while (getchar() != '\n')
continue;
return ch;
}
7.修改第7章的编程练习8,用字符代替数字标记菜单的选项。用q代替5作为结束输入的标记。
#include <stdio.h>
#include <ctype.h>
#define EXTRA_HOUR 1.5f
#define BASE_TAX 0.15f
#define EXTRA_TAX 0.2f
#define EXCEED_TAX 0.25f
int show_menu(void);
void show_salary(float base_salary, float hours);
int get_choice(void);
void eatline(void);
int main(void)
{
float n;
int ch;
while ((ch = show_menu()) != 'q')
{
printf("Enter the working hours a week:");
while ((!scanf("%f", &n)) || (n < 0))
{
eatline();
printf("Enter a positive number:");
}
eatline();
switch (ch)
{
case 'a':
{
show_salary(8.75f, n);
break;
}
case 'b':
{
show_salary(9.33f, n);
break;
}
case 'c':
{
show_salary(10.00f, n);
break;
}
case 'd':
{
show_salary(11.20f, n);
break;
}
}
putchar('\n');
}
printf("Done!\n");
return 0;
}
int get_choice(void)
{
int ch;
do
{
ch = tolower(getchar());
} while (isspace(ch));
eatline();
return ch;
}
void eatline(void)
{
while (getchar() != '\n')
continue;
return;
}
int show_menu(void)
{
int ch;
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");
printf("Please you choose:");
ch = get_choice();
while (ch != 'a' && ch != 'b' && ch != 'c' && ch != 'd' && ch != 'q')
{
printf("Please enter a,b,c,d or q:");
ch = get_choice();
}
return ch;
}
void show_salary(float base_salary, float hours)
{
float salary, tax, taxed_salary;
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;
if (salary <= 450)
{
tax = 300 * BASE_TAX + (salary - 300) * EXTRA_TAX;
}
else
{
tax = 300 * BASE_TAX + (salary - 300) * EXTRA_TAX + (salary - 450) * EXCEED_TAX;
}
taxed_salary = salary - tax;
}
printf("salary(before tax):$%g\n", salary);
printf("tax:$%g\n", tax);
printf("salary(after tax):$%g\n", taxed_salary);
return;
}
8.编写一个程序,显示一个提供加法、减法、乘法、除法的菜单。获得用户选择的选项后,程序提示用户输入两个数字,然后执行用户刚才选择的操作。该程序只接受菜单提供的选项。程序使用float类型的变量储存用户输入的数字,如果用户输入失败,则允许再次输入。进行除法运算时,如果用户输入О作为第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 get_choice(void);
float get_float(void);
int main(void)
{
int ct;
float i, j;
while ((ct = get_choice()) != 'q')
{
switch (ct)
{
case 'a':
{
printf("Enter first number: ");
i = get_float();
printf("Enter second number: ");
j = get_float();
printf("%g + %g = %g\n", i, j, i + j);
break;
}
case 's':
{
printf("Enter first number: ");
i = get_float();
printf("Enter second number: ");
j = get_float();
printf("%g - %g = %g\n", i, j, i - j);
break;
}
case 'm':
{
printf("Enter first number: ");
i = get_float();
printf("Enter second number: ");
j = get_float();
printf("%g * %g = %g\n", i, j, i * j);
break;
}
case 'd':
{
printf("Enter first number: ");
i = get_float();
printf("Enter second number: ");
while (fabs(j = get_float()) <= 1e-6)
{
//↑判断float型浮点数是否为0;
printf("Enter a number other than 0: ");
}
printf("%g / %g = %g\n", i, j, i / j);
break;
}
}
}
printf("Bye.\n");
return 0;
}
int get_first(void)
{
int ch;
do
{
ch = tolower(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;
}