江苏科技大学苏州理工学院《计算机程序设计基础实践VC++》实验报告

我是2014级江科大苏理工的机制专业,大一/二的时候学校会开设C++相关的课程,这门课我们系80多个人,挂了50多个,是学校最赚钱的课程之一。

主课会附属一门实践课,实践课就是学期末上交一份按照要求的报告。

下面是我当时写的报告。这个课只挂了一个,而且是因为实验报告找不到了。

虽然现在看来很简单,但是在当时的我来说,有一题没有完成。但是这是我编程秃头之路的开始,并持续至今。

图标

江苏科技大学
(张家港)
实验报告
专业班级: 2014级机制专业 1班
课程名称: 计算机程序设计基础实践VC++
学 号:
学生姓名:
指导教师: 孙 娜

2015年6月

一、函数编程:

1.求素数问题:定义函数判断一个整数是否为素数,求300以内的素数,每行3个数形式输出。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
using namespace std;

int judge(int a)
{
//特殊判断
if(a==2) return 1;
if(a==3) return 1;

//通用
if(a>=3)
{
for(int i=2;i<a;i++)
{
//从2开始直到a,如果a可以整除任意一个值就不是素数。
//比如8,可以整除2/4,9可以整除3/6
//上限可以是a/2,还可以是 sqrt(a) ,想想为什么
if(a%i==0)
return 0;
else
continue;
}
}
return 1;
}

int main()
{
int num=0;
for(int j=2;j<=300;j++)
{
//判断每一个数
if(judge(j))
{
cout<<j<<' ';
num++;
//每三个换行
if(num%3==0)
{
cout<<endl;
}
}
}
return 0;
}

2.最大公约数和最小公倍数问题:求两分数之和并输出结果,要求:将求最小公倍数和最大公约数设计成独立的函数。(提醒:分子、分母可分别用两个整型变量表示。之和的分母就是两分数分母的最小公倍数;输出示例:cout<<3<<”/”<<7; 就完成了分数3/7的输出显示。)

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
using namespace std;

int fenzi,fenmu;//分子 分母

//倍数判断
int judgebei(int a,int c)
{
int min=a>c?c:a;
for(int i=min;i<=a*c;i++)
{
//两个能同时整除,碰到即跳出以保证是最小
// 4 8的最小公倍数是8,即8/4=2 8/8=1
if(i%a==0&&i%c==0)
{
return i;
break;
}
}
}

//约数判断
int judgeyue(int fenmu,int fenzi)
{
int cou=1;
int min=fenmu<fenzi?fenmu:fenzi;
for(int i=1;i<=min;i++)
{
//两个能同时整除,碰到即跳出以保证是最小
// 4/8 = 1/2
if(fenmu%i==0&&fenzi%i==0)
{
if(i>=cou)
{
cou=i;
}
}
}
return cou;
}

int main()
{
int a,b,c,d;
cout<<"输入第一个分数:分子、分母"<<endl;
cin>>b>>a;
cout<<"输入第二个分数:分子、分母"<<endl;
cin>>d>>c;
int bei=judgebei(a,c);
fenmu=bei;
fenzi=b*fenmu/a+d*fenmu/c;//先把两个分数通分
int yue=judgeyue(fenmu,fenzi);
cout<<b<<"/"<<a<<"+"<<d<<"/"<<c<<"="<<fenzi/yue<<"/"<<fenmu/yue;
return 0;
}

3.排序与元素移动综合题:

(1)定义整型数组a[10],从键盘输入10个数为数组赋值,

(2)并分别定义两个独立函数,void print(int b[])实现将一维数组元素输出,void sort(int b[],int n)实现将一维数组的元素按从小到大排序(用两种方法实现)。

最后在主函数中分别调用两个函数。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
using namespace std;

int b[10];
int tem[100000000]={0,0};

//排序
void sort1(int a[],int n)
{
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}

