getchar()和putchar()每次都只能处理一个字符
空格 换行都能读,所以有时候要对数据进行处理
编写一个程序,遇到EOF之前。统计读入的字符
#include
int main(void)
{
int count;
while ( getchar() != EOF)
{
count++;
}
printf("%d", count);
return 0 ;
}
编写一个程序。读取大小写字母的个数
#include
#include
int main(void)
{
int ch ;
int Count, count;
while ((ch = getchar()) != EOF)
{
if (isalpha(ch))
{
if (islower(ch))
{
count++;
}
else
Count++;
}
}
printf("大写字母:%d个\n小写字母:%d个", Count, count);
return 0 ;
}
编写一个程序。把输入作为字符流读取报告平均每个单词的字母数。
#include
#include
int main(void)
{
char ch;
int letter = 0, word = 0;
int flag;
while ( (ch = getchar()) != EOF)
{
if (isspace(ch) && flag == 1)
{
word++;
flag = 0;
}
else if (isalpha(ch))
{
letter++;
flag = 1;
}
}
printf("单词:%d个, 字母:%d个\n", word, letter);
printf("平均每个单词%.2lf个字母\n", (double)letter / (double) word);
return 0;
}
修改程序清单8.4的程序
#include
int main(void)
{
int enter_;
int guess = 50;
char ch;
int x = 0, y = 100;
printf("Enter an int from 1 to 100. I will try to guess:\n");
scanf("%d", &enter_);
printf("your number %d", guess);
printf("Uh....is your number %d\n", guess);
printf("a) 大了 b)小了\n");
printf("y) yes\n");
getchar();
while ((ch = getchar()) != 'y')
{
if (ch == 'a')
{
y = guess;
guess = (guess - x) / 2 + x;
}
else if (ch == 'b')
{
x = guess;
guess = (y - guess) / 2 + guess;
}
printf("Uh....is your number %d\n", guess);
printf("a) 大了 b)小了\n");
printf("y) yes\n");
while (getchar() != '\n')
{
continue;
}
}
printf("厉害吧!!!");
return 0;
}
修改程序清单8.8中的get_first()函数,让该函数返回读取的第一个非空白字符,并在一个简单的程序中测试
#include
#include
void get_first(void);
int main(void)
{
get_first();
return 0;
}
void get_first(void)
{
int ch;
while ( isspace(ch = getchar()))
{
continue;
}
while (getchar() != '\n')
{
continue;
}
printf("%c", ch);
}
修改第7章的编程练习8,用字符代替数字标记菜单的选项。用q代替5作为结束输入的标记
#include
#include
#define AMT1 300
#define AMT2 150
#define OVERTIME 1.5
#define TIME 40
#define TAX15 0.15
#define TAX20 0.20
#define TAX25 0.25
#define SIGN "***********************************"
char get_choice(void);
char get_first(void);
float get_time(void);
float money(float mon_hr);
int main(void)
{
char ch;
while ((ch = get_choice()) != 'q')
{
switch (ch)
{
case 'a':
money(8.75);
break;
case 'b':
money(9.33);
break;
case 'c':
money(10.00);
break;
case 'd':
money(11.20);
break;
default:
printf("Enter a, b, c, d, q");
break;
}
}
return 0;
}
char get_choice(void)
{
int ch;
printf("%s%s\n", SIGN, SIGN);
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("%s%s\n", SIGN, SIGN);
ch = get_first();
return ch;
}
char get_first(void)
{
int ch;
while (isspace(ch = getchar()))
{
continue;
}
while (getchar() != '\n')
{
continue;
}
return ch;
}
float money(float mon_hr)
{
float time, gross, taxes, net;
time = get_time();
gross = time * mon_hr;
if (gross <= AMT1)
{
taxes = TAX15 * gross;
}
else if (gross <= AMT1 + AMT2)
{
taxes = TAX15 * AMT1 + (gross - AMT1) * TAX20;
}
else
{
taxes = TAX15 * AMT1 + TAX20 * AMT2 + TAX25 * (gross - AMT1 - AMT2);
}
net = gross - taxes;
printf("gross: $%.2lf; taxes: $%.2lf; net: $%.2lf.\n", gross, taxes, net);
}
float get_time(void)
{
float time;
char ch;
printf("Enter time:");
while ((scanf("%f", &time)) != 1)
{
while ((ch = getchar()) != '\n')
putchar;
printf(" is not number:\n");
}
if (time > TIME)
{
time = TIME + (time - TIME) * OVERTIME;
}
return time;
}
编写一个程序,显示一个提供加法、减法、乘法、除法的菜单。获得用户选择的选项后,程序提示用户输入两个数字,让后执行用户刚才选择的操作。改程序只接受菜单提供的选项。
#include
#include
char get_first(void); //获得一个字符
char get_choice(void); //选择语句
void add(void); //加法
void subtract(void); //减法
void multiply(void); //乘法
void divide(void); //除法
float get_float(void); //获得数字
int main(void)
{
char ch;
while ((ch = get_choice()) != 'q')
{
switch (ch)
{
case 'a':
add();
break;
case 's':
subtract();
break;
case 'm':
multiply();
break;
case 'd':
divide();
break;
default:
printf("Please respond with a, s, m, d, q.\n");
}
}
return 0;
}
char 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 = get_first();
return ch;
}
char get_first(void)
{
int ch;
while (isspace(ch = getchar()))
{
continue;
}
while (getchar() != '\n')
{
continue;
}
return ch;
}
void add(void)
{
float x, y;
printf("Enter first number:");
x = get_float();
printf("Enter seconde number:");
y = get_float();
printf("%.1f + %.1f = %.1f\n", x, y, x+y);
}
void subtract(void)
{
float x,y;
printf("Enter first number:");
x = get_float();
printf("Enter seconde number:");
y = get_float();
printf("%.1f - %.1f = %.1f\n", x, y, x-y);
}
void multiply(void)
{
float x,y;
printf("Enter first number:");
x = get_float();
printf("Enter seconde number:");
y = get_float();
printf("%.1f * %.1f = %.1f\n", x, y, x*y);
}
void divide(void)
{
float x,y;
printf("Enter first number:");
x = get_float();
printf("Enter seconde number:");
while ((y = get_float()) == 0)
continue;
printf("%.1f / %.1f = %.1f\n", x, y, x/y);
}
float get_float(void)
{
float input;
char ch;
while((scanf("%f", &input)) != 1)
{
while ((ch = getchar()) != '\n')
putchar(ch);
printf(" is not an number\n");
}
return input;
}