好文档 - 专业文书写作范文服务资料分享网站

C语言实验报告参考答案(原)

天下 分享 时间: 加入收藏 我要投稿 点赞

C语言实验报告参考答案

实验一 熟悉C语言程序开发环境及数据描述

四、程序清单

1.编写程序实现在屏幕上显示以下结果: The dress is long The shoes are big

The trousers are black 答案:

#include

main() { printf(\ printf(\ printf(\}

2.编写程序:

(1) a=150,b=20,c=45,编写求a/b、a/c(商)和a%b、a%c(余数)的程序。 (2)a=160,b=46,c=18,d=170, 编写求(a+b)/(b-c)*(c-d)的程序。 答案: (1)

#include

main() { int a,b,c,x,y; a=150; b=20; c=45; x=a/b; y=a/c; printf(\的商=%d\\n\ printf(\的商=%d\\n\ x=a%b; y=a%c; printf(\的余数=%d\\n\ printf(\的余数=%d\\n\ }

(2)

#include

main() { int a,b,c,d; float x; a=160; b=46; c=18; d=170; x=(a+b)/(b-c)*(c-d); printf(\ }

3. 设变量a的值为0,b的值为-10,编写程序:当a>b时,将b赋给c;当a<=b时,将0赋给c。(提示:用条件运算符) 答案:

#include

main() { int a,b,c; a=0; b=-10; c= (a>b) ? b:a; printf(\ }

五、调试和测试结果

1.编译、连接无错,运行后屏幕上显示以下结果: The dress is long The shoes are big

The trousers are black

2、(1) 编译、连接无错,运行后屏幕上显示以下结果:

a/b的商=7 a/c的商=3

a/b的余数=10 a/c的余数=15

(2) 编译、连接无错,运行后屏幕上显示以下结果: (a+b)/(b-c)*(c-d)=-1064.0000

3. 编译、连接无错,运行后屏幕上显示以下结果: c =-10

实验二 顺序结构程序设计

四、程序清单

1.键盘输入与屏幕输出练习 问题1 D 。

问题2 改printf(\这条语句 改成:printf(\

问题3 改scanf(\这条语句 改为:scanf(\,%c,%d\ 问题4 改printf(\这条语句 改成:printf(\’%c\\’ \\’ %c\\’ %d\\n\

问题5 把scanf(\和printf(\

改成scanf(\

printf(\

2(1)从键盘输入两个八进制数,计算两数之和并分别用十进制和十六进制数形式输出。 #include int main() {

int a,b,c;

scanf(\ c = a + b;

printf(\ printf(\

return 0; }

2(2)编写程序:从键盘输入两个实数a和x,按公式计算并输出y的值: y?a?sin(ax)?ln(a?x)?e #include #include

int main() {

5ax

float a,x,y;

scanf(\

y = pow(a,5) + sin(a*x) + exp(a*x) + log(a+x); printf(\

return 0; }

五、调试和测试结果

2(1) 输入: 12 14 输出:26 1a 2(2) 输入:1 0

输出:2.000000

实验三 选择结构程序设计

四、设计流程(算法描述)

(请写出上机内容2(3)题的算法描述) 主要是两两比较,然后得出最大的数 五、程序清单

(1)输入一个整数,若大于等于0,输出提示信息“is positive”,否则输出“is negative”。 #include #include

main() {

int a;

scanf(\ if(a>=0)

printf(\ else

printf(\

return 0; }

(2)输入两个整数a和b,若a>=b时,求其积c并显示;若a main() {

int a,b,c;

scanf(\ if(a>=b)

printf(\ else

printf(\

return 0; }

(3)输入a、b、c三个整数,输出最大数。 #include main() {

int a,b,c,x;

scanf(\

if(a>=b) x=a; else x=b; if (x

printf(\

return 0;

}

六、调试和测试结果

2(1) 输入: 2

输出:the number is positve 输入: 0

输出:the number is positve

输入: -2

输出:the number is negetive 2(2) 输入: 3 2 输出:c=6

输入: 2 3 输出:c=0

2(3) 输入:3 2 1 输出:the max number is:3 输入:2 3 1 输出:the max number is:3

输入:1 2 3 输出:the max number is:3

实验四 循环结构程序设计

四、设计流程(算法描述)

(请写出上机内容2的算法描述)

27xfo7p4qd9bpah88nwz
领取福利

微信扫码领取福利

微信扫码分享