Tôi muốn đơn giản hóa một phần trong ứng dụng của mình. Phần nhỏ là, x/y trong đó x và y là số nguyên. Tôi muốn đơn giản hóa phần nhỏ thành dạng đơn giản nhất. Bất cứ ai có thể vui lòng cho tôi gợi ý làm thế nào để làm điều đó. Cảm ơn trước.Cách đơn giản hóa một phần
5
A
Trả lời
21
- Tính ước số chung lớn nhất đối với x và y
- Divide cả trong số họ bởi GCD
Euclid's algorithm là một cách dễ dàng để tính toán GCD.
14
Divide cả bởi gcd(x,y)
Các Binary GCD algorithm là một cách nhanh chóng để tính toán GCD trên một máy tính.
-4
#include<iostream>
using namespace std;
struct fraction
{
int n1, d1, n2, d2, s1, s2;
};
void simplification(int a,int b)
{
bool e = true;
int t; int z;
for (int i = (a*b); i > 1;i--)
{ if ((a%i==0)&&(b%i==0))
{
t = a/i;
z = b/i;
}
else
{
e = false;
}
}
cout << "simplest form=" << t << "/" << z << endl;
}
void sum(int num1, int deno1, int num2, int deno2)
{
int k,y;
k = num1* deno2 + num2*deno1;
y = deno2*deno1;
cout << "addition of given fraction = " << k << "/" << y << endl;
simplification(k, y);
}
void sub(int num1, int deno1, int num2, int deno2)
{
int k, y;
k = num1*deno2 - num2*deno1;
y = deno1*deno2;
cout << "Substraction of given fraction = " << k << "/" << y << endl;
}
void mul(int num1, int deno1, int num2, int deno2)
{
int k, y;
k = num1*num2;
y = deno1*deno2;
cout << "multiplication of given fration= " << k<< "/" <<y; cout<< endl;
simplification(k, y);
}
void div(int num1, int deno1, int num2, int deno2)
{
int k, y;
;
k = num1*deno1;
y = deno1*num2;
cout << "division of given fraction" << k << "/" << y << endl;
simplification(k, y);
}
int main()
{ fraction a;
cout << "enter numirator of f1=";cin >> a.n1;
cout << "enter denominator of f1=";cin >> a.d1;
cout << "enter numirator of f2=";cin >> a.n2;
cout << "enter denominator of f2=";cin >> a.d2;
cout << "f1= " << a.n1 << "/" << a.d1 << endl;
cout << "f2= " << a.n2 << "/" << a.d2 << endl;
mul(a.n1, a.d1, a.n2, a.d2);
div(a.n1, a.d1, a.n2, a.d2);
sub(a.n1, a.d1, a.n2, a.d2);
sum(a.n1, a.d1, a.n2, a.d2);
system("pause");
}
Chào mừng bạn đến với SO. Vui lòng cung cấp một số ngữ cảnh cho câu trả lời của bạn, vì các câu trả lời chỉ có mã không đáp ứng các tiêu chuẩn SO. Và hãy định dạng lại mã của bạn. Xem http://stackoverflow.com/help/how-to-answer –