بسم الله الرحمن الرحيم
السلام عليكم ورحمة الله وبركاته
ارجو ان يكون هذا المطلوب
كود:
#include <iostream.h>
int first_numerator,second_numerator;
int first_denominator,second_denominator;
int res_up,res_down,whole_num,org_up,org_down;
int Low_Value, High_Value;
int a,b,c,d;
int pick;
char answer;
bool check,check2;
char symbol;
void cancel_fraction()
{
org_up = res_up/(res_up-first_denominator);
org_down = res_down/(res_up-first_denominator);
for (a=1;a<res_up;a++)
{
check=false;
for (b=1;b<a;b++)
{
if ((a%b==0) && (a!=1) && (b!=1))
{
check=true;
}
}
if ((check==false) && (a!=1))
{
while ((res_up%a==0) && (res_down%a==0))
{
res_up=res_up/a;
res_down=res_down/a;
}
}
}
for (c=1;c<res_down;c++)
{
check2=false;
for (d=1;d<c;d++)
{
if ((c%d==0) && (c!=1) && (d!=1))
{
check2=true;
}
}
if ((check2==false) && (c!=1))
{
while ((res_up%c==0) && (res_down%c==0))
{
res_up=res_up/c;
res_down=res_down/c;
}
}
}
}
void calculate()
{
switch(pick)
{
case 1 :res_up=(second_denominator*first_numerator) + (first_denominator*second_numerator);
res_down=first_denominator*second_denominator;
symbol = '+';
break;
case 2 :res_up=(second_denominator*first_numerator) - (first_denominator*second_numerator);
res_down=first_denominator*second_denominator;
symbol = '-';
break;
case 3 :res_up=first_numerator*second_numerator;
res_down=first_denominator*second_denominator;
symbol = '*';
break;
case 4 :res_up=first_numerator*second_denominator;
res_down=first_denominator*second_numerator;
symbol = '/';
break;
}
}
void input()
{
do
{
cout << "Enter the numerator and denominator\nof the first fraction:";
cin >> first_numerator >> first_denominator;
}while(first_numerator < 0);
do
{
cout << "Enter the numerator and denominator\nof the second fraction:";
cin >> second_numerator >> second_denominator;
}while(second_numerator < 0);
do
{
cout << "\nMenu of Operations\n";
cout << "Add...................1\n";
cout << "Subtract..............2\n";
cout << "Multiply..............3\n";
cout << "Divide................4\n\n";
cout << "Which operation?";
cin >> pick;
}while(pick != 1 && pick != 2 && pick != 3 && pick != 4);
}
void output()
{
if (res_down==1)
{
cout << "\n " << first_numerator << " " << second_numerator << " " << res_up << " " << res_up << endl;
cout << "--- " << symbol << " --- = --- =\n";
cout << " " << first_denominator << " " << second_denominator << " " << res_down << endl;
cout <<"\nDo you want to try again ? (y/n) : ";
cin >> answer;
if (answer=='y' || answer=='Y')
{
input();
}
else
{}
}
else
{
if(res_up > res_down)
{
Low_Value = res_down;
High_Value = res_up;
}
else
{
High_Value = res_down;
Low_Value = res_up;
}
for(int i = Low_Value; i > 0;i--)
{
if( (Low_Value % i == 0) && (High_Value % i == 0) )
{
res_up=res_up/i;
res_down= res_down/i;
}
}
if(res_up > res_down)
{
whole_num = res_up%res_down;
res_up = res_up-(res_down*whole_num);
cout << "\n " << first_numerator << " " << second_numerator << " " << org_up << " " << whole_num << " " << res_up << endl;
cout << "--- " << symbol << " --- = --- = ---\n";
cout << " " << first_denominator << " " << second_denominator << " " << org_down << " " << res_down << endl;
cout <<"\nDo you want to try again ? (y/n) : ";
cin >> answer;
}
else
{
cout << "\n " << first_numerator << " " << second_numerator << " " << org_up << " " << res_up << endl;
cout << "--- " << symbol << " --- = --- = ---\n";
cout << " " << first_denominator << " " << second_denominator << " " << org_down << " " << res_down << endl;
cout <<"\nDo you want to try again ? (y/n) : ";
cin >> answer;
}
if (answer=='y' ||answer=='Y')
{
input();
}
else
{}
}
}
int main()
{
input();
calculate();
cancel_fraction();
output();
return 0;
}
وشكرا