编程练习
1.
#include <stdio.h>
int main(void)
{
int count = 0;
while(getchar() != '\n')
{
count++;
}
printf("There are %d chars.\n", count);
return 0;
}
2.
#include <stdio.h>
int main(void)
{
int n;
char ch;
for(n = 0; (ch = getchar()) != EOF; n++)
{
if(ch >= " " || ch == "\n" || ch == "\t")
printf("%-5c", ch);
else
printf("%-4c", ch + 64);
printf("%-5d", ch);
if(n % 10 == 0)
printf("\n");
}
return 0;
}
3.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int u = 0, l = 0;
char ch;
while((ch = getchar()) != '\n')
{
if(isupper(ch))
l++;
if(islower(ch))
u++;
}
printf("There are %d uppers and %d lowers.\n", u, l);
return 0;
}
4.
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
int main(void)
{
int word = 0, sum = 0;
char ch;
bool inword = false;
while((ch = getchar()) != '\n')
{
if(!ispunct(ch) && !isspace(ch))
sum++;
if(!isspace(ch) && !inword)
{
inword = true;
word++;
}
if(isspace(ch))
inword = false;
}
printf("%d\n", word);
printf("%d\n", sum);
printf("The average of per words is %d.\n", sum / word);
return 0;
}
使用inword主要是用来统计单词数量。
5.
#include <stdio.h>
int main(void)
{
int guess, max = 100, min = 1;
char respond;
printf("Pick an integer from 1 to 100. I will try to guess ");
printf("it.\nRespond with a g if my guess is great and with ");
printf("\nan l if it is little.\n");
printf("If my guess is right, respond y.\n");
printf("If my guess is wrong, respond n.\n");
printf("Is it your number is %d.\n", guess = (max + min) / 2);
while((respond = getchar()) != 'y')
{
if(respond == 'g')
{
max = guess - 1;
printf("Well, is it %d.\n", guess = (max + min) / 2);
}
else if(respond == 'l')
{
min = guess + 1;
printf("Well, is it %d.\n", guess = (max + min) /2);
}
else
printf("Sorry, I can't understand it.\n");
while(getchar() != '\n');
}
printf("I did it.\n");
return 0;
}
6.
char get_first(void)
{
int ch;
ch = getchar();
while(isspace(ch = getchar()));
while(getchar() != '\n');
return ch;
}
利用isspace确定。
7.
#include <stdio.h>
#define TAX1 0.15
#define TAX2 0.2
#define TAX3 0.25
char get_first(void);
int main(void)
{
float pay, hours, tax, HOURPER;
while(1)
{
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("1) $8.75/hr\t\t\t2) $9.33/hr\n");
printf("3) $10.00/hr\t\t\t4) $11.20/hr\n");
printf("5) quit\n");
switch(get_first())
{
case 1: HOURPER = 8.75;
break;
case 2: HOURPER = 9.33;
break;
case 3: HOURPER = 10.00;
break;
case 4: HOURPER = 11.20;
break;
case 5: printf("quit.\n");
return 0;
}
printf("Pleas enter the hours:\n");
scanf("%f", &hours);
if(hours > 40.0 )
{
hours = 40 + (hours - 40) *1.5;
pay = hours * HOURPER;
printf("Pay is %f.\n", pay);
}
else
pay = hours * HOURPER;
if(pay <= 300)
tax = pay * TAX1;
else if(pay <= 450)
tax = 300 * TAX1 + (pay - 300) * TAX2;
else
tax = 300 * TAX1 + 150 * TAX2 + (pay - 450) * TAX3;
printf("pay is %f,tax is %f, income is %f\n", pay, tax, pay - tax);
return 0;
}
}
char get_first(void)
{
char ch;
while(isspace(ch = getchar()));
while(getchar() != '\n');
return ch;
}
其实就是把get_int函数改成get_first函数。。。。。。。。。
8.
#include <stdio.h>
#include <ctype.h>
float get_float(void);
char get_char(void);
int main(void)
{
float first_number, second_number;
char ch;
while(1)
{
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 = get_char();
if(ch != 'a' && ch != 's' && ch != 'm' && ch != 'd')
{
printf("Bye.\n");
break;
}
printf("Enter first number:");
first_number = get_float();
printf("Enter second number:");
second_number = get_float();
while(ch == 'd' && second_number)
{
printf("Enter another nunmber:");
second_number = get_float();
}
switch(ch)
{
case 'a':
printf("%.2f + %.2f = %.2f\n", first_number, second_number, first_number + second_number);
break;
case 's':
printf("%.2f - %.2f = %.2f\n", first_number, second_number, first_number - second_number);
break;
case 'm':
printf("%.2f * %.2f = %.2f\n", first_number, second_number, first_number * second_number);
break;
case 'd':
printf("%.2f / %.2f = %.2f\n", first_number, second_number, first_number / second_number);
break;
default:
break;
}
}
return 0;
}
float get_float(void)
{
float num;
char str[40];
while(scanf("%f", &num) != 1)
{
gets(str);
printf("%s is not a number.\n", str);
printf("Please enter a number, such as 2.5, -1.78E8, or 3:");
}
while(getchar() != '\n')
return num;
}
char get_char(void)
{
int ch;
while(isspace(ch = getchar()));
while(getchar() != '\n');
return ch;
}