《C语言程序设计》第三章练习与习题答案

[练3-1]
不需要;因为已经涵盖了所有可能的条件。
[练3-2]
#include<stdio.h>
int main(void)
{
int y;
double x;
printf("Enter x:");
scanf("%lf",&x);
if(x<0)
y=-1;
else if(x==0)
y=0;
else
y=1;
printf("y=sign(%f)=%d\n",x,y);
return 0;
}
[练3-3]
#include<stdio.h>
int main(void)
{
char ch;
int i,digit,letter,SE,other;
printf("Enter 15 characters:");
digit=letter=SE=other=0;
for(i=1;i<=15;i )
{
ch=getchar();
if(ch>='0'&&ch<='9')
digit ;
else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
letter ;
else if(ch==' '||ch=='\n')
SE ;
else
other ;
}
printf("digit=%d,letter=%d,SE=%d,other=%d\n",digit,letter,SE,other);
return 0;
}
[练3-4]
会改变;输出“price=0.0”;不使用break时,不但会执行符合语句段,还执行其后的所有语句段。
[练3-5]
#include<stdio.h>
int main(void)
{
double value1,value2;
char operato;
printf("Type in an expression:");
scanf("%lf%c%lf",&value1,&operato,&value2);
switch(operato)
{
case' ':
printf("=%.2f\n",value1 value2);
break;
case'-':
printf("=%.2f\n",value1-value2);
break;
case'*':
printf("=%.2f\n",value1*value2);
break;
case'/':
if(value2==0)
printf("You are wrong\n");
else
printf("=%.2f\n",value1/value2);
break;
default:
printf("Unknown operator\n");
break;
}
return 0;
}
[练3-6]
#include<stdio.h>
int main(void)
{
char level;
printf("Enter level(A~E):");
level=getchar();
switch(level)
{
case 'A':
printf("90~100\n");
break;
case'B':
printf("80~89\n");
break;
case'C':
printf("70~79\n");
break;
case'D':
printf("60~69\n");
break;
case'E':
printf("0~59\n");
break;
default:
printf("Wrong level\n");
break;
}
return 0;
}
[练3-7]
#include<stdio.h>
int main(void)
{
int i,choice;
double price;
printf("[1] apples\n");
printf("[2] pears\n");
printf("[3] oranges\n");
printf("[4] grapes\n");
printf("[0] Exit\n");
for(i=1;i<=5;i )
{
printf("Enter choice:");
scanf("%d",&choice);
if(choice==0)
{
printf("Byebye!\n");
break;
}
switch(choice)
{
case 1:
price=3.00;
break;
case 2:
price=2.50;
break;
case 3:
price=4.10;
break;
case 4:
price=10.20;
break;
default:
price=0.00;
}
printf("price=%.2f\n",price);
if((i==5)&&(choice!=0))
printf("You have used five times.Please try again.\n");
}
return 0;
}
【练3-8】
#include<stdio.h>
int main(void)
{
int year;
printf("Enter year:");
scanf("%d",&year);
if((year%4==0&&year0!=0)||(year@0==0))
{
printf("It is leap year.\n");
}
else
{
printf("It isn't leap year.\n");
}
return 0;
}
【练3-9】
#include<stdio.h>
int main(void)
{
int n,i,count;
double total,grade;
printf("Enter n:");
scanf("%d",&n);
total=0;
count=0;
for(i=1;i<=n;i )
{
printf("Enter grade #%d:",i);
scanf("%lf",&grade);
total=total grade;
if(grade>=60)
{
count ;
}
}
printf("The avaerage = %.2f\n",total/n);
printf("The number of qualifies = %d\n",count);
return 0;
}
【练3-10】
#include<stdio.h>
int main(void)
{
double x,y;
printf("Enter x:");
scanf("%lf",&x);
if(x<0)
{
y=0;
}
if(x>=0)
{
if(x<=15)
{
y=4*x/3;
}
else
{
y=2.5*x-10.5;
}
}
printf("f(%.2f) = %.2f\n",x,y);
return 0;
}
【练3-11】
改写前两条语句的执行条件是:x小于1;x大于等于1且小于2。改写后两条语句的执行条件是:x小于1;x大于等于2。
【习题3-1】
#include<stdio.h>
#include<math.h>
int main(void)
{
int a,b,c;
double area,s,perimeter,part;
printf("Enter a:");
scanf("%d",&a);
printf("Enter b:");
scanf("%d",&b);
printf("Enter c:");
scanf("%d",&c);
if((a b>c)&&(b c>a)&&(c a>b))
{
s=(a b c)/2;
part=s*(s-a)*(s-b)*(s-c);
area=sqrt(part);
perimeter=a b c;
printf("Area = %.2f\n",area);
printf("Perimeter = %.2f\n",perimeter);
}
else
{
printf("These sides do not correspond to a valid triangle.\n");
}
return 0;
}
【习题3-2】
#include<stdio.h>
int main(void)
{
int salary,rate;
double tax;
printf("Enter salary:");
scanf("%d",&salary);
if(salary<=850)
{
rate=0;
}
else if(salary<=1350)
{
rate=5;
}
else if(salary<=2850)
{
rate=10;
}
else if(salary<=5850)
{
rate=15;
}
else
{
rate=20;
}
tax=rate*(salary-850)/100;
printf("Tax = %.2f\n",tax);
return 0;
}
【习题3-3】
#include<stdio.h>
int main(void)
{
int km,min;
double part,money;
printf("Enter km:");
scanf("%d",&km);
printf("Enter min:");
scanf("%d",&min);
if(km<=3)
{
money=10;
}
else
{
money=10 (km-3)*3;
}
if(min>0)
{
part=min/5;
money=money part*3;
}
printf("Money = %.0f\n",money);
return 0;
}
【习题3-4】
#include<stdio.h>
int main(void)
{
int a,b,c,d,e,n,i;
double grade,sum,average;
printf("Enter n:");
scanf("%d",&n);
a=b=c=d=e=sum=0;
for(i=1;i<=n;i )
{
printf("Enter grade#%d:",i);
scanf("%lf",&grade);
sum=sum grade;
if(grade<60)
{
e ;
}
else if(grade<70)
{
d ;
}
else if(grade<80)
{
c ;
}
else if(grade<90)
{
b ;
}
else
{
a ;
}
}
average=sum/n;
printf("Average = %.2f\n",average);
printf("A = %d\nB = %d\nC = %d\nD = %d\nE = %d\n",a,b,c,d,e);
return 0;
}
【习题3-5】
#include<stdio.h>
int main(void)
{
int i;
for(i=2000;i<=3000;i )
{
if(((i%4==0)&&(i0!=0))||(i@0==0))
{
printf("%d\n",i);
}
}
return 0;
}

展开阅读全文

没有更多推荐了,返回首页