C Primer Plus 编程习题-第八章-字符输入\输出和输入验证
C Primer Plus 8.11
编程练习第一题
/* C Primer Plus 8.11 —— 编程练习第一题 */
/* 题目:设计一个程序,统计在读到文件结尾之前读取的字符数 */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int ch, i = 0;
FILE *fp;
char fname[50];
printf("Enter the name of the file: ");
scanf("%s", fname); // 输入文件名,需要文件路径
fp = fopen(fname, "r"); // 以只读形式打开
if (fp == NULL){
printf("Failed to open file.\n");
exit(1);
}
while ((ch = getc(fp)) != EOF){ // 文件结尾停止
putchar(ch);
i++; // 读到字符,加1
}
fclose(fp); // 关闭文件
printf("\nThe file has %d byte", i);
getchar();
getchar();
return 0;
}
// C:\Users\SieYuan\Desktop\test8.11-1.txt
编程练习第二题
/* C Primer Plus 8.11 —— 编程练习第二题 */
/* 题目:编写一个程序,在遇到EOF之前,把输入作为字符流读取。
程序要打印每个输入的字符及其相应的ASCⅡ十进制值。 */
#include <stdio.h>
int main(void)
{
int i = 0, ch;
while ((ch = getchar()) != EOF){
i++;
if (ch == '\n'){
putchar('\\');
putchar('n');
printf(" %d\n", ch);
i = 0;
}
else if (ch == '\t'){
putchar('\\');
putchar('t');
printf(" %d\t", ch);
}
else if (ch < ' '){
putchar('^');
putchar(ch + 64);
printf(" %d\t", ch);
}
else{
printf("%c %d\t", ch, ch);
}
if (i == 9){
putchar('\n');
i = 0;
}
}
getchar();
return 0;
}
编程练习第三题
/* C Primer Plus 8.11 —— 编程练习第三题 */
/* 题目:编写一个程序,在遇到EOF之前,把输入作为字符流读取。
该程序要报告输入中的大写字母和小写字母的个数。假设大小写字母
数值是连续的。或者使用ctype.h库中合适的分类函数更方便。 */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int capital = 0, lower = 0;
int ch;
printf("Enter a string and # to quit: ");
while ((ch = getchar()) != EOF && ch != '#'){
if (ch >= 'A' && ch <= 'Z')
capital++;
else if (ch >= 'a' && ch <= 'z')
lower++;
}
printf("Capital letters: %d, Lower case letter: %d\n", capital, lower);
system("pause");
return 0;
}
编程练习第四题
/* C Primer Plus 8.11 —— 编程练习第四题 */
/* 题目:编写一个程序,在遇到EOF之前,把输入作为字符流读取。
该程序要报告平均每个单词的字母数。不要把空白统计为单词的字母。
实际上标点符号也不应该统计,可考虑使用ctype.h系列中的ispunct()函数 */
#include <stdio.h>
#include <ctype.h> // ispunct()检查字符是否是标点符号, true, false
#include <stdbool.h>
#include <stdlib.h>
int main(void)
{
int ch, ch_bf;
int word = 0; //单词的个数
int word_ch = 0; //单词的字母数
printf("Enter a string and # to quit: ");
while ((ch = getchar()) != EOF && ch != '#'){
if (ch == ' ' || ispunct(ch) == true)
word++;
else
{
if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
// && 比 || 优先级高,故不加括号
word_ch++;
}
ch_bf = ch;
}
if (ch_bf == ' ' && ch == '#')
printf("The number of words: %d\nAverage letters per word: %.2f\n",
word, (float) word_ch / word);
else
printf("The number of words: %d\nAverage letters per word: %.2f\n",
word + 1, (float) word_ch / (word + 1));
system("pause");
return 0;
}
编程练习第五题
/* C Primer Plus 8.11 —— 编程练习第五题 */
/* 题目:修改程序清单8.4的猜数字程序,使用更智能的猜测策略。
使用二分查重(binary search)策略。 */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int guess = 50;
char response;
int min = 1;
int max = 100;
printf("Pick an integer from 1 to 100. I will try to guess it\n");
printf("Respond: y - yes, s - small, b - big.\n");
printf("Uh... is your number %d?\n", guess);
while ((response = getchar()) != 'y'){
if (response == 'b'){
max = guess;
guess = (max + min) / 2;
printf("Well, then, is it %d?\n", guess);
}
else if (response == 's'){
min = guess;
guess = (max + min) / 2;
printf("Well, then, is it %d?\n", guess);
}
else{
printf("Sorry, I understand only y or s or b.\n");
}
while (getchar() != '\n')
continue;
}
printf("I knew I could do it!\n");
system("pause");
return 0;
}
编程练习第六题
/* C Primer Plus 8.11 —— 编程练习第六题 */
/* 题目:修改程序清单8.8中的get_first()函数,让该函数返回读取的第一个
非空白字符,并在一个简单的程序中测试。 */
#include <stdio.h>
#include <stdlib.h> // exit()
#include <ctype.h>
char get_first(FILE *fp);
int main(void)
{
int ch;
char fname[50];
FILE *fp;
printf("Enter the name of the file: ");
scanf("%s", fname);
fp = fopen(fname, "r"); // 打开待读取文件
ch = get_first(fp);
printf("first char: %c\n", ch);
fclose(fp); //关闭文件
system("pause");
return 0;
}
char get_first(FILE *fp)
{
char c;
while ((c = getc(fp)) == ' ')
continue;
return c;
}
编程练习第七题
/* C Primer Plus 8.11 —— 编程练习第七题 */
/* 题目:修改第七章的7.12-8,用字符代替数字标记菜单的选项。
用q代替5作为结束输入的标记。 */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define WAGE_LEVEL1 8.75
#define WAGE_LEVEL2 9.33
#define WAGE_LEVEL3 10.00
#define WAGE_LEVEL4 11.20
#define OVERTIME 40.0
#define RATE_300 0.15
#define RATE_150 0.20
#define RATE_rest 0.25
void display(void);
float work_time(int level);
float taxrate(float sum);
int main(void)
{
char button;
float worktime;
float tax, wage_hr;
float salary, aft_salary; // after-tax salary;
display();
while ((button = getchar()) != EOF && button != 'q'){
while (button != '\n'){
switch (button){
case 'a': worktime = work_time(1);
wage_hr = WAGE_LEVEL1;
salary = wage_hr * worktime;
tax = taxrate(salary);
aft_salary = salary - tax;
break;
case 'b': worktime = work_time(2);
wage_hr = WAGE_LEVEL2;
salary = wage_hr * worktime;
tax = taxrate(salary);
aft_salary = salary - tax;
break;
case 'c': worktime = work_time(3);
wage_hr = WAGE_LEVEL3;
salary = wage_hr * worktime;
tax = taxrate(salary);
aft_salary = salary - tax;
break;
case 'd': worktime = work_time(4);
wage_hr = WAGE_LEVEL4;
salary = wage_hr * worktime;
tax = taxrate(salary);
aft_salary = salary - tax;
break;
default:printf("Error! Retry!\n");
break;
}
printf("时薪:%.2f美元/小时\n本周总工作时长: %6.2f小时\n应发工资: %6.2f美元\n"
"税 金: %6.2f美元\n实发工资: %6.2f美元\n\n",
wage_hr, worktime, salary, tax, aft_salary);
worktime = salary = tax = aft_salary = 0.0;
display();
button = '\n';
}
}
printf("Quit!");
system("pause");
return 0;
}
void display(void)
{
int i;
for(i = 0; i < 65; i++)
printf("%c",'*');
printf("\nEnter 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");
for(i = 0; i < 65; i++)
printf("%c",'*');
printf("\n");
}
float work_time(int level)
{
float t;
printf("You choose level %d hourly wage!\nEnter your worktime in a week (hr): ",
level);
scanf("%f", &t);
if (t > 40.0)
t = 40.0 + (t - 40.0) * 1.5;
return t;
}
float taxrate(float sum)
{
float tax_;
if (sum <= 300.0)
tax_ = RATE_300 * sum;
else if (sum > 300.0 && sum <= 150.0)
tax_ = 300.0 * RATE_300 + (sum - 300.0) * RATE_150;
else
tax_ = 300.0 * RATE_300 + 150.0 * RATE_150 + (sum - 450.0) * RATE_rest;
return tax_;
}
编程练习第八题
/* C Primer Plus 8.11 —— 编程练习第八题 */
/* 题目:编写一个程序,显示一个提供加法、减法、乘法、除法的菜单。获取用户
选择的选项后,程序提示用户输入两个数字,然后执行用户刚才选择的操作。该程序
只接受菜单提供的选项。程序使用float类型的变量储存用户输入的数字,如果用户
输入失败,则允许再次输入。进行除法运算时,如果用户输入0作为除数,程序应提
示用户,重新输入一个新值。示例如下: */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void display(void);
int op_add(void);
int op_sub(void);
int op_muti(void);
float op_div(void);
int input_first(void);
int input_second(void);
int main(void)
{
char button;
display();
while ((button = getchar()) != EOF && button != 'q'){
while (button != '\n'){
switch (button){
case 'a': op_add();
break;
case 's': op_sub();
break;
case 'm': op_muti();
break;
case 'd': op_div();
break;
default: printf("Error! Retry!\n");
break;
}
display();
button = '\n';
}
}
printf("Quit!");
system("pause");
return 0;
}
void display(void)
{
int i;
for(i = 0; i < 65; i++)
printf("%c",'*');
printf("\nEnter the number corresponding to the desired pay rate or action:\n");
printf("a) add s) substract\n");
printf("m) mutiply d) divide\n");
printf("q) quit\n");
for(i = 0; i < 65; i++)
printf("%c",'*');
printf("\n");
}
int op_add(void)
{
int first, second, res;
first = input_first();
second = input_second();
res = first + second;
printf(" %d + %d = %d\n", first, second, res);
}
int op_sub(void)
{
int first, second, res;
first = input_first();
second = input_second();
res = first - second;
printf(" %d - %d = %d\n", first, second, res);
}
int op_muti(void)
{
int first, second, res;
first = input_first();
second = input_second();
res = first * second;
printf(" %d × %d = %d\n", first, second, res);
}
float op_div(void)
{
int first, second;
float res;
first = input_first();
second = input_second();
while (second == 0.0){
printf("Error!");
second = input_second();
}
res = ((float)first) / ((float)second);
//printf("%d,%f\n\n", (int)res, res);
if (res == (int)res)
printf(" %d ÷ %d = %d\n", first, second, (int)res);
else
printf(" %d ÷ %d = %.3f\n", first, second, res);
}
int input_first(void)
{
int i1;
printf("Please input the first integer: \n");
while ((scanf("%d", &i1)) == 0){
printf("Error, retry!");
}
return i1;
}
int input_second(void)
{
int i2;
printf("Please input the second integer: \n");
while ((scanf("%d", &i2)) == 0){
printf("Error, retry!");
}
return i2;
}