01/10/2018, 00:24
Operator Mảng Phân Số
Mọi người xem giúp mình với. Mình đang bị sai chỗ istream và ostream của mảng phân số
MangPhanSo.h
#pragma once
#include "PhanSo.h"
#include "iostream"
using namespace std;
int BSCNN(int a, int b);
class MangPhanSo
{
private:
PhanSo *ArrPhanSo;
int KichThuoc;
public:
MangPhanSo()
{
KichThuoc = 0;
}
MangPhanSo(int n)
{
this->KichThuoc = n;
ArrPhanSo = new PhanSo[n];
for (int i = 0; i < n; i++)
{
ArrPhanSo[i].SetTuSo(0);
ArrPhanSo[i].SetMauSo(1);
}
}
MangPhanSo(PhanSo ps[], int n)
{
this->KichThuoc = n;
for (int i = 0; i < n; i++)
{
ArrPhanSo[i].SetTuSo(ps[i].GetTuSo());
ArrPhanSo[i].SetMauSo(ps[i].GetMauSo());
}
}
MangPhanSo(int ps[], int n)
{
this->KichThuoc = n;
for (int i = 0; i < n; i++)
{
ArrPhanSo[i].SetTuSo(ps[i]);
ArrPhanSo[i].SetMauSo(1);
}
}
MangPhanSo(MangPhanSo &ps)
{
this->KichThuoc = ps.KichThuoc;
ArrPhanSo = new PhanSo[this->KichThuoc];
int n = this->KichThuoc;
for (int i = 0; i < n; i++)
{
ArrPhanSo[i].SetTuSo(ps.ArrPhanSo[i].GetTuSo());
ArrPhanSo[i].SetMauSo(ps.ArrPhanSo[i].GetMauSo());
}
}
~MangPhanSo()
{
delete[] ArrPhanSo;
this->KichThuoc = 0;
ArrPhanSo = NULL;
}
void Nhap();
void NhapTD();
void Xuat();
PhanSo Tong();
PhanSo Hieu();
PhanSo Tich();
PhanSo Thuong();
MangPhanSo operator=(const MangPhanSo &mps);
MangPhanSo operator[](const MangPhanSo &mps);
MangPhanSo(PhanSo*);
PhanSo GetPhanSo(int);
void SetPhanSo(int, int);
friend istream & operator>>(istream &in, MangPhanSo &mps);
friend ostream & operator<<(ostream &out, MangPhanSo &mps);
void Sort();
void ToiGianToanMang();
void QuyDongToanMang();
PhanSo TinhTongMang();
PhanSo TinhHieuMang();
PhanSo TinhTichMang();
PhanSo TinhThuongMang();
};
MangPhanSo.cpp
#include<iostream>
using namespace std;
#include "MangPhanSo.h"
int BSCNN(int a, int b)
{
if (a < 0)
a *= -1;
if (b < 0)
b *= -1;
int BSCNN = (a * b) / USCLN(a, b);
return BSCNN;
}
void MangPhanSo::Nhap()
{
cout << "Nhap kich thuoc mang phan so:";
cin >> KichThuoc;
ArrPhanSo = new PhanSo[KichThuoc];
for (int i = 0; i < KichThuoc; i++)
{
cout << "Nhap phan so " << i + 1 << endl;
ArrPhanSo[i].Nhap();
}
}
void MangPhanSo::NhapTD()
{
cout << "Nhap kich thuoc mang phan so: ";
cin >> KichThuoc;
ArrPhanSo = new PhanSo[KichThuoc];
int n = KichThuoc;
for (int i = 0; i < n; i++)
{
ArrPhanSo[i].SetTuSo(rand() % 10);
ArrPhanSo[i].SetMauSo(rand() % 10);
}
}
void MangPhanSo::Xuat()
{
cout << "Mang phan so: ";
for (int i = 0; i < KichThuoc; i++)
{
ArrPhanSo[i].Xuat();
cout << " ";
}
cout << endl;
}
PhanSo MangPhanSo::Tong()
{
PhanSo t = ArrPhanSo[0];
for (int i = 1; i < KichThuoc; i++)
{
t = t + ArrPhanSo[i];
}
return t;
}
PhanSo MangPhanSo::Hieu()
{
PhanSo t = ArrPhanSo[0];
for (int i = 1; i < KichThuoc; i++)
{
t = t - ArrPhanSo[i];
}
return t;
}
PhanSo MangPhanSo::Tich()
{
PhanSo t = ArrPhanSo[0];
for (int i = 1; i < KichThuoc; i++)
{
t = t * ArrPhanSo[i];
}
return t;
}
PhanSo MangPhanSo::Thuong()
{
PhanSo t = ArrPhanSo[0];
for (int i = 1; i < KichThuoc; i++)
{
t = t / ArrPhanSo[i];
}
return t;
}
PhanSo MangPhanSo::GetPhanSo(int i)
{
return this->ArrPhanSo[i];
}
void MangPhanSo::SetPhanSo(int tu, int mau)
{
this->ArrPhanSo->SetTuSo(tu);
this->ArrPhanSo->SetMauSo(mau);
}
istream & operator>>(istream &in, MangPhanSo &mps)
{
int n, tu, mau;
in >> mps.KichThuoc;
n = mps.KichThuoc;
mps = new PhanSo[n];
for (int i = 0; i < n; i++)
{
cin >> mps[i];
in >> tu;
do{
in >> mau;
if (mau == 0)
cout << "Mau khong hop le" << endl;
} while (mau == 0);
if (mau < 0)
{
mau *= -1;
tu *= -1;
}
mps[i].SetPhanSo(tu, mau);
}
return in;
}
ostream & operator<<(ostream &out, MangPhanSo &mps)
{
int n = mps.KichThuoc;
for (int i = 0; i < n; i++)
{
out << mps[i].ArrPhanSo->GetTuSo() << "/" << mps[i].ArrPhanSo->GetMauSo();
}
return out;
}
void MangPhanSo::Sort()
{
int nChon;
cout << "Chon 1: Sap xep tang dan" << endl;
cout << "Chon 2: Sap xep giam dan" << endl;
cout << "Moi chon gia tri: ";
cin >> nChon;
if (nChon == 1)
{
for (int i = 0; i < this->KichThuoc - 1; i++)
{
for (int j = i + 1; j < this->KichThuoc; j++)
{
double ps1 = 1.0 * ArrPhanSo[i].GetTuSo() / ArrPhanSo[i].GetMauSo();
double ps2 = 1.0 * ArrPhanSo[j].GetTuSo() / ArrPhanSo[j].GetMauSo();
if (ps1 > ps2)
{
PhanSo temp = ArrPhanSo[i];
ArrPhanSo[i] = ArrPhanSo[j];
ArrPhanSo[j] = ArrPhanSo[i];
}
}
}
}
else
{
for (int i = 0; i < this->KichThuoc - 1; i++)
{
for (int j = i + 1; j < this->KichThuoc; j++)
{
double ps1 = 1.0 * ArrPhanSo[i].GetTuSo() / ArrPhanSo[i].GetMauSo();
double ps2 = 1.0 * ArrPhanSo[j].GetTuSo() / ArrPhanSo[j].GetMauSo();
if (ps1 < ps2)
{
PhanSo temp = ArrPhanSo[i];
ArrPhanSo[i] = ArrPhanSo[j];
ArrPhanSo[j] = ArrPhanSo[i];
}
}
}
}
this->Xuat();
}
void MangPhanSo::ToiGianToanMang()
{
for (int i = 0; i < this->KichThuoc; i++)
{
ArrPhanSo[i].ToiGian();
}
}
void MangPhanSo::QuyDongToanMang()
{
int BCNN = 1;
for (int i = 0; i < this->KichThuoc; i++)
{
BCNN = BSCNN(BCNN, ArrPhanSo[i].GetMauSo());
}
for (int i = 0; i < this->KichThuoc; i++)
{
this->ArrPhanSo[i].SetTuSo( this->ArrPhanSo[i].GetTuSo() * (BCNN / this->ArrPhanSo[i].GetMauSo()) );
this->ArrPhanSo[i].SetMauSo(BCNN);
}
this->Xuat();
}
PhanSo MangPhanSo::TinhTongMang()
{
PhanSo S = PhanSo(0);
for (int i = 0; i < this->KichThuoc; i++)
{
S = S + this->GetPhanSo(i); //this->ArrPhanSo[i];
}
return S;
}
PhanSo MangPhanSo::TinhHieuMang()
{
PhanSo S = PhanSo(0);
for (int i = 0; i < this->KichThuoc; i++)
{
S = S - this->GetPhanSo(i);
}
return S;
}
PhanSo MangPhanSo::TinhTichMang()
{
PhanSo S = ArrPhanSo[0];
for (int i = 1; i < this->KichThuoc; i++)
{
S = S * this->GetPhanSo(i);
}
return S;
}
PhanSo MangPhanSo::TinhThuongMang()
{
PhanSo S = ArrPhanSo[0];
for (int i = 1; i < this->KichThuoc; i++)
{
S = S / this->GetPhanSo(i);
}
return S;
}
Bài liên quan
bạn viết operator>> cho mảng phân số chưa? nếu chưa thì không thể gọi được
cin >> mps[i]
à mình cảm ơn bạn nhé, mình làm ra nó rồi