Sei sulla pagina 1di 8

#include <iostream>

#include<conio.h>
using namespace std;
int ucln(int a, int b)
{
int tam = a;
while(!(a%tam==0&&b%tam==0&&tam>0))
tam --;
return tam;
}
class Phanso
{
int ts, ms;
public:
Phanso();
Phanso(int ts1, int ms1);
friend istream & operator >> (istream &, Phanso &);
friend ostream & operator << (ostream &, Phanso &);
Phanso operator + (Phanso &);
Phanso operator - (Phanso &);
Phanso operator * (Phanso &);
void rutgon();
bool operator == (Phanso &);
bool operator != (Phanso &);
bool operator > (Phanso &);
bool operator < (Phanso &);
bool operator >= (Phanso &);
bool operator <= (Phanso &);
};
Phanso::Phanso()
{
ts = 0;
ms =1;
}
Phanso::Phanso(int ts1, int ms1)
{
ts = ts1;
ms = ms1;
}
ostream & operator << (ostream &os, Phanso &r)
{
os << r.ts << "/"<< r.ms;
return os;
}
istream & operator >> (istream &is, Phanso &r)
{
cout << "Nhap tu so:";
is >> r.ts;
cout << "Nhap mau so:";
is >> r.ms;
return is;
}
Phanso Phanso::operator+(Phanso &r)
{
int ts1, ms1;
ts1 = ts*r.ms + ms*r.ts;
ms1 = ms * r.ms;
int uc = ucln(ts1, ms1);
ts1 = ts1/uc;
ms1 = ms1/uc;
return Phanso(ts1, ms1);
}
Phanso Phanso::operator-(Phanso &r)
{
int ts1, ms1;
ts1 = ts*r.ms - ms*r.ts;
ms1 = ms * r.ms;
int uc = ucln(ts1, ms1);
ts1 = ts1/uc;
ms1 = ms1/uc;
return Phanso(ts1, ms1);
}
Phanso Phanso::operator*(Phanso &r)
{
int ts1, ms1;
ts1 = ts*r.ts;
ms1 = ms * r.ms;
int uc = ucln(ts1, ms1);
ts1 = ts1/uc;
ms1 = ms1/uc;
return Phanso(ts1, ms1);
}
bool Phanso::operator > (Phanso &r)
{
int ts1, ms1;
ts1 = ts*r.ms - ms*r.ts;
ms1 = ms * r.ms;
return (ts1*ms1>0);
}
bool Phanso::operator == (Phanso & r)
{
if(!(*this>r) && !(r>*this))
return true;
return false;
}
bool Phanso::operator != (Phanso & r)
{
if(!(*this==r))
return true;
return false;
}
bool Phanso::operator < (Phanso & r)
{
if(r>*this)
return true;
return false;
}
void Phanso::rutgon()
{
int uc = ucln(ts, ms);
ts = ts/uc;
ms = ms/uc;
}
void sapxep(Phanso a[], int n)
{
Phanso tam;
int i, j;
for (i=0;i<n;++i)
for(j=i+1;j<n;++j)
if(a[j]<a[i])
{
tam = a[i];
a[i] = a[j];
a[j] = tam;
}
}
int main()
{
Phanso * a;
int n, i;
Phanso tong, tich(1,1);
int m1, m2;
cout << "Nhap n=";
cin >> n;
a = new Phanso[n];
for(i=0;i<n;++i)
{
cout << "Nhap phan so thu "<< i << endl;
cin >> a[i];
}
cout << "Mang cac phan so vua nhap vao:\n";
for(i=0;i<n;++i)
cout << a[i] <<endl;
m1 = m2 = 0;
for(i=0;i<n;++i)
{
tong = tong + a[i];
tich = tich * a[i];
if(a[i]>a[m1])
m1 = i;
if(a[i]<a[m2])
m2 = i;
}
cout << "Phan so tong:" << tong << endl;
cout << "Phan so tich:"<< tich << endl;
cout << "Phan so Lon nhat:"<< a[m1] << endl;
cout << "Phan so Nho nhat:" << a[m2] << endl;
sapxep(a, n);
cout << "Mang sau khi sap xep:"<< endl;
for(i=0;i<n;++i)
cout << a[i] << endl;
delete [] a;
return 0;}
================================
/*include <iostream.h>
class phanso
{
private :
long tu,mau;
public :
uocluoc();
void nhap();
void xuat();
void gantu(long x);
void ganmau(long y);
friend phanso operator*(phanso a,phanso b);
friend phanso operator+(phanso a,phanso b);
friend phanso operator+(phanso a,long t);
bool operator==(phanso b);
};
int uscln(long t,long m)
{
int i,h;
if (t>m)
h=m;
else h=t;
for(i=h;i>0;i--)
if((t%i==0)&&(m%i==0))
{break;}
return i;
}
phanso::uocluoc()
{
long tam;
tam=long(uscln(tu,mau));
tu=tu/tam;
mau=mau/tam;
return 0;}
void phanso::nhap()
{
cout<<"nhap tu ";
cin>>tu;
cout<<"nhap mau ";
cin>>mau;
}
void phanso::xuat()
{
cout<<tu<<"/"<<mau;
}
void phanso::gantu(long x)
{
tu=x;}
void phanso::ganmau(long y)
{mau=y;}
phanso operator*(phanso a,phanso b)
{phanso tam;
tam.gantu(a.tu*b.tu);
tam.ganmau(a.mau*b.mau);
return tam;
}
phanso operator+(phanso a,phanso b)
{
phanso tam;
tam.gantu(a.tu*b.mau+a.mau*b.tu);
tam.ganmau(a.mau*b.mau);
return tam;
}
phanso operator+(phanso a,long b)
{
phanso tam;
tam.gantu(a.tu+b*a.mau);
tam.ganmau(a.mau);
return tam;
}
void main()
{
phanso a,b,c,d,kq;int t=1;
cout<<"nhap phan so a"<<'\n';
a.nhap();
cout<<"nhap phan so b"<<'\n';
b.nhap();
cout<<"nhap phan so c"<<'\n';
c.nhap();
cout<<"3 phan so vua nhap la : ";
a.xuat();cout<<" ";
b.xuat();cout<<" ";
c.xuat();cout<<" ";
kq=a*b+c+t;
kq.uocluoc();
cout<<'\n'<<"ket qua la ";kq.xuat();cout<<'\n';
}*/
/*
#include <iostream.h>
#include <conio.h>
typedef struct NODE
{
int data;
NODE *next;
};
typedef NODE *pnode;
typedef struct STACK
{
pnode H,T;
};
void Init(STACK &S)
{
S.H = S.T = NULL;
}
int IsEmpty(STACK S)
{
if(S.H == NULL)
return 1;
return 0;
}
pnode GetNode(int x)
{
pnode p;
p = new NODE;
if(p == NULL)
return NULL;
p->data = x;
p->next = NULL;
return p;
}
void AddFirst(STACK &S, pnode p)
{
if(IsEmpty(S) == 1)
S.H = S.T = p;
else
{
p->next = S.H;
S.H = p;
}
}
void InsertHead(STACK &S, int x)
{
pnode p;
p = GetNode(x);
if(p == NULL)
{
cout<<"Khong Du Bo Nho";
return;
}
AddFirst(S,p);
}
int RemoveHead(STACK &S)
{
if(IsEmpty(S) == 1)
return -1;
int x = S.H->data;
pnode p = S.H;
S.H = S.H->next;
p->next = NULL;
delete p;
return x;
}
void Output(STACK S)
{
pnode p;
p = S.H;
while(p != NULL)
{
cout<<p->data;
p = p->next;
}
}
void main()
{
clrscr();
STACK S;
Init(S);
int thapphan;
cout<<"\nNhap 1 So Nguyen Duong: ";
cin>>thapphan;
int sodu;
int coso;
int phannguyen;
while(thapphan != 0)
{
phannguyen = thapphan / 2;
sodu = thapphan - phannguyen * 2;
thapphan = phannguyen;
InsertHead(S,sodu);
}
cout<<"\nKet Qua: ";
Output(S);
getch();
} */
/*
#include
#include
char *doics(char *number, int n, int m)
{
static char ketqua[17];
char chuso[16] = "0123456789ABCDEF";
int i = 0, giatri = 0, len;
len = strlen(number);
while(i 0);
return (ketqua i 1);
}
void main()
{
unsigned n, m;
char number[17], *ketqua;
do {
printf("\nNhap gia tri N (2 - 16) : ");
scanf("%d", &n);
} while (n16);
getchar();
printf("Gia tri thuoc he %d : ", n);
gets(number);
do {
printf("Nhap gia tri M (2 - 16) : ");
scanf("%d", &m);
} while (m16);
printf("Gia tri %s o co so %u co gia tri o co so %u la : ", number, n, m);
ketqua = doics(number, n, m);
puts(ketqua);
getch();
} */
/*
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define STACKSIZE 100
#define TRUE 1
#define FALSE 0
struct stack
{
int top;
int nodes[STACKSIZE];
};
int empty(struct stack *ps)
{
if(ps->top == -1)
return(TRUE);
else
return(FALSE);
}
void push(struct stack *ps, int x)
{
if(ps->top == STACKSIZE-1)
{
printf("%s", "stack bi day");
exit(1);
}
else
ps->nodes[++(ps->top)] = x;
}
int pop(struct stack *ps)
{
if(empty(ps))
{
printf("%s", "stack bi rong");
exit(1);
}
return(ps->nodes[ps->top--]);
}
main()
{
struct stack s;
int coso, so, sodu;
char c;
clrscr();
do
{
s.top =- 1; // khoi dong stack
printf("\n\nNhap vao mot so thap phan: ");
scanf("%d", &so);
printf("%s","Muon doi so thap phan nay sang co so may: ");
scanf("%d", &coso);
while (so != 0)
{
sodu = so % coso;
push(&s, sodu); // push so du vao stack
so = so / coso;
}
printf("So da doi la: ");
while(!empty(&s))
printf("%X", pop(&s)); // pop so du ra khoi stack
printf("\n\nBan co muon tiep tuc khong? (c/k): ");
c = getche();
} while(c == 'c' || c == 'C');
}*/

Potrebbero piacerti anche