//排序2
void sort2(int a[],int n)
{
int d=0;
int countnum=a[0];
for(int i=0;i<n;i++)
{
if(a[i]>=countnum)
countnum=a[i];
tem[a[i]]++;
}
for(int j=0;j<=countnum;j++)
{
while(tem[j]!=0)
{
b[d]=j;
d++;
tem[j]--;
}
}
}

void print(int a[])
{
for(int i=0;i<10;i++)
{
cout<<a[i]<<' ';
}
cout<<endl;
}

int main()
{
int a[10];
for(int i=0;i<10;i++)
{
cin>>a[i];
}
print(a);
sort1(a,10);
print(a);
sort2(a,10);
print(b);
return 0;
}

4.二维数组与元素移动:
(1)定义一个二维数组int b[2][3],从键盘输入为二维数组赋值,将二维数组中每行元素循环后移一位,并以矩阵形式输出二维数组元素。
(2)定义一个二维数组,int a[3][4],求其外围元素之和,并以矩阵形式输出二维数组元素。(求和用两种方法实现)

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
using namespace std;

int main()
{
int b[2][3];
int a[3][4];
cout<<"输入移动2*3数组:"<<endl;
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
cin>>b[i][j];
}
}
for(int i=0;i<2;i++)
{
int temp=b[i][2];
b[i][2]=b[i][1];
b[i][1]=b[i][0];
b[i][0]=temp;
}
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
cout<<b[i][j]<<' ';
}
cout<<endl;
}
cout<<"输入3*4数组:"<<endl;
int sum=0,sum2;
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
cin>>a[i][j];
if(i==0||i==2||j==0||j==3)
sum=sum+a[i][j];
}
}
sum2=a[0][0]+a[0][1]+a[0][2]+a[0][3]+a[1][0]+a[1][3]+a[2][0]+a[2][1]+a[2][2]+a[2][3];
cout<<sum<<' '<<sum2<<endl;
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
cout<<a[i][j]<' ';
}
cout<<endl;
}
return 0;
}

5.字符串处理:设计一个通用函数实现在字符串s1中从第m个字符开始插入字符串s2,在主程序中对该函数进行测试。

初始状态:

1
2
s1:abcdefg
s2:1234

输入插入字符串的位置:3

目标状态:

1
2
s1: ab1234cdefg
s2:1234

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

void connect(char str1[80],char str2[80],int i)
{
char str3[80];
int j,k,m,h;
h=strlen(str1);
for(j=0;j<=i-2;j++)
{
str3[j]=str1[j];
}
for(m=0;str2[m]!='\0';j++,m++)
{
str3[j]=str2[m];
}
for(k=h-i-2;str1[k]!='\0';j++,k++)
{
str3[j]=str1[k];
}
str3[j]='\0';
//printf("%s",str3);
for(int i=0;i<strlen(str3);i++)
{
cout<<str3[i];
}
cout<<endl;
}

int main()
{
char str1[80],str2[80];
int i;
cin.getline(str1,80);//gets(str1);
cin.getline(str2,80);
cin>>i;
connect(str1,str2,i);
return 0;
}

6.拼数与合数问题:
(1)在主函数中定义一个一维数组a[4]用来存放4个整型数3,12,0,1,并依次输出一维数组中的元素。
(2)依次取出数组中的元素,利用合数算法将这些元素合成一个数,要求合数的过程写成独立函数的形式,编写int fun(int b[],int n)函数,其中b为一维数组,n为b数组中包含的元素个数。该函数用来实现合数过程,并在主函数中调用该函数,并在主函数中输出合成之后的数。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
using namespace std;

int sum=0;

int judge(int b)
{
int c=0;
while(b)
{
b=b/10;
c++;
}
return c;
}

int fun(int a[],int n)
{
for(int i=0;i<n;i++)
{
int b=a[i];
if(b!=0)
{
int num=judge(b);
while(num)
{
sum=sum*10;
num--;
}
sum=sum+b;
}
else{
sum=sum*10;
}
}
return sum;
}

int main()
{
int a[4]={3,12,0,1};
for(int i=0;i<4;i++)
{
cout<<a[i]<<' ';
}
cout<<endl;
cout<<fun(a,4)<<endl;
return 0;
}

7.数字字符与整数转换算法:将字符串#ab78c15fk23d*中整型数据提取出来,合成一个整型数781523并输出来。再将781523整数的各个数位数字拆开存放到一维数组int b[10]中,并将一维数组中的元素转换为数字字符存放到字符数组s[100]中,并输出该数字字符串"325187".

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string.h>
using namespace std;

int main()
{
string str;
cin>>str;
int a=str.length(),sum=0;
for(int i=0;i<a;i++)
{
if(str[i]>='0'&&str[i]<='9')
{
sum=sum*10+str[i]-'0';
}
}
cout<<sum<<endl;
int b[10],j=0;
while(sum)
{
b[j]=sum%10;
sum=sum/10;
j++;
}
for(int i=0;i<j;i++)
{
if(b[i])
{
cout<<b[i]<<' ';
}
}
cout<<endl;
char s[100];
int num=0;
for(int i=0;i<j;i++)
{
if((b[i]))
{
s[num]=b[i]+'0';
num++;
}
}
s[num]='\0';
for(int i=0;i<num;i++)
{
cout<<s[i]<<' ';
}
cout<<endl;
return 0;
}

8.循环结构:请从键盘输入一个正整数,求这个数是几位数,并求出该正整数的各个位上的数字之和。如输入1234,该正整数是4位数,各个数位上的数字之和为10.

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;

int a[100],b=0;

void fun(int n)
{
while(n)
{
a[b]=n%10;
n=n/10;
b++;
}
}

int main()
{
int n;
cin>>n;
fun(n);
cout<<"这是"<<b<<"位数"<<endl;
int sum=0;
for(int i=0;i<b;i++)
{
sum=sum+a[i];
}
cout<<"和为"<<sum<<endl;
return 0;
}

9.求1-1/2+1/3-1/4+……+1/99-1/100的和。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main()
{
int i;
float s,t,sum;
t=1;
sum=0;
s=1;
for(i=2;i<=100;i++)
{
sum=sum+t;
s=-s;
t=s/i;
}
cout<<sum<<endl;
return 0;
}

10.从键盘输入一个整数n,将该整数的每一位上的数字拆开,存放到一维数组int a[10]中,该过程用独立函数形式实现。并将一维数组中的元素取出合成一个数,并输出合成的数和一维数组元素。如n:1234,存放到一维数组中的顺序为4 3 2 1,合成一个数为4321。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;

int a[100],b=0;

void fun(int n)
{
while(n)
{
a[b]=n%10;
n=n/10;
b++;
}
}

int main()
{
int n;
cin>>n;
fun(n);
for(int i=0;i<b;i++)
{
cout<<a[i]<<' ';
}
cout<<endl;
int sum=0;
for(int i=0;i<b;i++)
{
sum=sum*10+a[i];
}
cout<<sum<<endl;
return 0;
}

二、类的问题:

1.问题描述如下:
(1)定义一个类Array, 它的数据成员和成员函数如下:
  私有数据: int b[10];
  公有成员函数:Array(int t[]); //功能为一维数组b初始化
         void sort(); //功能为实现对一维数组从大到小排序
         void move(); // 功能为将一维数组元素循环后移一位
         void print(); //功能为输出一维数组元素
(2)用类定义对象arr,并用对象arr调用成员函数,实现对一维数组元素排序和数组元素循环后移一位,并输出每次操作的结果。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
using namespace std;

class Array
{
public:
Array(int t[]);
void sort();
void move();
void print();
private:
int b[10];
};

Array::Array(int t[])
{
for(int i=0;i<10;i++)
{
b[i]=t[i];
}
}

