南昌大学实验报告
课程名称:C++语言程序设计姓名:学号:班级:
实验类型:□ 验证 ■ 综合 ■ 设计□ 创新实验日期:实验成绩:
一、实验名称:第3章函数
二、实验要求:
1、设计一个程序,要求main函数有返回值
2、借鉴p67 例3-5的思想,将p66例3-3的程序修改
3、运用随机数设计一个3*3的三子棋游戏
4、满二叉树的中序遍历
5、体会习题3-2的程序实现
6、实现习题3-9
7、实现习题3-10
8、实现习题3-14
9、实现习题3-15
三、实验方法:函数模板的编写。
四、主要仪器设备及耗材:PC。
五、实验步骤: 1.编写程序; 2.调试程序;
六、实验数据及处理结果
1. 上机习题的程序代码如下:
#include
#include
#include
#include
#define queuesize 100
#define ERROR 0
#define OK 1
using namespace std;
void ex3_1()
{
}
double arctan(double x)
{
double e=0,g=x,t=x;
int n=1;
do{ 3.运行程序。
t=g/n; e=e+t;
g=-g*x*x;
n=n+2;
}while(fabs(t)>1e-15);
return e;
}
void ex3_2()
{
}
double a,b; double arctan(double x); a=16.0*arctan(1/5.0); b=4.0*arctan(1/239.0); coutvoid ex3_3()
{
int i,j,y=0,n=1;
char a[3][3]={{'0','O','O'},{'O','O','O'},{'O','O','O'}};
cout
while(n
{
srand(unsigned(time(0)));
i=rand()%3;
j=rand()%3; if (a[i][j]=='O') a[i][j]='#'; else continue; coutcout
}
cout
cin>>i>>j;
a[i][j]='*';
cout
for(i=0;i
{
for(j=0;j
coutcout
}
cout
n++;
if( a[0][0]==a[0][1]&&a[0][0]==a[0][2]&&a[0][0]=='#') {cout
else if(a[0][0]==a[1][0]&&a[0][0]==a[2][0]&&a[0][0]=='*') {cout
else if(a[0][0]==a[0][1]&&a[0][0]==a[0][2]&&a[0][0]=='*') {cout
else if(a[0][0]==a[1][1]&&a[0][0]==a[2][2]&&a[0][0]=='*') {cout
else if(a[0][1]==a[1][1]&&a[0][1]==a[2][1]&&a[0][1]=='*') {cout
else if(a[0][2]==a[1][2]&&a[0][2]==a[2][2]&&a[0][2]=='*') {cout
else if(a[2][0]==a[2][1]&&a[2][0]==a[2][2]&&a[2][0]=='*') {cout
else if(a[1][0]==a[1][1]&&a[1][0]==a[1][2]&&a[1][0]=='*') {cout
else if(a[0][2]==a[1][1]&&a[0][2]==a[2][0]&&a[0][2]=='*') {cout
} 人人人人人人人人
void ex3_4()
{
int k,n,i,p,t,j=0; int a[100]; int b[100]; for(i=0;i>k; n=(int)pow((double)2,(int)k)-1; cout>a[i]; cout
} cout
}
void ex3_5()
{
int intOne;
int &rSomeRef = intOne;
intOne = 5; cout
cout
}
bool ZS(int a)
{
for(int i=2;i
else return true;
}
int ex3_6()
{
int a;
puts("请输入一个数字:");
cin>>a;
if (ZS(a))
cout
} } cout
}
void ex3_5()
{
int intOne;
int &rSomeRef = intOne;
intOne = 5; cout
cout
cout
cout
}
bool ZS(int a)
{
for(int i=2;i
if((a)%i==0) return false;
else return true;
}
int ex3_6()
{
int a;
puts("请输入一个数字:");
cin>>a;
if (ZS(a))
cout
return ERROR;
}
int YS(int x,int y)
{
int k;
if(x
{
k=x;
x=y;
y=k;
}
if(x%y==0)
return abs(y);
else
return YS(y,x%y);
}
void ex3_7()
{
int x,y,z;
int i=0,p=1,r=1;
cout
cin>>x;
cout
cin>>y;
z=YS(x,y);
cout
cout
}
long fib(int n)
{
long f;
if(n
else if(n==1||n==2) f=1;
else f=fib(n-1)+fib(n-2);
return(f);
}
void ex3_8()
long fib(int n);
int n; long y; cout
cin>>n;
y=fib(n);
cout
}
long fac(int n,long x)
{
long f;
if(n
else if(n==1) f=x;
else f=((2*n-1)*x*fac(n-1,x)-(n-1)*fac(n-2,x))/n; return (f);
}
void ex3_9()
{
long fac(int n,long x); int n; long x; long y; cout>n>>x; y=fac(n,x); cout
}
void main()
{
int y=1;
while(y==1)
cout>choose; system("cls"); switch(choose) { case 1: ex3_1();break;
case 2: ex3_2();break;
case 3: ex3_3();break;
case 4: ex3_4();break;
case 5: ex3_5();break;
case 6: ex3_6();break;
case 7: ex3_7();break;
case 8: ex3_8();break;
case 9: ex3_9();break;
}
cout>y;
system("cls");
}
}
2. 运行结果
七、实验心得
本章实验难度不大,但是有的地方要用到随机数,需要上网查询理解,还有二叉树实验,因为已经有一段时间没有接触数据结构了,所以做起来有点生疏。还是要多看看学过的知识
八、参考资料:
C++语言程序设计. 清华大学出版社, 2004年6月第1版
南昌大学实验报告
课程名称:C++语言程序设计姓名:学号:班级:
实验类型:□ 验证 ■ 综合 ■ 设计□ 创新实验日期:实验成绩:
一、实验名称:第3章函数
二、实验要求:
1、设计一个程序,要求main函数有返回值
2、借鉴p67 例3-5的思想,将p66例3-3的程序修改
3、运用随机数设计一个3*3的三子棋游戏
4、满二叉树的中序遍历
5、体会习题3-2的程序实现
6、实现习题3-9
7、实现习题3-10
8、实现习题3-14
9、实现习题3-15
三、实验方法:函数模板的编写。
四、主要仪器设备及耗材:PC。
五、实验步骤: 1.编写程序; 2.调试程序;
六、实验数据及处理结果
1. 上机习题的程序代码如下:
#include
#include
#include
#include
#define queuesize 100
#define ERROR 0
#define OK 1
using namespace std;
void ex3_1()
{
}
double arctan(double x)
{
double e=0,g=x,t=x;
int n=1;
do{ 3.运行程序。
t=g/n; e=e+t;
g=-g*x*x;
n=n+2;
}while(fabs(t)>1e-15);
return e;
}
void ex3_2()
{
}
double a,b; double arctan(double x); a=16.0*arctan(1/5.0); b=4.0*arctan(1/239.0); coutvoid ex3_3()
{
int i,j,y=0,n=1;
char a[3][3]={{'0','O','O'},{'O','O','O'},{'O','O','O'}};
cout
while(n
{
srand(unsigned(time(0)));
i=rand()%3;
j=rand()%3; if (a[i][j]=='O') a[i][j]='#'; else continue; coutcout
}
cout
cin>>i>>j;
a[i][j]='*';
cout
for(i=0;i
{
for(j=0;j
coutcout
}
cout
n++;
if( a[0][0]==a[0][1]&&a[0][0]==a[0][2]&&a[0][0]=='#') {cout
else if(a[0][0]==a[1][0]&&a[0][0]==a[2][0]&&a[0][0]=='*') {cout
else if(a[0][0]==a[0][1]&&a[0][0]==a[0][2]&&a[0][0]=='*') {cout
else if(a[0][0]==a[1][1]&&a[0][0]==a[2][2]&&a[0][0]=='*') {cout
else if(a[0][1]==a[1][1]&&a[0][1]==a[2][1]&&a[0][1]=='*') {cout
else if(a[0][2]==a[1][2]&&a[0][2]==a[2][2]&&a[0][2]=='*') {cout
else if(a[2][0]==a[2][1]&&a[2][0]==a[2][2]&&a[2][0]=='*') {cout
else if(a[1][0]==a[1][1]&&a[1][0]==a[1][2]&&a[1][0]=='*') {cout
else if(a[0][2]==a[1][1]&&a[0][2]==a[2][0]&&a[0][2]=='*') {cout
} 人人人人人人人人
void ex3_4()
{
int k,n,i,p,t,j=0; int a[100]; int b[100]; for(i=0;i>k; n=(int)pow((double)2,(int)k)-1; cout>a[i]; cout
} cout
}
void ex3_5()
{
int intOne;
int &rSomeRef = intOne;
intOne = 5; cout
cout
}
bool ZS(int a)
{
for(int i=2;i
else return true;
}
int ex3_6()
{
int a;
puts("请输入一个数字:");
cin>>a;
if (ZS(a))
cout
} } cout
}
void ex3_5()
{
int intOne;
int &rSomeRef = intOne;
intOne = 5; cout
cout
cout
cout
}
bool ZS(int a)
{
for(int i=2;i
if((a)%i==0) return false;
else return true;
}
int ex3_6()
{
int a;
puts("请输入一个数字:");
cin>>a;
if (ZS(a))
cout
return ERROR;
}
int YS(int x,int y)
{
int k;
if(x
{
k=x;
x=y;
y=k;
}
if(x%y==0)
return abs(y);
else
return YS(y,x%y);
}
void ex3_7()
{
int x,y,z;
int i=0,p=1,r=1;
cout
cin>>x;
cout
cin>>y;
z=YS(x,y);
cout
cout
}
long fib(int n)
{
long f;
if(n
else if(n==1||n==2) f=1;
else f=fib(n-1)+fib(n-2);
return(f);
}
void ex3_8()
long fib(int n);
int n; long y; cout
cin>>n;
y=fib(n);
cout
}
long fac(int n,long x)
{
long f;
if(n
else if(n==1) f=x;
else f=((2*n-1)*x*fac(n-1,x)-(n-1)*fac(n-2,x))/n; return (f);
}
void ex3_9()
{
long fac(int n,long x); int n; long x; long y; cout>n>>x; y=fac(n,x); cout
}
void main()
{
int y=1;
while(y==1)
cout>choose; system("cls"); switch(choose) { case 1: ex3_1();break;
case 2: ex3_2();break;
case 3: ex3_3();break;
case 4: ex3_4();break;
case 5: ex3_5();break;
case 6: ex3_6();break;
case 7: ex3_7();break;
case 8: ex3_8();break;
case 9: ex3_9();break;
}
cout>y;
system("cls");
}
}
2. 运行结果
七、实验心得
本章实验难度不大,但是有的地方要用到随机数,需要上网查询理解,还有二叉树实验,因为已经有一段时间没有接触数据结构了,所以做起来有点生疏。还是要多看看学过的知识
八、参考资料:
C++语言程序设计. 清华大学出版社, 2004年6月第1版