第19次实验


 1 #include<iostream>
 2 #include<iomanip>
 3 using namespace std;
 4 class Matrix
 5 {
 6 int a[2][3];
 7 public:
 8 Matrix(){};
 9 void setMatrixa();
10 void setMatrixb();
11 void getMatrix();
12 friend Matrix operator+(Matrix& t,Matrix temp);
13 };
14 void Matrix::setMatrixa()
15 {
16 cout<<"Please input 6 integers:"<<endl;
17 for(int i=0;i<2;i++)
18 for(int j=0;j<3;j++)
19 cin>>a[i][j];
20 }
21 void Matrix::setMatrixb()
22 {
23 cout<<"Please input 6 integers:"<<endl;
24 for(int i=0;i<2;i++)
25 for(int j=0;j<3;j++)
26 cin>>a[i][j];
27 }
28 Matrix operator+(Matrix&t,Matrix s)
29 {
30 Matrix temp;
31 for(int i=0;i<2;i++)
32 for(int j=0;j<3;j++)
33 temp.a[i][j]=t.a[i][j]+s.a[i][j];
34 return temp;
35 }
36 void Matrix::getMatrix()
37 {
38 for(int i=0;i<2;i++)
39 {for(int j=0;j<3;j++)
40 cout<<setw(6)<<a[i][j];
41 cout<<endl;}
42 }
43 int main()
44 {
45 Matrix m,n,o;
46 m.setMatrixa();
47 cout<<"The Matrixa is:"<<endl;
48 m.getMatrix();
49 n.setMatrixb();
50 cout<<"The Matrixb is:"<<endl;
51 n.getMatrix();
52 o=m+n;
53 cout<<"The add of Matrix is:"<<endl;
54 o.getMatrix();
55 system("pause");
56 }

 

 1 //有2个矩阵a和b,均为3行3列, 求2个矩阵之积。重载运算符“*”,使之能用于矩阵相乘,如c=a*b。运算符重载函数作为类的成员函数。(最后一题有修改)
 2 #include<iostream>
 3 #include<iomanip>
 4 using namespace std;
 5 class Matrix
 6 {
 7     int a[3][3];
 8 public:
 9     Matrix(){};
10     void setMatrixa();
11     void setMatrixb();
12     void getMatrix();
13     friend Matrix operator*(Matrix& t,Matrix s);
14 };
15 void Matrix::setMatrixa()
16 {
17     cout<<"Please input 9 integers:"<<endl;
18     for(int i=0;i<3;i++)
19         for(int j=0;j<3;j++)
20         cin>>a[i][j];
21 }
22 void Matrix::setMatrixb()
23 {
24         cout<<"Please input 9 integers:"<<endl;
25         for(int i=0;i<3;i++)
26         for(int j=0;j<3;j++)
27             cin>>a[i][j];
28 }
29 Matrix operator*(Matrix&t,Matrix s)//特别注意,矩阵算法
30 {
31     Matrix temp;
32     for(int i=0;i<3;i++)
33         for(int j=0;j<3;j++)
34             {   temp.a[i][j]=0;
35                 for(int k=0;k<3;k++)
36             temp.a[i][j]=t.a[i][k]*s.a[k][j]+temp.a[i][j];
37             }
38         return temp;
39 }
40 void Matrix::getMatrix()
41 {
42     for(int i=0;i<3;i++)
43         {for(int j=0;j<3;j++)
44         cout<<setw(6)<<a[i][j];
45         cout<<endl;}
46 }
47 int main()
48 {
49     Matrix m,n,o;
50     m.setMatrixa();
51     cout<<"The Matrixa is:"<<endl;
52     m.getMatrix();
53     n.setMatrixb();
54     cout<<"The Matrixb is:"<<endl;
55     n.getMatrix();
56     o=m*n;
57     cout<<"The add of Matrix is:"<<endl;
58     o.getMatrix();
59     system("pause");
60 }

 

第19次实验

课外务必阅读(谭浩强:第8章、第9章)、看完钱能教材第8章、第9

本次实验较简单

并将老师提供的第8章所有课件复习、并掌握,预习第9章课件

1. 定义一个复数类Complex, 重载运算符“+”、“-”、“*”、“/”、使之能够用于复数的加、减、乘、除。运算符重载函数作为类的友元函数。编程序,分别求两个复数之和、差、积、商。

2. 定义一个复数类Complex, 重载运算符“+”、“-”、“*”、“/”、使之能够用于复数的加、减、乘、除。运算符重载函数作为类的成员函数。编程序,分别求两个复数之和、差、积、商。

