一
// 下面的一些程序要求输入以EOF终止。如果你的操作系统很难或根本无法使用
// 重定向,请使用一些其他的测试来终止输入,如读到&字符时停止。
// 1.设计一个程序,统计在读到文件结尾之前读取的字符数.
#include "stdio.h"
int main(void)
{
int ch;
printf("Please enter some character\n");
while((ch=getchar())!=EOF)//Ctrl+Zj结尾
putchar(ch);
return 0;
}
二
// 编写一个程序,在遇到EOF之前,把输入作为字符流读取。程序要打印每个输入
// 的字符及其相应的ASCII十进制值。注意,在ASCII序列中,空格字符前面的字符
// 都是非打印字符,要特殊处理这些字符。如果非打印字符是换行符或制表符。则
// 分别打印\n或\t。否则,使用控制字符表示法。例如,ASCII的I是Ctrl+A,可显示
// 为^A。注意。A的ASCII值是Ctrl+A的值加上64.其他非打印字符也有类似的关系
// 除每次遇到换行符打印新的一行之外,每打印10对值。(注意:不同的操作系统其
// 控制字符可能不同。)
#include <stdio.h>
int main(void)
{
char ch;
int i=0;
printf("please enter some character\n");
while((ch=getchar())!=EOF)
{
if (ch=='\n')
printf("\\n-\\n");
else if (ch>=48&&ch<=57)
{
if (ch=='1')
printf("%3c-^A",ch);
else if(ch=='2')
printf("%3c-^B",ch);
else if(ch=='3')
printf("%3c-^C",ch);
else if(ch=='4')
printf("%3c-^D",ch);
else if(ch=='5')
printf("%3c-^E",ch);
else if(ch=='6')
printf("%3c-^F",ch);
else if(ch=='7')
printf("%3c-^G",ch);
else if(ch=='8')
printf("%3c-^H",ch);
else if(ch=='9')
printf("%3c-^I",ch);
else if(ch=='0')
printf("%3c-^@",ch);
}
else if (ch=='\t')
printf("\\t-\\t");
else if (ch==' ')
printf("\' \'-%d",ch);
else
printf( "%3c-%d ",ch,ch);
i++;
if (i%10==0)
printf("\n");
}
return 0;
}
三
// 编写一个程序,在遇到EOF之前,把输入作为字符流读取。该程序要报告输入中
// 的大写字母和小写字母的个数。假设大小写字母数值是连续的。或者使用ctype.h
// 库中合适的分类函数更方便。
#include "stdio.h"
#include "ctype.h"
int main(void)
{
int i=0,l=0;
char ch;
printf("Please enter some character\n");
while((ch=getchar())!=EOF)
{
if (isupper(ch))
i++;
if (islower(ch))
l++;
}
printf("There are %d capital letters\n",i);
printf("There are %d lowercase letters\n",l);
return 0;
}
四
// 编写一个程序,在遇到EOF之前,把输入作为字符流读取。该程序要报告平均每个
// 单词的字母数.不要把空白统计为单词的字母.实际上,标点符号也不应该统计,但是
// 现在暂时不同考虑这么多(如果你比较在意这点,考虑使用ctype.h系列中的ispunct()
// 函数.)
#include "stdio.h"
#include "ctype.h"
int main(void)
{
char ch;
int word=0,letter=0,average=0;
printf("Please enter some words\n");
while((ch=getchar())!=EOF)
{
if (isspace(ch))
word++;
if (isspace(ch))
continue;
else if (ispunct(ch))
continue;
letter++;
}
printf("words=%d\nletters=%d\n",word,letter);
printf("Each word has an average of %f letters\n",letter/(float)word);
return 0;
}
五
// 修改程序清单8.4的猜数字程序,使用更智能的猜测策略.例如,程序最初猜50,
// 询问用户是猜大了、猜小了还是猜对了.如果猜小了,那么下一次猜测的值应是
// 50和100中值,也就是75.如果这次猜大了,那么下一次猜测的值应是50和75的
// 中值,等等.使用二分查找策略,如果用户没有欺骗程序,那么程序很快就会猜到
// 正确的答案.
#include "stdio.h"
int main(void)
{
int i=50;
int h=100,l=1;
char respond;
printf("Pick an integer from 1 to 100.i will try to guess it.\n");
printf("Respond with a if my guess is right and with a d if");
printf("it is big and with a s if it is small.\n");
printf("Un...is your number %d?\n",i);
while((respond=getchar())!='y')
{
if (respond=='s')
l=i;
else if (respond=='d')
h=i;
else
printf("Sorry,I understand only y or n.\n");
i=(h+l)/2;
printf("Well,then,is it %d?\n",i);
while(getchar()!='\n');
continue;
}
printf("I knew I could do it\n");
return 0;
}
六
// 修改程序清单8.8中的get_first()函数,让函数返回读取的第1个非空白
// 字符,并在一个简单的程序中测试
#include "stdio.h"
int get_first(void);
int main(void)
{
int ch;
printf("Please enter some character\n");
ch=get_first();
printf("%c\n",ch);
return 0;
}
int get_first(void)
{
int ch;
ch=getchar();
while(getchar()!='\n')
continue;
return ch;
}
七(并没有大幅度修改,代码依然写的不好)
// 修改第7章的编程练习8,用字符代替数字标记菜单菜单的选项.用q代替
// 5作为结束输入标记.
#include <stdio.h>
#define UPTIME 40
#define EIMES 1.5
#define TAX1 0.15
#define TAX2 0.20
#define TAX3 0.25
void caidan(void);
int main(void)
{
float base,Total,time,tax,income;
char ch;
caidan();
while(scanf("%c",&ch)==1)
{
if((ch > 'd' || ch < 'a')&&ch!='q')
caidan();
switch(ch)
{
case 'a': base=8.75;
printf("Please enter your working hours.\n");
break;
case 'b': base=9.33;
printf("Please enter your working hours.\n");
break;
case 'c': base=10.00;
printf("Please enter your working hours.\n");
break;
case 'd': base=11.20;
printf("Please enter your working hours.\n");
break;
case 'q': goto bye;
}
scanf("%f",&time);
if (time<=40)
{
Total=time*base;
if (Total<=300)
tax=Total*TAX1;
else
tax=300*TAX1+(Total-300)*0.2;
income=Total-tax;
}
else if (time>40)
{
Total=(time-UPTIME)*base;
Total+=(time-UPTIME)*1.5*base;
if (Total<=450)
tax=300*TAX1+(Total-300)*0.2;
else
tax=300*TAX1+150*TAX3+(Total-450)*0.25;
income=Total-tax;
}
printf("Total wages:%f\n",Total);
printf("Taxes:%f\n",tax);
printf("Net income:%f\n",income);
printf("Please select according to the menu below again.\n");
printf("*****************************************\n\n");
printf("a)$8.75/hr b)$9.33/hr\n");
printf("c)$10.00/hr d)$11.20/hr\nq)quit\n");
printf("*****************************************\n");
while(getchar()!='\n')
continue;
}
bye:printf("Thank you.\n");
return 0;
}
void caidan(void)
{
printf("Please select according to the menu below.\n");
printf("*****************************************\n\n");
printf("a)$8.75/hr b)$9.33/hr\n");
printf("c)$10.00/hr d)$11.20/hr\nq)quit\n");
printf("*****************************************\n");
}
八
编写一个程序,显示一个提供加法、减法、乘法、除法的菜单。获得用户先择的选项后,程序提示用户输入两个数字,然后执行用户刚才选择的操作。该程序只接受菜单提供的选项。程序使用float类型的变量储存用户输入的数字,如果用户输入失败,则允许再次输入。进行除法运算时,如果用户输入0作为第二个数(除数),程序应提示用户重新输入一个新值。该程序的一个运行示例如下:
Enter the operation of your choice:
a. add s. subtract
m. 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 s. subtract
m. 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 s. subtract
m. multiply d. divide
q. quit
q
Bye.
代码如下
#include "stdio.h"
float jiafa(void);
float jianfa(void);
char caidan(void);
int main(void)
{
float f,s;
int choise;
choise=caidan();
while(choise=='a'||choise=='s'||choise=='m'||choise=='d'&&choise!='q')
{
f=jianfa();
s=jiafa();
switch (choise)
{
case 'a': printf("%f+%f=%f\n",f,s,f+s);
break;
case 's': printf("%f-%f=%f\n",f,s,f-s);
break;
case 'm': printf("%f*%f=%f\n",f,s,f*s);
break;
case 'd': printf("%f/%f=%f\n",f,s,f/s);
break;
}
while(getchar()!='\n')
continue;
choise=caidan();
}
printf("Bye.\n");
return 0;
}
char caidan(void)
{
char ch;
printf("Enter the operation of your choice:\n");
printf("a. add s. subtract\n");
printf("m. multiply d. divide\nq. quit\n");
ch=getchar();
while(getchar()!='\n')
continue;
while(ch!='a'&&ch!='s'&&ch!='m'&&ch!='d'&&ch!='q')
{
printf("Please respond with a,s,m,d or q.\n");
ch=getchar();
}
return ch;
}
float jianfa(void)
{
char ch;
float first;
printf("Enter first number:");
while (scanf("%f",&first)!=1)
{
while ((ch=getchar()) !='\n')
putchar(ch);
printf(" is not an number.\n");
printf("Please enter a number, such as 2.5, -1.78E8, or 3: 1\n");
}
return first;
}
float jiafa(void)
{
char c;
float second;
printf("Enter second number:");
while (scanf("%f",&second)!=1)
{
while ((c=getchar()) !='\n')
putchar(c);
printf(" is not an number.\n");
printf("Please enter a number, such as 2.5, -1.78E8, or 3: 1\n");
}
return second;
}