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

内容发布更新时间 : 2024/5/21 11:12:24星期一 下面是文章的全部内容请认真阅读。

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

The volume of the cylinder is: 4-11

/* Blue is

编写一个程序,读取一个实数f,将其四舍五入的值赋值给整型变量n,输

出n的值。(尝试不用if语句完成本程序,并考虑负数是否适用) Program (1) #include <> main() { float f; int n, m; printf(\ scanf(\ m=f*10; m=m/5; /*m是什么值?*/ n=f; n=n+m; /*m有何作用?*/ printf(\ } Program (2) 如果不明白if语句,可以在学完第5章后再看。 #include <> main() { float f; int n, m; printf(\ scanf(\ n=f+; /*是否理解该语句?*/ printf(\ } Output (1)

Please input a real number: ? */

The rounded integer is: 4 Output (2)

Please input a real number: ? */

The rounded integer is: -13 4-12

/* Blue is input

/* Blue is input

编写程序,读入两个两位数字的整数,并按照如下形式输出这两个整数的

乘积。 4-13

4 5

4-14 4-15 4-16 4-17 4-18

* 3 7 3 1 5 1 3 5 1 6 6 5

提示:注意格式!

Program #include <> main() { int x, y, m, n; printf(\ scanf(\ printf(\ m=y; n=y/10; printf(\ } 第5章 判断与分支

5-1 输入一个字符ch,根据不同情况进行输出: 5-2 (1)ch为小写字母,输出对应的大写字母; 5-3 (2)ch为大写字母,按照原样输出; 5-4 (3)ch为数字,输出对应的数字;

5-5 (4)ch为其他字符,输出“Other character.”。

Program #include <> main() { char ch; printf(\ ch=getchar(); if (ch>='a' && ch<='z') printf(\ else if (ch>='A' && ch<='Z') printf(\ else if (ch>='0' && ch<='9') printf(\ else printf(\ } Output (1)

Please input a character: A? */ A Output (2)

Please input a character: b? */ B Output (3)

Please input a character: 3? */ 3 Output (4)

Please input a character: @? */

Other character.

/* Blue is input

/* Blue is input

/* Blue is input

/* Blue is input

5-6 为鼓励居民节约用水,自来水公司采用按月用水量分段计费的办法,居民应交

水费y元与月用水量x吨的函数关系式如下(设x?0)。 5-7

4x??y?f(x)??3??2.5x?10.5x?15x?15

5-8 编写程序,输入用户的月用水量x吨,计算并输出该用户应支付的水费y元(保

留两位小数)。

Program #include <> main() { float x, y; printf(\ scanf(\ if(x<=15) y=4*x/3; /*是否可以写成y=4/3*x;?区别在哪里?*/ else y=*; printf(\ } Output

Please input the water consumption: ? */

The water price is:

/* Blue is input

5-9 输入一个年份year,判断year是否为闰年。year若为闰年,需要满足下列条

件之一: 5-10

(1)能被4整除,但不能被100整除(如2004年是闰年,2100年不是闰

年) 5-11

(2)能被400整除(如2000年是闰年)

Program #include <> main() { int year; printf(\ scanf(\ if ((year%4==0&&year0!=0)||(year@0==0)) printf(\ else } printf (\ Output

Please input the year: 1900? */

1900 is not a leap year! 5-12

/* Blue is input

输入3个实数,将其按照降序输出(较大数在前),保留3位小数。

Program (1) #include <> main() { float x, y, z; printf(\ scanf(\ if (x>y) if(y>z) printf(\ else { if(x>z) printf(\ else printf(\ } else if(x>z) printf(\ else { if(y>z) printf(\y, z, x); else printf(\z, y, x); } } /*程序结束时,x, y, z的值是否发生了变化?*/ Program (2) #include <> main() { float x, y, z,t; printf(\ scanf(\ if (x>y) { t=x; x=y; y=t; } if (x>z) { t=x; x=z; z=t; } if (y>z) { t=y; y=z; z=t; } /*此程序与上面的程序有何不同?*/ printf(\