0% found this document useful (0 votes)
24 views4 pages

Online C++ Fraction Operations Compiler

The document is a C++ program that defines structures and functions for handling fractions (phan_so) and arrays of fractions (mang_phan_so). It includes functionalities for inputting, outputting, adding fractions, reducing them, finding the maximum and minimum fractions, and sorting them. The main function orchestrates the input of fractions and outputs their sum after processing.

Uploaded by

nguyentrivan0904
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

Online C++ Fraction Operations Compiler

The document is a C++ program that defines structures and functions for handling fractions (phan_so) and arrays of fractions (mang_phan_so). It includes functionalities for inputting, outputting, adding fractions, reducing them, finding the maximum and minimum fractions, and sorting them. The main function orchestrates the input of fractions and outputs their sum after processing.

Uploaded by

nguyentrivan0904
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

/******************************************************************************

Online C++ Compiler.


Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
using namespace std;

struct phan_so
{
int tu;
int mau;
};
struct mang_phan_so
{
phan_so phan_tu[100];
int do_dai_mang;
};

void nhap_phan_so(phan_so &phan_so);


void xuat_phan_so(phan_so phan_so);
void nhap_so_phan_tu(mang_phan_so &mang);
void nhap_mang_phan_so(mang_phan_so &mang);
void xuat_mang_phan_so(mang_phan_so mang);
phan_so tong(phan_so phan_so1,phan_so phan_so2);
phan_so tong_mang(mang_phan_so mang);
void rut_gon(phan_so &phan_so);
phan_so tim_phan_so_lon_nhat(mang_phan_so mang);
phan_so tim_phan_so_nho_nhat(mang_phan_so mang);
int nhap_va_dem(mang_phan_so mang);
void xoa_phan_tu_giong_phan_so_da_nhap(mang_phan_so mang);
void sap_xep(mang_phan_so mang,bool so_sanh);
void dem_va_in(mang_phan_so mang);

int main()
{
phan_so phan_so1,phan_so2;
mang_phan_so mang;
nhap_so_phan_tu(mang);
nhap_mang_phan_so(mang);
xuat_phan_so(tong_mang(mang));
return 0;
}

void nhap_phan_so(phan_so &phan_so)


{
cout <<"moi ban nhap tu:";
cin >> phan_so.tu;
cout <<"moi ban nhap mau:";
cin >> phan_so.mau;
if(phan_so.mau==0)
{
cout <<"moi ban nhap lai mau";
cin >> phan_so.mau;
}
}
void xuat_phan_so(phan_so phan_so)
{
cout << phan_so.tu << "/" << phan_so.mau << endl ;
}
phan_so tong(phan_so phan_so1,phan_so phan_so2)
{
phan_so ket_qua;
rut_gon(phan_so1);
rut_gon(phan_so2);
ket_qua.tu=phan_so1.tu*phan_so2.mau+phan_so2.tu*phan_so1.mau;
ket_qua.mau=phan_so2.mau*phan_so1.mau;
rut_gon(ket_qua);
return ket_qua;
}
void rut_gon(phan_so &phan_so)
{
int so_be_hon=min(phan_so.tu,phan_so.mau);
for(int i=2;i<so_be_hon;i++)
{
if(phan_so.tu%i==0 && phan_so.mau%i==0)
{
phan_so.tu/=i;
phan_so.mau/=i;
}
if(phan_so.tu<i || phan_so.mau<i) return;
}
return;
}
phan_so tim_phan_so_lon_nhat(mang_phan_so mang)
{
phan_so phan_so_lon_nhat=mang.phan_tu[0];
for(int i=1;i<mang.do_dai_mang;i++)
{

if(float(mang.phan_tu[i].tu/mang.phan_tu[i].mau)>float(phan_so_lon_nhat.tu/
phan_so_lon_nhat.mau))
{
phan_so_lon_nhat=mang.phan_tu[i];
}
}
return phan_so_lon_nhat;
}
phan_so tim_phan_so_nho_nhat(mang_phan_so mang,int n)
{
phan_so phan_so_nho_nhat=mang.phan_tu[0];
for(int i=1;i<mang.do_dai_mang;i++)
{

if(float(mang.phan_tu[i].tu/mang.phan_tu[i].mau)<float(phan_so_nho_nhat.tu/
phan_so_nho_nhat.mau))
{
phan_so_nho_nhat=mang.phan_tu[i];
}
}
return phan_so_nho_nhat;
}
int nhap_va_dem(mang_phan_so mang)
{
phan_so phan_so_nhap_vao;
nhap_phan_so(phan_so_nhap_vao);
int dem=0;
rut_gon(phan_so_nhap_vao);
for(int i=0;i<mang.do_dai_mang;i++)
{

if(float(mang.phan_tu[i].tu/mang.phan_tu[i].mau)==float(phan_so_nhap_vao.tu/
phan_so_nhap_vao.mau))
{
dem++;
}
}
return dem;
}
void xoa_phan_tu_giong_phan_so_da_nhap(mang_phan_so mang)
{
phan_so phan_so_nhap_vao;
nhap_phan_so(phan_so_nhap_vao);
for(int i=0;i<mang.do_dai_mang;i++)
{
int j=i;

while(float(mang.phan_tu[i].tu/mang.phan_tu[i].mau)==float(phan_so_nhap_vao.tu/
phan_so_nhap_vao.mau)&&j<mang.do_dai_mang)
{
j++;
mang.do_dai_mang--;
}
if(j==mang.do_dai_mang) break;
mang.phan_tu[i]=mang.phan_tu[j];
i+=(j-i);
}
return;
}
void sap_xep(mang_phan_so mang,bool so_sanh) //true giam dan, false tang dan
{
for(int i=0;i<mang.do_dai_mang-1;i++)
{
float m=(mang.phan_tu[i].tu/mang.phan_tu[i].mau);
int k=i;
for(int j=i+1;j<mang.do_dai_mang;j++)
{
if(so_sanh==(float(mang.phan_tu[j].tu/mang.phan_tu[j].mau)>m))
{
m=float(mang.phan_tu[j].tu/mang.phan_tu[j].mau);
k=j;
}
}
swap(mang.phan_tu[i],mang.phan_tu[k]);
}
return ;
}
void dem_va_in(mang_phan_so mang)
{
for(int i=0;i<mang.do_dai_mang;i++)
{
if(mang.phan_tu[i].tu/mang.phan_tu[i].mau>0)
{
xuat_phan_so(mang.phan_tu[i]);
cout << " ";
}
}
cout << endl;
for(int i=0;i<mang.do_dai_mang;i++)
{
if(mang.phan_tu[i].tu/mang.phan_tu[i].mau<0)
{
xuat_phan_so(mang.phan_tu[i]);
cout << " ";
}
}
cout << endl;
for(int i=0;i<mang.do_dai_mang;i++)
{
if(mang.phan_tu[i].tu==0)
{
xuat_phan_so(mang.phan_tu[i]);
cout << " ";
}
}
cout << endl;
}
void nhap_so_phan_tu(mang_phan_so &mang)
{
cin >> mang.do_dai_mang;
return;
}
void nhap_mang_phan_so(mang_phan_so &mang)
{
for(int i=0;i<mang.do_dai_mang;i++)
{
nhap_phan_so(mang.phan_tu[i]);
}
return;
}
void xuat_mang_phan_so(mang_phan_so mang)
{
for(int i=0;i<mang.do_dai_mang;i++)
{
xuat_phan_so(mang.phan_tu[i]);
}
return;
}
phan_so tong_mang(mang_phan_so mang)
{
phan_so tong_mang=mang.phan_tu[0];
for(int i=1;i<mang.do_dai_mang;i++)
{
tong_mang=tong(tong_mang,mang.phan_tu[i]);
}
return tong_mang;
}

You might also like