C语言实验报告参考答案
实验一熟悉C语言程序开发环境及数据描述
四、程序清单
1.编写程序实现在屏幕上显示以下结果:
The dress is long
The shoes are big
The trousers are black
答案:
#include<>
main()
{
p rintf("The dress is long\n");
p rintf("The shoes are big\n");
p rintf("The trousers are black\n");
}
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()
{
i nt a,b,c,x,y;
a=150;
b=20;
c=45;
x=a/b;
y=a/c;
p rintf("a/b的商=%d\n",x);
p rintf("a/c的商=%d\n",y);
x=a%b;
y=a%c;
p rintf("a/b的余数=%d\n",x);
p rintf("a/c的余数=%d\n",y);
}
(2)
#include<>
main()
{
i nt a,b,c,d;
f loat x;
a=160;
b=46;
c=18;
d=170;
x=(a+b)/(b-c)*(c-d);
p rintf("(a+b)/(b-c)*(c-d)=%f\n",x);
}
3. 设变量a的值为0,b的值为-10,编写程序:当a>b时,将b赋给c;当a<=b时,将0赋给c。(提示:用条件运算符)
答案:
#include<>
main()
{
i nt a,b,c;
a=0;
b=-10;
c= (a>b) b:a;
p rintf("c = %d\n",c);
}
五、调试和测试结果
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)=
3. 编译、连接无错,运行后屏幕上显示以下结果:
c =-10
实验二顺序结构程序设计
四、程序清单
1.键盘输入与屏幕输出练习
问题1 D 。
问题2 改printf("%c,%c,%d\n",a,b,c);这条语句
改成:printf("%c %c %d\n",a,b,c);
问题3 改scanf("%c%c%d",&a,&b,&c);这条语句
改为:scanf("%c,%c,%d",&a,&b,&c);
问题4 改printf("%c,%c,%d\n",a,b,c);这条语句
改成:printf("\’%c\’ \’ %c\’ %d\n",a,b,c);
问题5 把scanf("%c%c%d",&a,&b,&c);和printf("%c,%c,%d\n",a,b,c);
改成scanf("%c%*c%c%*c%d",&a,&b,&c);
printf("\'%c\',\'%c\',%d\n",a,b,c);
2(1)从键盘输入两个八进制数,计算两数之和并分别用十进制和十六进制数形式输出。#include <>
int main()
{
int a,b,c;
s canf("%d%d",&a,&b);
c = a + b;
p rintf("%d\n",c);
p rintf("%x\n",c);
r eturn 0;
}
2(2)编写程序:从键盘输入两个实数a和x,按公式计算并输出y的值:
5
ax
+
+
=)
ln(
)
sin(
y+
+
e
a
x
a
ax
#include<>
#include<>
int main()
{
float a,x,y;
s canf("%f%f",&a,&x);
y = pow(a,5) + sin(a*x) + exp(a*x) + log(a+x);
p rintf("y=%f\n",y);
r eturn 0;
}
五、调试和测试结果
2(1) 输入: 12 14
输出:26
1a
2(2) 输入:1 0
输出:
实验三选择结构程序设计
四、设计流程(算法描述)
(请写出上机内容2(3)题的算法描述)
主要是两两比较,然后得出最大的数
五、程序清单
(1)输入一个整数,若大于等于0,输出提示信息“is positive”,否则输出“is negative”。#include<>
#include<>
main()
{
int a;
s canf("%d",&a);
i f(a>=0)
printf("the number is positve\n");
e lse
printf("the number is negetive\n");
r eturn 0;
}
(2)输入两个整数a和b,若a>=b时,求其积c并显示;若a
main()
{
int a,b,c;
s canf("%d%d",&a,&b);
i f(a>=b)
printf("c=%d\n",a*b);
e lse
printf("c=%d\n",a/b);
r eturn 0;
}
(3)输入a、b、c三个整数,输出最大数。
#include<>
main()
{
int a,b,c,x;
scanf("%d%d%d",&a,&b,&c);
i f(a>=b)
x=a;
e lse
x=b;
i f (x x=c; p rintf("the max number is:%d\n",x); r eturn 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的算法描述) 首先求出每一个给定数的所有因子和,然后从2到5000循环,那一个数x 与因子之和相等,就是完数。 五、程序清单 1.编写程序:求1+2+3+...+100和12+22+33+ (1002) #include<> #include<> int main() { int i,j,sum; s um = 0; f or (i=1;i<=100;i++) sum += i; p rintf("the sum is:%d\n",sum); s um =0; f or(i=1;i<=100;i++) { j=pow(i,2); sum +=j; } p rintf("the square sum is:%d\n",sum); r eturn 0; } 2.一个数如果恰好等于它的因子之和,这个数就称为“完数”,编写程序找出2~5000中的所有完数。 #include<> #include<> main() { int i,j,sum=0; f or(i=2;i<=5000;i++) 写程序:计算sinx 的近似值,精确到10-6。 ++-+-= ! 7!5!3sin 7 53x x x x x 其实 )! 12() 1(sin )12(1n )1(--=-∞=-∑n x x n n 所以程序 #include <> #include <> main() { float x,sinx,i,t; printf("请输入一个x 值(弧度值):"); scanf("%f",&x); sinx=0; t=x;i=1; while(fabs(t)>=1e-6) { sinx=sinx+t; t=t*(-x*x/(2*i*(2*i+1))); i++; } printf("sin(%.2f)=%.6f\n",x,sinx); } 六、调试和测试结果 1:结果:the sum is :5050 the square sum is :338350 2:结果:6 28 496 3、输入0,输出sin= 输入,输出sin= 输入,输出sin= 实验五 函数和编译预处理 四、设计流程(算法描述) (请写出上机内容2的算法描述) 求素数的方法就是:给定一个大于3的数x ,从2到X 的平方根遍历,只要有数可以被x 整除,就不是素数 五、程序清单 1.编写自定义函数long power(int m,int n),计算n m 的值。利用此函数编程序实现:从键盘输入两个整数m 和n ,计算出n m 的值。 #include<> long power(int m,int n)编写自定义函数count(int x),计算x 的因子个数。利用此函数找出并输出1~1000中有奇数个不同因子的整数。 #include<> #include<> int count(int x) { i nt sum,i; s um =0;入:2 3 输出:s=8 2.输出:共有668个素数 2. 3、输出结果为: 实验六数组 四、设计流程(算法描述) (请写出上机内容1的算法描述) 设置两个变量分别指示头和尾。第一个和最后一个元素值互换,然后头和尾变量向里移动,最终到两变量相遇为止。 五、程序清单 1.编写程序:从键盘输入一串整数保存到数组中,调用函数antitone()将数组反序输出。自定义函数void antitone(int a[],int n)实现将数组中的n个数据按逆序存放。void antitone(int a[],int n) { i nt i,j; i nt k; i=0; j=n-1;