C语言上机练习参考答案
- 格式:docx
- 大小:185.17 KB
- 文档页数:106
第1章 C语言概述
1-1 编写程序,在屏幕上显示一个如下输出:
1-2 ---------------------------------
1-3 Programming in C is fun!
1-4 I love C language.
1-5 ---------------------------------
Program
#include <>
main()
{ printf("---------------------------------\n");
printf("Programming in C is fun!\n");
printf("I love C language.\n");
printf("---------------------------------\n");
}
1-6 编写程序,在屏幕上显示一个如下图案:
1-7 * * * *
1-8 * * *
1-9 * *
1-10 *
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-11 已知某个圆的半径,编写一个程序,用来计算并显示面积。
1-12 要求:将π定义为符号常量,并假设一个恰当的半径值。
Program
#include <>
#define PI
main()
{ float r=5, s;
s = PI*r*r;
printf("The area of circle is: %.2f\n", s);
}
Output
The area of circle is:
1-13 已知两个整数20和10,编写程序,自定义函数add( )将这两个数相加,自定义函数sub( )计算这两个数的差,并按照下面形式显示计算结果:
1-14 20+10=30
1-15 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-16 已知变量a、b和c的值,编写程序,用来计算并显示x的值,其中cbax
1-17 请分别用以下数值运行该程序
1-18 (1)a=250,b=85,c=25
1-19 (2)a=300,b=70,c=80 Program (1)
#include <>
main()
{
int a=250, b=85, c=25;
float x;
x=*a/(b-c);
printf("x = %.2f\n", x);
}
Output (1)
x =
Program (2)
#include <>
main()
{
int a=300, b=70, c=80;
float x;
x=*a/(b-c); /*试写成x=a/(b-c); 得到什么运行结果为什么*/
printf("x = %.2f\n", x);
}
Output (2)
x = 第2章 常量、变量及数据类型 & 第3章 运算符和表达式
3-1 编写程序,求华氏温度100oF对应的摄氏温度。计算公式如下:
3-2 9)32(5fc
3-3 式中:c表示摄氏温度,f表示华氏温度。(c定义为实型,f定义为整型)
Program
#include <>
main()
{
int f=100;
float c;
c=*(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: .
3-4 一个物体从100m的高空自由落下,编写程序,求它在前3s内下落的垂直距离。设重力加速度为10m/s2。
3-5 要求,将重力加速度定义为符号常量,尝试将其改为9.8 m/s2,看结果有何不同
Program
#include <>
#define G 10
main()
{ int t=3;
float s;
s=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:.
3-6 将球的半径R定义为符号常量,计算球的表面积(4πR2)和体积(4/3*πR3)。
Program
#include <>
#define R
#define PI
main()
{ float s, v; s=4*PI*R*R;
v=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 is: , and the volume .
3-7 给定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-8 编写一个程序,给定一个浮点数(例如),显示该数的十位数字与个位数字之和(例如5+6=11)。
Program (1)
#include <>
main()
{ float f=;
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=;
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 is: 5+ 6 = 11.
3-9 某种物品每年折旧费的计算方法如下:
3-10
使用年限废品价值购买价格折旧费
3-11 编写一个程序,当给定某物品的购买价格、使用年限和每年的折旧费时,计算出其废品价值。
Program
#include <>
main()
{
float price=, year=3, depreciation=, value;
value = price - year*depreciation;
printf("The scrap value is %.2f.\n", value);
}
Output
The scrap value is .
3-12 在库存管理中,某单个物品的经济定购数EOQ由下面等式给定:
3-13 储备成本单位时间内每种物品的生产成本需求率2EOQ
3-14 而最优的定购时间间隔TBO由下面等式给定:
3-15
储备成本单位时间内每种物品的需求率生产成本2TBO
3-16 编写程序,给定需求率(单位时间内的物品数)、生产成本(每个定购)和储备成本(单位时间内每种物品),计算EOQ和TBO。
Program
#include <>
#include <>
main()
{ int demand=1000;
float product_cost=, storage_cost=, eoq, tbo;