C Primer Plus 第八章 程序清单……2015.4.27

C Primer Plus 
                  ——第五版    
  

第八章 字符输入/输出和输入确认



程序清单8.1
#include<stdio.h>
int main(void)
{
char ch;
while((ch=getchar())!='#')
     putchar(ch);
return 0;

8.2
#include<stdio.h>
int main(void)
{
int ch;
while((ch=getchar())!=EOP)
putchar(ch);
return 0;
}
8.3
#include<stdio.h>
#include<stdlib.h>
int main()
{
int ch;
FILE *fp;
char fname[50];
printf("Enter the name of the file: ");
scanf("%s",fname);
fp=fopen(fname,"r");//打开文件 
if(fp==NULL)//成立则打开文件失败 
{
printf("Failed to open file,Bye\n");
exit(1);//终止程序 
}
while((ch=getchar(fp)!=EOP))//从文件中获取字符 
   putchar(ch);
fclose(fp);//关闭文件 
return 0;
}


8.4
#include<stdio.h>
int main(void)
{
int guess=1;
printf("Pick an integer from 1 to 100.I will try 


to guess ");
printf("it,\nRespond with a y if my guess is 


right and with");
printf("\nan if it is wrong.\n");
printf("Uh^ is your number %d?\n",guess);
while(getchar()!='y')
   printf("Well,then ,is it %d?\n",++guess);
   
return 0;

8.5
#include<stdio.h>
void display(char cr,int lines,int width);
int main(void)
{
int ch;
int rows,cols;
printf("Enter a character and two integer;\n");
while((ch=getchar())!='\n')
{
scanf("%d %d",&rows,&cols);
display(ch,rows,cols);
printf("Enter another character and two 


integer :\n");
printf("Enter a newline to quit.\n");

}
printf("Bye.\n");
return 0;
}
void display(char cr,int lines,int width)
{
int row,col;
for(row=1;row<=lines;row++)
{
for(col=1;col<=width;col++)
putchar(cr);
 putchar('\n');
}
}
8.6
#include<stdio.h>
void display(char cr,int lines,int width);
int main(void)
{
int ch;
int rows,cols;
printf("Enter a character and two integer;\n");
while((ch=getchar())!='\n')
{
scanf("%d %d",&rows,&cols);
display(ch,rows,cols);
printf("Enter another character and two 


integer :\n");
printf("Enter a newline to quit.\n");
                /*去掉空白字符*/
while(getchar()!='\n')
   continue;
}
printf("Bye.\n");
return 0;
}
void display(char cr,int lines,int width)
{
int row,col;
for(row=1;row<=lines;row++)
{
for(col=1;col<=width;col++)
putchar(cr);
 putchar('\n');
}
}


#include<stdio.h>
int main(void)
{
int input;
char ch;
while(scanf("%d",&input)!=1)
{
while((ch=getchar())!='\n')
    putchar(ch);
    printf("  is not an integer.\nPlease 


enter an ");
    printf("integer value,such as 


25,178,or 3:");
}
printf("%d",input);
return 0;
}




#include<stdio.h>
#include<stdbool.h>
//确认输入一个整数
int get_int (void);
//确认范围的上下界是否有效
bool bad_limits(int begin,int end,int low,int hight);
//计算从a到b之间整数的平方和 
double sum_squares(int a,int b);
int main(void)
{
const int MIN=-1000;
const int MAX=1000;
int start;
int stop;
double answer;
printf("This program computes the sum of the 


squares of \n");
printf("lower limit:\n");
start=get_int();
printf("upper limit:");
stop=get_int();
while(start!=0||stop!=0)
{
if(bad_limits(start,stop,MIN,MAX))
       printf("Try again!\n");
else
{
answer=sum_squares(start,stop);
printf("The sum of the squares of 


the integrers from \n");
printf("from %d to %d is 


%f",start,stop,answer);
}
printf("Enter the limits(enter 0 for both limits 


to quit);\n");
printf("lower limit:\n");
start=get_int();
printf("upper limit:");
stop=get_int();
}
printf("Done.\n");
return 0;
}


int get_int (void)
{   
   int num;
   char ch;
while(scanf("%d",&num)!=1)
{
while((ch=getchar())!='\n')
    putchar(ch);
printf(" is nor an integer.\n Please 


enter an ");
printf("integer value,such as 25,-27,or 


3: ");
  
}
return num;
}
bool bad_limits(int begin,int end,int low,int high)
{
bool not_good=false;
if(begin>end)
 {
  printf("%d is not smaller %d",begin,end);
  not_good=true;
 }
 if(begin<low||end<low)
 {
  printf("Value must be %d or greater.


\n",low);
  not_good=true;
 }
  if(begin>high||end>high)
 {
  printf("Value must be %d or greater.


\n",high);
  not_good=true;
 }
 return not_good;
}


double sum_squares(int a,int b)
{
double total=0;
 int i;
 for(i=a;i<=b;i++)
 total+=i*i;
 return total;
}


#include<stdio.h>
char get_choice(void);
char get_first(void);
int get_int(void);
void count(void);
int main(void)
{
int choice;
void count(void);
while((choice=get_choice())!='q')
{
switch(choice)
{
case 'a': printf(" Buy low,sell 


high.\n");
          break;
   case 'b':printf("'\a'");
            break;
   case 'c':count();
            break;
   default:printf("Program errow!\n");
         break; 
}
}
printf("Bye!\n");
return 0;
}
void count(void)
{
int n,i;
printf("Count how far?Enter an integer:\n");
n=get_int();
for(i=1;i<=n;i++)
  printf("%d\n",i);
  while(getchar()!='\n')
  continue;
}
char get_choice(void)
{
int ch;
printf("Enter the letter of your choice:\n");
printf("a.advice  b.bell\nc.count   q.quit\n");
ch=get_first();
while((ch<'a'||ch>'c')&&ch!='q')
{
printf("Please respond with a,b,c or q


\n");
ch=get_first();
}
return ch;
}


char get_first(void)
{
int ch;
ch=getchar();
while(getchar()!='\n')
continue;
return ch;
}
int get_int(void)
{
int input;
char ch;
while(scanf("%d",&input)!=1)
{
while((ch=getchar())!='\n')
 putchar(ch);
 printf(" is not an integer.\nPlease 


enter integer valu");
}
return input;
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值