下面的一些程序要求输入以EOF终止。如果您的操作系统难以使用或不能使用重定向,则使用一些其他的判断来终止输入,例如读取&字符。
1.设计一个程序,统计从输入到文件结尾为止的字符数。
#include<stdio.h>
int main(void) { int count = 0 ; printf("input characters:\n"); while(getchar() != EOF) count++; printf("there are%d characters\n",count); return 0; }
2.编写一个程序,把输入作为字符流读取,直到遇到EOF。令该程序打印每个输入字符及其ASCII编码的十进制值。注意在ASCII序列中空格字符前面的字符是非打印字符,要特殊处理这些字符。如果非打印字符是换行符或制表符,则分期打印\n或\t。否则,使用控制字符符号。例如,ASCII的l是Ctrl+A,可以显示为AA。注意A的ASCⅡ值是Ctrl+A的值加64。对其他非打印字符也保持相似的关系。除去每次遇到一个换行符时就开始一个新行之外,每行打印10对值。
#include<stdio.h> #define SPACE ' ' #define ENTER '\n' #define TAB '\t' int main(void) { printf("input characters:\n"); char input ; int count = 1; while( (input = getchar() )!= EOF) { if(input == ENTER) printf("\\n|%d ",input); else if(input == TAB) printf("\\t|%d ",input); else if( input >= 0 && input < SPACE) printf("^%c|%d ",input + 64,input); else printf("%c|%d ",input,input); if(count++ % 10 == 0) putchar(ENTER); } return 0; }
3.编写一个程序,把输入作为字符流读取,直至遇到EOF。令其报告输入中的大写字母个数和小写字母个数。假设小写字母的数值是连续的,大写字母也是如此。或者你可以使用ctypc,h库中的合适的函数来区分大小写。
#include<stdio.h> #include<ctype.h> int main(void) { printf("enter charactes:\n"); char input; int COUNT = 0; int count = 0; while( (input =getchar() ) != EOF) { if(islower(input)) count++; if(isupper(input)) COUNT++; } printf("lower character is %d, upper character is %d \n",count,COUNT); return 0; }
4.编写一个程序,把输入作为字符流读取,直至遇到EOF。令其报告每个单词的平均字母数。不要将空白字符记为单词中的字母。实际上,标点符号也不应该计算,但现在不必考虑这一点(如果您想做得好一些,可以考虑使用ctype.h系列中的ispunct()函数)。
源自:http://hi.baidu.com/ch314156/blog/item/a14fb9d79d916f173bf3cf22.html
#include<stdio.h> #include<ctype.h> int main(void) { char input; int words_count = 0; int character_count = 0; bool inwords = false; // printf("enter words:\n"); while( (input = getchar() ) != EOF) { if(!isspace(input) && !ispunct(input)) character_count++; //字母数 if(!isspace(input) && !inwords) { inwords = true; //开始一个新单词 words_count++; //统计单词数 } if( (ispunct(input) || isspace(input) ) && inwords) inwords = false; //到达单词结尾 } printf("%d characters read.\n",character_count); printf("%d words read.\n",words_count); printf("the average number of letters per word is %.2f .\n",(float)words_count/(float)character_count); return 0; }
5.修改程序清单8,4中的猜测程序,使其使用更智能的猜测策略。例如,程序最初猜50,让其询问用户该猜测值是大、小还是正确。如果该猜测值小,则令下一次猜测值为50和100的中值,也就是75。如果75大,则下一次猜测值为75和50的中值,等等。使用这种二分搜索(binary search)策略,起码如果用户没有欺骗,该程序很快会获得正确答案。
#include<stdio.h> void clean(void); int main(void) { int min = 1; int max = 100; int guess = 50; printf("is it %d?\n",guess); bool answer = false; while( getchar() != 'y') { clean(); printf("%d is biger or smaller?(b or s): ",guess); char adjust = getchar(); clean(); if(adjust == 's') //guess 猜小了 { min = guess; guess = (max + min) / 2; printf("is it %d?\n",guess); continue; } if(adjust == 'b') //guess 猜大了 { max = guess; guess = (min + max) / 2; printf("is it %d?\n",guess); continue; } } printf("Good job\n"); return 0; } void clean(void) { while(getchar() != '\n') continue; }
6.修改程序清单8.8中的get_first()函数,使其返回所遇到的第一个非空白字符。在一个简单的程序中测试该函数。
#include<stdio.h> #include<ctype.h> char get_first(void); int main(void) { printf("enter:\n"); char firstChar = get_first(); putchar(firstChar); return 0; } char get_first(void) { int input; while(isspace(input = getchar())); while(getchar() != '\n') continue; return input; }
8.编写一个程序,显示一个菜单,为您提供加法、减法、乘法或除法的选项。获得您的选择后,该程序请求两个数,然后执行您选择的操作。该程序应该只接受它所提供的菜单选项。它应该使用float类型的数,并且如果用户未能输入数字应允许其重新输入。在除法的情况中,如果用户输入O作为第二个数,该程序应该提示用户输入一个新的值。一个典型的程序运行应该如下所示:
Enter the operation of your choice:
a. add s. subtract
m. multiply d. divide
q. quic
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 s. subtract
m. multiply d. divide
q. quit
Enter first number: 18.4
Enter second number: O
Enter a number other than 0: 0.2
18.4 / 0.2 = 92
Enter the operation of your choice:
a. add s. subtract
m. multiply d. divide
q. quit
q
Bye.
#include<stdio.h> #include<ctype.h> static float num1,num2; char get_first(void); void menu(void); float get_float(void); char get_choice(void); int main(void) { menu(); char choice; while((choice = get_choice()) != 'q') { printf("enter first number:"); num1 = get_float(); printf("enter second number:"); num2 = get_float(); switch(choice) { case 'a': printf("%.2f + %.2f = %.2f\n",num1,num2,num1+num2);break; case 's': printf("%.2f - %.2f = %.2f\n",num1,num2,num1-num2);break; case 'm': printf("%.2f x %.2f = %.2f\n",num1,num2,num1*num2);break; case 'd': printf("%.2f / %.2f = %.2f\n",num1,num2,num1/num2);break; default: printf("program error\n"); break; } menu(); } printf("Bye!\n"); return 0; } char get_first(void) { int input; while(isspace(input = getchar())); while(getchar() != '\n') continue; return input; } void menu() { printf("Enter the operation of your choice:\n"); printf("a. add s. substract\n"); printf("m. multiply d.divide\n"); printf("q. quit\n"); } char get_choice() { char choice; choice = get_first(); while(choice != 'a' && choice != 's' && choice != 'm' & choice != 'd' && choice != 'q') { printf("please response with a,s,m,d or q.\n"); choice = get_first(); } return choice; } float get_float() { float num; char ch; while(scanf("%f",&num) != 1) { while((ch = getchar()) != '\n') putchar(ch); printf("is not an number.\n"); printf("enter a number,such as 2.5,-1.78E8 or 3:"); } return num; }