C Primer Plus 第七章 C控制语句:分支和跳转
复习题(我的答案):
a.100>3&&'a'>'c' is false
b.100>3||'a'>'c' is true
c.!(100>3) is false
a.number>=90&&number<100
b.ch!='q'&&ch!='k'
c.number>=1&&number<=9&&number!=5//(number>=1 && number<=9) && number!=5
d.number<1&&number>9//number<1 || number>9
#include<stdio.h>
int main(void)
{
int weight,height;
scanf("%d %d",&weight,&height);
if(weight>100&&weight<300)
{
if(height>=72)
{
printf("You are very tall for your weight.\n");
}
if(height>64&&height<72)
{
printf("You are tall for your weight.\n");
}
}
if(weight>300)
{
if(height>48&&height<=64)
{
printf("Your weight is ideal.\n");
}
if(height<=48)
{
printf("You are quite short for your weight.\n");
}
}
return 0;
}
a.1
b.0
c.1
d.d=6
e.10
f.表达式:x>y?y>x:x>y 结果:x>y//结果应该为0,如果x>y为真,
表达式的值就是y>x,这种情况下它为假,如果x>y为假,
那么表达式的值就是x>y,这种情况为假
**$**$**$**#%//*#%*#%$#%*#%*#%$#%*#%*#%$#%*#%*#%
fat hat cat Oh no!
//错了,应该是:
fat hat cat Oh no!
hat cat Oh no!
cat Oh no!
#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\n",lc,uc,oc);
return 0;
}
You are 20. Here is a raise.
You are 40. Here is a raise.
You are 60. Here is a raise.
You are 65. Here is your gold watch.//错,应该不断打印这一行,因为代码中出现
了age=65这样的赋值语句
9.10.
略
编程练习:
1.编写一个程序读取输入,读到字符#停止,然后报告读取的空格数、换行符数和所有其他字符的数量。
#include<stdio.h>
int main(void)
{
char ch;
int space=0,line=0,charactor=0;
while((ch=getchar())!='#')
{
if(ch==' ')//注意,这里读取空格字符不需要用双引号!
space++;
else if(ch=='\n')
line++;
else
charactor++;
}
printf("There are %d spaces, %d lines, and %d charactors.\n",space,line,charactor);
return 0;
}
2.编写一个程序读取输入,读到#字符停止。程序要打印每个输入的字符及对应的ASCII码(十进制)。每行打印8个“字符-ASCII码”组合。建议:使用字符计数和求模运算符在每8个循环周期打印一个换行符。
#include<stdio.h>
int main(void)
{
char ch;
int number=0;
printf("Please enter some letters:");
while((ch=getchar())!='#')
{
printf("%c-%d ",ch,ch);
number++;
if(number%8==0)
{
printf("\n");
}
}
printf("\n");
return 0;
}
3.编写一个程序,读取整数直到用户输入0。输入结束后,程序应报告用户输入的偶数(不包括0)个数、这些偶数的平均值、输入的奇数个数和奇数平均值。
#include<stdio.h>
int main(void)
{
int n;
double avgEven,avgOdd;
double iEven=0,iOdd=0;
double sEven=0,sOdd=0;
printf("Please enter some integer(0 to quit):");
scanf("%d",&n);
while(n!=0)
{
scanf("%d",&n);
if(n%2==0)
{
sEven+=n;
iEven++;
}
if(n%2!=0)
{
sOdd+=n;
iOdd++;
}
}
printf("There are %.0lf even numbers. The average of all even number is %lf\n",iEven,sEven/iEven);
printf("There are %.0lf odd numbers. The average of all odd number is %lf\n",iOdd,sOdd/iOdd);
return 0;
}
4.使用if else语句编写一个程序读取输入,读到#停止。用感叹号代替句号,用两个感叹号替换原来的感叹号,最后报告进行了多少次替换。
#include<stdio.h>
int main(void)
{
char ch;
int count=0;
printf("Please enter some letters(# to quit):");
while((ch=getchar())!='#')
{
if(ch=='.')
{
printf("!");
count++;
}
else if(ch=='!')
{
printf("!!");//不能ch='!!';
count++;
}
else
{
printf("%c",ch);//可以用putchar()
}
}
printf("\n");
if(count==1)
{
printf("%d time change.\n",count);
}
else
{
printf("%d times change.\n",count);
}
return 0;
}
5.用switch重写练习4。
#include<stdio.h>
int main(void)
{
char ch;
int count=0;
printf("Please enter some letters(# to quit):");
while((ch=getchar())!='#')
{
switch(ch)
{
case '.':printf("!");
count++;
break;
case '!':printf("!!");
count++;
break;
default:printf("%c",ch);
break;
}
}
printf("\n");
if(count==1)
{
printf("%d time change.\n",count);
}
else
{
printf("%d times change.\n",count);
}
return 0;
}
6.编写程序读取输入,读到#为止,报告ei出现的次数。
#include<stdio.h>
int main(void)
{
char ch;
int count=0;//一定要记得给变量赋值
printf("Please enter some letters(# to qiut):");
while((ch=getchar())!='#')
{
if(ch=='e')
{
ch=getchar();
if(ch=='i')
{
count++;
}
}
}
printf("There are %d 'ei' here.\n",count);
return 0;
}
7.编写一个程序,提示用户输入一周工作的小时数,然后打印工资总额、税金和净收入。(基本工资=10美元/小时,加班(超过40小时)=基本工资的1.5倍,税率:前300美元为15%,续150美元为20%,余下的为25%,用#define定义符号常量)
#include<stdio.h>
#define BASESEL 10.00
#define EXTRASAL 15.00
#define TEX1 0.15
#define TEX2 0.20
#define TEX3 0.25
int main(void)
{
double sal;
int hours;
double grossPayroll,expensesOfTaxation,netReceipt;
printf("Please enter your working hours in one week:");
scanf("%d",&hours);
if(hours<=40)
{
grossPayroll=hours*BASESEL;
}
if(hours>40)
{
grossPayroll=400+(hours-40)*EXTRASAL;
}
if(grossPayroll<=300)
{
expensesOfTaxation=grossPayroll*TEX1;
}
if(grossPayroll<=450&&grossPayroll>300)
{
expensesOfTaxation=grossPayroll*TEX2;
}
if(grossPayroll>450)
{
expensesOfTaxation=grossPayroll*TEX3;
}
printf("Your gross payroll is $%.2lf, expenses of taxation is $%.2lf, net receipt is $%.2lf.\n",grossPayroll,expensesOfTaxation,grossPayroll-expensesOfTaxation);
return 0;
}
8.修改第7题的基本工资,让程序可以给出一个供选择的工资等级菜单。使用switch完成工资等级的选择。
#include<stdio.h>
#define TEX1 0.15
#define TEX2 0.20
#define TEX3 0.25
int main(void)
{
double sal;
double basesal;
int hours;
int choose;
double grossPayroll,expensesOfTaxation,netReceipt;
printf("**********************************************************************\n");
printf("Enter the number corresponding to the desired pay rate of 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)
{
switch(choose)
{
case 1:
basesal=8.75;
break;
case 2:
basesal=9.33;
break;
case 3:
basesal=10.00;
break;
case 4:
basesal=11.20;
break;
default:break;
}
printf("Please enter your working hours in one week:");
scanf("%d",&hours);
if(hours<=40)
{
grossPayroll=hours*basesal;
}
if(hours>40)
{
grossPayroll=40*basesal+(hours-40)*basesal*1.5;
}
if(grossPayroll<=300)
{
expensesOfTaxation=grossPayroll*TEX1;
}
if(grossPayroll<=450&&grossPayroll>300)
{
expensesOfTaxation=grossPayroll*TEX2;
}
if(grossPayroll>450)
{
expensesOfTaxation=grossPayroll*TEX3;
}
printf("Your gross payroll is $%.2lf, expenses of taxation is $%.2lf, net receipt is $%.2lf.\n",grossPayroll,expensesOfTaxation,grossPayroll-expensesOfTaxation);
printf("\n");
printf("**********************************************************************\n");
printf("Enter the number corresponding to the desired pay rate of 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);
}
return 0;
}
9.编写一个程序,只接受正整数输入,然后显示所有小于或等于该数的素数。
#include<stdio.h>
int main(void)
{
int n;
int isPrime;
int i,j;
printf("Please enter an integer:");
scanf("%d",&n);//可以加一个while循环限定整数输入:while(scanf("%d",&n)==1&&n>0)
printf("2 ");
for(i=3;i<=n;i++)// for(i=2;i<=n;i++)
{
isPrime=1;
for(j=2;j<i;j++)//for (j = 2, isPrime = true; (j * j) <= i;j++)对照上面的for循环,这种方法能把2也算出来
{
if(i%j==0)
isPrime=0;
}
if(isPrime==1)
{
printf("%d ",i);
}
}
printf("\n");
return 0;
}
10.1988年的美国联邦税收计划是近代最简单的税收方案。它分为4个类别,每个类别有两个等级。(不同类别不同等级税收详见书本)编写一个程序,让用户指定那水晶的种类和应纳税收入,然后计算税金。程序应通过循环让用户多次输入。
#include<stdio.h>
int main(void)
{
double income;
double incomeOne;
int choose;
printf("**********************************************************************\n");
printf("Enter the number corresponding to the desired pay rate of action:\n");
printf("1) 单身 2) 户主\n");
printf("3) 已婚,共有 4) 已婚,离异\n");
printf("5) quit\n");
printf("**********************************************************************\n");
scanf("%d",&choose);
while(choose!=5)
{
switch(choose)
{
case 1:
incomeOne=17850;
break;
case 2:
incomeOne=23900;
break;
case 3:
incomeOne=29750;
break;
case 4:
incomeOne=14875;
break;
default:break;
}
printf("Please enter your income:");
scanf("%lf",&income);
printf("Your tex is $%.2lf.\n",0.15*incomeOne+0.28*(income-incomeOne));
printf("\n");
printf("**********************************************************************\n");
printf("Enter the number corresponding to the desired pay rate of action:\n");
printf("1) 单身 2) 户主\n");
printf("3) 已婚,共有 4) 已婚,离异\n");
printf("5) quit\n");
printf("**********************************************************************\n");
scanf("%d",&choose);
}
return 0;
}
11.题目太长了不想写