您的位置:首页 > 其它

谭浩强 《C程序设计》 第三版 第五章习题答案 (修正)

2010-03-02 17:26 459 查看
第五章习题答案
5.1
所谓关系运算实际上是比较运算,例如a>3是一个关系表达式,大于号是一个关系运算符,如果a的值是5,则满足给定的”a>3”条件,因此关系表达式的值“真“。。。
用逻辑运算符将关系表达式或逻辑表达式连接起来的式子就是逻辑表达式。
5.2 C语言编译系统在表示逻辑运算结果时,以数值1代表“真“,以0代表”假“,但在判断一个量是否为”真“时,以0代表”假“,以非0代表”真“,即将一个非零的数值认作为”真“。
5.3


1  1  0 
1
5.4

//practice 5.4
void main()
{
        
int a,b,c,t,max;
        
scanf("%d%d%d",&a,&b,&c);
        
max=a;
        
if(max<b)
        
{
                  
t=max;
                  
max=b;
                  
b=t;
        
}
        
if(max<c)
        
{
                  
t=max;
                  
max=c;
                  
c=t;
        
}
        
printf("%2d/n",max);
}
void main()
{
        
int a,b,c,temp,max;
        
scanf("%d%d%d",&a,&b,&c);
        
temp=(a>b)?a:b;
        
max=(temp>c)?temp:c;
        
printf("%3d/n",max);
}
//practice 5.5
void main()
{
        
double x,y;
        
scanf("%lf",&x);
        
if(x-1<1e-6)
        
{
        
   y=x;   
        
}
        
else if( x-1>=1e-6 && x-10<1e-6)
        
{
                  
y=2*x-1;
        
}
        
else if( x-10>=1e-6)
        
{
                  
y=3*x-11;
        
}
        
printf("%3lf/n",y);
}
//practice 5.6
void main()
{
        
double score;
        
char grade;
        
printf("请输入成绩:/n");
        
scanf("%lf",&score);
        
while(score>100 || score<0)
        
{
                  
printf("/n 输入有误,请重新输入:/n");
                  
scanf("%lf",&score);
        
}
        
switch((int)score/10)
        
{
        
case 9:grade='A';break;
        
case 8:grade='B';break;
        
case 7:grade='C';break;
        
case 6:grade='D';break;
        
default:grade='E';
        
}
        
printf("输入的成绩是%lf,相应的等级是%c/n",score,grade);
}
//pra 5.7
//第一种方法
void main()
{
        
int num,i;
        
char str[200]; 
        
sprintf(str,"%d",num);    
        
printf("the size of the integer is %d/n",strlen(str));
        
puts(str);
        
putchar('/n');
        
i=strlen(str)-1;
        
while(i>=0)
        
{
                  
putchar(*(str+i));
                  
i--;
        
}
   
putchar('/n');
}*/
//第二种方法
//pra 5.7
void main()
{
        
int num,size;
        
int indiv,ten,hundred,thousand,ten_thousand;
        
scanf("%d",&num);
        
while(num>99999 || num<=0)
        
{
                  
printf("/n输入有误,请重新输入:/n");
                  
scanf("%d",&num);
        
}
        
if(num>9999)
        
{
                  
size=5;
        
}
        
else if(num>999)
        
{
                  
size=4;
        
}
        
else if(num>99)
        
{
                  
size=3;
        
}
        
else if(num>9)
        
{
                  
size=2;
        
}
        
else
        
{
                  
size=1;
        
}
        
printf("size=%2d/n",size);
   
printf("每位数字为:/n");
        
ten_thousand=num/10000;
        
thousand=num%10000/1000;
        
hundred=num%10000%1000/100;
        
ten=num%10000%1000%100/10;
        
indiv=num%10000%1000%100%10;
        
switch(size)
        
{
                  
case 5:printf("%2d,%2d,%2d,%2d,%2d/n",ten_thousand,thousand,hundred,ten,indiv);
                           
printf("反序为:/n");
                           
printf("%2d,%2d,%2d,%2d,%2d/n",indiv,ten,hundred,thousand,ten_thousand);
                           
break;
                  
case 4:printf("%2d,%2d,%2d,%2d/n",thousand,hundred,ten,indiv);
                           
printf("反序为:/n");
                           
printf("%2d,%2d,%2d,%2d/n",indiv,ten,hundred,thousand);
                           
break;
                  
case 3:printf("%2d,%2d,%2d/n",hundred,ten,indiv);
                           
printf("反序为:/n");
                           
printf("%2d,%2d,%2d/n",indiv,ten,hundred);
                           
break;
                  
case 2:printf("%2d,%2d/n",ten,indiv);
                           
printf("反序为:/n");
                           
printf("%2d,%2d/n",indiv,ten);
                           
break;
                  
case 1:printf("%2d/n",indiv);
                           
printf("反序为:/n");
                           
printf("%2d/n",indiv);
                           
break;
                  
default :printf("ERROR/n");
        
}
}
//pra 5.8
//IF
语句的写法
/*int main()
{
        

        
double I;//Interest
        
double J;//奖金
        
printf("单位
万元 /n");

        
scanf("%lf",&I);
        
if(I<=10)
        
{
                  
J=10*0.1;
        
}
        
else if((I>10)&&(I<=20))
        
{
        
    J=10*0.1+(I-10)*0.075;
        
}
        
else if((I>20)&&(I<=40))
        
{
                  
J=10*0.1+10*0.075+(I-20)*0.05;
        
}
        
else if((I>40)&&(I<=60))
        
{
                  
J=10*0.1+10*0.075+20*0.05+(I-40)*0.03;
        
}else if((I>60)&&(I<=100))
        
{
                  
J=10*0.1+10*0.075+20*0.05+20*0.03+(I-60)*0.015;
        
}
        
else if(I>100)
        
{
                  
J=10*0.1+10*0.075+20*0.05+20*0.03+40*0.015+(I-100)*0.01;
        
}/
        
printf("%2lf/n",J);
        

        
return 0;
}*/
int main()
{
        

        
double I;//Interest
        
double J;//奖金
        
int c;
        
printf("单位
万元 /n");

        
scanf("%lf",&I);
        
c=(int)I/10;
        
if(c>10)
        
{
                  
c=10;
        
}
        
switch(c)
        
{
                  
case 0:J=10*0.1;break;
                  
case 1:J=10*0.1+(I-10)*0.075;break;
                  
case 2:
                  
case 3:J=10*0.1+10*0.075+(I-20)*0.05;;break;
                  
case 4:
                  
case 5:J=10*0.1+10*0.075+20*0.05+(I-40)*0.03;break;
                  
case 6:
                  
case 7:
                  
case 8:
                  
case 9:J=10*0.1+10*0.075+20*0.05+20*0.03+(I-60)*0.015;break;
                  
case 10:J=10*0.1+10*0.075+20*0.05+20*0.03+40*0.015+(I-100)*0.01;break;
                  
default:printf("error/n");
        
}        printf("%2lf/n",J);
        
return 0;
}
5.9
此题就是两两排序。。。略
5.10

// 5.10
void main()
{
        
double x,y;
        
scanf("%lf%lf",&x,&y);
        
if(fabs(x)>=1 && fabs(x)<=3 && fabs(y)>=1 && fabs(y)<=3)
        
{
                  
printf("height is 10/n");
        
}
        
else
        
{
                  
printf("height is 0/n");
        
}
}
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c integer 语言