作者在 2021-04-17 23:00:29 发布以下内容
8.1.c
#include <stdio.h>
int main(void) {
int ch;
int ct = 0;
while ((ch = getchar()) != EOF)
++ct;
printf("%d characters read\n", ct);
return 0;
}
8.2.c
#include <stdio.h>
int main(void) {
int ch, ct;
ct = 0;
while((ch = getchar()) != EOF) {
if(ct == 10) {
printf("\n");
ct = 0;
ct++;
}
if(ch == '\n')
printf("\\n ");
else if(ch == '\t')
printf("\\t ");
else if(ch < ' ')
printf("^%c ", ch + 64);
else
printf("%c = %d ", ch, ch);
ct++;
}
return 0;
}
8.3.c
#include <stdio.h>
#include <ctype.h>
int main(void) {
int ch;
unsigned long uct = 0;
unsigned long lct = 0;
unsigned long oct = 0;
while ((ch = getchar()) != EOF)
if (isupper(ch))
uct++;
else if (islower(ch))
lct++;
else
oct++;
printf("%lu uppercase characters read\n", uct);
printf("%lu lowercase characters read\n", lct);
printf("%lu other characters read\n", oct);
return 0;
}
8.4.c
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
int main(void) {
int ch;
int ct_letter = 0;
int ct_word = 0;
float num;
bool isword = false;
while((ch = getchar()) != EOF) {
if(ispunct(ch))
continue;
if(isalpha(ch))
++ct_letter;
if(!isspace(ch) && !isword) {
isword = true;
++ct_word;
}
if(isspace(ch) && isword)
isword = false;
}
num = (float)ct_letter / ct_word;
printf("Total words: %d.\n", ct_word);
printf("Total letters: %d.\n", ct_letter);
printf("Average letters of words: %f.\n", num);
return 0;
}
8.5.c
#include <stdio.h>
#include <ctype.h>
int main(void) {
int high = 100;
int low = 1;
int guess = (high + low) / 2;
char 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 (response != 'h' && 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 (response == 'h')
high = guess - 1;
else if (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;
}
8.6.c
#include <stdio.h>
#include <ctype.h>
#define STOP '#'
int get_first(void);
int main(void) {
int ch;
while ((ch = get_first()) != STOP) {
printf("%c\n", ch);
}
return 0;
}
int get_first(void) {
int ch;
do {
ch = getchar();
} while (isspace(ch));
while (getchar() != '\n')
continue;
return ch;
}
8.7.c
#include <stdio.h>
#include <ctype.h>
#define BASEPAY1 8.75 // $8.75 per hour
#define BASEPAY2 9.33 // $9.33 per hour
#define BASEPAY3 10.00 // $10.00 per hour
#define BASEPAY4 11.20 // $11.20 per hour
#define BASEHRS 40 // hours at basepay
#define OVERTIME 1.5 // 1.5 time
#define AMT1 300 // 1st rate tier
#define AMT2 150 // 2st rate tier
#define RATE1 0.15 // rate for 1st tier
#define RATE2 0.20 // rate for 2nd tier
#define RATE3 0.25 // rate for 3rd tier
int getfirst(void);
void menu(void);
int main(void) {
double hours;
double gross;
double net;
double taxes;
double pay;
char response;
menu();
while ((response = getfirst()) != 'q') {
if (response == '\n')
continue;
response = tolower(response);
switch (response) {
case 'a':
pay = BASEPAY1;
break;
case 'b':
pay = BASEPAY2;
break;
case 'c':
pay = BASEPAY3;
break;
case 'd':
pay = BASEPAY4;
break;
default :
printf("Please enter a, b, c, d, or q.\n");
menu();
continue;
}
printf("Enter the number of hours worked this week: ");
scanf("%lf", &hours);
if (hours <= BASEHRS)
gross = hours * pay;
else
gross = BASEHRS * pay + (hours - BASEHRS) * pay * OVERTIME;
if (gross <= AMT1)
taxes = gross * RATE1;
else if (gross <= AMT1 + AMT2)
taxes = AMT1 * RATE1 + (gross - AMT1) * RATE2;
else
taxes = AMT1 * RATE1 + AMT2 * RATE2 + (gross - AMT1 - AMT2) * RATE3;
net = gross - taxes;
printf("gross: $%.2f; taxes: $%.2f; net: $%.2f\n", gross, taxes,
net);
menu();
}
printf("Done.\n");
return 0;
}
void menu(void) {
printf("********************************************************"
"*********\n");
printf("Enter the letter corresponding to the desired pay rate"
" or action:\n");
printf("a) $%4.2f/hr b) $%4.2f/hr\n", BASEPAY1,
BASEPAY2);
printf("c) $%5.2f/hr d) $%5.2f/hr\n", BASEPAY3,
BASEPAY4);
printf("q) quit\n");
printf("********************************************************"
"*********\n");
}
int getfirst(void) {
int ch;
ch = getchar();
while (isspace(ch))
ch = getchar();
while (getchar() != '\n')
continue;
return ch;
}
8.8.c
#include <stdio.h>
int get_choice(void);
float get_input(void);
int main(void) {
int ch;
float n_first, n_second, value;
while((ch = get_choice()) != 'q') {
printf("Enter first number: ");
n_first = get_input();
printf("Enter second number: ");
n_second = get_input();
switch(ch) {
case 'a':
value = n_first + n_second;
printf("%g + %g = %g\n", n_first, n_second, value);
break;
case 's':
value = n_first - n_second;
printf("%g - %g = %g\n", n_first, n_second, value);
break;
case 'm':
value = n_first * n_second;
printf("%g * %g = %g\n", n_first, n_second, value);
break;
case 'd':
if(n_second == 0) {
printf("Enter a number other than 0: ");
n_second = get_input();
}
value = n_first / n_second;
printf("%g / %g = %g\n", n_first, n_second, value);
break;
}
}
printf("Bye.\n");
return 0;
}
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 = getchar();
while(ch != 'a' && ch != 'b' && ch != 'c' && ch != 'd' && ch != 'q') {
printf("Please enter a,b,c,d or q\n");
ch = getchar();
}
return ch;
}
float get_input(void) {
float input;
int ch;
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: ");
}
while(getchar() != '\n')
continue;
return input;
}