文档库 最新最全的文档下载
当前位置:文档库 › 程序设计实践报告

程序设计实践报告

江苏科技大学

课程实践报告

设计题目:计算机程序设计实践(c++)

设计时间:2015-12-14至2015-12-16

学院:计算机科学与技术学院

专业班级:

学生姓名:学号

指导老师:

2015年12月

实践任务一(基本题第二题)

1.实践任务

试建立一个类SP,求f(n,k)=1^k +2^k+3^k+……+n^k ,另有辅助函数power(m,n)用于求m^n。

2.系统设计

1)概要设计

通过类SP的成员函数power()求出m的n次方的值,再通过成员函数fun( )调用power( )函数求出f(n,k)的值。

2)详细设计

①类的描述与定义

class SP{

int n,k;//私有成员,n对应底数,k对应指数

public:

SP(int n1,int k1)//构造函数

{

n=n1;

k=k1;

}

int power(int c,int d);//求c^d

int fun();//求f(n,k)

void show();//输出结果

};

②主要成员函数

int power(int c,int d){

int add=1;

for(int i=1;i

return add;

}

int fun(){

int r=0;

for(int i=1;i

r+=power(i,k);//循环加调用power()求f(n,k)

return r;

}

void show(){//调用fun()输出结果

cout<<"f("<

for(int i=1;i

{

cout<

if(i!=n) cout<<'+';

}

cout<<'='<

}

3.系统测试

用以测试的数据为:

f1(1,1),f2(3,4),f3(9,9)

预期的输出结果为:

1 98 574304985

4.实践小结

简单的类和循环的运用

5.原程序清单

#include

class SP{

int n,k;

public:

SP(int n1,int k1){

n=n1;

k=k1;

}

int power(int c,int d){

int add=1;

for(int i=1;i

return add;

}

int fun(){

int r=0;

for(int i=1;i

return r;

}

void show(){

cout<<"f("<

for(int i=1;i

{

cout<

if(i!=n) cout<<'+';

}

cout<<'='<

}

};

void main()

{

SP f1(1,1),f2(3,4),f3(9,9);

f1.show();

f2.show();

f3.show();

}

实践任务二(基本题二十题)

1.实践任务 定义一个方阵类Array ,实现对方阵进行逆时针90゜旋转,如图

2.系统设计

1)概要设计

通过类Array 的成员函数xuanzhuan ()求出方阵的旋转结果,并通过函数show ()输出结果

2)详细设计

①类的描述与定义

class Array{

int a[4][4];//私有成员,矩阵爸爸

public:

Array(int a1[][4],int n)//构造函数,简单的双重循环赋值私有成员 {

for(int i=0;i<4;i++)

for(int j=0;j<4;j++)

a[i][j]=a1[i][j];

}

void xuanzhuan();//实现旋转

void show();//实现输出

};

②主要成员函数

void xuanzhuan(){//旋转过程

int b[4][4];

for(int j=0;j<4;j++)// 通过独立的二维数组保存改变的数组 for(int i=0;i<4;i++) b[3-j][i]=a[i][j];

for(int m=0;m<4;m++)// 重新改变原数组

for(int n=0;n<4;n++) a[m][n]=b[m][n];

}

void show(){//循环输出结果

for(int i=0;i<4;i++)

{

for(int j=0;j<4;j++)

cout<

cout<

}

}

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 4 8 12 16 3 7 11 15 2 6 10 14 1 5 9 13

3.系统测试

用以测试的数据为:

1 2 3 4

5 6 7 8

9 10 11 12

1314 15 16

预期的输出结果为:

4 8 12 16

3 7 11 15

2 6 10 14

1 5 9 13

4.实践小结

当二维数组为类的成员时,构造处理过程,和旋转的数学逻辑过程,规律的发现和实现

5.原程序清单

#include

class Array{

int a[4][4];

public:

Array(int a1[][4],int n){

for(int i=0;i<4;i++) for(int j=0;j<4;j++)a[i][j]=a1[i][j];

}

void xuanzhuan(){

int b[4][4];

for(int j=0;j<4;j++) for(int i=0;i<4;i++)b[3-j][i]=a[i][j];

for(int m=0;m<4;m++) for(int n=0;n<4;n++)a[m][n]=b[m][n];

}

void show(){

for(int i=0;i<4;i++){

for(int j=0;j<4;j++)cout<

cout<

}

}

};

void main()

{

int b[][4]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};

Array m(b,4);

m.show();

m.xuanzhuan();

cout<

m.show();

}

实践任务三(提高题第十题)

1.实践任务

设计一个程序通过虚函数求长方形的面积和长方体的表面积

2.系统设计

1)概要设计

通过类Rectangle的构造函数初始化长宽,再构造虚函数fun()求面积和show()输出结果。然后再派生类Cuboid表示长方体再动态联编虚函数fun()求面积和show()输出结果。并在主函数中用基类的指针调用函数实现函数功能

类的结构简单的单线继承类Rectangle=>类Cuboid

2)详细设计

