Notes
![]() ![]() Notes - notes.io |
using namespace std;
long gcd(long n, long d){
long gcd = 0;
long max = 0;
if(n < d){
max = n;
}else max = d;
while(max > 0){
if(n % max == 0 && d % max == 0){
gcd = max;
break;
}
max--;
}
return gcd;
}
class Rational{
long n_;
long d_;
public:
Rational(long n, long d){
n_= n;
d_= d;
}
long get_numerator() const{
return n_;
}
long get_denominator() const{
return d_;
}
Rational& set_numerator(long n){
n_ = n;
return *this;
}
Rational& set_denominator(long d){
d_ = d;
return *this;
}
Rational& add(Rational& other){
if(d == other.d){
n_+= other.n_;
}else{
n_= n_* other.d_;
other.n = other.n * d_;
n_= n_+ other.n_;
d_= d_* other.d_;
}
long div = gcd(n_, d_);
n_= n_/div;
d_= d_/div;
return *this;
}
Rational& sub(Rational& other){
if(d == other.d){
n_-= other.n_;
}else{
n_= n_* other.d_;
other.n = other.n * d_;
n_= n_- other.n_;
d_= d_* other.d_;
}
long div = gcd(n_, d_);
n_= n_/div;
d_= d_/div;
return *this;
}
Rational& mul(const Rational& other){
n_*= other.n_;
d_*= other.d_;
long div = gcd(n_, d_);
n_= n_/div;
d_= d_/div;
return *this;
}
Rational& div( Rational& other){
long val = other.n_;
other.n_= other.d_;
other.d_= val;
n_*= other.n_;
d_*= other.d_;
long div = gcd(n_, d_);
n_= n_/div;
d_= d_/div;
return *this;
}
void print() const{
cout << "(" << n << "/" << d << ")" << endl;
}
Rational operator-(void) const{
Rational result(-n_, -d_);
return result;
}
};
Rational operator-(const Rational& r1, Rational& r2){
Rational result(r1.get_numerator(), r1.get_denominator());
result.sub(r2);
return result;
}
Rational operator+(const Rational& r1, Rational& r2){
Rational result(r1.get_numerator(), r1.get_denominator());
result.add(r2);
return result;
}
Rational operator*(const Rational& r1, const Rational& r2){
Rational result(r1.get_numerator(), r1.get_denominator());
result.mul(r2);
return result;
}
Rational operator/(const Rational& r1, Rational& r2){
Rational result(r1.get_numerator(), r1.get_denominator());
result.div(r2);
return result;
}
ostream& operator<<(ostream& out, const Rational& r){
out << "(" << r.get_numerator() << "/" << r.get_denominator() << ")";
return out;
}
istream& operator>>(istream& in, Rational& r){
char ch=0;
long n=0, d, p=0;
in >> ch;
if(ch!='('){
in.clear(ios_base::badbit);
return in;
}
in >> n;
in >> ch;
if(ch!='/'){
in.clear(ios_base::badbit);
return in;
}
in >> d;
in >> ch;
if(ch!=')'){
in.clear(ios_base::badbit);
return in;
}
if(in.good()) {
r.set_numerator(n).set_denominator(d);
}
return in;
}
int main(){
Rational r1(0,0);
Rational r2(0,0);
Rational r3(0,0);
char op, eq;
while(true){
(cin >> r1) >> op >> r2 >> eq;
if(cin.bad()) {
break;
}
switch (op) {
case '+': cout << ">" << " " << r1 + r2 << endl;
cout << ">";
break;
case '-': cout << ">" << " " << r1 - r2 << endl;
cout << ">";
break;
case '*': cout << ">" << " " << r1 * r2 << endl;
cout << ">";
break;
case '/': cout << ">" << " " << r1 / r2 << endl;
cout << ">";
break;
}
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team