大二上学期期末C++课程设计

题目一矩阵运算

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include<iostream>
using namespace std;

class Matrix
{
private:
int a[2][2];
int b[2][2];
int result[2][2];

public:
Matrix();
void add();
void subtraction();
void multiplication();
void division();
void transpose();
};
Matrix::Matrix()
{

a[2][2]=0;
b[2][2]=0;
result[2][2]=0;
cout<<"请输入矩阵A:"<<endl;
int i,j;
for (i = 0; i < 2; ++i)
for (j = 0; j < 2; ++j)
{
cin>>a[i][j];

}
cout<<"请输入矩阵B:"<<endl;
for (i = 0; i < 2; ++i)

for (j = 0; j < 2; ++j)
{
cin>>b[i][j];

}
cout<<"你输入的矩阵A为"<<endl;
for (i = 0; i < 2; ++i){
for (j = 0; j < 2; ++j)
{
cout<<a[i][j]<<" ";

}cout<<endl;
}
cout<<endl<<"你输入的矩阵B为"<<endl;
for (i = 0; i < 2; ++i)
{
for (j = 0; j < 2; ++j)
{
cout<<b[i][j]<<" ";

}cout<<endl;
}

}
void Matrix::add()
{
int i,j;
cout<<"相加结果为"<<endl;
for(i=0;i<2;++i)
{
for(j=0;j<2;++j)
{
result[i][j] = a[i][j]+b[i][j];
cout<<result[i][j]<<" ";
}cout<<endl;
}

}
void Matrix::subtraction()
{
int i,j;
cout<<"相加结果为"<<endl;
for(i=0;i<2;++i)
{
for(j=0;j<2;++j)
{
result[i][j] = a[i][j]-b[i][j];
cout<<result[i][j]<<" ";
}cout<<endl;
}

}
void Matrix::multiplication()
{
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
result[i][j] = a[i][j]*b[i][j] + a[i][j]*b[j][i];
cout<<result[i][j]<<" ";
}cout<<endl;
}

}
void Matrix::division()
{

}
void Matrix::transpose()
{
int i,j;
cout<<"A转置为:"<<endl;
for(i=0;i<2;++i)
{
for(j=0;j<2;++j)
{
result[i][j]=a[j][i];
cout<<result[i][j]<<" ";
}cout<<endl;
}

cout<<"B转置为:"<<endl;
for(i=0;i<2;++i)
{
for(j=0;j<2;++j)
{
result[i][j]=b[j][i];
cout<<result[i][j]<<" ";
}cout<<endl;
}

}
int main()
{
int h;
cout<<"-------------------"<<endl;
cout<<": 菜单 "<<endl;
cout<<": 1.加法 "<<endl;
cout<<": 2.减法 "<<endl;
cout<<": 3.乘法 "<<endl;
cout<<": 4.除法 "<<endl;
cout<<": 5.转置 "<<endl;
cout<<": 0.退出 "<<endl;
cout<<"-------------------"<<endl;
cout<<"请输入要进行的操作"<<endl;
cin >>h;
cout<<endl;
switch(h)
{
case 1:
{
Matrix M;
M.add();
break;
}
case 2:
{
Matrix M;
M.subtraction();
break;
}
case 3:
{
Matrix M;
M.multiplication();
break;
}
case 4:
{
Matrix M;
M.division();
break;
}
case 5:
{
Matrix M;
M.transpose();
break;
}
case 0:
{
cout<<"退出成功";
return 0;
}
default: cout<<"输入错误"<<endl;
}
}

题目二 求不同图形面积

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
#include <bits/stdc++.h>
using namespace std;

class Graphic{
public:
string name;
double length;
double tall;
Graphic(string s,double a,double b=0):name(s),length(a),tall(b){}
virtual void show()=0;
};
class Square:public Graphic
{
public:
Square(double a):Graphic("Square",a)
{}
void show(){
cout<<"Square'area:"<<length*length<<endl;
}


// ~Square();
};
class Circle:public Graphic
{
public:
Circle(double a):Graphic("Circle",a){}
void show(){
cout<<"Circle'area:"<<length*length*3.14<<endl;
}
};
class triangle:public Graphic
{
public:
triangle(double a,double b):Graphic("Circle",a,b){}
void show(){
//double i
//i =
cout<<"triangle'area:"<<(length*tall)/2<<endl;
}
};
int main()
{
Graphic *gp;
string type; // 定义图形的类型
double value;
double tall;
cout<<"what'type"<<endl;
cin>>type;
// cout<<"what'value"<<endl;
// cin>>value;
if(type=="Square")
{
cout<<"what'value"<<endl;
cin>>value;
gp = new Square(value);
gp ->show();
}
if(type=="Circle")
{
cout<<"what'value"<<endl;
cin>>value;
gp = new Circle(value);
gp ->show();
}
if(type=="triangle")
{
cout<<"what'value"<<endl;

cin>>value;
cout<<"what'tall"<<endl;
cin>>tall;
gp = new triangle(value,tall);
gp ->show();
}
}