3. 有2个矩阵a和b,均为2行3列, 求2个矩阵之和。重载运算符“+”,使之能用于矩阵相加,如c=a+b。运算符重载函数作为类的友元函数。

4. 有2个矩阵a和b,均为3行3列, 求2个矩阵之积。重载运算符“*”,使之能用于矩阵相乘,如c=a*b。运算符重载函数作为类的成员函数。(最后一题有修改)

 

 

提交至21650478@qq.com

邮件题目务必命名为:19_你的名字

邮件附件中提交:程序源文件,运行结果截图(如:19_1.cpp, 19_1.jpg)

请每个同学都提交,且独立完成,由于老师每天收到的邮件非常多,请一定严格命名

 

 第二题:

 1 /* 定义一个复数类Complex, 重载运算符“+”、“-”、“*”、“/”、使之能够用于复数的加、减、乘、除。
 2 运算符重载函数作为类的成员函数。编程序,分别求两个复数之和、差、积、商。*/
 3 #include<iostream>
 4 using namespace std;
 5 class Complex
 6 {
 7 int x;
 8 int y;
 9 public:
10 Complex(){};
11 Complex(int x1,int y1)
12 {
13 x=x1;
14 y=y1;
15 }
16 Complex operator+(Complex c)
17 {
18 Complex tmp;
19 tmp.x=x+c.x;
20 tmp.y=y+c.y;
21 return tmp;
22 }
23 Complex operator-(Complex c)
24 {
25 Complex tmp;
26 tmp.x=x-c.x;
27 tmp.y=y-c.y;
28 return tmp;
29 }
30 Complex operator*(Complex c)
31 {
32 Complex tmp;
33 tmp.x=x*c.x;
34 tmp.y=y*c.y;
35 return tmp;
36 }
37 Complex operator/(Complex c)
38 {
39 Complex tmp;
40 tmp.x=x/c.x;
41 tmp.y=y/c.y;
42 return tmp;
43 }
44 void display()
45 {
46 cout<<"("<<x<<","<<y<<")"<<endl;
47 }
48 };
49 void main()
50 {
51 Complex c1(1,2),c2(3,4),c3,c4,c5,c6;
52 c3=c1+c2;
53 c4=c1-c2;
54 c5=c1*c2;
55 c6=c1/c2;
56 cout<<"c1+c2=";
57 c3.display();
58 cout<<"c1-c2=";
59 c4.display();
60 cout<<"c1*c2=";
61 c5.display();
62 cout<<"c1/c2=";
63 c6.display();
64 system("pause");
65 }

 

第二题:

 1 /* 定义一个复数类Complex, 重载运算符“+”、“-”、“*”、“/”、使之能够用于复数的加、减、乘、除。
 2 运算符重载函数作为类的友员函数。编程序,分别求两个复数之和、差、积、商。*/
 3 #include<iostream>
 4 using namespace std;
 5 class Complex
 6 {
 7 int x;
 8 int y;
 9 public:
10 Complex(){};
11 Complex(int x1,int y1)
12 {
13 x=x1;
14 y=y1;
15 }
16 friend Complex operator+(Complex c1,Complex c2)//友元方式
17 {
18 Complex tmp;
19 tmp.x=c1.x+c2.x;
20 tmp.y=c1.y+c2.y;
21 return tmp;
22 }
23 friend Complex operator-(Complex c1,Complex c2)
24 {
25 Complex tmp;
26 tmp.x=c1.x-c2.x;
27 tmp.y=c1.y-c2.y;
28 return tmp;
29 }
30 friend Complex operator*(Complex c1,Complex c2)
31 {
32 Complex tmp;
33 tmp.x=c1.x*c2.x;
34 tmp.y=c1.y*c2.y;
35 return tmp;
36 }
37 friend Complex operator/(Complex c1,Complex c2)
38 {
39 Complex tmp;
40 tmp.x=c1.x/c2.x;
41 tmp.y=c1.y/c2.y;
42 return tmp;
43 }
44 void display()
45 {
46 cout<<"("<<x<<","<<y<<")"<<endl;
47 }
48 };
49 void main()
50 {
51 Complex c1(1,2),c2(3,4),c3,c4,c5,c6;
52 c3=c1+c2;
53 c4=c1-c2;
54 c5=c1*c2;
55 c6=c1/c2;
56 cout<<"c1+c2=";
57 c3.display();
58 cout<<"c1-c2=";
59 c4.display();
60 cout<<"c1*c2=";
61 c5.display();
62 cout<<"c1/c2=";
63 c6.display();
64 system("pause");
65 }

 

posted @ 2013-05-23 12:22  herizai  阅读(190)  评论(0编辑  收藏  举报