IE盒子

搜索
查看: 138|回复: 1

C语言程序设计(第四版,苏小红)课后习题解析

[复制链接]

1

主题

1

帖子

3

积分

新手上路

Rank: 1

积分
3
发表于 2023-1-12 08:39:03 | 显示全部楼层 |阅读模式
第二,三章习题解析

习题   2

2.1
以下不正确的C语言标识符是()。
A.AB1   B.a2_b  C._ab3  D.4ab:选D。C语言标识符只能一字母和下划线开始。
2.2
下面程序位变量x,y,z赋初值2.5,然后在屏幕上打印这些变量的值。程序存在错误,请改正错误,并写出程序的正确运行结果。
#include <stdio.h>
int main(void)
{
printf ("These values are :\n");
int x = y = 2.5;
printf ("x = %d\n",X);
printf ("y = %d\n",y);
printf ("z = %d\n",z);
return 0;
} :程序改正如下:
#include <stdio.h>
int main(void)
{
printf ("These values are :\n");
int x,y,z; //先定义变量x,y,z
x = y = z; //统一赋值
printf ("x = %d\n",x); //将输出值参数表中的X改为x
printf ("y = %d\n",y);
printf ("z = %d\n",z);
return 0;
}输出结果如下:
These values are :
x = 2
y = 2
z = 2习题   3

3.1
分析并写出下列程序的运行结果。
(1)
#inlcude <stdio.h>
int main(void)
{
int a = 12,b = 3;
float x = 18.5,y = 4.6;
printf ("%f\n",(float)(a*b)/2);
printf ("%d\n",(int)x%(int)y);
return 0;
}

(2)
#include <stdio.h>
int main(void)
{
int x = 32,y = 81,p,q;
p = x++;
q = y--;
printf ("%d %d\n",p,q);
printf ("%d %d\n",x,y);
return 0;
}
(1)
a*b = 12*3 =36
(flaot)(a*b)/2 = 18.000000 //一般情况flaot占4个字节,能存储7位数字
(int)x%(int)y = 18%4 =2故程序输出如下:
18.000000
2(2)
程序输出如下
32 81
33 803.2
参考例3.1程序,从键盘任意输入一个三位数,编程计算并输出它的逆序数(忽略证书前的正负号)。例如,输入-123,则忽略符号,由123分离出其百位1,十位2,个位3,然后计算3*100+2*10+1=321,并输出321。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
    int a= 10000;
    int c,b,d;
    while((a>999 || a<-999) && (a>99 || a<-99))     //判断输入数据是否位三位数
    {
    printf ("Please enter a three digits: ");
    scanf ("%d",&a);
    }
    if (a<0) a = fabs(a);   // 将负数转换为其的绝对值
    c = a / 100;
    b = (a-c*100)/10;
    d = (a-c*100-b*10);     //分离百,十,个位上的数
    a = d*100 + b*10 + c;
    printf ("%d",a);
    return 0;
}3.3
设银行定期存款的年利率rate位2.23%,已知存款期位n年,存款本金为capital元,试编程一复利方式计算并输出n年后的本利之和deposit。
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int year,i;
    float capital,deposit,rate = 0.0225;
    printf ("Please enter your capital: ");
    scanf ("%f",&capital);
    printf ("Please enter your deposit period : ");
    scanf ("%d",&year);
    for (i=1;i<=year;i++)
    {
        capital = capital *(1 + rate);
        deposit = capital;
    }
    printf ("After %d years,your deposit is %.2fyuan ",year,deposit);
    return 0;
}3.4


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
    float a,b,c,x1,x2;
    a=b=c=0;
    while (b*b-4*a*c<=0)
    {
    printf ("Please enter a,b and c :");
    scanf ("%f %f %f",&a,&b,&c);
    }
    x1=(-b+sqrt(b*b-4*a*c))/(2*a);
    x2=(-b-sqrt(b*b-4*a*c))/(2*a);
    printf ("%.2f %.2f",x1,x2);
    return 0;
}3.5



宏定义
#include <stdio.h>
#include <stdlib.h>
#define PI 3.14159

int main(void)
{
    float r;
    printf ("Please enter the radius of the ball: ");
    scanf ("%f",&r);
    printf ("The volume of the ball is %.2f\n",4*PI*r*r*r/3);
    printf ("The surface area of the ball is %.2f\n",4*PI*r*r);
    return 0;
}const 常量
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
    const float PI = 3.14159;
    float r;
    printf ("Please entre the radius of the ball :");
    scanf ("%f",&r);
    printf ("The volume of the ball is %.2f\n",(4/3)*PI*pow(r,3));
    printf ("The surface area of the ball is %.2f\n",4*PI*pow(r,2));
    return 0;
}
回复

使用道具 举报

2

主题

14

帖子

27

积分

新手上路

Rank: 1

积分
27
发表于 2025-5-21 03:26:13 | 显示全部楼层
楼主呀,,,您太有才了。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表