迴圈
輸入
先算出分子分母
再約分
注意負號位置
輸出
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int a, b, c, d;
int numerator , denumerator, gcd;
char mani;
while(cin >> a >> b >> c >> d >> mani)
{
switch(mani)
{
case '+':
numerator = a*d + c*b;
denumerator = b*d;
break;
case '-':
numerator = a*d - c*b;
denumerator = b*d;
break;
case '*':
numerator = a*c;
denumerator = b*d;
break;
case '/':
numerator = a*d;
denumerator = b*c;
break;
default:
break;
}
gcd = __gcd(numerator, denumerator);
numerator /= gcd;
denumerator /= gcd;
if(numerator % denumerator == 0)
cout << numerator / denumerator << endl;
else
{
if(denumerator < 0 || (numerator < 0 && denumerator < 0))
{
numerator *= -1;
denumerator *= -1;
}
cout << numerator << "/" << denumerator << endl;
}
}
return 0;
}
留言列表