①类的描述与定义

基类Rectangle有三个保护成员,定义如下:

class Rectangle

{

protected:

int l,w;//长宽

int s;//面积

public:

//成员函数

};

派生类Cuboid有一个保护成员,定义如下:

class Cuboid{

int h;//高

public:

//成员函数

};

②主要成员函数设计

基类Rectangle的构造函数定义如下:

Rectangle(int x,int y)

{

l=x;w=y;

}

基类Rectangle的成员函数定义如下:

virtual void fun()

{

s=l*w;

}

virtual void show(){

cout<<"长方形的长为"<

}

派生类Cuboid的构造函数定义如下:

Cuboid(int x,int y,int z):Rectangle(x,y)

{

h=z;

}

派生类Cuboid的成员函数定义如下:

void fun()

{

s=2*l*w+2*w*h+2*l*h;

}

void show()

{

cout<<"长方体的长为"<

}

3.系统测试

分别对两个类进行测试,但是派生类Cuboid的测试需要基类指针的调用。

用以测试的数据为:

Rectangle a1(2,3),*p;

Cuboid b1(2,3,4);

预期的输出结果为:

长方形的长为2,宽为3,表面积为6

长方体的长为2,宽为3,高为4,表面积为96

4.实践小结

类的继承与多态性的简单使用,虚函数是类中被声明为virtual的非静态成员函数。学会动态联编在主函数中的使用,有一定的理解难度,但是数学概念很简单,面积的运算。需要扎实的基础知识

5.原程序清单

#include

class Rectangle

{

protected:

int l,w;

int s;

public:

Rectangle(int x,int y)

{

l=x;

w=y;

}

virtual void fun()

{

s=l*w;

}

virtual void show()

{

cout<<"长方形的长为"<

}

};

class Cuboid:public Rectangle{

int h;

public:

Cuboid(int x,int y,int z):Rectangle(x,y)

{

h=z;

}

void fun()

{

s=2*l*w+2*w*h+2*l*h;

}

void show()

{

cout<<"长方体的长为"<

}

};

void main ()

{

Rectangle a1(2,3),*p;

Cuboid b1(2,3,4);

a1.fun();

a1.show();

p=&b1;

p->fun();

p->show();

}

实践任务四(提高题十一题)

1.实践任务

设计一个程序,查询2000年1年1月(星期六)后的某天是星期几

2.系统设计

1)概要设计

首先定义两个函数 leap (int year)判断是否为闰年,判断输入的日期是否合法,创建一个类date保存日期,定义一个派生类判断星期,输出结果解决问题。此函数难点在于如何判断星期

2)详细设计

两个判断函数定义如下

int leap (int year)//判断是否为闰年

{

if(year%400==0) return 1;

if(year%4==0&&year%100!=0) return 1;

return 2;

}

int f(int y,int m,int d,int *m1,int *m2)//判断输入日期合法性

{

if(y>=2000)

if(m>=1&&m<=12)

{

if(leap(y)==2)

if(d<=m1[m-1]&&d>=1) return 1;

if(leap(y)==1)

if(d<=m2[m-1]&&d>=1) return 1;

}

return 0;

}

基类date的定义如下

class date

{

int year,month,day;//表示年月日

public:

//成员函数

};

派生类week定义如下

class week:public date{

date d1;

int m1[12];//非闰年每月天数

int m2[12];//闰年每月天数

int w;//判断的星期

public:

};

①判断用函数设计

int leap (int year)//判断是否为闰年

{

if(year%400==0) return 1;

if(year%4==0&&year%100!=0) return 1;

return 2;

}

