Писала программу для сложения/ вычитания/умножения матриц в которые надо было посылать разные типы. Для int все работало float, но надо было ее добавить тип простая дробь. В Неader.h не могу понять как реализовать перегрузку оператора "==", когда правый операнд число. Еще есть проблема с оператора %. Я его перегрузила, но не могу понять как его реализовать с float.
Буду благодарна за помощь. P.S. знаю, русские названия переменным лучше не давать
Header.h
#ifndef Array_h
#define Array_h
#include <iostream>
using namespace std;
template<typename T> class arrr
{
template <typename T>
friend arrr<T> operator+ (arrr<T> &m1, arrr<T> &m2);
template <typename T>
friend arrr<T> operator- (arrr<T> &m1, arrr<T> &m2);
template <typename T>
friend arrr<T> operator* (arrr<T> &m1, arrr<T> &m2);
friend istream &operator>>(istream &istr, arrr &k)
{
for (int i = 0; i<k.n; i++)
for (int j = 0; j<k.m; j++)
istr >> k(i, j);
return(istr);
}
friend ostream &operator<<(ostream &ostr, arrr &k)
{
for (int i = 0; i<k.n; i++)
{
for (int j = 0; j<k.m; j++)
ostr << k(i, j) << "\t";
ostr << "\n";
}
return(ostr);
}
private:
int n, m;
T **value;
public:
T & operator()(int n, int m);
arrr(int no, int mo) {
n = no;
m = mo;
value = new T *[n];
for (int i = 0; i < n; i++)
value[i] = new T[m];
}
arrr()
{
n = 0;
m = 0;
}
class error {};
const arrr& operator= (const arrr& a)
{
if (&a != this)
{
if (value)
delete (value);
n = a.n;
m = a.m;
value = new T*[n];
for (int i = 0; i < n; i++)
{
value[i] = new T[m];
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
value[i][j] = a.value[i][j];
}
}
}
return *this;
}
bool operator == (const arrr& a) const
{
if (&a == this)
return true;
if (n != a.n && m != a.m)
return false;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++) {
if (value[i][j] != a.value[i][j])
return false;
}
}
return true;
}
};
template <typename T> arrr<T> operator+(arrr<T>& m1, arrr<T>& m2)
{
if (m1.n == m2.n && m1.m == m2.m)
{
arrr<T> temp(m1.n, m1.m);
for (int i = 0; i < m1.n; i++)
for (int j = 0; j < m1.m; j++)
temp(i, j) = m1(i, j) + m2(i, j);
return(temp);
}
else throw arrr<T>::error();
}
template <typename T> arrr<T> operator-(arrr<T>& m1, arrr<T>& m2)
{
if (m1.n == m2.n && m1.m == m2.m)
{
arrr<T> temp(m1.n, m1.m);
for (int i = 0; i < m1.n; i++)
for (int j = 0; j < m1.m; j++)
temp(i, j) = m1(i, j) - m2(i, j);
return(temp);
}
else throw arrr<T>::error();
}
template <typename T> arrr<T> operator*(arrr<T>& m1, arrr<T>& m2)
{
if (m1.n == m2.m)
{
arrr<T> temp2(m1.n, m1.m);
for (int i = 0; i < m1.n; i++)
{
for (int j = 0; j < m2.m; j++)
{
temp2(i, j) = 0;
for (int k = 0; k < m1.m; k++)
temp2(i, j) = temp2(i, j) + (m1(i, k) * m2(k, j));
}
}
return(temp2);
}
else throw arrr<T>::error();
}
template <typename T> void min(arrr<T>& value, int n, int m)
{
int i = 0, j = 0;
T min = value(i, j);
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
if (value(i, j)<min)
{
min = value(i, j);
}
}
}
cout << min << "\t";
}
template <typename T> T& arrr<T>::operator()(int n, int m)
{
return (value[n][m]);
}
template <typename T> static void parn(arrr<T>& value, int n, int m)
{
int i, j;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
T u = value(i, j);
if ((u % 2) == 0)
{
value(i, j) = value(i, j) * 3;
}
cout << value(i, j) << "\t";
}
cout << "\n";
}
}
#endif
Header1.h
#include "stdafx.h"
#include <iostream>
using namespace std;
class drobi {
private:
int chislitel, znamenatel,cell;
public:
friend bool operator <(drobi&f1, drobi&f2);
friend bool operator < (drobi&f, int a);
friend drobi operator %(const drobi &dr, int ymn);
friend drobi operator*(drobi dr, int z);
drobi(int chisl, int zn)
{
chislitel = chisl;
znamenatel = zn;
}
drobi()
{
chislitel = 0;
znamenatel = 0;
}
const drobi& operator=(const drobi&f)
{
chislitel = f.chislitel;
znamenatel = f.znamenatel;
cell = f.cell;
return *this;
}
const drobi& operator=(int a)
{
chislitel = 1;
znamenatel = 1;
cell = a;
return *this;
}
void set()
{
int NOD = 0;
if (chislitel>znamenatel) NOD = znamenatel; else NOD = chislitel;
for (int i = NOD; i>0; i--)
{
if ((fmod(chislitel, i) == 0) && (fmod(znamenatel, i) == 0))
{
chislitel = chislitel / i;
znamenatel = znamenatel / i;
}
}
}
void sett()
{
int cell;
int c = chislitel;
int z = znamenatel;
if ((chislitel > znamenatel) && znamenatel != 1)
{
cell = c / z;
c = c % z;
cout << "OR";
cout << '\t' << '\t' << '\t';
cout << cell << " " << c << "/" << z;
}
if (znamenatel > chislitel)
{
z = z / c;
c = c / c;
cout << "OR";
cout << '\t' << '\t' << '\t';
cout << c << "/" << z;
}
if (znamenatel == 1) {
cout << "OR";
cout << '\t' << '\t' << '\t';
cout << c;
}
else cout << " ";
}
drobi operator+(drobi dr)
{
int ymn;
if (znamenatel != dr.znamenatel)
{
ymn = znamenatel;
znamenatel = znamenatel * dr.znamenatel;
chislitel = chislitel * dr.znamenatel;
dr.chislitel = dr.chislitel*ymn;
dr.znamenatel = znamenatel;
}
int x = chislitel + dr.chislitel;
return drobi(x, znamenatel);
}
drobi operator-(drobi dr)
{
int ymn;
if (znamenatel != dr.znamenatel)
{
ymn = znamenatel;
znamenatel = znamenatel * dr.znamenatel;
chislitel = chislitel * dr.znamenatel;
dr.chislitel = dr.chislitel*ymn;
dr.znamenatel = znamenatel;
}
int x = chislitel - dr.chislitel;
return drobi(x, znamenatel);
}
drobi operator*(drobi dr)
{
int x = chislitel * dr.chislitel;
int y = znamenatel * dr.znamenatel;
return drobi(x, y);
}
friend ostream& operator<<(ostream& stream, const drobi& obj)
{
stream << obj.chislitel << "/";
stream << obj.znamenatel << endl;
return stream;
}
friend istream& operator>>(istream& stream, drobi& obj)
{
stream >> obj.chislitel;
stream >> obj.znamenatel;
return stream;
}
};
bool operator <(drobi&f1, drobi&f2)
{
drobi a = f1;
drobi b = f2;
if (a.cell != 0)
{
a.chislitel = a.znamenatel*a.cell + a.chislitel;
}
if (b.cell != 0)
{
b.chislitel = b.znamenatel*b.cell + b.chislitel;
}
if (a.znamenatel != b.znamenatel)
{
a.chislitel = b.znamenatel*a.chislitel;
b.chislitel = a.znamenatel*b.chislitel;
}
return(a.chislitel < b.chislitel) ? true : false;
}
bool operator < (drobi&f, int a)
{
if (a == 0) return (f.chislitel < 0 || f.znamenatel < 0) ? true : false;
return (f.cell < a) ? true : false;
}
drobi operator %(const drobi &dr, int ymn)
{
if ((dr.chislitel % ymn == 0) && (dr.znamenatel % ymn == 0))
{
int x = dr.chislitel;
int y = dr.znamenatel;
return drobi(x, y);
}
}
drobi operator*(drobi dr, int z)
{
int x = dr.chislitel *z;
int y = dr.znamenatel;
return drobi(x, y);
}
7.cpp
#include "stdafx.h"
#include <iostream>
#include "Header.h"
#include "Header1.h"
using namespace std;
void PlayINT(int n, int m, int n1, int m1)
{
arrr<int> a(n, m);
arrr<int> b(n1, m1);
arrr<int> c;
arrr<int> d;
arrr<int> z;
cout << "enter matrix A:\n";
cin >> a;
cout << "enter matrix B:\n";
cin >> b;
cout << "MIN A: ";
min(a, n, m);
cout << "\n";
cout << "MIN B: ";
min(b, n1, m1);
cout << "\n";
try {
c = a + b;
cout << "sum of two matrices A and B :\n" << c;
}
catch (arrr<int>::error) {
cout << "error: the sizes of summable matrices must coincide " << endl;
}
try {
d = a - b;
cout << "subtraction of two matrices A and B:\n" << d;
}
catch (arrr<int>::error)
{
cout << "error:the sizes of matrices must coincide" << endl;
}
try {
z = a * b;
cout << "multiplication of two matrices A and B:\n" << z;
}
catch (arrr<int>::error)
{
cout << "error: the number of columns of the first matrix must coincide with the number of rows of the second matrix" << endl;
}
cout << "All items with paired values are increased three times, matrice A:" << endl;
parn(a, n, m);
cout << "All items with paired values are increased three times, matrice B:" << endl;
parn(b, n1, m1);
}
void PlayDROBI(int n, int m, int n1, int m1)
{
arrr<drobi> a(n, m);
arrr<drobi> b(n1, m1);
arrr<drobi> c;
arrr<drobi> d;
arrr<drobi> z;
cout << "enter matrix A:\n";
cin >> a;
cout << "enter matrix B:\n";
cin >> b;
cout << "MIN A: ";
min(a, n, m);
cout << "\n";
cout << "MIN B: ";
min(b, n1, m1);
cout << "\n";
try {
c = a + b;
cout << "sum of two matrices A and B :\n" << c;
}
catch (arrr<drobi>::error) {
cout << "error: the sizes of summable matrices must coincide " << endl;
}
try {
d = a - b;
cout << "subtraction of two matrices A and B:\n" << d;
}
catch (arrr<drobi>::error)
{
cout << "error:the sizes of matrices must coincide" << endl;
}
try {
z = a * b;
cout << "multiplication of two matrices A and B:\n" << z;
}
catch (arrr<drobi>::error)
{
cout << "error: the number of columns of the first matrix must coincide with the number of rows of the second matrix" << endl;
}
cout << "All items with paired values are increased three times, matrice A:" << endl;
parn(a, n, m);
cout << "All items with paired values are increased three times, matrice B:" << endl;
parn(b, n1, m1);
}
void PlayFloat(int n, int m, int n1, int m1)
{
arrr<float> a(n, m);
arrr<float> b(n1, m1);
arrr<float> c;
arrr<float> d;
arrr<float> z;
cout << "enter matrix A:\n";
cin >> a;
cout << "enter matrix B:\n";
cin >> b;
cout << "MIN A: ";
min(a, n, m);
cout << "\n";
cout << "MIN B: ";
min(b, n1, m1);
cout << "\n";
try {
c = a + b;
cout << "sum of two matrices A and B :\n" << c;
}
catch (arrr<float>::error) {
cout << "error: the sizes of summable matrices must coincide " << endl;
}
try {
d = a - b;
cout << "subtraction of two matrices A and B:\n" << d;
}
catch (arrr<float>::error)
{
cout << "error:the sizes of matrices must coincide" << endl;
}
try {
z = a * b;
cout << "multiplication of two matrices A and B:\n" << z;
}
catch (arrr<float>::error)
{
cout << "error: the number of columns of the first matrix must coincide with the number of rows of the second matrix" << endl;
}
cout << "All items with paired values are increased three times, matrice A:" << endl;
parn(a, n, m);
cout << "All items with paired values are increased three times, matrice B:" << endl;
parn(b, n1, m1);
}
int main()
{
int n, m, n1, m1, ch, y = 0;
cout << "enter number of rows and columns of matrice A: ";
cin >> m >> n;
cout << "enter number of rows and columns of matrice B: ";
cin >> m1 >> n1;
do {
cout << "\n 1.INT";
cout << "\n 2.FLOAT";
cout << "\n 3.DROBI";
cout << "\n 4.Exit ";
cout << "\nEnter Your Choice: ";
cin >> ch;
switch (ch)
{
case 1:PlayINT(n, m, n1, m1);
break;
case 2: PlayFloat(n, m, n1, m1);
break;
case 3: PlayDROBI(n, m, n1, m1);
break;
case 4:y = 1;
break;
}
} while (y != 1);
system("pause");
return 0;
}
Виртуальный выделенный сервер (VDS) становится отличным выбором
У меня есть проект в Visual Studio, к которому написан тест, при помощи Google testСтатическая библиотека собрана от сюда, но как я сейчас начинаю понимать,...
Решил перестать циклом проверять все сокеты на наличие новых сообщений, и объединить всё в selectНо теперь абсолютно непонятная проблема, которая...
При установки boost через Command Prompt for VS, а именно при прописывании команды: b2exe, вылезает ошибка: ТЫЦ