C Primer Plus 第七章程序清单……2014.4.24

C Primer Plus 第五版 普拉塔著
                                  ——学习总结

第七章 程序清单

7.1 求出温度低于零度的天数百分比
#include<stdio.h>
int main(void)
{
const int FREEZING=0;
float temperature;
int cold_days=0;
int all_days=0;
printf("Enter the list of daily low temperature.


\n");
printf("Use Celsius,and enter q to quit.\n");
while(scanf("%f",&temperature)==1)
{
all_days++;
if(temperature<FREEZING)
cold_days++;
}
if(all_days!=0)
 printf("%d days total:%.1f%% were freezing.


\n",all_days,100.0*(float)cold_days/all_days);
if(all_days==0)
 printf("No data entered!\n");
return 0;




7.2 改变输入的字符,除了空格
#include<stdio.h>
#define SPACE ' '
int main(void)
{
char ch;
ch=getchar();
while(ch!='\n')
{
if(ch==SPACE)
 putchar(ch);
else
 putchar(ch+1);
 ch=getchar();
}
  putchar(ch);
return 0;

7.3改变输入,只保留输入非字母字符
#include<stdio.h>
#include<ctype.h>
int main(void)
{
char ch;

while((ch=getchar())!='\n')
{
if(isalpha(ch))
 putchar(ch+1);
else
 putchar(ch);
 
}
  putchar(ch);
return 0;


程序清单7.4 计算用电账目
#include<stdio.h>
#define RATE1 0.12589
#define RATE2 0.17901
#define RATE3 0.20971
#define BREAK1 360.0
#define BREAK2 680.0
#define BASE1 (RATE1*BREAK1)
#define BASE2 (BASE1+(RATE2*(BREAK2-BREAK1)))
int main(void)
{
double kwh,bill;
printf("Please enter the kwh used.\n");
scanf("%lf",&kwh);
if(kwh<=BREAK1)
  bill=BASE1*kwh;
else if(kwh<=BREAK2)
  bill=BASE1+(RATE2*(kwh-BREAK1));
else 
  bill=BASE2+(RATE3*(kwh-BREAK2));
printf("The charge for %.1f kwh is $%1.2f.
\n",kwh,bill);
return 0;
}

计算一个数值的素数
#include<stdio.h>
#include<stdbool.h>
int main(void)
{
unsigned long num;
unsigned long div;
bool isPrime;
printf("Please enter an integer for analysis: ");
printf("Enter q to quit.\n");
while(scanf("%lu",&num)==1)
{
for(div=2,isPrime=true;div*div<=num;div+
+)
{
if(num%div==0)
{
if((div*div)!=num)
  printf("%lu is divisible by %lu and %lu.\n",num,div,num/div);
else
  printf("%lu is 
divisible by %lu\n",num,div);
  isPrime=false;
}

}
if(isPrime)
  printf("%lu is prime.\n",num);
printf("Please enter another integer for 
analysis: ");
printf("Enter q to quit.\n");
}
printf("Bye.\n");
return 0;
}


使用逻辑运算符

#include<stdio.h>
#define PERIOD '.'
int main(void)
{
int ch;
int charcount =0;
while((ch=getchar())!=PERIOD)
{
if(ch!='*'&&ch!='\'')
  charcount ++;
}
printf("There are %d non-quote characters.
\n",charcount);
return 0;
}




#include<stdio.h>
#include<ctype.h>
#include<stdbool.h>
#define STOP '|'
int main(void)
{
char c;
char prev;
long n_chars=0l;//字符
int n_lines=0;//行
int n_words =0;//单词
int p_lines=0;//不完整行
bool inword=false;//若C在一个单词中,则inword等于


true
printf("Enter text to be analyzed(| to 
terminte)");
prev='\n'; 
while((c=getchar())!=STOP)
{
n_chars++;
if(c=='\n')
n_lines++;
if(!isspace(c)&&!inword)
{
inword=true;
n_words++;
}
if(isspace(c)&&inword)
inword=false;
prev=c;
}
if(prev!='\n')
p_lines=1;
printf("characters=%ld,words=%d,lines=%d 


",n_chars,n_words,n_lines);
printf("Partial lines=%d\n",p_lines);
return 0;
}


程序清单7.8

#include<stdio.h>
#define COVERAGE 200
int main(void)
{
int sq_feet;
int cans;
printf("Enter number of square feet to be 


pained:\n");
while(scanf("%d",&sq_feet)==1)
{
cans=sq_feet/COVERAGE;
cans+=((sq_feet%COVERAGE==0))?0:1;
printf("You need %d %s of paint.


\n",cans,cans==1?"can":"cans");
printf("Enter next value(q to quit)\n");
}
return 0;
}
程序清单7.9


#include<stdio.h>
int main(void)
{
const float MIN=0.0f;
const float  MAX=100.0f;
float score;
float total=0.0f;
int n=0;
float min=MAX;
float max=MIN;
printf("Enter the first score(q to quit):");
while(scanf("%f",&score)==1)
{
if(score<MIN||score>MAX)
{
printf("%0.1f is an invalid 
value.Try again: ",score);
continue;
}
printf("Accepting %0.1f:\n",score);
min=(score<min)?score:min;
max=(score>max)?score:max;
total+=score;
n++;
printf("Enter next score(q to quit ):");
}
if (n>0)
{
printf("Average of %d scores is %0.1f.


\n",n,total/n);
printf("low=%0.1f.high=%0.1f\n",min,max);
}
else
  printf(" No valid scores were entered.\n");
  return 0;
}




7.10
#include<stdio.h>
int main(void)
{
float length,width;
printf("enter the length of the rectangle:\n");
while(scanf("%f",&length)==1)
{
printf("Length =%0.2f:\n",length);
printf("Enter its width:\n");
if(scanf("%f",&width)!=1)
break;
printf("width=%0.2f:\n",width);
printf("Area=%0.2f:\n",length*width);
printf("Enther the length of the 


rectangle:\n");
}
printf("Done.\n");
return 0;
}
7.11
#include<stdio.h>
#include<ctype.h>
int main(void)


{
char ch;
printf("give me a letter of the alphabet,and I 


will give ");
printf("an animal name\nbeginning with that 


letter.\n");
printf("please type in a letter:type # to end my 


act.\n");
while((ch=getchar())!='#')
{
if('\n'==ch)
  continue;
  if(islower(ch))
     switch(ch)
     {
      case 'a':
      printf("argali,a wild 


sheep of asia\n");
      break;
      case 'b':
      printf("babirusa,a wild 


pig of Malay\n");
      break;
      case 'c':
      printf("coati,racoonlike 


mammal\n");
      break;
      default :
      printf("That's a 


stumper!\n'");
     }
     else
        printf("I recongnize only 


lowercase letters.\n");
     while(getchar()!='\n')
     continue;
     printf("Please type another letter 


or a #.\n");
}
printf("Bye!\n");
return 0;
}
7.12
#include<stdio.h>
int main(void)
{
char ch;
int a_ct,e_ct,i_ct,o_ct,u_ct;
a_ct=e_ct=i_ct=o_ct=u_ct=0;
printf("Enter some text:enter # to quit.\n");
while((ch=getchar())!='#')
{
switch(ch)
{
case 'a':
case 'A':a_ct++;
        break;
default:break;

}
}
printf("number of vowels:A E I O \n");
printf(" %4d\n",a_ct);
return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值