//第 1 题
#include <stdio.h>
int main(void)
{
int count = 0;
while (getchar() != EOF)
count++;
printf("%d", count);
return 0;
}
//第 2 题
#include <stdio.h>
int main(void)
{
int i = 0;
char ch;
while ((ch = getchar()) != EOF)
{
if (ch == '\n')
{
putchar('\\');
putchar('n');
printf(" %3d", ch);
}
else if (ch == '\t')
{
putchar('\\');
putchar('t');
}
else if (ch < ' ')
{
putchar('^');
putchar(ch + 64);
printf(" %3d", ch);
}
else
printf(" %c,%3d",ch, ch);
if (i++ == 9)
{
printf("\n");
i = 0;
}
}
return 0;
}
//第 3 题
#include <stdio.h>
int main(void)
{
int NUM = 0, num = 0;
char ch;
while ((ch = getchar()) != EOF && ch != '#')
{
putchar(ch);
if (ch >= 'A' && ch <= 'Z')
NUM++;
else if (ch >= 'a' && ch <= 'z')
num++;
}
printf("大写:%d", NUM);
printf("小写:%d", num);
return 0;
}
//第 4 题
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int ch;
int words = 0, sign = 0, count = 0;
double average;
while ((ch = getchar()) != EOF)
{
if (!isspace(ch) && !sign)
{
words++;
sign = 1;
}
if (isalpha(ch))
count++;
if (isspace(ch) && sign)
sign = 0;
}
average = (double)count / (double)words;
printf("%d 个单词,平均每个单词有 %lf 个字母。\n", average);
return 0;
}
//第 5 题
#include <stdio.h>
char get_first(void);
int main(void)
{
char ch;
int guess = 50;
int up_limit = 100, low_limit = 1;
printf("Pick an integer from 1 to 100. I will try to guess ");
printf("it.\nRespond with a y if my guess is right.\n");
printf("If my guess is too big,respond with b,\n"
"if my guess is too small,respond with s\n");
printf("Uh...is your number %d?\n",guess);
while ((ch = get_first()) != 'y')
{
if (ch == 'b')
{
up_limit = guess;
guess = (low_limit + up_limit) / 2;
}
if (ch == 's')
{
low_limit = guess;
guess = (low_limit + up_limit) / 2;
}
printf("Well, then, is it %d?\n",guess);
}
printf("I knew i could do it!\n");
return 0;
}
char get_first(void)
{
char ch;
ch = getchar();
while (getchar() != '\n')
continue;
return ch;
}
//第 6 题
char get_first(void)
{
int ch;
while((ch = getchar()) == '\n' || ch == ' ' || ch == '\t')
;
return ch;
}
//第 7 题
#include <stdio.h>
#define NON_OVERTIME 40.00
#define BASIC 10.00
#define FIRST_HOUR 300.00
#define NEXT_HOUR 450.00
#define FIRST_RATE 0.15
#define NEXT_RATE 0.20
#define LAST_RATE 0.25
void interface(void);
char star(void);
char get_char(void);
int main(void)
{
int x = 1;
char num;
float hour,sum,tax,gain,perhour;
star();
interface();
star();
scanf("%c",&num);
while (x)
{
switch (num)
{
case 'a':
perhour = 8.75;
x = 0;
break;
case 'b':
perhour = 9.33;
x = 0;
break;
case 'c':
perhour = 10.00;
x = 0;
break;
case 'd':
perhour = 11.20;
x = 0;
break;
case 'q':
return 0;
break;
default:
printf("please input Letters from a to d(input q to quit): \n");
continue;
}
printf("you have select $%.2lf\n",perhour);
printf("Please enter you work hour a week.\n");
scanf("%f",&hour);
if (hour > NON_OVERTIME)
hour = NON_OVERTIME + (hour - NON_OVERTIME) * 1.5;
sum = perhour * hour;
if (sum <= FIRST_HOUR)
{
tax = sum * 0.15;
gain = sum - tax;
}
else if (sum > FIRST_HOUR && sum <= NEXT_HOUR)
{
tax = FIRST_HOUR * 0.15 + (sum - FIRST_HOUR) * 0.20;
gain = sum - tax;
}
else
{
tax = FIRST_HOUR * 0.15 + 150 * 0.2 + (hour - 450) * 0.25;
gain = sum - tax;
}
printf("real hour:%.f perhour:%.2f tax:%.2f gain:%.2f", hour, perhour, tax, gain);
x = 1;
}
return 0;
}
void interface(void)
{
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");
}
char star(void)
{
int i = 1;
for (i;i < 100;i++)
printf("*");
printf("\n");
}
char get_char(void)
{
int ch;
ch = getchar();
while((ch = getchar()) != '\n')
continue;
return ch;
}
//第 8 题
#include <stdio.h>
char get_choice(void);
float first_input(void);
float last_input(void);
float add(void);
float subtract(void);
float multipy(void);
float divide(void);
int main(void)
{
char choice;
while ((choice = get_choice()) != 'q')
{
switch(choice)
{
case 'a':add();
break;
case 's':subtract();
break;
case 'm':multipy();
break;
case 'd':divide();
break;
default :printf("Please input a right word.\n");
continue;
}
}
printf("Done!\n");
return 0;
}
char get_choice(void)
{
char choice;
printf("Enter the operation of your choice:\n");
printf("a. add s. subtract\n");
printf("m. multiply d. divide\n");
printf("q. quit\n");
choice = getchar();
while (getchar() != '\n')
continue;
return choice;
}
float first_input(void)
{
float input;
char ch;
printf("Enter first number: \n");
while (scanf("%f",&input) == 0)
{
while((ch = getchar()) != '\n')
putchar(ch);
printf(" is not a number.\n");
printf("Please enter a number, such as 2.5, -1.78E8, or 3: \n");
}
while (getchar() != '\n')
continue;
return input;
}
float last_input(void)
{
float input;
char ch;
printf("Enter second number: \n");
while (scanf("%f",&input) == 0)
{
while((ch = getchar()) != '\n')
putchar(ch);
printf(" is not a number.\n");
printf("Please enter a number, such as 2.5, -1.78E8, or 3: \n");
}
while (getchar() != '\n')
continue;
return input;
}
float add(void)
{
float first_num, last_num;
float result;
first_num = first_input();
last_num = last_input();
result = first_num + last_num;
printf("%.1f + %.1f = %.1f\n",first_num, last_num, result);
return result;
}
float subtract(void)
{
float first_num, last_num;
float result;
first_num = first_input();
last_num = last_input();
result = first_num - last_num;
printf("%.1f - %.1f = %.1f\n",first_num, last_num, result);
return result;
}
float multipy(void)
{
float first_num, last_num;
float result;
first_num = first_input();
last_num = last_input();
result = first_num * last_num;
printf("%.1f * %.1f = %.1f\n",first_num, last_num, result);
return result;
}
float divide(void)
{
float first_num, last_num;
float result;
first_num = first_input();
last_num = last_input();
if (last_num == 0)
{
printf("Enter a number other than 0: \n");
last_num = last_input();
}
result = first_num / last_num;
printf("%.1f / %.1f = %.1f\n",first_num, last_num, result);
return result;
}