• API
• FAQ
• Tools
• Archive
A Pastebin account makes a great Christmas gift
SHARE
TWEET

# Untitled

a guest Nov 14th, 2017 47 Never
ENDING IN00days00hours00mins00secs

1. #include <cmath>
2. #include <cstdio>
3. #include <vector>
4. #include <iostream>
5. #include <algorithm>
6. using namespace std;
7.
8. int gcd(int number1, int number2) {
9.     if(number1 == 0){
10.         return number2;
11.     }
12.     int gcd = abs(number1 < number2 ? number1 : number2);
13.     while (gcd != 1) {
14.         if (number1 % gcd == 0 && number2 % gcd == 0) {
15.             return gcd;
16.         }
17.         --gcd;
18.     }
19.     return 1;
20. }
21.
22. class rational {
23.     int numerator_;
24.     int denominator_;
25.
26. public:
27.     rational(int numerator, int denominator){
28.         int divisor = gcd(numerator, denominator);
29.         numerator_ = numerator / divisor;
30.         denominator_ = denominator / divisor;
31.     }
32.
33.     int get_numerator() const {
34.         return numerator_;
35.     }
36.
37.     rational& set_numerator(int numerator){
38.         numerator_ = numerator;
39.         return *this;
40.     }
41.
42.     int get_denominator() const {
43.         return denominator_;
44.     }
45.
46.     rational& set_denominator(int denominator){
47.         denominator_ = denominator;
48.         return *this;
49.     }
50.
51.     void fix(){
52.         int gcd1 = gcd(numerator_, denominator_);
53.         numerator_ /= gcd1;
54.         denominator_ /= gcd1;
55.         if(denominator_ < 0){
56.             denominator_ *= -1;
57.             numerator_ *= -1;
58.         }
59.     }
60.
62.         long numerator_first = other.denominator_ * numerator_;
63.         long numerator_second = denominator_ * other.numerator_;
64.
65.         numerator_ = numerator_first + numerator_second;
66.         denominator_ *= other.denominator_;
67.         this->fix();
68.
69.         return *this;
70.     }
71.
72.     rational& substract(const rational& other){
73.         long numerator_first = other.denominator_ * numerator_;
74.         long numerator_second = denominator_ * other.numerator_;
75.
76.         numerator_ = numerator_first - numerator_second;
77.         denominator_ *= other.denominator_;
78.         this->fix();
79.
80.         return *this;
81.     }
82.
83.     rational& multiplication(const rational& other){
84.         numerator_ = numerator_ * other.numerator_;
85.         denominator_ *= other.denominator_;
86.         this->fix();
87.
88.         return *this;
89.     }
90.
91.     rational& division(const rational& other){
92.         numerator_ = numerator_ * other.denominator_;
93.         denominator_ *= other.numerator_;
94.         this->fix();
95.
96.         return *this;
97.     }
98.
99.     rational operator+=(const rational& input){
101.         return *this;
102.     }
103.
104.     rational operator-=(const rational& input){
105.         this->substract(input);
106.         return *this;
107.     }
108.
109.     rational operator*=(const rational& input){
110.         this->multiplication(input);
111.         return *this;
112.     }
113.     rational operator/=(const rational& input){
114.         this->division(input);
115.         return *this;
116.     }
117. };
118.
119. rational operator-(const rational& r1, const rational& r2){
120.     rational result(r1.get_numerator(), r1.get_denominator());
121.     result.substract(r2);
122.     return result;
123. }
124.
125. rational operator/(const rational& r1, const rational& r2){
126.     rational result(r1.get_numerator(), r1.get_denominator());
127.     result.division(r2);
128.     return result;
129. }
130.
131. istream& operator>>(istream& in, rational& input) {
132.     char ch=0;
133.     int numerator=0, denominator=0;
134.
135.     in >> ch; //(
136.     if(ch!='(') {
138.         return in;
139.     }
140.     in >> numerator;
141.     in >> ch; //,
142.     if(ch!='/') {
144.         return in;
145.     }
146.     in >> denominator;
147.     in >> ch; //)
148.     if(ch!=')') {
150.         return in;
151.     }
152.
153.     if(in.good()) {
154.         input.set_numerator(numerator);
155.         input.set_denominator(denominator);
156.     }
157.     return in;
158. }
159.
160. ostream& operator<<(ostream& out, const rational& result){
161.     out << "(" << result.get_numerator() << "/" << result.get_denominator() << ")" << endl;
162.     return out;
163. }
164.
165. int main(){
166.     rational result(0,1);
167.     rational input(0,1);
168.     string str;
169.     char symbol;
170.     char last;
171.
172.     while(1){
173.         cin >> input;
174.
175.         if(!cin.good()){
176.             break;
177.         }
178.
179.         result = input;
180.
181.         while(1){
182.             last = symbol;
183.
184.             cin >> symbol;
185.             if(symbol == '='){
186.                 cout << ">" << " " << result;
187.                 break;
188.             }
189.             cin >> input;
190.
191.             if(!cin.good()){
192.                 break;
193.             }
194.
195.             if(symbol == '+'){
196.                 result += input;
197.             }
198.             else if(symbol == '-'){
199.                 result = result - input;
200.             }
201.             else if(symbol == '*'){
202.                 result *= input;
203.             }
204.             else if(symbol == '/'){
205.                 result = result / input;
206.             }
207.        }
208.     }
209.     cout << ">";
210.     return 0;
211. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top