C++-重载运算符-矩阵

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<string>
 4 #include<cstring>
 5 #include<cmath>
 6 using namespace std;
 7 class Matrix
 8 {
 9 private:
10     int s;
11     int arr[110][110];
12 public:
13     Matrix(int);
14     Matrix operator+(Matrix &it);
15     Matrix operator-(Matrix &it);
16     Matrix operator*(Matrix &it);
17     friend istream & operator>>(istream &is, Matrix &it);
18     friend ostream & operator<<(ostream &os,const Matrix &it);
19 };
20 Matrix::Matrix(int n=0)
21 {
22     s = n;
23 }
24 Matrix Matrix::operator+(Matrix &it)
25 {
26     Matrix ans(s);
27     for (int i = 1; i <= it.s; i++)
28         for (int j = 1; j <= it.s; j++)
29             ans.arr[i][j] = arr[i][j] + it.arr[i][j];
30     return ans;
31 }
32 Matrix Matrix::operator-(Matrix &it)
33 {
34     Matrix ans(s);
35     for (int i = 1; i <= it.s; i++)
36         for (int j = 1; j <= it.s; j++)
37             ans.arr[i][j] = arr[i][j] - it.arr[i][j];
38     return ans;
39 }
40 Matrix Matrix::operator*(Matrix &it)
41 {
42     Matrix ans(s);
43     for (int i = 1; i <= it.s; i++)
44         for (int j = 1; j <= it.s; j++)
45         {
46             ans.arr[i][j] = 0;
47             for (int k = 1; k <= it.s; k++)
48                 ans.arr[i][j] += arr[i][k] * it.arr[k][j];
49         }
50     return ans;
51 }
52 istream & operator>>(istream &is, Matrix &it)
53 {
54     for (int i = 1; i <= it.s; i++)
55         for (int j = 1; j <= it.s; j++)
56             is >> it.arr[i][j];
57     return is;
58 }
59 ostream & operator<<(ostream &os,const Matrix &it)
60 {
61     for (int i = 1; i <= it.s; i++)
62     {
63         for (int j = 1; j <= it.s; j++)
64             os << it.arr[i][j] << " ";
65         cout << endl;
66     }
67     return os;
68 }
69 int main()
70 {
71     int n; cin >> n;
72     Matrix a(n), b(n);
73     cin >> a >> b;
74     cout << a*b;
75     system("pause");
76     return 0;
77 }

重复造了个矩阵加减乘的轮子,真是累

时间: 2024-08-09 05:43:40

C++-重载运算符-矩阵的相关文章

矩阵求和--重载运算符

C++习题 矩阵求和--重载运算符 [Submit][Status][Web Board] Description 有两个矩阵a和b,均为2行3列.求两个矩阵之和.重载运算符"+",使之能用于矩阵相加(如c=a+b). 重载流插入运算符"<<"和流提取运算符">>",使之能用于该矩阵的输入和输出. Input 两个2行3列矩阵 Output 矩阵之和 Sample Input 1 2 34 5 67 8 91 2 3 Sa

第十五周oj刷题——Problem M: C++习题 矩阵求和--重载运算符

Description 有两个矩阵a和b,均为2行3列.求两个矩阵之和.重载运算符"+",使之能用于矩阵相加(如c=a+b). 重载流插入运算符"<<"和流提取运算符">>".使之能用于该矩阵的输入和输出. Input 两个2行3列矩阵 Output 矩阵之和 Sample Input 1 2 3 4 5 6 7 8 9 1 2 3 Sample Output 8 10 12 5 7 9 /* All rights res

Python 正确重载运算符

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px Helvetica } 有些事情让我不安,比如运算符重载.我决定不支持运算符重载,这完全是个人选择,因为我见过太多 C++ 程序员滥用它. --James Gosling Java 之父 p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px Helvetica } 运算符重载的作用是让用户定义的对象使用中缀运算符(如 + 和 |)或一元运算

C++学习之重载运算符1

C++除可重载函数之后,还允许定义已有的运算符,这样通过运算符重载可像处理数据使用它们. 先来个代码 1 #include<iostream> 2 using namespace std; 3 4 class num 5 { 6 public: 7 num(){n=1;} 8 ~num(){} 9 int get() const{return n;} 10 void set(int x){n=x;} 11 private: 12 int n; 13 }; 14 15 int main() 16

重载()运算符和重载强制类型转换

// 研究了半宿.最终弄清楚了 // 写了这段測试代码能够非常好的演示效果 class CConvert { public: CConvert(){m_nValue = 10;} // 重载()运算符 int operator ()(); // 重载int强制类型转换 operator int(); protected: private: int m_nValue; }; int CConvert::operator ()() { return m_nValue; } CConvert::ope

C++自学笔记_重载运算符_《C++ Primer》

#include <iostream> #include <stdexcept> using namespace std; class CheckedPtr{ public: CheckedPtr(int *b,int *e,int *c): beg(b),end(e),curr(c){ } CheckedPtr(const CheckedPtr &obj): //复制构造函数 beg(obj.beg),end(obj.end),curr(obj.curr){ } Chec

作为类的成员函数,重载运算符只能有一个参数

1 overload a operator of a class, you can only use one para., this pointer is automatically used. class Rational { public: //not correct since this ponit would be used automatically. //Rational operator+ (const Rational& lhs, const Rational& rhs);

C++习题 复数类--重载运算符+,-,*,/

Description 定义一个复数类Complex,重载运算符"+","-","*","/",使之能用于复数的加.减.乘.除.运算符重载函数作为Complex类的成员函数.编写程序,分别求两个复数之和.差.积和商. Input 两个复数 Output 两个复数之和.差.积和商 Sample Input 3 4 5 -10 Sample Output c1+c2=(8.00,-6.00i) c1-c2=(-2.00,14.00

operator重载运算符

1.重载运算符的函数一般格式如下 函数类型    operator  运算符名称    (形参表列) {对运算符的重载处理} 例如,想将“+”用于Complex(复数)的加法运算,函数的原型可以是这样的: Complex operator + (Complex & c1,Complex &c2); operator+函数表示对运算符+重载.其中,operator是关键字,专门用于定义重载运算符的函数的,运算符名称就是C++提供给用户的预定运算符. 注意:函数名是由operator和运算符组