#include"conio.h"
#include"math.h"
#include <iostream>
using namespace std;
class phanso
{
private:
float tu,mau;
public:
phanso(float =0,float =1);
~phanso();
int gettu();
int getmau();
phanso operator+(phanso b);
phanso operator-(phanso b);
phanso operator*(phanso b);
phanso operator/(phanso b);
friend ostream &operator<<(ostream &out,const phanso &a);
friend istream &operator>>(istream &in,phanso &a);
};
class dsphanso
{
private:
int n;
int soluong;
phanso a[10000];
public:
dsphanso(int n=0);
~dsphanso();
void append(phanso x);
void remove(int vt);
void nhap();
void xuat();
phanso max();
phanso min();
};
dsphanso::dsphanso(int x)
{
n=x;
soluong=0;
}
dsphanso::~dsphanso()
{
soluong=0;
n=10000;
}
void dsphanso::append(phanso x)
{
a[soluong++]=x;
}
void dsphanso::remove(int vt)
{
--soluong;
for(int i = vt ; i < soluong ; i++)
{
a[i]=a[i+1];
}
}
phanso dsphanso::max()
{
float tmp = (float)a[0].gettu()/(float)a[0].getmau();
int vt=0;
for(int i = 1 ; i < soluong ; i ++)
{
if( ((float)a[i].gettu()/(float)a[i].getmau()) > tmp)
{
tmp = ((float)a[i].gettu()/(float)a[i].getmau());
vt=i;
}
}
return a[vt];
}
phanso dsphanso::min()
{
float tmp = (float)a[0].gettu()/(float)a[0].getmau();
int vt=0;
for(int i = 1 ; i < soluong ; i ++)
{
if( ((float)a[i].gettu()/(float)a[i].getmau()) < tmp)
{
tmp = ((float)a[i].gettu()/(float)a[i].getmau());
vt=i;
}
}
return a[vt];
}
void dsphanso::nhap()
{
cout<<"Nhap so phan so: ";
int tmp;
do
{
cin>>tmp;
if(tmp+soluong>n || tmp+soluong <= 0)
cout<<"Sai so luong roi"<<endl;
}while(tmp+soluong>n || tmp+soluong <= 0);
for(int i = 0 ; i < tmp ; i ++)
{
phanso tmp;
cin>>tmp;
dsphanso::append(tmp);
}
}
void dsphanso::xuat()
{
for(int i = 0 ; i < soluong ; i ++)
cout<<a[i]<<endl;
}
phanso::phanso(float b,float c)
{
tu=b;
mau=c;
}
phanso::~phanso()
{
}
int phanso::gettu()
{
return tu;
}
int phanso::getmau()
{
return mau;
}
ostream &operator<<(ostream &out,const phanso &a)
{
if(a.tu/a.mau==0) out<<0;
else
if(float(a.tu/a.mau)==int(a.tu/a.mau)) out<<a.tu/a.mau;
else
{
int x=abs((int)a.tu),y=abs((int)a.mau);
while(x!=y)
if(x>y)x-=y;
else y-=x;
out<<(a.tu<0 || a.mau<0 ?"-":"")<<abs((int)a.tu/x)<<"/"<<abs((int)a.mau/x);
}
return out;
}
istream &operator>>(istream &in,phanso &a)
{
cout<<"tu=";
in>>a.tu;
cout<<"mau=";
do
{
in>>a.mau;
if(a.mau ==0)
cout<<"Mau Phai Khac Khong"<<endl;
}
while(a.mau==0);
return in;
}
phanso phanso::operator+(phanso x)
{
phanso c;
c.tu=tu*x.mau+mau*x.tu;
c.mau=mau*x.mau;
return c;
}
phanso phanso::operator-(phanso x)
{
phanso c;
c.tu=tu*x.mau-mau*x.tu;
c.mau=mau*x.mau;
return c;
}
phanso phanso::operator*(phanso x)
{
phanso c;
c.tu=tu*x.tu;
c.mau=mau*x.mau;
return c;
}
phanso phanso::operator/(phanso x)
{
phanso c;
c.tu=tu*x.mau;
c.mau=mau*x.tu;
return c;
}
int menu()
{
cout<<"1.Them phan so moi"<<endl;
cout<<"2.Xoa 1 phan so khoi day"<<endl;
cout<<"3.Tim phan so lon nhat"<<endl;
cout<<"4.Tim phan so nho nhat"<<endl;
cout<<"5.Xuat "<<endl;
cout<<"0.Thoat"<<endl;
int chon ;
cin>>chon;
return chon;
}
int main()
{
int sl,chon=0,vt;
cout<<"So Luong phan tu cua mang, toi da la 10000: ";
do
{
cin>>sl;
if(sl<=0 || sl>10000)
cout<<"So luong phai lon hon 0 va nho hon 10000"<<endl;
}while(sl<=0 || sl>10000);
dsphanso A= dsphanso(sl);
do
{
chon=menu();
switch(chon)
{
case 1:
A.nhap();
break;
case 2:
cout<<"Nhap vi tri can xoa(bat dau tu 0): ";
cin>>vt;
A.remove(vt);
break;
case 3:
cout<<A.max();
break;
case 4:
cout<<A.min();
break;
case 5:
A.xuat();
break;
case 0:
cout<<"BYE"<<endl;
break;
default:
cout<<"Chon Sai"<<endl;
}
}while(chon != 0);
// cout<<endl;
system("pause");
}