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

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

101~200 201~300 大于300

编写程序,计算某顾客应付的款项。

5% % % % % % 要求:同时用switch和if语句实现。

Program #include <> main ( ) { float price, discount; char category; printf ( \scanf ( \ printf ( \ scanf ( \ if ( category == 'm' ) { switch ( (int) (price-1)/100 ) { case 0: discount = 0; break; case 1: discount = ; break; case 2: discount = ; break; default: discount = ; } } else { switch ( (int) (price-1)/100 ) { case 0: discount = ; break; case 1: discount = ; break; case 2: discount = ; break; default: discount = ; } } printf (\net amount to be paid is %.2f\\n\price*(1-discount)); } Output

Category (m/h): h? */

Price: ? The net amount to be paid is 5-39

/* Blue is input

/* Blue is input */

编写程序,输入x,计算并输出下列分段函数f(x)的值(保留2位小数)。

5-40

1?5?x?2x?y?f(x)??x?x?x?0x?0

5-41 提示:请调用sqrt( )函数求平方根,调用pow( )函数求幂。

Program #include <> #include <> main() { float x, y; printf(\ scanf(\ if(x<0) y=pow(x,5)+2*x+1/x; else y=sqrt(x); printf(\ } Output

Please input the value of x: -2? input */

The value of y is: 5-42

/* Blue is

编写程序,输入一个实数(例如),求不小于该数的最小整数和不大于该数

的最大整数。(提示:注意负数)。

Program #include <> main() { float f; int n; printf(\scanf(\ n=f; if(f>=0) { printf(\largest integer (not larger than %.2f) is: %d\\n\f, n); if (f-n>0) printf(\smallest integer (not less than %.2f) is: %d\\n\f, n+1); else printf(\smallest integer (not less than %.2f) is: %d\\n\f, n); } else { if (f-n<0) printf(\largest integer (not larger than %.2f) is: %d\\n\f, n-1); else printf(\largest integer (not larger than %.2f) is: %d\\n\f, n); printf(\n); } } Output (1)

Please input a real number: ? The largest integer (not larger than is 6 The smallest integer (not less than is 7 Output (2)

Please input a real number: 6? */

The largest integer (not larger than is 6 The smallest integer (not less than is 6

/* Blue is input */

/* Blue is input

Output (3)

Please input a real number: ? /* Blue is input */ The largest integer (not larger than is -7 The smallest integer (not less than is -6 5-43

输入一个整数(考虑负数),判断它是奇数还是偶数(认为0是偶数),并

输出它的绝对值(不要使用labs( )函数)。 5-44 5-45 5-46

(1)使用if-else语句;

(2)使用if语句,但不使用else语句; (3)使用switch语句。

Program (1) #include <> main() { int number, m; printf(\ scanf(\ m= number<0?-1*number:number; if (m%2==0) /*如果条件是if(number%2==0),是否正确?*/ { printf(\is an even number, and the absolute number is %d.\\n\ } else { printf(\is an odd number, and the absolute number is %d.\\n\/*如果条件改为if(m%2),if语句是否需要调整?*/ } } Program (2) #include <> main() { int number, m; printf(\ scanf(\ m=number; if (number<0) m*=-1; if (number%2==0) printf(\is %d.\\n\ if (number%2==1||number%2==-1) printf(\is an odd number, and the absolute number is %d.\\n\ } Program (3) #include <> main() { int number; printf(\ scanf(\ switch (number%2) { case 1: printf(\is an odd number, and the absolute number is %d.\\n\ break; case -1: printf(\number is %d.\\n\ break; default: printf(\is an even number, and the absolute number is %d.\\n\ } } Output

Please input a number: -9?

/* Blue is input */

-9 is an odd number, and the absolute number is 9. 5-47

求解简单的二元运算表达式。输入一个形如“操作数 运算符 操作数”的

四则运算表达式(如:2 + 3或4 * ),输出运算结果。

Program #include <> main() { float x, y; char op; printf(\ scanf(\ switch(op) { case '+': printf(\result is: %.2f.\\n\x+y); break; case '-': printf(\result is: %.2f.\\n\x-y); break; case '*': printf(\ break; case '/': printf(\result is: %.2f.\\n\x/y); break; default: printf(\ } } Output

Please input the expression: 4*? */

The result is: . 5-48

/* Blue is input

某城市普通出租车收费标准如下:“起步里程3公里,起步费10元;超过

起步里程后10公里内,每公里租费2元;超过10公里以上的部分加收50%的空回补贴费,即每公里3元。营运过程中,因路阻及乘客要求临时停车的,每5分钟按1公里租费计收。运价计费尾数四舍五入,保留到元。” 5-49

编写程序,输入行驶里程(公里)与等待时间(分钟),并计算输出乘客应

支付的车费(元)。

Program (1) #include <> main() { float money; int distance, time; printf(\