下面的一些程序要求输入以EOF终止。如果你的操作系统很难或根本无法使用重定向,请使用一些其他的测试来终止输入,如读到&字符时停止。
1.设计一个程序,统计在读到文件结尾之前读取的字符数。
#include<stdio.h>
int main(void)
{
unsigned auto short int count=0;
int ch;
while((ch=getchar())!=EOF)
count++;
printf("%hd\n",count);
return 0;
}
*/
```c
``/* 2.编写一个程序,在遇到 EOF 之前,把输入作为字符流读取。程序要打印每个输入的字符及其相应的ASCII十进制值。注意,在ASCII序列中,空格字符前面的字符都是非打印字符,要特殊处理这些字符。如果非打印字符是换行符或制表符,则分别打印\n或\t。否则,使用控制字符表示法。例如,ASCII的1是Ctrl+A,可显示为^A。注意,A的ASCII值是Ctrl+A的值加上64。其他非打印字符也有类似的关系。除每次遇到换行符打印新的一行之外,每行打印10对值。(注意:不同的操作系统其控制字符可能不同。)
#include<stdio.h>
#include<ctype.h>
int main(void)
{
int ch,count=0;
while((ch=getchar())!=EOF)
{
count++;
if(!isprint(ch))
{
if(ch=='\n')
printf(" \\n--%d ",ch);
else if(ch=='\t')
printf(" \\t--%d ",ch);
}
if(iscntrl(ch))
printf(" ^%c--%d ",64+ch,ch);
if(count==10)
putchar('\n');
}
return 0;
}
```c
/* 3.编写一个程序,在遇到 EOF 之前,把输入作为字符流读取。该程序要报告输入中的大
写字母和小写字母的个数。假设大小写字母数值是连续的。或者使用ctype.h库中合适的分>类函数更方便。*/
#include<stdio.h>
#include<ctype.h>
int main(void)
{
int ch,u_count=0,l_count=0;
while((ch=getchar())!=EOF)
{
if(islower(ch))
l_count++;
else if(isupper(ch))
u_count++;
}
printf("lower==%d-----uper==%d\n",l_count,u_count);
return 0;
}
```c
* 4.编写一个程序,在遇到EOF之前,把输入作为字符流读取。该程序要报告平均每个单词
的字母数。不要把空白统计为单词的字母。实际上,标点符号也不应该统计,但是现在暂时
不同考虑这么多(如果你比较在意这点,考虑使用ctype.h系列中的ispunct()函数)。*/
#include<stdio.h>
#include<ctype.h>
int main(void)
{
int ch,count=0;
while((ch=getchar())!=EOF)
{
if(isspace(ch)||ispunct(ch))
continue;
else
count++;
}
printf("%d\n",count);
return 0;
}
``/* 5.修改程序清单8.4的猜数字程序,使用更智能的猜测策略。例如,程序最初猜50,询问用户是猜大了、猜小了还是猜对了。如果猜小了,那么下一次猜测的值应是50和100中值,也就是75。如果这次猜大了,那么下一次猜测的值应是50和75的中值,等等。使用二分查找(binary search)策略,如果用户没有欺骗程序,那么程序很快就会猜到正确的答案。*/
#include<stdio.h>
int main(void)
{
signed short int num=100;
int ch,x=50,i=50;
printf("系统初值%d\n",x);
printf("请确认系统输入——》l u y \n");
while(EOF!=(ch=getchar()))
{
if('l'==ch)
{
i=i+x/2;
printf("%d\n",i);
printf("猜小了\n");
}
else if('u'==ch)
{
i=x+(i-x)/2;
printf("%d\n",i);
printf("猜大了\n");
}
else if('y'==ch && i==num)
{
printf("%d\n",i);
printf("猜对了\n");
break;
}
while(getchar()!='\n')
continue;
}
return 0;
}
* 6.修改程序清单8.8中的get_first()函数,让该函数返回读取的第1个非空白字符,并在
一个简单的程序中测试。*/
char get_first()
{
char ch;
scanf(" %c",&ch);
while(getchar()!='\n')
continue;
return ch;
}
```* 7.修改第7章的编程练习8,用字符代替数字标记菜单的选项。用q代替5作为结束输入的标记。*/
#include<stdio.h>
//#define BASE_PAY 1000//本工资
#define WORK_TIME 40 //每周40小时
#define OVERTIME_PAY 1.5//加班1.5倍时间
#define THREE_PAY 300//前三百美元
#define THREE_TAX 0.15//前三百美元的税率
#define CONTINUED_PAY 150//续150美元
#define CONTINUED_TAX 0.2//续150美元的税率
#define MORE_TAX 0.25//余下的税率
void select(void);
int main(void)
{
start:
printf("请选择每小时的薪水\n");
select();
unsigned auto short int time;
char ch;
printf("Enter the number corresponding to the desired pay rate or action:\n");
float pay=0.0;
while((ch=getchar())!='q')
{
switch(ch)
{
case 'a':
pay=8.75;
break;
case 'b':
pay=9.33;
break;
case 'c':
pay=10.0;
break;
case 'd':
pay=11.20;
break;
case 'q':
goto done;
default :
{
while(getchar()!='\n')
continue;
printf("请正确选择时薪\n");
select();
continue;
}
}
float base_pay=0,tax=0,net_income=0;
printf("请输入加班时间\n");
while(scanf("%hu",&time)==1 && time>=0)
{
if(time>WORK_TIME)
base_pay=pay*time*OVERTIME_PAY;
base_pay=pay*time;
if(base_pay<=THREE_PAY)
tax=base_pay*THREE_TAX;
else if(base_pay<=(THREE_PAY+CONTINUED_PAY))
tax=base_pay*CONTINUED_TAX;
else if(base_pay>(THREE_PAY+CONTINUED_PAY))
tax=base_pay*MORE_TAX;
net_income=base_pay-tax;
printf("总工资=%.2f\n税金=%.2f\n净收入=%.2f\n",base_pay,tax,net_income);
select();
}
}
done:printf("!Done\n");
return 0;
}
void select(void)
{
printf("*****************************************************************\n");
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("(a: $8.75/hr");
printf("%30s\n","b) $9.33/hr");
printf("c) $10.00/hr");
printf("%30s\n","d) $11.20/hr");
printf("q) quit\n");
printf("******************************************************************\n");
}
```c
/* 8.编写一个程序,显示一个提供加法、减法、乘法、除法的菜单。获得用户选择的选项后,程序提示用户输入两个数字,然后执行用户刚才选择的操作。该程序只接受菜单提供的选项。程序使用float类型的变量储存用户输入的数字,如果用户输入失败,则允许再次输入。进行除法运算时,如果用户输入0作为第2个数(除数),程序应提示用户重新输入一个新值。该程序的一个运行示例如下:*/
#include<stdio.h>
void select(void);
float add(void);
float sub(void);
float mu(void);
float divide(void);
float num(void);
char get_char(void);
int main(void)
{
select();
char ch;
float x=0;
while((ch=get_char())!=‘q’)
{
switch(ch)
{
case ‘a’:
x=add();
printf(“和=%.2f\n”,x);
select();
break;
case ‘s’:
x=sub();
printf(“差=%.2f\n”,x);
select();
break;
case ‘m’:
x=mu();
printf(“积=%.2f\n”,x);
select();
break;
case ‘d’:
x=divide();
printf(“商=%.2f\n”,x);
select();
break;
case ‘q’:
break;
default:
{
select();
continue;
}
}
}
printf(“Done!\n”);
return 0;
}
float add(void)
{
printf(“输入被加数\n”);
float a,b;
a=num();
printf(“请输入加数\n”);
b=num();
return (a+b);
}
float sub(void)
{
float a,b;
printf(“请输入被减数\n”);
a=num();
printf(“请输入减数\n”);
b=num();
return (a-b);
}
float mu(void)
{
float a,b;
printf(“请输入被乘数\n”);
a=num();
printf(“请输入乘数\n”);
b=num();
if(b0)
{
printf(“0不能作乘数,请重新输入\n”);
b=num();
}
return (a*b);
}
float divide(void)
{
float a,b;
printf(“请输入被除数\n”);
a=num();
printf(“请输入除数\n”);
b=num();
if(b0)
{
printf(“0不能做除数,请重新输入\n”);
b=num();
}
return (a/b);
}
void select(void)
{
printf(“Enter the operation of your choice:\n”);
printf(“a. add”);
printf("%30s\n",“s. subtract”);
printf(“m. multiply”);
printf("%30s\n",“d. divide”);
printf(“quit!\n”);
}
float num(void)
{
float a;
char ch;
while(scanf("%f",&a)!=1)
{
while((ch=getchar())!=’\n’)
putchar(ch);
printf(“请输入数字\n”);
}
while(getchar()!=’\n’)
continue;
return a;
}
char get_char(void)
{
char ch;
scanf(" %c",&ch);
while(getchar()!='\n')
continue;
return ch;
}