设计一个程序,统计在读到文件结尾之前读取的字符数
# include
int main(){
int num = 0;
char c;
while((c = getchar())!=EOF) ++num;
printf("\n共计 %d 个字符\n", num);
return 0;
}
输入文本内容:
abc
e
f
编写一个程序,在遇到EOF之前,把输入作为字符流读取。程序要打印每个输入的字符及其相应的ASCII 十进制值。注意,在 ASCII 序列中,空格字符前面的字符都是非打印字符,要特殊处理这些字符。如果非打印字符是换行符或制表符,则分别打印\n或\t。否则,使用控制字符表示法。例如ASCII的1是Ctrl+A,可显示为^A。注意A的ASCII值是Ctrl+A的值加上64.其他非打印字符也有类似的关系。除每次遇到换行符打印新的一行外,每行打印10对值。(注意:不同的操作系统其控制字符可能不同)。
# include
int main(){
int num = 0;
char c;
while((c = getchar())!=EOF){
++num;
if('\n' == c) {
printf("\n\\n-%d\t", c);
num = 1;
} else if('\t' == c) printf("\\t-%d\t", c);
else printf("%c-%d\t", c,c);
if(num%10 == 0) putchar('\n');
}
return 0;
}
输入文本内容:
abc 123
e
f
编写一个程序,在遇到EOF之前,把输入作为字符流读取。该程序要报告输入中的大写字母和小写字符个数。假设大小写字母数值是连续的。或者使用ctype.h库中合适的分类函数更方便。
# include
# include
int main(){
int num = 0;
int n=0;
char c;
while((c = getchar())!=EOF){
if(isupper(c)) ++n;
if(islower(c)) ++num;
}
printf("大写字母的个数:%d\n", n);
printf("小写字母的个数:%d\n", num);
return 0;
}
输入文本内容:
ab cde123 445
AAA AAB C
212324
f
编写一个程序,在遇到EOF之前,把输入作为字符流读取。该程序要报告平均每个单词的字母数。不要把空白统计为单词的字母。实际上,标点符号也不应该统计,但是现在暂时不考虑这么多(如果你比较在意这点,考虑使用ctype.h系列中的ispunct()函数。
# include
# include
int main(){
int num_w = 0;
float num_c=0;
char c;
int flag = 0;
while((c = getchar())!=EOF){
if(flag){
if(isalpha(c)) ++num_c;
else{
++num_w;
flag = 0;
}
}else{
if(isalpha(c)){
flag = 1;
num_c++;
}
}
}
printf("\n该文章共计%d个单词,每个单词平局%f个字符\n", num_w,num_c/num_w);
return 0;
}
输入文本内容:
this is a dog.
dog is running on the grass.
修改程序清单8.4的猜数字程序,使用更智能的猜测策略。例如,程序最初猜50,询问用户是猜大了、猜小了、猜对了。如果是猜小了,那么下一次猜测的值应是50和100的中值,也就是75.如果这次猜大了,那么下一次猜测的值应该是50和75的中值,等等。使用二分查找策略,如果用户没有欺骗程序,那么程序很快就会猜到正确的答案。
# include
int main(void){
int guess = 1;
int low = 1;
int up = 100;
printf("Pick an integer from 1 to 100. I will try to guess ");
printf("it. \nRespond with a y if my guess is right and with");
printf("\nan n if it is wrong.\n");
printf("Uh... is your number %d?\n", guess);
char c;
printf("it is big 、small or equal?please input b s e or q(to quit):");
while((c=getchar()) && c!=EOF && c!='q'){
while(getchar()!= '\n');
if(c == 'e'){
printf("I knew I could do it !\n");
break;
}
switch(c){
case 'b': up = guess;guess = (low+up)>>1;break;
case 's': low = guess;guess = (low+up)>>1;break;
}
printf("well then, is it %d ?please input b s e or q(to quit):\n ", guess);
}
printf("Bye\n");
return 0;
}
修改程序清单8.8中的get_first()函数,让该函数返回读取的第1个非空白字符,并在一个简单的程序中测试。
# include
char get_choice(void);
char get_first(void);
int get_int(void);
void count(void);
int main(void){
int choice;
void count(void);
while((choice = get_choice()) != 'q'){
switch(choice){
case 'a': printf("Buy low, sell high.\n");break;
case 'b': putchar('\a');break;
case 'c': count();break;
default: printf("Program error!\n");break;
}
}
printf("Bye.\n");
return 0;
}
void count(void){
int n,i;
printf("Count how far? Enter an integer:\n");
n = get_int();
for(i=1;i<=n;++i) printf("%d\n", i);
while(getchar() != '\n') ;
}
char get_choice(void){
int ch;
printf("Enter the letter of your choice:\n");
printf("a. advice b.bell\n");
printf("c. count q.quit\n");
ch = get_first();
while((ch <'a' || ch>'c') && ch != 'q'){
printf("Please respond with a,b,c, or q.\n");
ch = get_first();
}
return ch;
}
char get_first(void){
int ch;
while((ch=getchar())!=EOF && ch == ' ');
while(getchar() !='\n') ;
return ch;
}
int get_int(void){
int input;
char ch;
while(scanf("%d", &input)!=1){
while((ch=getchar())!='\n')putchar(ch);
printf(" is not an integer.\nPlease enter an ");
printf("integer value, such as 25, -178, or3:");
}
}
修改第7章的编程练习8,用字符代替数字标记菜单的选项。用q代替5作为结束输入的标记
# include
# define BASE_HOURS 40
# define RATE_OVERTIEM 1.5
# define LINE_PRE 300
# define RATE_PRE 0.15
# define LINE_XU 150
# define RATE_XU 0.2
# define RATE_LAST 0.25
void display(void){
printf("\n**************************************************************************\n");
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("**************************************************************************\n");
printf("请输出您的选项(a~d,q):");
}
float get_a(void){
char id ;
float num = 0;
display();
while((id=getchar()) != '\n'){
if(id == 'q') break;
while(getchar() != '\n') ;
if(id < 'a' || id > 'd') {
printf("\n2请根据菜单提示输入。");
display();
continue;
}
switch(id){
case 'a':num = 8.75;return(num);
case 'b':num = 9.33;return(num);
case 'c':num = 10;return(num);
case 'd':num = 11.2;return(num);
default:break;
}
}
printf("Bye\n");
return -1;
}
int main(){
float num = 0;
float base = 0;
float salary_sum = 0;
float salary_rate = 0;
float salary = 0;
float work_hours = 0;
while( base = get_a()){
if(base < 0)break;
printf("\n请输入您一周的工作小时数:");
scanf("%f", &num);
while(getchar() != '\n') ;
work_hours = num>BASE_HOURS?BASE_HOURS+(num-BASE_HOURS)*RATE_OVERTIEM:num;
salary_sum = work_hours * base;
if(salary_sum <= LINE_PRE){
salary_rate = salary_sum * RATE_PRE;
salary = salary_sum - salary_rate;
}else if(salary_sum <= LINE_PRE + LINE_XU){
salary_rate = LINE_PRE * RATE_PRE + (salary_sum - LINE_PRE) * RATE_XU;
salary = salary_sum - salary_rate;
}else{
salary_rate = LINE_PRE * RATE_PRE + LINE_XU * RATE_XU + (salary_sum - LINE_XU) * RATE_LAST;
salary = salary_sum - salary_rate;
}
printf("工资总计:%f\n", salary_sum);
printf("工资税金:%f\n", salary_rate);
printf("工资到手:%f\n\n", salary);
}
return 0;
}
编写一个程序,显示一个提供加法、减法、乘法、除法的莱单。获得用户选择的选项后,程序提示用户输入两个数字,然后执行用户刚才选择的操作。该程序只接受莱单提供的迭项。程序使用 float类型的变量储存用户输入的数字,如果用户输入失败,则允许再次输入。进行除法运算时,如果用户输入 0作为笫2 个数(除数),程序应提示用户垂新输入一个新值。该程序的一个运行示例如下:
Enter the operation of your choice:
a. add b. subtract
c. multiply d. divide
q. quit
a
Enter first number: 22.4
Enter second number: one
one is not an number.
Please enter a number, such as 2.5, -1.78E8 , or 3: 1
22.4 + 1 = 23.4
Enter the operation of your choice:
a. add b. subtract
c. multiply d. divide
q. quit
d
Enter first number: 18.4
Enter second number: 0
Enter a number other than 0: 0.2
18.4 / 0.2 = 92
Enter the operation of your choice:
a. add b. subtract
c. multiply d. divide
q. quit
q
Bye
# include
# define BASE_HOURS 40
# define RATE_OVERTIEM 1.5
# define LINE_PRE 300
# define RATE_PRE 0.15
# define LINE_XU 150
# define RATE_XU 0.2
# define RATE_LAST 0.25
void display(void){
printf("Enter the operation of your choice: \n");
printf("a. add b. subtract\n");
printf("c. multiply d. divide\n");
printf("q. quit\n");
}
int main(){
char id;
float f = 0;
float s = 0;
float sum = 0;
display();
while((id=getchar()) != '\n'){
if(id == 'q') break;
while(getchar() != '\n') ;
printf("Enter first number:");
while(scanf("%f",&f) != 1){
char c;
while((c=getchar())!= '\n')putchar(c);
printf("is not a number\n");
printf("Please enter a number, such as 2.5, -1.78E8 , or 3: ");
}
while(getchar() != '\n') ;
printf("Enter second number:");
while(scanf("%f",&s) != 1){
char c;
while((c=getchar())!= '\n')putchar(c);
printf("is not a number\n");
printf("Please enter a number, such as 2.5, -1.78E8 , or 3: ");
}
while(getchar() != '\n') ;
int flag = 0;
switch(id){
case 'a':sum = f + s;printf("%f + %f = %f \n",f,s,sum);;break;
case 'b':sum = f-s;printf("%f - %f = %f \n",f,s,sum);break;
case 'c':sum = f*s;printf("%f * %f = %f \n",f,s,sum);break;
case 'd':flag = 1;break;
default:break;
}
if(flag){
if(s< 0.0000001 || s> -0.0000001){
printf("Enter a number other than 0: ");
while(scanf("%f",&s) != 1){
char c;
while((c=getchar())!= '\n')putchar(c);
printf("is not a number\n");
printf("Please enter a number, such as 2.5, -1.78E8 , or 3: ");
}
while(getchar() != '\n') ;
}
sum = f/s;
printf("%f / %f = %f \n",f,s,sum);
}
display();
}
printf("Bye\n");
return 0;
}