矩阵计算器

来源:互联网 发布:阿里域名服务器 编辑:程序博客网 时间:2024/06/08 19:24

矩阵计算器实现

能够实现从文件或控制台中读入相应的矩阵,可以在控制台输出显示相应的矩阵。

 能够完成矩阵的一般计算(两个矩阵相加、相减、相乘) 能够完成矩阵的除法、求逆计算 能够完成矩阵的多个运算符的混合计算。 

使用堆栈的思想完成运算命令的处理。

#include "stdafx.h"#include <iostream>#include <iomanip>#include <fstream>#define DEFAULT_WIDTH 5#define EPSILON 0.0001#define MAX_MATRIX 10using namespace std;class Matrix{double *arr;int ln, col;public:Matrix() : arr(nullptr), ln(0), col(0) {}Matrix(const int L, const int C) : arr(L * C == 0 ? nullptr : new double[L * C]), ln(L), col(C) {}Matrix(const Matrix &m) : ln(m.ln), col(m.col){int size = ln * col;arr = size == 0 ? nullptr : new double[size];for (int i = 0; i < size; ++i) arr[i] = m.arr[i];}Matrix(istream &is){is >> ln >> col;int size = ln * col;arr = size == 0 ? nullptr : new double[size];for (int i = 0; i < size; ++i) is >> arr[i];}~Matrix(){delete[] arr;}double &operator ()(int, int) const;Matrix operator +(const Matrix &) const;Matrix operator -(const Matrix &) const;Matrix operator *(const Matrix &) const;Matrix operator /(const Matrix &) const;Matrix inverse() const;double det() const;friend istream &operator >>(istream &, Matrix &);friend ostream &operator <<(ostream &, Matrix &);};int width = DEFAULT_WIDTH;Matrix matrix_error(const char *err_msg){cout << "Matrix error: " << err_msg << endl;return{};}double &element_error(const char *err_msg){double ans = 0;cout << "Element error: " << err_msg << endl;return ans;}double &Matrix::operator()(const int l, const int c) const{if (!this->arr) return element_error("empty matrix");if (l >= ln) return this->operator()(this->ln - 1, this->col - 1);if (l < 0) return this->operator()(0, 0);if (c >= col) return this->operator()(l, this->col - 1);if (c < 0) return this->operator()(l, 0);return this->arr[l * col + c];}Matrix Matrix::operator +(const Matrix &m) const{if (this->ln != m.ln || this->col != m.col) return matrix_error("invalid addition operation");Matrix ans(this->ln, this->col);int size = this->ln * this->col;for (int i = 0; i < size; ++i) ans.arr[i] = this->arr[i] + m.arr[i];return ans;}Matrix Matrix::operator -(const Matrix &m) const{if (this->ln != m.ln || this->col != m.col) return matrix_error("invalid subtraction operation");Matrix ans(this->ln, this->col);int size = this->ln * this->col;for (int i = 0; i < size; ++i) ans.arr[i] = this->arr[i] - m.arr[i];return ans;}Matrix Matrix::operator *(const Matrix &m) const{if (this->col != m.ln) return matrix_error("invalid multiplication operation");Matrix ans(this->ln, m.col);int s = 0;for (int i = 0; i < this->ln; ++i) for (int j = 0; j < m.col; ++j){ans.arr[s] = 0;for (int k = 0; k < this->col; ++k) ans.arr[s] += this->operator()(i, k) * m(k, j);++s;}return ans;}Matrix Matrix::operator/(const Matrix &m) const {return *this * m.inverse();}Matrix Matrix::inverse() const{if (this->ln != this->col) return matrix_error("invalid inverse of matrix");if (!this->ln) return matrix_error("empty matrix");double d = this->det();if (abs(d) <= EPSILON) return matrix_error("matrix with zero determinant is not invertible");Matrix ans(this->ln, this->col);for (int i = 0; i < ans.ln; ++i) for (int j = 0; j < ans.col; ++j){Matrix temp(this->ln - 1, this->col - 1);int s = 0, ts = 0;for (int ti = 0; ti < this->ln; ++ti) for (int tj = 0; tj < this->col; ++tj){if (ti != i && tj != j) temp.arr[ts++] = this->arr[s];++s;}ans(j, i) = (i + j) % 2 ? -(temp.det() / d) : temp.det() / d;}return ans;}double Matrix::det() const{if (this->ln != this->col) return element_error("invalid determinant of matrix");if (!this->ln) return element_error("empty matrix");if (this->ln == 1) return this->arr[0];Matrix temp(*this);double ans = temp.arr[0];for (int i = 1; i < temp.ln; ++i){for (int j = 0; j < i; ++j){const double c = temp(i, j) / temp(j, j);temp(i, j) = 0;for (int k = j + 1; k <= i; ++k) temp(i, k) -= temp(j, k) * c;}ans *= temp(i, i);if (!ans) return 0;}return ans;}istream &operator >>(istream &is, Matrix &m){if (m.arr) delete[] m.arr;is >> m.ln >> m.col;int size = m.ln * m.col;m.arr = size ? new double[size] : nullptr;for (int i = 0; i < size; ++i) is >> m.arr[i];return is;}ostream &operator <<(ostream &os, Matrix &m){int s = 0;for (int i = 0; i < m.ln; ++i){for (int j = 0; j < m.col; ++j) os << setw(width) << m.arr[s++];os << endl;}return os;}Matrix M_Arr[MAX_MATRIX];Matrix str2mat(char *head, char *tail){if (head + 1 != tail) return matrix_error("invalid identifer in expression");char ch = *head;if (ch >= 'a' && ch <= 'a' + MAX_MATRIX) ch -= 'a';else if (ch >= 'A' && ch <= 'A' + MAX_MATRIX) ch -= 'A';else return matrix_error("invalid identifer in expression");return M_Arr[ch];}Matrix calculate(char *head, char *tail){while (*head == ' ') ++head;while (*(tail - 1) == ' ' && tail != head) --tail;if (head == tail) return matrix_error("invalid operator in expression");int bracket = 0;char *itr = tail;while (itr != head){char ch = *--itr;if (ch == ')') ++bracket;else if (ch == '(') --bracket;if (bracket < 0) return matrix_error("brackets don't match");if (bracket) continue;break;}if (bracket) return matrix_error("brackets don't match");if (itr == head && itr + 1 != tail) return calculate(head + 1, tail - 1);itr = tail;while (itr != head){char ch = *--itr;if (ch == ')') ++bracket;else if (ch == '(') --bracket;if (ch == '+' && !bracket) return calculate(head, itr) + calculate(itr + 1, tail);if (ch == '-' && !bracket) return calculate(head, itr) - calculate(itr + 1, tail);}itr = tail;while (itr != head){char ch = *--itr;if (ch == ')') ++bracket;else if (ch == '(') --bracket;if (ch == '*' && !bracket) return calculate(head, itr) * calculate(itr + 1, tail);if (ch == '/' && !bracket) return calculate(head, itr) / calculate(itr + 1, tail);}itr = tail;while (itr != head){char ch = *--itr;if (ch == ')') ++bracket;else if (ch == '(') --bracket;if (ch == '|' & !bracket) return calculate(head, itr).inverse();}if (head + 1 != tail) return matrix_error("invalid identifer in expression");return str2mat(head, tail);}int main(){char cmd[100];int p, flag = 1; int N; int i; fstream fin("ttest.in");while (flag) {cout << "**********小可爱的矩阵计算器**********\n";cout << "请选择输入方式:\n0.退出 1.从文件中读取 2.从控制台读取\n";cin >> p;switch (p) {case 0:flag = 0; break;case 1:fin >> N;for (i = 0; i < N; i++) {fin >> M_Arr[i];}fin.get();while (fin.getline(cmd, 100)){cout << ">>>>>>>>>>>>>>>>>\n";cout << cmd<<endl;cout << calculate(cmd, cmd + strlen(cmd));}break;case 2:cout << "输入参与运算的矩阵的数目\n";cin >> N;for (i = 0; i < N; i++) {cout << "请输入矩阵的行、列以及元素,以空格间隔\n";cin >> M_Arr[i];}cout << "请输入表达式\n";cin.get();while (cin.getline(cmd, 100)){cout << ">>>>>>>>>>>>>>>>>\n";cout << calculate(cmd, cmd + strlen(cmd));cout << "请输入表达式\n";}break;}}return 0;}

原创粉丝点击