View Code of Problem 3494

#include<iostream>
#include<string>
using namespace std;
int main() {
	char s1, f1, s2, f2,o, lj;
	while (cin >> s1 >> lj >> f1 >> o >> s2 >> lj >> f2) {
		string flag = "";
		int a = s1 - '0', b = f1 - '0', c = s2 - '0', d = f2 - '0';
		if (a*d < b*c&&o=='-') {
			swap(a, c);
			swap(b, d);
			flag = "-";
		}
		a *= d;
		c *= b;
		b *= d;
		if (o == '+')a += c;
		else a -= c;
		for(int i=2;i<=a;i++)
			if (a%i == 0 && b%i == 0) {
				a /= i;
				b /= i;
				i = 1;
			}
		
		cout << flag << a;
		if (a&&b!=1)cout << lj << b;
		cout<< endl;
	}
}

Double click to view unformatted code.


Back to problem 3494