C Primer Plus
——普拉塔(第五版)
第七章 课后习题
1、a c 假 b真2、a、number>=90&&number<100
b、ch!='q'||ch!='k'
c、number>=1&&number<=9&&number!=5;
d、number<1||number>9;
3、
4、1 ,0 ,1,,6,5,0
5、
#include<stdio.h>
int main(void)
{
int num;
for(num=1;num<=11;num++)
{
if(num%3==0)
putchar('$');
else
putchar('*');
putchar('#');
putchar('%');
putchar('\n');
}
return 0;
}
6
#include<stdio.h>
int main(void)
{
int i=0;
while(i<3)
{
switch(i++)
{
case 0:printf("fat");
case 1:printf("hat");
case 2:printf("cat");
default:printf("oh no! ");
}
putchar('\n');
}
return 0;
}
7
#include<stdio.h>
int main(void)
{
char ch;
int lc=0;
int uc=0;
int oc=0;
while((ch=getchar())!='#')
{
if(ch>='a'&&ch<='z')
lc++;
else if(!(ch<'A')&&!(ch>'Z'))
uc++;
else
oc++;
}
printf("%d lowercase,%d uppercase,%d
other",lc,uc,oc);
return 0;
}
8
#include<stdio.h>
int main(void)
{
int age =20;
while(age++<=65)
{
if((age%20)==0)
printf("You are %d.Here is a raise.\n",age);
if(age==65)
printf("You are %d.Here is your gold watch.
\n",age);
}
return 0;
}
9
#include<stdio.h>
int main(void)
{
char ch;
while((ch=getchar())!='#')
{
if(ch=='\n')
continue;
printf("Step 1\n");
if(ch=='c')
continue;
else if(ch=='b')
break;
else if(ch=='g')
goto laststep;
printf("Step 2\n");
laststep:printf("Step 3\n");
}
printf("Done\n");
return 0;
}
10
#include<stdio.h>
int main(void)
{
char ch;
while((ch=getchar())!='#')
{
if(ch!='\n')
{
printf("Step 1\n");
if(ch!='c')
{ if(ch=='b')
break;
else if(ch=='g')
printf("Step 3\n");
printf("Step 2\n");
}
}
}
printf("Done\n");
return 0;
}
编程练习
1
#include<stdio.h>
int main(void)
{
char ch;
int n_num=0;//空格数
int h_num=0;//换行数
int q_num=0;//其它字符数
while((ch=getchar())!='#')
{
if(ch==' ')
n_num++;
else if(ch=='\n')
h_num++;
else
q_num++;
}
printf("空格数目%d \n换行数%d \n 其他字符
数%d",n_num,h_num,q_num);
return 0;
}
2
#include<stdio.h>
int main(void)
{
char ch;
int count;
count=0;
while((ch=getchar())!='#')
{
if(count%8==0&&count!=0)
{
printf("\n");
count=0;
}
printf("%c%d",ch,ch);
count++;
}
return 0;
}
3
#include<stdio.h>
int main(void)
{
int count,num1,num2,num;
int sum1,sum2;
sum1=sum2=num1=num2=0;
float aver_even,aver_no_even;
while(scanf("%d",&num))
{
if(num==0)
break;
if(num%2==0)
{
sum1=sum1+num;
num1++;
}
else
{
sum2=sum2+num;
num2++;
}
}
aver_even=sum1/num1;
aver_no_even=sum2/num2;
printf("even number is %d and average of even is %f
\n",num1,aver_even);
printf("noeven number is %d and average of noeven is
%f\n",num2,aver_no_even);
return 0;
}
4
#include<stdio.h>
int main(void)
{
char ch;
int count;
while(scanf("%c",&ch))
{
if(ch=='#')
break;
else if(ch=='.')
{
ch=ch-13;
count++;
printf("%c",ch);
}
else if(ch=='!')
{
printf("%c%c",'!','!');
count++;
}
else
printf("%c",ch);
}
printf("\n");
printf("%d",count);
return 0;
}
5.
#include<stdio.h>
int main(void)
{
int count,num1,num2,num;
int sum1,sum2;
sum1=sum2=num1=num2=0;
float aver_even,aver_no_even;
while(scanf("%d",&num))
{
if(num==0)
break;
/*
if(num%2==0)
{
sum1=sum1+num;
num1++;
}
else
{
sum2=sum2+num;
num2++;
}
*/
switch(num%2)
{ case 0:
{
sum1=sum1+num;
num1++;
break;
}
case 1:
{
sum2=sum2+num;
num2++;
break;
}
default:
break;
}
}
aver_even=sum1/num1;
aver_no_even=sum2/num2;
printf("even number is %d and average of even is %f
\n",num1,aver_even);
printf("noeven number is %d and average of noeven is
%f\n",num2,aver_no_even);
return 0;
}
6
#include<stdio.h>
int main(void)
{
char ch,cp;
int count=0 ;
ch=getchar();
while(ch!='#')
{
if(ch=='e')
cp=ch;
ch=getchar();
if(cp=='e'&&ch=='i')
count++;
cp=0;
}
printf(" ei is %d times",count);
return 0;
}
7.
#include<stdio.h>
#define MONEY 10.00
int main(void)
{
float all_money,tax,net_worth;
int times;
printf("Please enter time\n");
scanf("%d",×);
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;
}
#include<stdio.h>
int main(void)
{
float all_money,tax,net_worth,MONEY;
int times,num;
printf("Please enter time\n");
while(1)
{
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");
if(scanf("%d",&num)&&num>=1&&num<=5)
{
switch(num)
{
case 1:MONEY=8.75;
break;
case 2:MONEY=9.33;
break;
case 3:MONEY=10.00;
break;
case 4:MONEY=11.2;
break;
case 5:printf("quit");
break;
default:break;
}
break;
}
scanf("%d",×);
}
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;
}
8.
#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",×);
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;
}
9.
#include<stdio.h>
#include<stdbool.h>
int PRIME(int num);
int main(void)
{
int num;
printf("Please enter number\n");
scanf("%d",&num);
for(num;num>0;num--)
{
// printf("%d\n",PRIME(num));
if(PRIME(num))
printf("%d\n",PRIME(num));
}
return 0;
}
int PRIME(int n )
{
int div=0;
bool TR_FA=false;
for(div=2;div*div<=n ;div++)
if(n%div==0)
{
TR_FA=true;
break;
}
if(TR_FA)
return 0 ;
else
return n;
}
10
#include<stdio.h>
float CHOICE(int n,float all_money);
float TAX(float money,float all_money);
int main(void)
{
int type;
float input;
printf("Please choice type\n");
printf("1)单身 2)户主 3)已婚共有 4)已婚离异
\n");
while(scanf("%d",&type))
{
printf("Please enter input\n");
scanf("%f",&input);
printf("Tax is %f\n",CHOICE(type,input));
printf("Please choice type(q to quit)\n");
}
return 0;
}
float CHOICE(int n,float all_money)
{
float tax;
switch(n)
{
case 1:tax=TAX(17850,all_money);
break;
case 2:tax=TAX(23900,all_money);
break;
case 3:tax=TAX(29750,all_money);
break;
case 4:tax=TAX(148750,all_money);
break;
}
return tax;
}
float TAX(float money,float all_money)
{
float tax_2;
if(all_money>money)
tax_2=(all_money-money)*0.28+money*0.15;
else
tax_2=all_money*0.15;
return tax_2;
}
11
#include<stdio.h>
float CHOICE(char ch,float weigth);
float COST(float price,float weigth);
int main(void)
{
char ch;
float weigth;
printf("Please choice type\n");
printf("a)朝鲜蓟 b)甜菜 c)胡萝卜 q)退出订购
\n");
while(scanf("%c",&ch))
{
printf("Please enter input\n");
scanf("%f",&weigth);
printf("Tax is %f\n",CHOICE(ch,weigth));
printf("Please choice type(q to quit)\n");
}
return 0;
}
float CHOICE(char ch,float weigth)
{
float money;
switch(ch)
{
case 'a':money=COST(1.25,weigth);
break;
case 'b':money=COST(0.65,weigth);
break;
case 'c':money=COST(0.89,weigth);
break;
case 'd':printf("停止选购");
break;
}
return money;
}
float COST(float price,float weigth)
{
float cost;
if(weigth<5)
cost=weigth*price-3.5;
else if(weigth>=5&&weigth<20)
cost=weigth*price-10;
else
cost=weigth*price*1.1-8;
if(cost<100)
return cost;
else
return cost*0.95;
}