void Array::sort()
{
for(int i=0;i<9;i++)
{
for(int j=i+1;j<10;j++)
{
if(b[i]<b[j])
{
int temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}
}

void Array::move()
{
int temp=b[9];
for(int i=8;i>=0;i--)
{
b[i+1]=b[i];
}
b[0]=temp;
}

void Array::print()
{
for(int i=0;i<10;i++)
{
cout<<b[i]<<' ';
}
cout<<endl;
}

int main()
{
int t[10]={1,4,2,5,3,7,8,6,9,22};
Array arr(t);
arr.print();
arr.sort();
arr.print();
arr.move();
arr.print();
return 0;
}

2.问题描述如下:
(1)定义一个类Array, 它的数据成员和成员函数如下:
  私有数据: int b[4][5];int s;
  公有成员函数:Array(int t[][5]);//功能为二维数组b初始化
         void sum();//功能为求二维数组外围元素之和
         void print();//功能为以矩阵形式输出二维数组元素
(2)用类定义对象array,并用对象array调用成员函数求出二维数组外围元素之和,输出二维数组元素。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
using namespace std;

class Array
{
public:
Array(int t[][5]);
void sum();
void print();
private:
int b[4][5];
int s;
};

//构造函数使用输入参数初始化成员变量
Array::Array(int t[][5])
{
for(int i=0;i<4;i++)
{
for(int j=0;j<5;j++)
{
b[i][j]=t[i][j];
}
}
s=0;
}

void Array::sum()
{
for(int i=0;i<4;i++)
{
for(int j=0;j<5;j++)
{
if(i==0||i==3||j==0||j==4)
{
s=s+b[i][j];
}
}
}
cout<<s<<endl;
}

void Array::print()
{
for(int i=0;i<4;i++)
{
for(int j=0;j<5;j++)
{
cout<<b[i][j]<<' ';
}
cout<<endl;
}
}

int main()
{
int t[4][5]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
Array array(t);
array.print();
array.sum();
array.print();
return 0;
}

3.问题描述如下:定义一个字符串类String,将字符串b拼接到字符串a中,(不能用拼接函数)。具体要求如下:
(1)私有数据成员
char a[100],b[100];
(2)公有成员函数
String(char c[],char d[]):构造函数,用参数初始化数据成员
void fun():功能函数,实现将b拼接到a的后面
void insert(int m);实现将字符数组b中的字符串插入到字符数组a中的第m个字符的位置。
void show():功能函数,输出数据成员
(3)用数据"123456789","abc"在主函数中对类测试。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <string.h>
using namespace std;

char str1[100],str2[100];
int i;

class String
{
public:
String(char c[],char d[]);
void fun();
void insert(int m);
void show();
private:
char a[100],b[100];
};

String::String(char c[],char d[])
{
for(int i=0;i<strlen(c)+1;i++)
{
a[i]=c[i];
}
for(int i=0;i<strlen(d)+1;i++)
{
b[i]=d[i];
}
}

void String::fun()
{
char str3[10000]={0};
int j=0,i,k;
for(i=0;i<strlen(a);i++,j++)
{
str3[j]=a[i];
}
for(k=0;k<strlen(b)+1;k++,j++)
{
str3[j]=b[k];
}
a[j]='\0';
for(int f=0;f<j;f++)
{
a[f]=str3[f];
}
cout<<"fun done"<<endl;
}

void String::insert(int m)
{
char str3[100]={0};
int j,k,md,h;
h=strlen(a);
for(j=0;j<=m-2;j++)
{
str3[j]=a[j];
}
for(md=0;b[md]!='\0';j++,md++)
{
str3[j]=b[md];
}
for(k=h-m-2;a[k]!='\0';j++,k++)
{
str3[j]=a[k];
}
str3[j]='\0';
for(int i=0;i<strlen(str3);i++)
{
a[i]=str3[i];
}
cout<<"insert done and result:"<<endl;
}

void String::show()
{
for(int i=0;i<strlen(a)+1;i++)
{
cout<<a[i];
}
cout<<endl;
}

int main()
{
cin.getline(str1,80);
cin.getline(str2,80);
String str(str1,str2);
str.show();
str.fun();
str.show();
cin>>i;
str.insert(i);
str.show();
return 0;
}

4.问题描述如下:定义一个字符串类String,为指针变量p动态申请空间,并初始化。对p所指向的字符串进行逆序操作。具体要求如下:
(1)私有数据成员
 char *p;
(2)公有成员函数
 String(char *t):构造函数,用参数初始化数据成员
 void fun():功能函数,实现对字符串进行逆序。
 void show():功能函数, 输出数据成员
 ~String();析构函数,撤销对象。
(3)用数据"123456789"在主函数中对类测试。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
using namespace std;

string s1;
int num;

class String
{
public:
String(char *t);
~String();
void fun();
void show();
private:
char *p;
};

String::String(char *t)
{
p=t;
}

String::~String()
{
delete []p;
}

void String::fun()
{
num=s1.length();
int temp=*(p+0);
*p=*(p+num);
*(p+0)=temp;
}

void String::show()
{
for(int i=num-1;i>=0;i--)
{
cout<<*(p+i)<<' ';
}
cout<<endl;
}

int main()
{
cin>>s1;输入
char *t=&s1[0];可以直接赋值char s1[10000]=”???????”那么上面一句就不要了
String s(t);
s.fun();
s.show();
return 0;
}

5.问题描述如下:定义一个类名为Array,为指针变量p动态申请空间,并初始化。对p所指向的一维数组逆序排序。具体要求如下:
(1)私有数据成员
 int *p;
(2)公有成员函数
 Array(int *t):构造函数,用参数初始化数据成员
 void fun():功能函数,实现对一维数组进行逆序。
 void show():功能函数, 输出数据成员
 ~Array();析构函数,撤销对象。
(3)在主函数中对类测试。例如int a[10]={1,2,3,4,5,6,7,8,9,0};

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using namespace std;

class Array
{
private:
int *p;
public:
Array(int *t);
void fun();
void show();
~Array();
};

Array::Array(int *t)
{
p=t;
}

void Array::fun()
{
int temp=*(p+0);
*p=*(p+9);
*(p+0)=temp;
}

void Array::show()
{
for(int i=9;i>=0;i--)
{
cout<<*(p+i)<<' ';
}
cout<<endl;
}

Array::~Array()
{
delete []p;
}

int main()
{
int a[10]={1,2,3,4,5,6,7,8,9,0};
Array arr(a);
arr.fun();
arr.show();
return 0;
}

6.问题描述如下:定义一个类名为Array,为指针变量p动态申请空间,并初始化。具体要求如下:
(1)私有数据成员
 char *p;int s;//s用来存放合成的数
(2)公有成员函数
 Array(char *t):构造函数,用参数初始化数据成员
 void fun():功能函数,实现对一个字符串中的数字字符,组合成一个整数。
 void show():功能函数, 输出数据成员
 ~Array();析构函数,撤销对象。
(3)在主函数中对类测试。(如输入”a2bc3de4,输出234)。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
using namespace std;

string str1;

class Array
{
private:
char *p;
int s;
public:
Array(char *t);
void fun();
void show();
~Array();
};

Array::Array(char *t)
{
p=t;
s=0;
}

void Array::fun()
{
int temp;
int num=str1.length();
for(int i=0;i<num;i++)
{
if(*(p+i)>='0'&&*(p+i)<='9')
{
temp=*(p+i)-'0';
s=s*10+temp;
}
}
}

void Array::show()
{
cout<<s<<endl;
}

Array::~Array()
{
delete []p;
}

int main()
{
cin>>str1;
char *t=&str1[0];
Array arr(t);
arr.fun();
arr.show();
return 0;
}

7.试定义一个类Array,将二维数组的各行按其所有元素的和从小到大进行行排序,各行内元素之间的顺序保持不变。具体要求如下:
(1)私有数据成员
 int a[4][4]; 原始数组。
(2)公有成员函数
 Array(int k[][4],int n); 构造函数,用数组k的前n行元素初始化数组a。
 int sum(int b[4][4],int k); 求数组b中第k行元素的和。
 void exch(int b[4][4],int x,int y); 交换数组b的第x行与第y行。
 void fun( ); 按题意对数组进行排序。
 void print( ); 输出数组。
(3)在主函数中对该类进行测试。

输入示例:

原数组:

1
2
3
4
5      5	  5	    5
4 4 4 4
3 3 3 3
2 2 2 2

排序后的数组:

1
2
3
4
2      2	  2	    2
3 3 3 3
4 4 4 4
5 5 5 5

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
using namespace std;

class Array
{
private:
int a[4][4];
public:
Array(int k[][4],int n);
int sum(int b[4][4],int k);
void exch(int b[4][4],int x,int y);
void fun();
void print();
};

Array::Array(int k[][4],int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<4;j++)
{
a[i][j]=k[i][j];
}
}
}

