C语言上机练习参考标准答案
- 格式:docx
- 大小:18.97 KB
- 文档页数:9
C语言上机练习参考标准答案
C语言上机练习参考答案
————————————————————————————————作者:————————————————————————————————日期:
第1章C语言概述
1-1编写程序,在屏幕上显示一个如下输出:
---------------------------------
Programming in C is fun!
I love C language.
---------------------------------
Program
#include
main()
{ printf("---------------------------------\n");
printf("Programming in C is fun!\n");
printf("I love C language.\n");
printf("---------------------------------\n");
}
1-2编写程序,在屏幕上显示一个如下图案:
* * * *
* * *
* *
*
Program (1)
#include
main()
{ printf("* * * *\n");
printf(" * * *\n"); printf(" * *\n");
printf(" *\n ");
}
Program (2)
#include
main()
{ printf("%c%4c%4c%4c\n", '*', '*', '*', '*');
printf("%3c%4c%4c\n", '*', '*', '*');
printf("%5c%4c\n", '*', '*');
printf("%7c\n ", '*');
}
1-3已知某个圆的半径,编写一个程序,用来计算并显示面积。
要求:将π定义为符号常量,并假设一个恰当的半径值。
Program
#include
#define PI 3.14
main()
{ float r=5, s;
s = PI*r*r;
printf("The area of circle is: %.2f\n", s);
}
Output
The area of circle is: 78.50
1-4已知两个整数20和10,编写程序,自定义函数add( )将这两个数相加,自定义函数sub( )计算这两个数的差,并按照下面形式显示计算结果:
20+10=30
20-10=10
Program
#include int add(int a, int b)
{ return (a+b);
}
int sub(int a, int b)
{ return (a-b);
}
main()
{ int a=20, b=10;
printf("%d + %d = %d\n", a, b, add(a, b));
printf("%d - %d = %d\n", a, b, sub(a, b));
}
Output
20 + 10 = 30
20 – 10 = 10
1-5 已知变量a 、b 和c 的值,编写程序,用来计算并显示x 的值,其中c
b a -=
x 请分别用以下数值运行该程序
(1)a=250,b=85,c=25
(2)a=300,b=70,c=80 Program (1)
#include
main()
{ int a=250, b=85, c=25;
float x;
x=1.0*a/(b-c);
printf("x = %.2f\n", x);
}
Output (1)
x = 4.17
Program (2) #include
main()
{ int a=300, b=70, c=80;
float x;
x=1.0*a/(b-c); /*试写成x=a/(b-c); 得到什么运行结果?为
什么?*/
printf("x = %.2f\n", x);
}
Output (2)
x = -30.00
第2章 常量、变量及数据类型 & 第3章 运算符和表达式 3-1 编写程序,求华氏温度100o F 对应的摄氏温度。计算公式如下:
9
)32(5-?=f c 式中:c 表示摄氏温度,f 表示华氏温度。(c 定义为实型,f 定义为整型) Program
#include
main()
{ int f=100;
float c;
c=5.0*(f-32)/9; /*如果是c=5*(f-32)/9; 会是什么结果?为什
么?*/
printf("Celsius degree (corresponding to %d Fahrenheit)
is : %.2f.\n",
f, c);
}
Output
Celsius degree (corresponding to 100 Fahrenheit) is: 37.78.
3-2 一个物体从100m 的高空自由落下,编写程序,求它在前3s
内下落的垂直距离。
设重力加速度为10m/s 2。 要求,将重力加速度定义为符号常量,尝试将其改为9.8 m/s 2,看结果有何不同? Program
#include
#define G 10
main()
{ int t=3;
float s;
s=1.0/2*G*t*t; /*如果是s=1/2*G*t*t; 会是什么结果?为什么?
*/
printf("The falling vertical distance (in %d seconds)
is: %.2f.\n", t, s);
}
Output
The falling vertical distance (in 3 seconds) is:45.00.
3-3将球的半径R定义为符号常量,计算球的表面积(4πR2)和体积(4/3*πR3)。
Program
#include
#define R 5.2
#define PI 3.14
main()
{ float s, v;
s=4*PI*R*R;
v=4.0/3*PI*R*R*R;
printf("The surface area of the ball (radius is %.2f) is: %.2f,
and the volume is: %.2f.\n", R, s, v);
}
Output
The surface area of the ball (radius is 5.20) is: 339.62, and the
volume 588.68. 3-4给定x、y和z的值,编写程序,使x等于y的值,y等于z的值,z等于x的值。
Program
#include
main()
{ int x=1, y=2, z=3, t;
printf("Before swap: x=%d, y=%d, z=%d.\n", x, y, z);
t=x;
x=y;
y=z;
z=t; /*变量t的作用是什么?*/
printf("After swap: x=%d, y=%d, z=%d.\n", x, y, z);
}
Output
Before swap: x=1, y=2, z=3.
After swap: x=2, y=3, z=1.
3-5 编写一个程序,给定一个浮点数(例如456.78),显示该数的十位数字与个位数
字之和(例如5+6=11)。 Program (1)
#include
main()
{ float f=456.78;
int n, a, b;
n=f;
a = n%10; /*赋值后,a 是什么值?*/
b = n/10%10; /*赋值后,b 是什么值?*/
printf("The sum of the tens digit and units digit of %.2f
is: %d + %d = %d.\n", f, b, a, a+b); }
Program (2)
#include main()
{ float f=456.78;
int n, a, b;
n=f;
a = n%10;
b = n%100/10; /*该语句与上面程序不同,看看有何区
别?*/
printf("The sum of the tens digit and units digit of %.2f
is: %d + %d = %d.\n", f, b, a, a+b);
}
Output
The sum of the tens digit and units digit of 456.78 is: 5+ 6 =
11.
3-6 某种物品每年折旧费的计算方法如下:
使用年限
废品价值购买价格折旧费-= 编写一个程序,当给定某物品的购买价格、使用年限和每年的折旧费时,计算出其废品价值。
Program
#include
main()
{ float price=120.65, year=3, depreciation=10.2, value;
value = price - year*depreciation;
printf("The scrap value is %.2f.\n", value);
}
Output
The scrap value is 90.05.
3-7 在库存管理中,某单个物品的经济定购数EOQ 由下面等式给定:
储备成本
单位时间内每种物品的生产成本需求率??=2EOQ 而最优的定购时