习题八

来源:互联网 发布:智联招聘java简历模板 编辑:程序博客网 时间:2024/06/10 03:45


下面的一些程序要求输入以EOF终止。如果您的操作系统难以使用或不能使用重定向,则使用一些其他的判断来终止输入,例如读取&字符。

 

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

    #include <stdio.h>

 

int main(void)

{

 int i;

 for(i=0; getchar() != EOF; i++);

 printf("There are %d char",i);

 return 0;

}

 

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

    #include <stdio.h>

 

int main(void)

{

 char ch;

 int i;

 for(i=1; (ch=getchar()) != EOF; i++)

 {

  if (ch >= ' ' || ch == '\n' || ch == '\t') printf("%-5c",ch);

  else printf("^%-4c",ch+64);

  printf("%-5d",ch);

  if(i%8 == 0) printf("\n");

 }

 return 0;

}

 

3、编写一个程序,把输入作为字符流读取,直至遇到EOF。令其报告输入中的大写字母个数和小写字母个数。假设小写字母的数值是连续的,大写字母也是如此。或者你可以使用ctypc,h库中的合适的函数来区分大小写。

 

#include <stdio.h>

#include <ctype.h>

 

int main(void)

{

 char ch;

 int lower=0,upper=0;

 while ( (ch=getchar()) != EOF )

 {

  if ( islower(ch) ) lower++;

  if ( isupper(ch) ) upper++;

 }

 printf("lower: %d, upper: %d\n",lower,upper);

 

 return 0;

}

 

4、编写一个程序,把输入作为字符流读取,直至遇到EOF。令其报告每个单词的平均字母数。不要将空白字符记为单词中的字母。实际上,标点符号也不应该计算,但现在不必考虑这一点(如果您想做得好一些,可以考虑使用ctype.h系列中的ispunct()函数)。

    #include <stdio.h>

#include <ctype.h>

 

int main(void)

{

 int num=0;

 char ch;

 bool word=0;

 while ( (ch=getchar()) != EOF )

 {

  if ( isalpha(ch) && !word )

  {

   word = 1;

   num++;

  }

  if ( !isalpha(ch) )

  {

   word = 0;

  }

 }

 printf("words: %d\n",num);

 

 return 0;

}

 

5、修改程序清单8,4中的猜测程序,使其使用更智能的猜测策略。例如,程序最初猜50,让其询问用户该猜测值是大、小还是正确。如果该猜测值小,则令下一次猜测值为50和100的中值,也就是75。如果75大,则下一次猜测值为75和50的中值,等等。使用这种二分搜索(binary search)策略,起码如果用户没有欺骗,该程序很快会获得正确答案

#include <stdio.h>

 

int main(void)

{

 int guess, max = 100, min = 1;

 char response;

 printf("Pick an integer from 1 to 100.I will try to guess ");

 printf("it.\nRespond with a b if my quess is big and with");

 printf("\nan l if it is little.\n");

 printf("Also,Respond a y if it is right.\n");

 printf("Uh...is your number %d?\n", guess = ( max + min ) / 2 );

 while((response = getchar()) != 'y')

 {

  if (response == 'b')

  {

   max = guess - 1;

   printf("Well,then,is it %d?\n",guess = ( max + min ) / 2 );

  }

  else if (response == 'l')

  {

   min = guess + 1;

   printf("Well,then,is it %d?\n",guess = ( max + min ) / 2 );

  }

  else printf("Sorry,I understand only y or n.\n");

  while (getchar() != '\n');

 }

 printf("I know I could do it!\n");

 return 0;

}

 

6、修改程序清单8.8中的get_first()函数,使其返回所遇到的第一个非空白字符。在一个简单的程序中测试该函数。

#include <stdio.h>

#include <ctype.h>

char get_first(void);

 

int main(void)

{

 char ch;

 while((ch = get_first() ) != EOF)

 {

  putchar(ch);

 }

 return 0;

}

 

char get_first(void)

{

 int ch;

 while( isspace( ch = getchar() ) );

 while ( getchar() != '\n');

 return ch;

}

 

7、修改第7章的练习8,使菜单选项由字符代替数字进行标记。

#include<stdio.h>

#include<ctype.h>

char get_first(void);

#define TIME 40  

#define ADD  1.5  

#define LIMIT1 300  

#define RATE1 0.15

#define LIMIT2 150  

#define RATE2 0.20

#define RATE3 0.25

 

int main(void)

{

 double basic,hours,gross,tax;

 printf("Enter the number corresponding to the desired pay rate or action:\n");

 printf("1) $8.75/hr\t\t\t2) $9.33/hr\n");

 printf("3) $10.00/hr\t\t\t4) $11.20/hr\n");

 printf("5) quit\n");

 switch( get_first() )

 {

 case '1': basic = 8.75; break;

 case '2': basic = 9.33; break;

 case '3': basic = 10.00; break;

 case '4': basic = 11.20; break;

 default: printf("quit\n"); return(0); //退出程序

 }

 printf("you have select $%.2lf\n",basic);

 printf("input the work hours of a week:");

 scanf("%lf",&hours);

 if (hours > 40) hours = 40 + (hours - 40) * 1.5;

 gross = hours * basic;

 printf("gross income:\t\t%lf\n",gross);

 if (gross <= LIMIT1) tax = gross * RATE1;

 else if (gross <= LIMIT2) tax = LIMIT1 * RATE1 + (gross - LIMIT1) * RATE2;

 else tax = LIMIT1 * RATE1 + LIMIT2 * RATE2 + (gross - LIMIT1 - LIMIT2) * RATE3;

 printf("tax:\t\t\t%lf\n",tax);

 printf("net income:\t\t%lf\n",gross - tax);

 return(0);

}

 

char get_first(void) //得到字符串中的第一个非空字符

{

 int ch;

 while( isspace( ch = getchar() ) );

 while ( getchar() != '\n');

 return ch;

}

 

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>

float get_float(void);

char get_first(void);

int main(void)

{

 char select;

 float num1,num2;

 while(1)

 {

  printf("Enter the operation of your choice:\n");

  printf("a.add            s.subtract:\n");

  printf("m.multiply       d.divide\n");

  printf("q.quit\n");

  select = get_first();

  if( select != 'a' && select != 's' && select != 'm' && select != 'd')

  {

   printf("Bye.\n");

   break;

  }

  printf("Enter first number:");

  num1 = get_float();

  printf("Enter second number:");

  num2 = get_float();

  while( select == 'd' && num2 == 0)

  {

   printf("Enter a number other than 0:");

   num2 = get_float();

  }

  switch(select)

  {

   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 * %.2f = %.2f\n",num1, num2, num1 * num2); break;

   case 'd': printf("%.2f / %.2f = %.2f\n",num1, num2, num1 / num2); break;

   default : break;

  }

 }

 return(0);

}

 

float get_float(void) //得到一个合适的浮点数,滤除非法数

{

 float num;

 char str[40];

 while(scanf("%f",&num)!=1)

 {

  gets(str);

  printf("%s is not a number.\n",str);

  printf("Please enter a numbe, such as 2.5, -1.78E8, or 3:");

 }

 while ( getchar() != '\n');

 return num;

}

 

char get_first(void) //得到字符串中的第一个字符,滤除其他字符

{

 int ch;

 while( isspace( ch = getchar() ) );

 while ( getchar() != '\n');

 return ch;

}

 

0 0
原创粉丝点击