Đây là lời giải của tớ:
Code
#include<iostream>
#include<math.h>
using namespace std;
struct phanso
{
int tu;
int mau;
};
int ucln(int a, int b)
{
int c=abs(a);
int d=abs(b);
while(c!=d)
{
if(c>d) c=c-d;
else d=d-c;
}
return d;
}
int main()
{
phanso m;
cout<<"tu so bang: ";cin>>m.tu;
cout<<"mau so bang: ";cin>>m.mau;
if(m.mau==0) cout<<" khong thoa man"<<endl;
else
{
if(m.tu==0) cout<<"phan so bang 0"<<endl;
else
{
int x;
x=ucln(m.tu, m.mau);
m.tu=m.tu/x;
m.mau=m.mau/x;
cout<<"phan so o dang toi gian la: ";
if(abs(m.mau)==1)
{
if((m.tu*m.mau)<0) cout<<"-"<<abs(m.tu)<<endl;
else cout<<abs(m.tu)<<endl;
}
else
{
if((m.tu*m.mau)<0) cout<<"-"<<abs(m.tu)<<"/"<<abs(m.mau)<<endl;
else cout<<abs(m.tu)<<"/"<<abs(m.mau)<<endl;
}
}
}
system("pause");
}