您的位置:首页 > 编程语言

8.11 编程练习

2016-03-07 22:10 253 查看
下面的程序要求输入以EOF终止,如果你您的系统难以使用或者不能使用重新定向,则使用其他的判断来终止输入,例如读取&字符。

1.设计一个程序,统计从输入到文件结尾为止的字符数。

#include <stdio.h>
int main(void)
{
int ch;
int count=0;
while((ch=getchar())!=EOF)
{
count++;
}
printf("您输入的%d个字符。\n",count);
return 0;
}

2.编写一个程序,把输入作为字符流读取,直到遇到EOF。令改程序打印每个输入字符以及其ASCII编码的十进制值。注意在ASCII序列中空格字符前面的字符是非打印字符,要特殊处理这些字符。如果非打印字符是换行符或者制表符,则分别打印\n或者\t。否则,使用控制字符符号。例如,ASCII的1是Ctrl+A,可以显示为^A。注意A的ASCII值是Ctrl+A的值加64.对于其他非打印字符也保持相似的关系。除去每次遇到的换行符时就开始一个新行之外。每行打印10对值。

#include <stdio.h>
int main(void)
{
int ch;
int count=0;
while((ch=getchar())!=EOF)
{
if(count%10==0)
{
putchar('\n');
}
if(ch<' ')
{
if(ch!='\n'&&ch!='\t')
{
putchar('^');
putchar(ch+64);
count+=2;
}
if(ch=='\t')
{
putchar(ch);
count++;
}
if(ch=='\n')
{
putchar(ch);
count=0;
}
}
else
{
putchar(ch);
count++;
}
}
return 0;
}


3.编写一个程序,把输入作为字符流读取,自导遇到EOF。令其报告收入中的大写字母个数和小写字母个数。假设小写字母的数值是练习的,大写字母也是如此。

#include <stdio.h>
#include <ctype.h>

int main(void)
{
int ch;
int lower=0;
int upper=0;
while((ch=getchar())!=EOF)
{
if(islower(ch))
lower++;
if(isupper(ch))
upper++;
}
printf("小写字母总数是%d,大写字母总数是%d",lower,upper);
return 0;
}


4.编写一个程序,把输入作为字符流读取,直到遇到EOF。令其报告每个单词的平均字母数量。不要讲空白字符记为单词中的字母。标点符号也不应该计算。
5.修改程序清单8.4中的猜测程序,使其使用更加智能的猜测策略。例如,程序最初猜测50,让其询问用户该猜测是大还是小或者正确。如果该猜测值小,则令下一次猜测值为50和100的中值,也就是75.如果75大,则猜测值为75和50的中值。等等。这种二分搜索策略,起码用户没有欺骗,该程序很快就会获得正确答案。

#include <stdio.h>
int main(void)
{

int answer=55;
int max=100;
int min=1;
char response;
int guess=75;

printf("pick an integer from 1 to 100. I will try to guess it\n");
printf("Respind with a y if my guess is right and with nan n if it is wrong.\n");
printf("Uh...is your number %d?\n",guess);
printf("The answer is %d\n",answer);
while((response=getchar())!='y')
{
printf("%c\n",response);
while(getchar()!='\n')
continue;
if(response=='b')
{
max=guess;
guess=(guess+min)/2;
printf("It is big than my number.\nThen is your number %d\n",guess);
printf("min=%d\nmax=%d\n",min,max);
}
else if(response=='s')
{
min=guess;
guess=(guess+max)/2;
printf("it is small than my number.\nThen is your number %d\n",guess);
printf("min=%d\nmax=%d\n",min,max);
}
else
{
printf("Sorry,I understand only y or b or s.\n");
}

}

printf("Well,then ,is it %d?\n",++guess);
printf("I knew I could do it!\n");
return 0;
}
6.修改程序清单中8.8中的get_first()函数。使其返回所遇到的第一个非空白字符,在一个简单的程序中测试该函数。
#include <stdio.h>
#include <ctype.h>
char get_firstnospace();

int main(void)
{
int ch;
ch=get_firstnospace();
putchar(ch);
return 0;

}
char get_firstnospace()
{
char ch;
while(isspace(ch=getchar()))
continue;
/*	while(getchar!='\n')
continue;*/
return ch;
}
7.修改7章中的练习8,使菜单选项由字符代替数字进行标记。

#include <stdio.h>
#define OVERTIME 1.5
#define RATE1 0.15
#define RATE2 0.2
#define RATE3 0.25
void print();
float tax_amount(float income);
float hourlywage(int choose);
int main(void)
{
float income;
float after_tax_income;
float hour;
float hourlypay;
float tax;
char choose;
do
{
print();
choose=getchar();
while(getchar()!='\n');
continue;

switch(choose)
{
case 'a':
hourlypay=8.75;break;
case 'b':
hourlypay=9.33;break;
case 'c':
hourlypay=10.00;break;
case 'd':
hourlypay=11.20;break;
case 'q':
printf("程序结束\n");break;
default:
printf("请输入a,b,c,d,q\n");continue;
}

if(choose=='q')
{
break;
}

printf("你的时薪是%.2f\n",hourlypay);

printf("请输入你的工作小时\n");
scanf("%f",&hour);
printf("你的实际工作小时是%.2f\n",hour);

if(hour>40)
{
hour=(hour-40)*OVERTIME+40;
printf("折算的加班时间为%.2f\n",hour);
}

income=hour*hourlypay;

tax=tax_amount(income);

after_tax_income=income-tax;
after_tax_income=income-tax;
printf("税前工资%.2f元,需缴税%.2f元,税后工资%.2f元\n",income,tax,after_tax_income);
while(getchar()!='\n');
continue;
}while(choose!=5);
return 0;
}

void print()
{
printf("\n");
printf("*****************************************************************\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");
}

float tax_amount(float income)
{
float tax;

if(income<=300)
{
tax=income*RATE1;
}
else if(income<=450)
{
tax=300*RATE1+(income-300)*RATE2;
}
else
{
tax=300*RATE1+150*RATE2+(income-450)*RATE3;
}
return tax;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: