C Primer Plus 第五版 第7章 编程练习
#include //第一题
int main(void)
{
int n=0,kong=0,other=0;
char ch;
while(ch=getchar()!='#')
{
if(ch==' ')
++kong;
else if(ch=='\n')
++n;
else
++other;
}
printf("Done!\n");
printf("kongge huanhang qita\n");
printf("%5d %5d %5d",kong,n,other);
return 0
}
----------
#include <stdio.h> //第4题
int main(void)
{
int count_1=0,count_2=0,count=0;
char s[100];
char letter;
printf("please input words to test:");
while((letter=getchar())!='#')
{
if(letter=='!')
{
letter='.';
count_1++;
putchar(letter);
}
else if(letter=='.')
{
printf("!!");
count_2++;
}
else
putchar(letter);
}
printf("\ndone!\n");
printf("you had %d times change!",count_1+count_2);
return 0;
}`
----------
include<stdio.h>//第5题
int main(void)
{
int number,count_1=0,count_2=0,sum_1=0,sum_2=0;
double average_1,average_2;
scanf("%d",&number);
while(number!=0)
{
switch(number%2)
{
case 0:count_2++,sum_2+=number;break;
default:count_1++,sum_1+=number;
}
scanf("%d",&number);
}
average_1=sum_1/count_1;
average_2=sum_2/count_2;
printf("all of oushu is %d and the average is %.2f\n",count_2,average_2);
printf("all of jishu is %d and the average is %.2f",count_1,average_1);
return 0;
}
第6题
#include <stdio.h>
int main(void)
{
char letter_1,letter_2;
int count=0;
while((letter_2=getchar())!='#')
{
if(letter_2=='i')
{
if(letter_1=='e')
{
count++;
}
}
letter_1=letter_2;
}
printf("\nthere are %d times which the 'ei'had appeared!",count);
return 0;
}
第7题
#include <stdio.h>
#define BASIC 10.00
#define MORE 1.5
#define TAX_1 0.15
#define TAX_2 0.20
#define TAX_3 0.25
int main(void)
{
int time;
float sum,tax,salary;
printf("Input your work hour:");
scanf("%d",&time);
if(time<=40)
{
sum=time*BASIC;
if(time>=30)
{
tax=300*TAX_1+(time-30)*BASIC*TAX_2;
}
else
tax=time*BASIC*TAX_1;
}
else
{sum=MORE*(time-40)*BASIC+40*BASIC;
if(time<50)
{
tax=300*TAX_1+(time-30)*BASIC*TAX_2;
}
else
tax=300*TAX_1+150*TAX_2+(time-50)*BASIC*TAX_3;
}
salary=sum-tax;
printf("\nyou have worked for %d hours,and your salary without tax is %.2f,your tax has %.2f,finally,your salary is %.2f !",time,sum,tax,salary);
return 0;
}
第8题
#include <stdio.h>
#define MORE 1.5
#define LEVEL_1 8.75
#define LEVEL_2 9.33
#define LEVEL_3 10.00
#define LEVEL_4 11.20
#define TAX_1 0.15
#define TAX_2 0.20
#define TAX_3 0.25
int main(void)
{
int time,choose;
float sum,tax,salary,BASIC;
printf("*****************************************************************\n");
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");
printf("*****************************************************************\n");
scanf("%d",&choose);
while(choose!=5)
{
if(choose>5||choose<1)
{
printf("you intered wrong number!please input 1-5!");
scanf("%d",&choose);
}
else
{
switch(choose)
{
case 1:BASIC=LEVEL_1;break;
case 2:BASIC=LEVEL_2;break;
case 3:BASIC=LEVEL_3;break;
case 4:BASIC=LEVEL_4;
}
}
printf("Input your work hour:");
scanf("%d",&time);
if(time<=40)
{
sum=time*BASIC;
if(time>=30)
{
tax=300*TAX_1+(time-30)*BASIC*TAX_2;
}
else
tax=time*BASIC*TAX_1;
}
else
{sum=MORE*(time-40)*BASIC+40*BASIC;
if(time<50)
{
tax=300*TAX_1+(time-30)*BASIC*TAX_2;
}
else
tax=300*TAX_1+150*TAX_2+(time-50)*BASIC*TAX_3;
}
salary=sum-tax;
printf("\nyou have worked for %d hours,and your salary without tax is %.2f,your tax has %.2f,finally,your salary is %.2f !",time,sum,tax,salary);
scanf("%d",&choose);
}
return 0;
}
第9题
int main(void){
int num,i,k;
bool isPrime;
printf("Enter int number:\n");
scanf("%d",&num);
for(i=2;i<=num;i++){
for(k=2,isPrime = true;(k*k)<=i;k++){
if (i%k==0){
isPrime = false;
}
}
if (isPrime){
printf("%lu ",i);
}
}
return 0;
}
第10题
#include<stdio.h>
#define simple_dog 17850
#define master 23900
#define couple 29750
#define deverse 14875
#define tax_rate 0.15
#define more 0.28
int main(void){
int income,choose;
long basic;
float tax;
printf("please choose your tax type:\n1)simple_dog 2)master 3)couple 4)deverse\n");
while(scanf("%d",&choose)!=0)
{
switch(choose)
{
case 1:basic=simple_dog;break;
case 2:basic=master;break;
case 3:basic=couple;break;
case 4:basic=deverse;break;
default:printf("wrong!you must input a number from 1 to 4!\n");
scanf("%d",&choose);
}
printf("please input your income:\n");
scanf("%d",&income);
if(income<=basic)
{
tax=income*tax_rate;
}
else
tax=basic*tax_rate+(income-basic)*more;
printf("your tax is %f",tax);
printf("please choose your tax type:\n1)simple_dog 2)master 3)couple 4)deverse\n");
}
return 0;
}
第11题
#include<stdio.h>
#include<stdbool.h>
#define pa 1.25
#define pb 0.65
#define pc 0.89
int main(void){
float price,trans,sum,a,b,c,weight;
bool cut;
printf("a=");
scanf("%f",&a);
printf("b=");
scanf("%f",&b);
printf("c=");
scanf("%f",&c);
while(getchar()=='q')
goto last;
weight=a+b+c;
price=pa*a+pb*b+pc*c;
if(price>=100)
{
price*=0.95;
cut=true;
}
else cut=false;
if(weight<=5)
trans=3.50;
else if(weight>5&&weight<20)
trans=10.00;
else
trans=8.00+weight*0.1;
sum=price+trans;
printf("朝鲜蓟:%.2f美元/磅;甜菜:%.2f美元/磅;胡萝卜:%.2f美元/磅\n",pa,pb,pc);
printf("您订购:%.2f %.2f %.2f \n",a,b,c);
printf("分费用: %.2f %.2f %.2f \n",pa*a,pb*b,pc*c);
if(cut)
printf("折扣信息:您享受到了5%%的打折优惠!\n");
else
printf("很抱歉,您的订单不足100元,无法享受折扣优惠!\n");
printf("运输费用:%.2f\n",trans);
printf("您需付款:%.2f 美元\n",sum);
last: printf("您已退出购物系统,欢迎下次光临,祝您生活愉快!");
return 0;
}