int Array::sum(int b[4][4],int k)
{
int num=0;
for(int i=0;i<4;i++)
{
if(i==k-1)
{
for(int j=0;j<4;j++)
{
num=num+b[i][j];
}
}
else
continue;
}
return num;
}

void Array::exch(int b[4][4],int x,int y)
{
//约定x<y;
int temp[4];
int c=x-1,d=y-1;
for(int i=0;i<4;i++)
{
if(i==c)
{
for(int j=0;j<4;j++)
{
temp[j]=b[c][j];
}
}
if(i==d)
{
for(int j=0;j<4;j++)
{
b[c][j]=b[d][j];
b[d][j]=temp[j];
cout<<"第"<<i<<"and"<<y<<"changed"<<endl;
}
}
}
}

void Array::fun()
{
for(int i=1;i<=3;i++)
{
for(int j=i+1;j<=4;j++)
{
if(sum(a,i)>sum(a,j))
{
exch(a,i,j);
}
}
}
}

//输出数组内容,四个一行
void Array::print()
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
cout<<a[i][j]<<' ';
}
cout<<endl;
}
}

int main()
{
int k[4][4]={5,5,5,5,4,4,4,4,3,3,3,3,2,2,2,2};
int m[5][4]={20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
Array arr1(k,4);
Array arr2(m,4);
arr1.print();
cout<<endl;
arr2.print();
cout<<endl;
arr1.fun();
cout<<endl;
arr2.fun();
cout<<endl;
arr1.print();
cout<<endl;
arr2.print();
cout<<endl;
return 0;
}

8.继承与派生:定义一个圆类circle作为基类,公有派生出圆柱体的类colum,求出圆柱体的体积。具体要求如下:
(1)类circle
 保护数据成员: float r; //圆的半径
 公有构造函数: circle(float); //初始化r
 公有成员函数: float getr(); //取圆的半径r
        double area(); //求圆的面积
(2)类colum
 保护数据成员: float h; //圆柱体的高
 公有构造函数: colum(float,float); //初始化r和h
 公有成员函数: float geth(); //取圆柱体的高h
        double vol (); //求圆柱体的体积
(3)编写一个程序测试该类,测试数据及要求如下:
 圆柱体对象:v(1.0,3.0);
 本程序的执行结果如下:
  圆柱体属性数据:
   半径:1米
   高度:3米
   面积:3.14平方米
   体积:9.42立方米

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#define pi 3.1415926
using namespace std;

class circle
{
protected:
float r;
public:
float getr();
double area();
circle(float a);
};

circle::circle(float a)
{
r=a;
}

float circle::getr()
{
return r;
}

//计算面积
double circle::area()
{
return pi*r*r;
}

class colum: public circle
{
protected:
float h;
public:
colum(float ri,float high);
float geth();
double vol();
};

colum::colum(float ri,float high):circle(ri)
{
h=high;
}

float colum::geth()
{
return h;
}

//计算体积
double colum::vol()
{
return pi*r*r*h;
}

int main()
{
colum v(1.0,3.0);
cout<<"半径:"<<v.getr()<<"米"<<endl;
cout<<"高度:"<<v.geth()<<"米"<<endl;
cout<<"面积:"<<v.area()<<"平方米"<<endl;
cout<<"体积:"<<v.vol()<<"立方米"<<endl;
return 0;
}

所有代码下载:


江苏科技大学苏州理工学院《计算机程序设计基础实践VC++》实验报告
https://feater.top/book/report-of-vcpp-cjst/
作者
JackeyLea
发布于
2021年10月10日
许可协议