Здравствуйте! Проблема состоит в следующем: у меня дана матрица состоящая из рациональных дробей. Есть 2 класса Рационльных дробей и матрицы. В 1-м классе операции сложения, вычитания и т.д выполнены при помощи перегрузки операторов. В классе матриц для сложения, вычитания матриц у меня используется перегруженные + - / * из базового класса. когда я запускаю вылазит ошибка типа _Block_type_is_valid (pHead->nBlockUse) я читал что это ошибка выделения памяти но вроде как с этим все норм. Помогите пожалуйста скоро сдача а я застрял и дальше не могу продвинуться. вот код, скидываю все: | Код | //Rational.h #ifndef RATIONAL_H #define RATIONAL_H #include <iostream> using namespace std;
class Rational{ private: int numerator; // числитель int denominator; // знаменатель
public: // конструкторы Rational(): numerator(0), denominator(1){} // по умолчанию Rational(int m): numerator(m), denominator(1){} // целочисленной дроби Rational(int m, int n): numerator(m), denominator(n){} // рациональной дроби // методы Rational Enter() const; void Show() const; // печать int GCD(int x, int y); // наибольший общий делитель void Minimize(Rational &); // сокращение на наибольший общий делитель // перегрузка операторов // friend istream& operator >> (istream& in, Rational& r); // friend ostream& operator << (ostream& out, Rational& r); Rational operator + (Rational &); // оператора сложения Rational operator - (Rational &); // оператора вычитания Rational operator * (Rational &); // оператора умножения Rational operator / (Rational &); // оператора деления }; #endif
//Rational.cpp #include "Rational.h" using namespace std;
int Rational::GCD(int a, int b) { int c; while (b) { c = a % b; a = b; b = c; } return abs(a); }
void Rational::Minimize(Rational &f) { int t; if (GCD(f.numerator,f.denominator)) { t=GCD(f.numerator,f.denominator); f.numerator /= t; f.denominator /= t; } }
Rational Rational::operator + (Rational &a) { Rational b; b.numerator = a.numerator * denominator + numerator * a.denominator; b.denominator = a.denominator * denominator; Minimize(b); return b; }
Rational Rational::operator - (Rational &a) { Rational b; b.numerator = numerator * a.denominator - a.numerator * denominator; b.denominator = a.denominator * denominator; Minimize(b); return b; }
Rational Rational::operator * (Rational &a) { Rational b; b.numerator = a.numerator * numerator; b.denominator = a.denominator * denominator; Minimize(b); return b; }
Rational Rational::operator / (Rational &a) { Rational b; b.numerator = numerator * a.denominator; b.denominator = denominator * a.numerator; Minimize(b); return b; }
//istream& operator >> (istream& in, Rational& r) //{ // char symbol; // in >> r.numerator>> symbol >> r.denominator; // return in; //} // //ostream& operator << (ostream& out, Rational& r) //{ // out << r.numerator << "/" << r.denominator; // return out; //}
Rational Rational::Enter() const { Rational r; char symbol; cin >> r.numerator >> symbol >> r.denominator; return r; }
void Rational::Show() const { cout << numerator << "/" << denominator; }
//Matrix.h #ifndef MATRIX_H #define MATRIX_H #include <iostream> #include "Rational.h"
class Matrix { private: int rows; int columns; Rational* Rmatrix;
public: // Конструктор Matrix(int, int); // Matrix(const Matrix &); // конструктор копирования // Деструктор ~Matrix(); // Перегрузки операторов bool operator == (Matrix &); Matrix operator = (Matrix &); Matrix operator + (Matrix &); //Matrix operator - (Matrix &); //Matrix operator / (Matrix &); //Matrix operator * (Matrix &);
int GetRows()const{return rows;} int GetColumns()const{return columns;} void EnterMatrix() const; void ShowMatrix() const; }; #endif
//Matrix.cpp #include <iostream> #include "Matrix.h" using namespace std;
Matrix::Matrix(int r, int c): rows(r), columns(c) { Rmatrix = new Rational [rows * columns]; }
void Matrix::EnterMatrix() const { Rational r; for (int i = 0; i < (rows * columns); i++) { Rmatrix[i] = r.Enter(); } }
void Matrix::ShowMatrix() const { for (int i = 0; i < (rows * columns); i++) { Rmatrix[i].Show(); cout << " "; } }
bool Matrix::operator == (Matrix & m) { if ((rows == m.rows) && (columns == m.columns)) return true; else return false; }
Matrix Matrix::operator = (Matrix & m) { for (int i = 0; i < (rows * columns); i++) Rmatrix[i] = m.Rmatrix[i]; return *this; }
Matrix Matrix::operator + (Matrix & m) { Matrix rm(m.rows, m.columns); for (int i = 0; i < (rows * columns); i++) rm.Rmatrix[i] = Rmatrix[i] + m.Rmatrix[i]; return rm; }
// //Matrix::Matrix operator - (Matrix & m); //{ // Matrix rm; // for (int i = 0; i < (rows * columns); i++) // rm.Rmatrix[i] = Rmatrix[i] - m.Rmatrix[i]; // return rm; //}
//Matrix::Matrix operator * (Matrix & m); //{ // Matrix c(rows,a.columns); // int i=0,j,k; // for(i;i<rows;++i){ // for(j=0;j<a.columns;++j) // { // c(i,j)=0; // for(k=0;k<columns;++k)c(i,j)+=(*this)(i,k)*a(k,j); // } // } // return c; //}
//Matrix::Matrix operator / (Matrix & m); //{ // //}
//Matrix::Matrix(const Matrix & m) //{ // rows = m.rows; // columns = m.columns; // Rmatrix = new Rational [rows * columns]; // for (int i = 0; i < rows * columns; i++) // Rmatrix[i] = m.Rmatrix[i]; //}
Matrix::~Matrix() { delete [] Rmatrix; }
//Main.cpp #include <iostream> #include <conio.h> #include "Rational.h" #include "Matrix.h" using namespace std;
int main() // для матриц { Matrix m1(2,2); Matrix m2(2,2); /*if(m1 == m2) cout << "Matrices are equal" << endl; else cout << "Matrices are not equal" << endl;*/ Matrix m3(2,2); cout << "Enter first matrix: "; m1.EnterMatrix(); cout << endl; cout << "Enter second matrix: "; m2.EnterMatrix(); if(m1 == m2) m3 = m1 + m2; else cout << "for addition matrices their length must be identical"; m3.ShowMatrix(); getch();
return 0; }
|
Заранее благодарю!
|