C语言上机练习参考答案 下载本文

Program (2) #include <> main() { float f=; int n, a, b; n=f; a = n; b = n0/10; /*该语句与上面程序不同,看看有何区别?*/ printf(\is: %d + %d = %d.\\n\ } 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(\ } Output

The scrap value is . 3-12 3-13 3-14 3-15 3-16

在库存管理中,某单个物品的经济定购数EOQ由下面等式给定:

EOQ?2?需求率?生产成本

单位时间内每种物品的储备成本而最优的定购时间间隔TBO由下面等式给定:

TBO?2?生产成本

需求率?单位时间内每种物品的储备成本编写程序,给定需求率(单位时间内的物品数)、生产成本(每个定购)和

储备成本(单位时间内每种物品),计算EOQ和TBO。

Program #include <> #include <> main() { int demand=1000; float product_cost=, storage_cost=, eoq, tbo; eoq=sqrt(2*demand*product_cost/storage_cost); tbo=sqrt(2*product_cost/demand/storage_cost); printf(\ } Output

EOQ is , and TBO is .

第4章 输入输出操作管理

4-1 输入两个数,将它们交换后输出。

Program #include <> main() { int x, y, t; printf(\ scanf(\ printf(\ t=x; x=y; y=t; printf(\ } Output

Please input 2 numbers: 3 5? */

Before swap, the 2 numbers are: 3, 5 After swap, the 2 numbers are: 5, 3

/* Blue is input

4-2 输入一个十进制数,输出对应的八进制数和十六进制数。

Program #include <> main() { int n; printf(\ scanf(\ printf(\n); } Output

Please input a decimal number: 10? */

The octal is 12, and the hexadecimal is a.

考虑:如何得到下面的输出?

/* Blue is input

Please input a decimal number: 10? /* Blue is input

*/

The octal is 012, and the hexadecimal is 0xa. 4-3 编写程序,输入3个整数,计算并输出它们的平均值。 Program #include <> main() { int a, b, c; printf(\ scanf(\ printf(\ } Output

Please input 3 integers: 4 7 -19? input */

The average is .

/* Blue is

4-4 编写一个程序,读取x和y的值,显示下面表达式的值: 4-5 (1)4-6 (2)

x?y x?yx?y 24-7 (3)?x?y??x?y?

Program #include <> main() { float x, y; printf(\ scanf(\ printf(\ printf(\ printf(\ } Output

Please input x and y: ? (1) (x+y)/(x-y) = (2) (x+y)/2 = (3) (x+y)(x-y) =

/* Blue is input */

4-8 计算银行存款的本息。编写程序,输入存款金额money、存期year和年利率rate,

根据下列公式计算存款到期时的本息合计sum(税前),输出时保留两位小数。

4-9

sum?money(1?rate)year

Program #include <> #include <> main() { float money, rate, sum; int year; printf(\ scanf(\ printf(\ scanf(\ printf(\ scanf(\ sum=money*pow(1+rate, year); printf(\ } Output

Please input the deposit money: 2500? input */

Please input the deposit period: 3? is input */

Please input the annual interest rate: ? */

The total principal and interest is: 4-10

/* Blue is

/* Blue

/* Blue is input

输入圆柱的高h和半径r,求圆柱体积volume=π*r2*h。

Program #include <> #define PI main() { float volume, r, h; printf(\ scanf(\ printf(\ scanf(\ volume=PI*r*r*h; printf(\ } Output

Please input the radius of the cylinder: ? input */

/* Blue is