题目三 运算符重载

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
#include<iostream>
using namespace std;
class Complex{
public:
Complex(double r=0.0,double i=0.0);
void print();
friend Complex operator+ (Complex& a,Complex& b);
friend Complex operator- (Complex& a,Complex& b);
friend Complex operator* (Complex& a,Complex& b);
friend Complex operator/ (Complex& a,Complex& b);
private:
double real;
double imag;

};
Complex::Complex(double r,double i)
{
real = r;
imag = i;
}
Complex operator+ (Complex& a,Complex& b)
{
Complex temp;
temp.real = a.real + b.real;
temp.imag = a.imag + a.imag;
return temp;
}
Complex operator- (Complex& a,Complex& b)
{
Complex temp;
temp.real = a.real - b.real;
temp.imag = a.imag - a.imag;
return temp;
}
Complex operator* (Complex& a,Complex& b)
{
Complex temp;
temp.real = a.real * b.real + a.imag * b.imag;
temp.imag = a.real * b.imag + a.imag * b.real;
return temp;
}
Complex operator/ (Complex& a,Complex& b)
{
Complex temp;
double t;
t = 1/(b.real * b.real + b.imag * b.imag);
temp.real = (a.real * b.real + a.imag * b.imag)*t;
temp.imag = (b.real * b.imag + a.imag * b.real)*t;
return temp;
}
void Complex::print()
{
cout<<real;
if(imag>0)
cout<<"+";
if(imag!=0)
cout<<imag<<'i'<<endl;
}
int main()
{
Complex A1(2.3,4.6),A2(3.6,2.8),A3,A4,A5,A6;
A3 = A1 + A2;
A4 = A1 - A2;
A5 = A1 * A2;
A6 = A1 / A2;
A1.print();
A2.print();
A3.print();
A4.print();
A5.print();
A6.print();
return 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

int num; //总学生人数

class student {
friend class studentMessage; // 友员函数
private:
string ID;
string name;
double score[5]; //1-4代表math、English、OPP和总成绩 ,0号单元空出
student * next;
public:
student();
string getName(){return name;}
string getID(){return ID;}
double getMath(){return score[1];}
double getEnglesh(){return score[2];}
double getOPP(){return score[3];}
double gettotol(){return score[4];}
student* getNext(){return next;}

static void analyse(int ,student * ); //分析成绩
void display(); //打印成绩
void swap(); //交换对象和对象->next的信息
};

student::student()
{
cout<<"请输入学生学号 : ";cin>>ID;
cout<<"请输入学生姓名 : ";cin>>name;
cout<<"请输入学生的数学、英语和OPP成绩 : "<<endl;
int a,b,c;
cin>>a;
while(a<0||a>100)
{
cout<<"输入成绩有误,请重新输入: "<<endl;
cin>>a;
}
cin>>b;
while(b<0||b>100)
{
cout<<"输入成绩有误,请重新输入: "<<endl;
cin>>b;
}
cin>>c;
while(c<0||c>100)
{
cout<<"输入成绩有误,请重新输入: "<<endl;
cin>>c;
}
score[1]=a;
score[2]=b;
score[3]=c;
score[4]=a+b+c;
next=NULL;
}

void student::analyse(int a,student * first)
{
int you=0,liang=0,zhong=0,jige=0,bujige=0;
student * t=first;
double average=0; //平均分
while(t)
{
int b = int(t->score[a]/10);
switch(b)
{
case 10:
case 9:you++;break;
case 8:liang++;break;
case 7:zhong++;break;
case 6:jige++;break;
default:bujige++;
}
average+=t->score[a];
t=t->next;
}
average=average/num;
cout<<" 考生总人数 : "<<num<<" 平均分 : "<<average<<endl;
cout<<"\n 优 良 中 及格 不及格 "<<endl;
cout<<" "<<you<<" \t"<<liang<<" \t"<<zhong<<"\t"<<jige<<"\t"<<bujige<<endl;
}

void student::display()
{
cout<<" "<<setw(3)<<ID<<" "<<setw(3)<<name<<" ";
for (int i=1;i<=4;i++)
{
cout<<setw(3)<<score[i]<<" ";
}
cout<<endl;
}

void student::swap()
{
int j;
string na,id;
double sco[5];
na=name;name=next->name;next->name=na;
id=ID;ID=next->ID;next->ID=id;
for(j=1;j<=4;j++)
{
sco[j]=score[j];
score[j]=next->score[j];
next->score[j]=sco[j];
}
}

class studentMessage{
private:
student * first;
student * last;
public:
studentMessage();
student* getFirst(){return first;}
student* getLast(){return last;}
int getNum(){return num;}
void add(); //向表中增加学生成绩记录
void search(); //在表中查找学生成绩记录
void del(); //在表中删除学生成绩记录
void showOne(); //分析一门课的成绩
void showAll(); //输出所有同学所有科目的成绩
void sort(); //按一门课成绩或总成绩排序
void clear(); //清空所有信息并退出系统

};

studentMessage::studentMessage()
{
first=NULL;
last=NULL;
num=0;
}

void studentMessage::add()
{
student * t =new student;
student *p = first;
while(p){
if(p->ID==t->ID)
{
cout<<"\n学号输入错误或该学生成绩已经存在!(如需修改,请先删除再重新录入)"<<endl;
return;
}
p=p->next;
}
num++;
if(first==NULL)
{
first=last=t;

}
else
{
last->next=t;
last=last->next;
}
}

void studentMessage::search()
{
string a;
cout<<"\n请输入要查找的学生的学号:";cin>>a;
student *t = first;
while(t){
if(t->ID==a)
break;
t=t->next;
}
if(!t)
{
cout<<"\n未找到要查找学生!"<<endl;
return;
}
cout<<"\n查找成功!"<<endl;
cout << " 学号 姓名 数学 英语 OPP 总成绩" << endl;
t->display();
}

void studentMessage::del()
{
string a;
cout<<"\n请输入要删除的学生的学号: ";cin>>a;
student *t = first;
student *p=NULL;
while(t){
if(t->ID==a)
break;
p=t;
t=t->next;
}
if(!t)
{
cout<<"\n未找到要删除学生!"<<endl;
return;
}
if(!p)
{
first=first->next;
cout<<"\n成功删除学生"<<a<<endl;
delete t;
}
else
{
p->next=t->next;
cout<<"成功删除学生"<<a<<endl;
delete t;
}

num--;
}

void studentMessage::showOne()
{
int a;
while(1)
{
cout<<"\n想要分析哪一门成绩?请输入相应序号(1:math 2:English 3:OPP): ";cin>>a;
if(a!=1&&a!=2&&a!=3)
cout<<"\n输入序号有误,请重新输入!"<<endl;
else
break;
}
cout<<"\n\t成绩分析如下: \n"<<endl;
student::analyse(a,first);
}

void studentMessage::showAll()
{
cout << "---------------------成绩列表----------------------"<< endl;
cout << " 学号 姓名 数学 英语 OPP 总成绩" << endl;
student *t = first;
while(t){
t->display();
t=t->next;
}
}

void studentMessage::sort()
{
int a,n=0; //n--排名
cout<<"想要按照什么成绩排序?请输入相应序号(1.数学 2.英语 3.OPP 4.总成绩): ";
while(1)
{
cin>>a;
if (a!=1&&a!=2&&a!=3&&a!=4)
cout<<"\n输入序号有误,请重新输入 : ";
else
break;
}
student *t=first;
student *p=t;
while(p->next)
{
t=first;
while(t->next)
{
if(t->next->score[a]>=t->score[a])
{
t->swap();
}
t=t->next;
}
p=p->next;
}
t = first;
cout<<"\n\t 成绩表如下: "<<endl;
cout<<"学号 姓名 成绩 排名"<<endl;
while(t){
n++;
cout<<" "<<t->getID()<<" \t"<<t->getName()<<" \t";
switch(a)
{
case 1 :cout<<t->getMath()<<" \t"<<n<<endl;break;
case 2 :cout<<t->getEnglesh()<<" \t"<<n<<endl;break;
case 3 :cout<<t->getOPP()<<" \t"<<n<<endl;break;
case 4 :cout<<t->gettotol()<<" \t"<<n<<endl;break;
}
t=t->next;
}
}

void studentMessage::clear()
{
int x;
cout<<"确认要清空所有信息?请输入1或2 (1:确认 2:取消) : ";cin>>x;
if (x==2)
{
cout<<"\n取消清空\n"<<endl;
return;
}
student *t = first;
student *p;
while(t){
p=t;
t=t->next;
delete p;
}
cout<<"\n清空成功!\n";
cout<<"退出成功!\n";
}

void showMenu()
{
cout << " \n";
cout << "===============================\n";
cout << " 学生成绩管理系统\n\n";
cout << " 1.显示所有学生成绩\n";
cout << " 2.添加信息\n";
cout << " 3.查询信息\n";
cout << " 4.删除信息\n";
cout << " 5.成绩排序\n";
cout << " 6.单科成绩分析\n";
cout << " 7.清空所有信息并退出系统\n";
cout << " 0.退出系统\n";
cout << "===============================\n";
cout << " \n";
}

int main()
{
int h;
studentMessage stuM=studentMessage();
while(1)
{
showMenu();
cout << "请输入操作对应的序号 : ";
cin >>h;
cout<<endl;
switch(h)
{
case 1: stuM.showAll(); break;
case 2: stuM.add(); break;
case 3: stuM.search(); break;
case 4: stuM.del(); break;
case 5: stuM.sort(); break;
case 6: stuM.showOne(); break;
case 7: stuM.clear(); return 0;
case 0: cout<<"\n退出成功!";return 0;
default:cout<<"\n输入序号有误!请输入正确的序号。"<<endl;
}
}
}