8.11.1
#include <stdio.h>
int main() {
int ch;
int num = 0;
while ((ch = getchar()) != EOF) {
num++;
//putchar(ch); 此两行为文件输出验证项
//printf("%d \n", num);
}
printf("There are %d strings in the file.", num);
return 0;
}
8.11.2
#include <stdio.h>
int main() {
int ch;
int n = 0;
while ((ch = getchar()) != EOF) {
if (ch == '\n') {
printf("\\n");
}
else if (ch == ' ') {
printf("\' \' - 0 ");
n++;
}
else {
putchar('\'');
putchar(ch);
putchar('\'');
printf(" - %d ", ch);
n++;
}
if (n == 10 || ch == '\n') {
printf("\n");
n = 0;
}
}
printf("^Z - %d ", ch);
return 0;
}
8.11.3
#include <stdio.h>
int main() {
int ch;
int num_Clt = 0, num_Slt = 0;
while ((ch = getchar()) != EOF) {
if (ch >= 65 && ch <= 90)
num_Clt++;
else if (ch >= 97 && ch <= 122)
num_Slt++;
}
printf("There are %d Captial letters.", num_Clt);
printf("There are %d Small letters.", num_Slt);
return 0;
}
8.11.4
本题书中提示的ctype.h头文件中的ispunct函数存在一定局限性。ispunct(ch)为判定ch是否为字符,ch是特殊字符则返回非零数,ch若是数字和普通字符则返回0。但在实操中发现ispunct(ch)判定的特殊字符不包括空白字符。因此应该同时选用isspace()函数进行空白字符的筛选。
#include <stdio.h>
#include <ctype.h>
int main() {
int ch, temp_ch = 0;
int word = 0, letter = 0;
int avg_letter, sum = 0;
while ((ch = getchar()) != EOF) {
if (ispunct(ch) == 0 && ch != ' ') {
letter++;
}
if ((ispunct(ch) || isspace(ch)) && ispunct(temp_ch) == 0 && isspace(temp_ch) == 0)
{
sum += letter;
word++;
// 验证项
//printf("letter = %d sum = %d word = %d\n", letter, sum, word);
letter = 0;
}
temp_ch = ch;
}
printf("On averagyae,There are %d letters one per word.", sum / word);
return 0;
}
8.11.5
程序设计略显臃肿,并不优越。
#include <stdio.h>
int main() {
int max = 100, min = 1;
int guess = 50, ch;
printf("Pick an integer from 1 to 100. I will try to guess it.\n");
printf("Respond with \'l\' if my guess is large.\n");
printf("And respond with \'s\' if my guess is small.\n");
printf("Please respond with \'y\' if my guess is right\n");
printf("Uh...is your number is %d?\n", (max + min) / 2);
while (ch = getchar()) {
if (ch == 's' || ch == 'l') {
while (getchar() != '\n') //
continue; //输入正确后清除缓冲区直到下次输入
if (ch == 's') {
min = guess;
guess = (max + min) / 2;
}
else if (ch == 'l') {
max = guess;
guess = (max + min) / 2;
}
if (max - min == 2) {
printf("Aha! your number must be %d!", guess);
break;
}
if (max < min || max == min) {
printf("Are you joking now?\n");
printf("You fucking forget your number! Try it again!");
max = 100;
min = 0;
guess = 50;
printf("\nUh...is your number is %d?\n", guess);
continue;
}
printf("Uh...is your number is %d?\n", guess);
}
else if (ch == 'y') { //猜测正确的反馈输入
printf("Aha! I get you!");
break;
}
else { //不合法的输入,提示后重新输入
while (getchar() != '\n')
continue;
printf("Your enter is wrong. Please enter again!\n");
printf("Uh...is your number is %d?\n", guess);
}
}
return 0;
}
8.11.6
#include <stdio.h>
#include <ctype.h>
char get_first(void);
int main() {
char ch;
printf("Please enter a sentence!\n");
ch = get_first();
if (ch != 0)
printf("The first nonspace character is %c", ch);
else
printf("There no nonspce character.");
}
char get_first(void) {
int ch = 0;
while (isspace(ch = getchar())) { //判断第一个字符是否是非空白字符。若是,继续获取直到不是为止。
if (ch == '\n')
return 0; //若到输入结束全部为非空白字符,返回0给主函数
else
continue;
}
while (getchar() != '\n')
continue;
return ch;
}
8.11.7
#include <stdio.h>
#define OVERTIME 40
#define OVERWORK 1.5
#define TAX1 0.15
#define TAX2 0.2
#define TAX3 0.25
int main() {
int num;
float salary, hour, money;
//交互界面
UI:
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");
num = getchar();
while (getchar() != '\n')
continue;
if (num < 'e' && num >= 'a') {
printf("Please enter your work time(hour): ");
scanf_s("%f", &hour);
switch (num) {
case 'a':
money = 8.75;
break;
case 'b':
money = 9.33;
break;
case 'c':
money = 10.00;
break;
case 'd':
money = 11.20;
break;
}
if (hour > OVERTIME)
hour = OVERTIME + (hour - OVERTIME) * OVERWORK;
salary = hour * money;
if (salary < 300)
salary -= salary * TAX1;
else if (salary < 450)
salary -= 300 * TAX1 + (salary - 300) * TAX2;
else if (salary > 450)
salary -= 300 * TAX1 + 150 * TAX2 + (salary - 450) * TAX3;
printf("Your last salary is :%.2f", salary);
}
else if (num == 'q')
printf("Done.");
else {
printf("Please enter right number!\n");
goto UI;
}
return 0;
}
8.11.8
#include<stdio.h>
float get_float(void);
void UI();
int main() {
int ch;
float fst_num = 0, scd_num = 0;
UI();
ch = getchar();
while (getchar() != '\n')
continue;
while (ch != 'q') {
while (ch != 'a' && ch != 's' && ch != 'd' && ch != 'm') {
printf("Please enter right character!\n");
ch = getchar();
while (getchar() != '\n')
continue;
}
printf("Enter first number: ");
fst_num = get_float();
printf("Enter second number: ");
scd_num = get_float();
switch (ch) {
case 'a':
printf("%.1f + %.1f = %.1f\n", fst_num, scd_num, fst_num + scd_num);
break;
case 's':
printf("%.1f - %.1f = %.1f\n", fst_num, scd_num, fst_num - scd_num);
break;
case 'm':
printf("%.1f * %.1f = %.1f\n", fst_num, scd_num, fst_num * scd_num);
break;
case 'd':
while (scd_num == 0) {
printf("Enter enter a number more than 0:");
scd_num = get_float();
}
printf("%.1f / %.1f = %.1f\n", fst_num, scd_num, fst_num / scd_num);
break;
}
UI();
ch = getchar();
while (getchar() != '\n')
continue;
}
printf("Bye.");
}
void UI() {
printf("***********************************\n");
printf("Enter the operation of your choice:\n");
printf("a. add s. subtract\n");
printf("m. multiply d. divide\n");
printf("q. quit\n");
}
float get_float(void) {
float input;
char ch;
while (scanf_s("%f", &input) != 1) {
while ((ch = getchar()) != '\n')
putchar(ch);
printf(" is not an number.\n Please enter an number,");
printf("such as 2.5, -1.78E8, or 3: ");
}
while (getchar() != '\n')
continue;
return input;
}