-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic1.cpp
72 lines (69 loc) · 1.15 KB
/
basic1.cpp
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
#include<iostream>
using namespace std;
int** init();
void input(int** p);
void output(int** A);
void add(int** A1,int** A2,int** A3);
void sub(int** A1,int** A2,int** A3);
void deletematrix(int** p);
int main()
{
int** A1;
int** A2;
int** A3=init();
A1=init();
input(A1);
A2=init();
input(A2);
add(A1,A2,A3);
cout << "A1+A2的结果是:" <<endl;
output(A3);
sub(A1,A2,A3);
cout << "A1-A2的结果是:" <<endl;
output(A3);
deletematrix(A1);
deletematrix(A2);
deletematrix(A3);
return 0;
}
int** init()
{
int** p=new int*[4];
for(int i=0;i<4;i++)
p[i]=new int[5];
return p;
}
void input(int** p)
{
cout<< "请输入矩阵元素" <<endl;
for(int i=0;i<4;i++)
for(int j=0;j<5;j++)
cin >> p[i][j];
}
void output(int** A)
{
for(int i=0;i<4;i++)
{
for(int j=0;j<5;j++)
cout << A[i][j] << " ";
cout << endl;
}
}
void add(int** A1,int** A2,int** A3)
{
for(int i=0;i<4;i++)
for(int j=0;j<5;j++)
A3[i][j]=A1[i][j]+A2[i][j];
}
void sub(int** A1,int** A2,int** A3)
{
for(int i=0;i<4;i++)
for(int j=0;j<5;j++)
A3[i][j]=A1[i][j]-A2[i][j];
}
void deletematrix(int** p)
{
for(int i=0;i<4;i++)
delete []p[i];
delete []p;
}