C Primer Plus 第八章复习题……2015.4.28

来源:互联网 发布:易语言软件加壳免杀 编辑:程序博客网 时间:2024/06/05 02:02
 C Primer Plus 

                        ——普拉塔(第五版)

第八章 课后习题


 1、putchar(getchar()),输入输出;
getchar(putchar()),无效,因为函数getchar不需参数;


2、字符 H, ^G, 换行符 ,退一格
3、count<essay>essayct or else count>essayct<essay
4、C 
5、文件结尾。getchar putchar 返回的一个特定信号
6、If you qu     HJacrthjacrt
7、C的标准把不同的文件形式映射为统一的流
8、scanf跳过空白符,getchar则读取所有字符


编程练习


1、#include<stdio.h>
#include<stdlib.h>
int main(void)
{
   int i;
   for(i=0;getchar()!=EOF;i++)
          ;
    printf("These are %d char",i);
    return 0;


2、
#include<stdio.h>
int main(void)
{
char ch;
int i;
for(i=0;(ch=getchar())!='#';i++)
{
if(ch>=' '||ch=='\n'||ch=='\t')
   printf("%c",ch);
else
   printf("%-4c",ch+64);
printf("%-5d",ch);
if(i%8==0)
printf("\n");
}
return 0;

3.
#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、
#include<stdio.h>
#include<ctype.h>
#include<stdbool.h>
int main(void)
{
char ch;
int num=0;
bool in_word=false;
while((ch=getchar())!=EOF)
{
if(isalpha(ch)&&!in_word)
{
in_word=true;
num++;
}
if(!isalpha(ch))
{
in_word=false;
}
}
printf("words:%d\n",num);
return 0;



5、
#include <stdio.h>
int main(void)
{
 int guess, max = 100, min = 1;
 char respond;
 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((respond = getchar()) != 'y')
 {
  if (respond == 'h')
  {
   max = guess - 1;
   printf("Well,then,is it %d?\n",guess = ( max + min ) / 2 );
  }
  else if (respond == '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
#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、


#include<stdio.h>
double CHOICE(int num);
int main(void)
{
float all_money,tax,net_worth,MONEY;
int times,num;
    printf("Enter the number corresponding to the desired pay rate or action\n");
printf("1) $8.75/hr    2)$9.33/hr\n");
printf("3) $10.00/hr    4)$11.20/hr\n");
printf("5) quit\n");
while(1)
{   if(scanf("%d",&num)) 
{
if(num>=1&&num<=5)
     {
      MONEY=CHOICE(num);
     
      break;
     }
   else
      printf("Please Enter another number for 1 2 3 4 5\n") ;
}
else
   printf("Enter another number for 1 2 3 4 5") ;
 
}

printf("Please enter time\n");   
scanf("%d",&times);


if(times<=40)
 all_money=MONEY*times;
else 
  all_money=MONEY*times+MONEY*(times-40)*1.5;
if(all_money<=300)
{
tax=all_money*0.15;
net_worth=all_money-tax;
}
      
else if(all_money>300&&all_money<=450)
{
tax=(all_money-300)*0.2+300*0.15;
net_worth=all_money-tax;
}
      
else 
{
tax=300*0.15+150*0.15+(all_money-450)*0.25;
}
net_worth=all_money-tax;
       
printf("%d times is %f money and %f tax %f networth",times,all_money,tax,net_worth);
return 0;
}
double CHOICE(int num)
{       
       float MONEY1;
switch(num)
{
case 1:MONEY1=8.75;
      break;
case 2:MONEY1=9.33;
      break;
case 3:MONEY1=10.00;
      break;
case 4:MONEY1=11.2;
      break;
case 5:printf("quit");
      break;
default:break; 
}
return MONEY1;
}








#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
原创粉丝点击