Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- using namespace std;
- int main()
- {
- int numeratorOne;
- int denominatorOne;
- int numeratorTwo;
- int denominatorTwo;
- int numeratorResult;
- int denominatorResult;
- int operation;
- cout << "Enter the Numerator of the first fraction: ";
- while(true)
- {
- if(cin >> numeratorOne)
- {
- break;
- }
- else
- {
- cin.clear();
- cin.ignore();
- cout << "Sorry I didn't get that, please enter the numerator of the first fraction: ";
- }
- }
- cout << "Enter Denominator of the first fraction: ";
- while(true)
- {
- if(cin >> denominatorOne)
- {
- if(denominatorOne == 0)
- {
- cout << "Sorry the denominator can not be zero, please enter a non-zero integer: ";
- }
- else
- {
- break;
- }
- }
- else
- {
- cin.clear();
- cin.ignore();
- cout << "Sorry I didn't get that, please enter the denominator of the first fraction: ";
- }
- }
- cout << "Enter the Numerator of the second fraction: ";
- while(true)
- {
- if(cin >> numeratorTwo)
- {
- break;
- }
- else
- {
- cin.clear();
- cin.ignore();
- cout << "Sorry I didn't get that, please enter the numerator of the second fraction: ";
- }
- }
- cout << "Enter Denominator of the second fraction: ";
- while(true)
- {
- if(cin >> denominatorTwo)
- {
- if(denominatorTwo == 0)
- {
- cout << "Sorry the denominator can not be zero, please enter a non-zero integer: ";
- }
- else
- {
- break;
- }
- }
- else
- {
- cin.clear();
- cin.ignore();
- cout << "Sorry I didn't get that, please enter the denominator of the second fraction: ";
- }
- }
- cout << "Please select which operation you would like to do. "
- << "\n" << "1-Add"
- << "\n" << "2-Subtract"
- << "\n" << "3-Multiply"
- << "\n" << "4-Divide"
- << "\n" << "Select from the above list: ";
- while(true)
- {
- if(cin >> operation)
- {
- if(operation<1 || operation >4)
- {
- cout << "Sorry please select a number from 1-4: ";
- }
- else
- {
- break;
- }
- }
- else
- {
- cin.clear();
- cin.ignore();
- cout << "Sorry I didn't get that, please enter a number between 1-4: ";
- }
- }
- //Add
- if (operation == 1)
- {
- numeratorResult = (numeratorOne * denominatorTwo + numeratorTwo * denominatorOne);
- denominatorResult = (numeratorOne * denominatorTwo);
- cout << numeratorOne << "/" << denominatorOne
- << " + "
- << numeratorTwo << "/" << denominatorTwo
- << " = "
- << numeratorResult << "/" << denominatorResult << "\n";
- }
- //Subtract
- if (operation ==2)
- {
- numeratorResult = (numeratorOne * denominatorTwo - numeratorTwo * denominatorOne);
- denominatorResult = (numeratorOne * denominatorTwo);
- cout << numeratorOne << "/" << denominatorOne
- << " - "
- << numeratorTwo << "/" << denominatorTwo
- << " = "
- << numeratorResult << "/" << denominatorResult << "\n";
- }
- //Multiply
- if(operation == 3)
- {
- numeratorResult = numeratorOne * numeratorTwo;
- denominatorResult = denominatorOne * denominatorTwo;
- cout << numeratorOne << "/" << denominatorOne
- << " x "
- << numeratorTwo << "/" << denominatorTwo
- << " = "
- << numeratorResult << "/" << denominatorResult << "\n";
- }
- //Divide
- if(operation == 4)
- {
- numeratorResult = numeratorOne * denominatorTwo;
- denominatorResult = denominatorOne * numeratorTwo;
- cout << numeratorOne << "/" << denominatorOne
- << " / "
- << numeratorTwo << "/" << denominatorTwo
- << " = "
- << numeratorResult << "/" << denominatorResult << "\n";
- }
- system("PAUSE");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement