#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "math.h"
#include "stdio.h"
using namespace std;
//2 Khai bao kieu
typedef unsigned int word;
typedef unsigned char byte;
typedef unsigned int mang[100];
//2.1 Khai bao cau truc
typedef unsigned struct PS {
int tuso;
int mauso;
};
//Ham nhap gia tri
void nhapps(PS &a);
//Ham in gia tri
void inps(PS a);
//Ham toi gian
void toigian(PS &a);
//Tim USCLN
unsigned uscln(unsigned a, unsigned b);
//Tinh tich 2 phan so
PS nhanps(PS a, PS b);
int main()
{
PS a, b, t;
do
{
////a = b;
//// Thao tac tren tung thanh phan nho tren cau truc
////Nhap lieu cho tu so
//cout << "Nhap tu so: ";
//cin >> a.tuso;
//do {
// cout << "Nhap mau so:";
// cin >> a.mauso;
//} while (a.mauso == 0);
cout << "Nhap phan so thu nhat: \n";
nhapps(a);
cout << "Nhap phan so thu hai:\n";
nhapps(b);
t = nhanps(a, b);
cout << "Phan so tich =";
inps(t);
cout << endl;
cout << "Nhan ESC de thoat! ";
} while (_getch() != 27);
return 0;
}
//Ham nhap gia tri
void nhapps(PS &a)
cout << "Nhap tu so: ";
cin >> a.tuso;
do {
cout << "Nhap mau so:";
cin >> a.mauso;
} while (a.mauso == 0);
//Ham in gia tri
void inps(PS a)
{
cout << a.tuso << "/" << a.mauso;
}
//Ham toi gian
void toigian(PS &a) {
unsigned u = uscln(a.tuso, a.mauso);
a.tuso /= u;
a.mauso /= u;
}
//Tim USCLN
unsigned uscln(unsigned a, unsigned b) {
if (!a && !b) return 0;//loi
if (!b) return a; // b ==0
unsigned r = a%b;
while (r) {
a = b;
b = r;
r = a%b;
}
return b;
}
//Tinh tich 2 phan so
PS nhanps(PS a, PS b) {
PS t; // bien cuc bo chua tich ---> tra ve`
t.tuso = a.tuso*b.tuso;
t.mauso = a.mauso*b.mauso;
toigian(t); // toi gian phan so tich
return t;
}
em có thử xài mấy cách thông thường, hay copy paste lại vẫn bị