-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbai1.cpp
45 lines (36 loc) · 1.13 KB
/
bai1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
using namespace std;
struct MixedNumber {
int wholePart; // phần nguyên
int numerator; // tử số
int denominator; // mẫu số
};
// hàm tìm ước chung lớn nhất của hai số nguyên a và b
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
// hàm rút gọn phân số
void reduceFraction(int &numerator, int &denominator) {
int gcdNumber = gcd(numerator, denominator);
numerator /= gcdNumber;
denominator /= gcdNumber;
}
// hàm nhập hỗn số
void (MixedNumber &mixedNum) {
cout << "Nhap phan nguyen: ";
cin >> mixedNum.wholePart;
cout << "Nhap tu so: ";
cin >> mixedNum.numerator;
do {
cout << "Nhap mau so (khac 0): ";
cin >> mixedNum.denominator;
} while (mixedNum.denominator == 0);
// rút gọn phân số
reduceFraction(mixedNum.numerator, mixedNum.denominator);
}
// hàm xuất hỗn số
void outputMixedNumber(MixedNumber mixedNum) {
cout << mixedNum.wholePart << " " << mixedNum.numerator << "/" << mixedNum.denominator << endl;
}