(编译环境 Microsoft Visual Studio 2019)
1.
/*1.*/
#include<stdio.h>
int main(void)
{
unsigned int count = 0;
char ch;
printf("Please enter some text.(end with EOF)\n");
while ((ch = getchar()) != EOF)
count++;
printf("This file has %u characters.\n", count);
return 0;
}
2.
/*2.*/
#include<stdio.h>
int main(void)
{
char ch;
int row = 0;
printf("Please enter some text.(end with EOF)\n");
while ((ch = getchar()) != EOF)
{
if (ch != '\t' && ch != '\n' && ch < ' ')
printf("^%c", ch + 64);
else if (ch == '\t')
printf("\\t");
else if (ch == '\n')
printf("\\n");
else
putchar(ch);
printf("--%d ", ch);
if (row == '\n')
row = 1;
else
row++;
if (row == 10)
{
putchar('\n');
row = 0;
}
}
putchar('\n');
return 0;
}
3.
/*3.*/
#include<stdio.h>
#include<ctype.h>
int main(void)
{
char ch;
unsigned int u_count = 0;
unsigned int l_count = 0;
printf("Please enter some text.(end with EOF)\n");
while ((ch = getchar()) != EOF)
{
if (isupper(ch))
u_count++;
else if (islower(ch))
l_count++;
}
printf("Upper case letter: %u\n"
"Lower case letter: %u\n", u_count, l_count);
return 0;
}
4.
/*4.*/
#include<stdio.h>
#include<ctype.h>
int main(void)
{
char ch;
unsigned int l_count = 0;
unsigned int w_count = 0;
_Bool inword = 0;
printf("Please enter some text.(end with EOF).\n");
while ((ch = getchar()) != EOF)
{
if (!isspace(ch) && !ispunct(ch) && !inword)
{
w_count++;
inword = 1;
}
else if (!isspace(ch) && !ispunct(ch) && inword)
l_count++;
else if(isspace(ch) || ispunct(ch) && inword)
inword = 0;
}
printf("Average number of letter per word: %.2f\n",
(float)l_count / w_count);
return 0;
}
5.
/*5.*/
#include<stdio.h>
int main(void)
{
char ch;
int guess = 50;
int max = 100;
int min = 0;
printf("Pick an integer from 1 to 100. "
"I will try to guess it.\n"
"Respond with a y if my guess is right.\n"
"Respond with a b if it is too big "
"and a s if it is too small.\n");
printf("Uh...is your number %d?\n", guess);
while ((ch = getchar()) != 'y')
{
if (ch == 'b')
{
max = guess;
guess = (guess + min) / 2;
printf("Well, then, is it %d\n", guess);
}
else if (ch == 's')
{
min = guess;
guess = (guess + max) / 2;
printf("Well, then, is it %d\n", guess);
}
else
printf("Sorry, I can undersanded y, b, s.\n");
while (getchar() != '\n')
continue;
}
printf("I know i can do it!\n");
return 0;
}
6.
/*6.*/
#include<stdio.h>
#include<ctype.h>
char get_first(void);
int main(void)
{
printf("Please enter some text.\n");
putchar(get_first());
putchar('\n');
return 0;
}
char get_first(void)
{
char ch;
ch = getchar();
while (isspace(ch))
ch = getchar();
while (getchar() != '\n')
continue;
return ch;
}
7.
/*7.*/
#include<stdio.h>
#define BASE_TIME 40
#define OVER_PAY 15
#define TAX_1 300
#define TAX_2 150
#define TAX_1_RATE 0.15f
#define TAX_2_RATE 0.2f
#define TAX_OT_RATE 0.25f
char get_choice(void);
char get_first(void);
void count(float level);
unsigned int get_unsigned(void);
void star(void);
int main(void)
{
const float level_1 = 8.75f;
const float level_2 = 9.33f;
const float level_3 = 10.00f;
const float level_4 = 11.20f;
char choice;
while ((choice = get_choice()) != 'q')
{
switch (choice)
{
case 'a': count(level_1); break;
case 'b': count(level_2); break;
case 'c': count(level_3); break;
case 'd': count(level_4); break;
default: printf("Program error\n"); break;
}
}
printf("bye\n");
return 0;
}
char get_choice(void)
{
char ch;
star();
printf("\nEnter the number corresponding to the desired pay rate or action.\n\n");
printf("a) $8.75/hr\t\tb) $9.33/hr\n\n");
printf("c) $10.00/hr\t\td) $11.20/hr\n\n");
printf("q) quit\n\n");
star();
printf("Your choice: ");
ch = get_first();
while ((ch < 'a' || ch > 'd') && ch != 'q')
{
printf("Please enter correct letter.\n");
ch = get_first();
}
return ch;
}
char get_first(void)
{
char ch;
ch = getchar();
while (getchar() != '\n')
continue;
return ch;
}
void count(float level)
{
unsigned int time;
float wage, tax;
printf("Please enter the number of hours you work a week.\n");
time = get_unsigned();
if (time <= BASE_TIME)
wage = time * (float)level;
else
wage = BASE_TIME * (float)level + (time - BASE_TIME) * OVER_PAY;
if (wage < TAX_1)
tax = wage * TAX_1_RATE;
else if (wage < TAX_2)
tax = TAX_1 * TAX_1_RATE + (wage - TAX_1) * TAX_2_RATE;
else
tax = TAX_1 * TAX_1_RATE + TAX_2 * TAX_2_RATE + (wage - TAX_2) * TAX_OT_RATE;
printf("Total wage: %.2f\n", wage);
printf("Tax: %.2f\n", tax);
printf("Net income: %.2f\n", wage - tax);
}
unsigned int get_unsigned(void)
{
unsigned int value;
char ch;
while (!scanf_s("%u", &value))
{
while ((ch = getchar()) != '\n')
putchar(ch);
printf(" is not an integer.\n");
printf("Please enter an integer value.\n");
}
while (getchar() != '\n')
continue;
return value;
}
void star(void)
{
printf("********************************************************************\n");
}
8.
/*8.*/
#include<stdio.h>
void add(void);
void subtract(void);
void multiply(void);
void divide(void);
float get_number(void);
char get_first(void);
char get_choice(void);
int main(void)
{
char choice;
while ((choice = get_choice()) != 'q')
{
switch (choice)
{
case 'a': add(); break;
case 's': subtract(); break;
case 'm': multiply(); break;
case 'd': divide(); break;
default: printf("Program error.\n"); break;
}
}
printf("bye\n");
return 0;
}
char get_choice(void)
{
char ch;
printf("Enter the operation of your choice.\n");
printf("a. %-15s s. subtract\n","add");
printf("m. %-15s d. divide\nq. quit\n","multiply");
ch = get_first();
while (ch != 'a' && ch != 's' && ch != 'm' && ch != 'd' && ch != 'q')
{
printf("Please enter correct letter.\n");
ch = get_first();
}
return ch;
}
char get_first(void)
{
char ch;
ch = getchar();
while (getchar() != '\n')
continue;
return ch;
}
void add(void)
{
float add;
float f_number, s_number;
printf("Enter first number: ");
f_number = get_number();
printf("Enter second number: ");
s_number = get_number();
add = f_number + s_number;
printf("%.1f + %.1f = %.1f\n", f_number, s_number, add);
putchar('\n');
}
void subtract(void)
{
float subtract;
float f_number, s_number;
printf("Enter first number: ");
f_number = get_number();
printf("Enter second number: ");
s_number = get_number();
subtract = f_number - s_number;
printf("%.1f - %.1f = %.1f\n", f_number, s_number, subtract);
putchar('\n');
}
void multiply(void)
{
float multiply;
float f_number, s_number;
printf("Enter first number: ");
f_number = get_number();
printf("Enter second number: ");
s_number = get_number();
multiply = f_number * s_number;
printf("%.1f * %.1f = %.1f\n", f_number, s_number, multiply);
putchar('\n');
}
void divide(void)
{
float divide;
float f_number, s_number;
printf("Enter first number: ");
f_number = get_number();
printf("Enter second number: ");
s_number = get_number();
while (s_number == 0)
{
printf("Enter a number other than 0: ");
s_number = get_number();
}
divide = f_number / s_number;
printf("%.1f / %.1f = %.1f\n", f_number, s_number, divide);
putchar('\n');
}
float get_number(void)
{
float number;
char ch;
while (!scanf_s("%f", &number))
{
while((ch=getchar())!='\n')
putchar(ch);
printf(" is not an number.\n"
"Please enter a number: ");
}
while (getchar() != '\n')
continue;
return number;
}