Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <stdlib.h>
- #include <unistd.h>
- using namespace std;
- class Fraction{
- int numer, denom;
- int find_gcd(int a, int b){
- return a==0?b:find_gcd(b%a, a);
- }
- public:
- void setNum(int num){
- numer = num;
- }
- void setDen(int num){
- denom = num;
- }
- int getNum(){
- return numer;
- }
- int getDen(){
- return denom;
- }
- Fraction operator +(const Fraction& fr){
- Fraction f;
- if(this->denom!=fr.denom){
- f.setDen(this->denom*fr.denom);
- f.setNum(this->numer*fr.denom + this->denom*fr.numer);
- }else{
- f.setDen(this->denom);
- f.setNum(this->numer+fr.numer);
- }
- f.reduce_fraction();
- return f;
- }
- Fraction operator -(const Fraction& fr){
- Fraction f;
- if(this->denom!=fr.denom){
- f.setDen(this->denom*fr.denom);
- f.setNum(this->numer*fr.denom - this->denom*fr.numer);
- }else{
- f.setDen(this->denom);
- f.setNum(this->numer-fr.numer);
- }
- f.reduce_fraction();
- return f;
- }
- Fraction operator *(const Fraction& fr){
- Fraction f;
- f.setNum(this->numer*fr.numer);
- f.setDen(this->denom*fr.denom);
- f.reduce_fraction();
- return f;
- }
- Fraction operator /(const Fraction& fr){
- Fraction f;
- f.setNum(this->numer*fr.denom);
- f.setDen(this->denom*fr.numer);
- f.reduce_fraction();
- return f;
- }
- Fraction operator %(const Fraction& fr){
- Fraction f;
- if(this->denom!=fr.denom){
- f.setDen(this->denom*fr.denom);
- f.setNum(this->numer*fr.denom % this->denom*fr.numer);
- }else{
- f.setDen(this->denom);
- f.setNum(this->numer%fr.numer);
- }
- f.reduce_fraction();
- return f;
- }
- void power(int power){
- this->numer = pow(this->numer,power);
- this->denom = pow(this->denom,power);
- this->reduce_fraction();
- }
- void reduce_fraction(){
- int gcd = find_gcd(numer, denom);
- this->numer/=gcd;
- this->denom/=gcd;
- }
- void print_fraction(){
- if(numer==denom){
- cout << numer << endl;
- }else{
- cout << numer << "/" << denom << endl;
- }
- }
- };
- void printMenu(){
- system("cls");
- cout << "1. Addition (+)" << endl;
- cout << "2. Substraction (-)" << endl;
- cout << "3. Multiplication (*)" << endl;
- cout << "4. Division (/)" << endl;
- cout << "5. Modulo (%)" << endl;
- cout << "6. Fraction to the power n" << endl;
- cout << "7. Exit" << endl;
- cout << "Enter the operation: " << endl;
- }
- void getFraction(Fraction& first, Fraction& second){
- int temp;
- cout << "Enter first fraction numerator and denominator" << endl;
- cin >> temp; first.setNum(temp);
- cin >> temp; first.setDen(temp);
- cout << "Enter the second fraction numerator and denominator" << endl;
- cin >> temp; second.setNum(temp);
- cin >> temp; second.setDen(temp);
- }
- int main(){
- Fraction first, second, result;
- int input, temp, p;
- do{
- printMenu();
- cin >> input;
- switch(input){
- case 1:
- getFraction(first, second);
- result = first+second;
- break;
- case 2:
- getFraction(first, second);
- result = first-second;
- break;
- case 3:
- getFraction(first, second);
- result = first*second;
- break;
- case 4:
- getFraction(first, second);
- result = first/second;
- break;
- case 5:
- getFraction(first, second);
- result = first%second;
- break;
- case 6:
- cout << "Enter fraction numerator and denominator" << endl;
- cin >> temp; result.setNum(temp);
- cin >> temp; result.setDen(temp);
- cout << "Enter the power (integer): ";
- cin >> p;
- result.power(p);
- break;
- case 7:
- cout << "Exiting...";
- exit(0);
- break;
- default:
- cout << "Only 1 - 6 allowed! Restarting..." << endl;
- sleep(1);
- continue;
- }
- cout << "Result : ";
- result.print_fraction();
- cout << endl;
- system("pause");
- }while(true);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement