《计算机科学》课程主页在:http://blog.csdn.net/sxhelijian/article/details/13705597
1、下面的两段程序体现了函数参数传值与传地址的区别,写出运行结果,上机时运行程序并记录结果,理解这两种机制的原理
(1)
#include <stdio.h>
void swap(int x, int y);
int main( )
{
int a=0, b=0;
a = 20;
b = 45;
if(a<b) swap(a, b);
printf("%d,%d", a, b);
return 0;
}
void swap(int x, int y)
{
int tmp = 0;
tmp = x;
x = y;
y = tmp;
}
你认为运行结果为:_________________
实际运行的结果为:_________________
(2)
#include <stdio.h>
void swap(int *x, int *y);
int main( )
{
int a=0, b=0;
a = 20;
b = 45;
if(a<b) swap(&a, &b);
printf("%d,%d", a, b);
return 0;
}
void swap(int *x, int *y)
{
int tmp = 0;
tmp = *x;
*x = *y;
*y = tmp;
}
你认为运行结果为:_________________
实际运行的结果为:_________________
2、下面是根据教材11.2中例11.1分析得到的一部分程序,请将尚未定义的函数写出来,运行程序得到每个方案的造价。
#include <stdio.h>
#include <math.h>
#define PI 3.1415926
float f(float c1,float c2,float t1,float t2,float s1,float s2,float r1,float r2,float h);
float f1(float c1,float c2,float h);
float f2(float t1,float t2,float h);
float f3(float s1,float s2,float h);
float f4(float r1,float r2,float h);
int main( )
{
printf("方案一造价: %.2f元\n", f(3,6,4,12,4,12,5,10,3));
printf("方案二造价: %.2f元\n", f(5,7,2,12,4,12,9,8,3.2));
printf("方案三造价: %.2f元\n", f(2,8,3,4,10,9,4,10,3.25));
return 0;
}
float f(float c1,float c2,float t1,float t2,float s1,float s2,float r1,float r2,float h)
{
return f1(c1,c2,h)+f2(t1,t2,h)+f3(s1,s2,h)+f4(r1,r2,h);
}
float f1(float c1,float c2,float h)
{
float a, s;
a=PI*c2*c2*3000;
s=2*PI*c2*h*2000;
return c1*(a+s+400000);
}
//下面定义f2,f3和f4函数
参考解答:
#include <stdio.h>
#include <math.h>
#define PI 3.1415926
float f(float c1,float c2,float t1,float t2,float s1,float s2,float r1,float r2,float h);
float f1(float c1,float c2,float h);
float f2(float t1,float t2,float h);
float f3(float s1,float s2,float h);
float f4(float r1,float r2,float h);
int main( )
{
printf("方案一造价: %.2f元\n", f(3,6,4,12,4,12,5,10,3));
printf("方案二造价: %.2f元\n", f(5,7,2,12,4,12,9,8,3.2));
printf("方案三造价: %.2f元\n", f(2,8,3,4,10,9,4,10,3.25));
return 0;
}
float f(float c1,float c2,float t1,float t2,float s1,float s2,float r1,float r2,float h)
{
return f1(c1,c2,h)+f2(t1,t2,h)+f3(s1,s2,h)+f4(r1,r2,h);
}
float f1(float c1,float c2,float h)
{
float a, s;
a=PI*c2*c2*3000;
s=2*PI*c2*h*2000;
return c1*(a+s+400000);
}
float f2(float t1,float t2,float h)
{
float a, s;
a=sqrt(3)*t2*t2*3000;
s=3*t2*h*2000;
return t1*(a+s+400000);
}
float f3(float s1,float s2,float h)
{
float a, s;
a=s2*s2*3000;
s=4*s2*h*2000;
return s1*(a+s+400000);
}
float f4(float r1,float r2,float h)
{
float a, s;
a=3*sqrt(3)*r2*r2*3000/2;
s=6*r2*h*2000;
return r1*(a+s+400000);
}
3、利用随机函数与自定义函数,制作一个帮助小学生学算术的程序,参考运行的部分截图:
下面是给出的main()函数,请写出自定义函数answer。
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int answer();
int main()
{
int i, sum=0; //sum: 学生得分
srand(time(0)); //初始化随机种子
for(i=0; i<10; i++)
{
printf("第%d题: ",i+1);
sum+=answer();
}
printf("共答对了%d道题. \n", sum);
return 0;
}
提示:answer()函数中该做的事:
- 产生两个随机数,分别作为被加数和加数;
- 计算这两数的和作为答案;
- 提示题目,并由小学生输入他/她计算的结果;
- 将小学生的输入与答案比较;
- 根据比较结果,提示小学生正误;
- 返回结果,正确为1,错误为0
参考解答:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int answer(); //函数声明,完成一道题的测试并返回评判结果,正确1为,错误为0
int main()
{
int i, sum=0; //sum: 学生得分
srand(time(0)); //初始化随机种子
for(i=0; i<10; i++)
{
printf("第%d题: ",i+1);
sum+=answer();
}
printf("共答对了%d道题. \n", sum);
return 0;
}
int answer()
{
int a,b,d,t;
a=rand()%10+1;
b=rand()%10+1;
d=a+b;
printf("%d + %d = ", a, b);
scanf("%d", &t);
if(t==d)
printf("right! \n");
else
printf("wrong! \n");
return (t==d);
}