int f(int y,int m,int d,int *m1,int *m2)//判断输入日期合法性{

if(y>=2000)

if(m>=1&&m<=12)

{

if(leap(y)==2)

if(d<=m1[m-1]&&d>=1) return 1;

if(leap(y)==1)

if(d<=m2[m-1]&&d>=1) return 1;

}

return 0;

}

②构造函数设计

基类date

date(int y,int m,int d)

{

year=y,month=m,day=d;

}

派生类week

week(int y,int m,int d,int *p1,int *p2):d1(y,m,d) {

for(int i=0;i<12;i++)

{

m1[i]=p1[i];

m2[i]=p2[i];

}

}

③成员函数设计

基类date

int get_year() { return year; }

int get_month() { return month; }

int get_day() { return day; }

void show()//输出日期

{

cout<

}

派生类week

int days()//距离标准的的天数

{

int num=0;

for(int y=2000;y

{

if(leap(y)==1)num+=366;

else num+=365;

}

for(int m=1;m

{

if(leap(d1.get_year())==1)num+=m2[m-1];

else num+=m1[m-1];

}

int d=d1.get_day();

num+=(d-1);

return num;

}

void fun()//输出星期

{

int r=days();

int w=r%7-1;

if(w==0) cout<<"天";

if(w==1) cout<<"一";

if(w==2) cout<<"二";

if(w==3) cout<<"三";

if(w==4) cout<<"四";

if(w==5) cout<<"五";

if(w==6) cout<<"六";

}

void print()//输出结果

{

cout<

}

3.系统测试

用以测试的数据为:

int r1[12]={31,28,31,30,31,30,31,31,30,31,30,31};

int r2[12]={31,29,31,30,31,30,31,31,30,31,30,31};

week n(2009,9,15,r1,r2);

预期的输出结果为:

2009/9/15,是星期二。

4.实践小结

简单的类的派生关系运用,复杂的数学逻辑过程需要严密的过程推导和程序的实现能力。

5.源程序清单

#include

int leap (int year)//判断是否为闰年

{

if(year%400==0) return 1;

if(year%4==0&&year%100!=0) return 1;

return 2;

}

int f(int y,int m,int d,int *m1,int *m2)//判断输入日期合法性{

if(y>=2000)

if(m>=1&&m<=12)

{

if(leap(y)==2)

if(d<=m1[m-1]&&d>=1) return 1;

if(leap(y)==1)

if(d<=m2[m-1]&&d>=1) return 1;

}

return 0;

}

class date

{

int year,month,day;//表示年月日

public:

date(int y=0,int m=0,int d=0)

{

year=y,month=m,day=d;

}

int get_year() { return year; }

int get_month() { return month; }

int get_day() { return day; }

void show()//输出日期

{

cout<

}

};

class week:public date{

date d1;

int m1[12];//非闰年每月天数

int m2[12];//闰年每月天数

int w;//判断的星期

public:

week(int y,int m,int d,int *p1,int *p2):d1(y,m,d)

{

for(int i=0;i<12;i++)

{

m1[i]=p1[i];

m2[i]=p2[i];

}

}

int days()//距离标准的的天数

{

int num=0;

for(int y=2000;y

{

if(leap(y)==1)num+=366;

else num+=365;

}

for(int m=1;m

{

if(leap(d1.get_year())==1)num+=m2[m-1];

else num+=m1[m-1];

}

int d=d1.get_day();

num+=(d-1);

return num;

}

void fun()//输出星期

{

int r=days();

int w=r%7-1;

if(w==0) cout<<"天";

if(w==1) cout<<"一";

if(w==2) cout<<"二";

if(w==3) cout<<"三";

if(w==4) cout<<"四";

if(w==5) cout<<"五";

if(w==6) cout<<"六";

}

void print()//输出结果

{

d1.show();

cout<<"是星期";

fun();

cout<<"。"<

}

};

void main()

{

int r1[12]={31,28,31,30,31,30,31,31,30,31,30,31};

int r2[12]={31,29,31,30,31,30,31,31,30,31,30,31};

if(f(2009,9,15,r1,r2)==0){

cout<<"日期不合法\n";

return 0;

}

date m(2009,9,15);

week n(2009,9,15,r1,r2);

n.print();

}

相关文档
相关文档 最新文档