矩阵计算器实现
能够实现从文件或控制台中读入相应的矩阵,可以在控制台输出显示相应的矩阵。
能够完成矩阵的一般计算(两个矩阵相加、相减、相乘) 能够完成矩阵的除法、求逆计算 能够完成矩阵的多个运算符的混合计算。
使用堆栈的思想完成运算命令的处理。
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#define DEFAULT_WIDTH 5
#define EPSILON 0.0001
#define MAX_MATRIX 10
using 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 <<