Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This is a simple Fraction Math Program
- This is a really retarded version of it
- */
- #include <iostream>
- using namespace std;
- // This function allows for typing in a fraction
- void typed_fraction(int& num, int& dem) {
- cin >> num;
- cout << "__" << endl;
- cin >> dem;
- }
- // This will make both fraction have the same denomenator
- void equalize_fraction(int& n1, int& d1, int& n2, int& d2) {
- int temp1 = d1, temp2 = d2;
- n1 *= temp2;
- d1 *= temp2;
- n2 *= temp1;
- d2 *= temp1;
- }
- // This simply prints of the answer
- void print_answer(char op, int& n1, int& d1, int& n2, int& d2, int& n3, int& d3) {
- if(n3 == d3) {
- cout << "1";
- } else {
- cout << n3 << "/" << d3;
- }
- cout << " = " << n1 << "/" << d1 << " " << op << " "<< n2 << "/" << d2 << endl;
- }
- int main() {
- unsigned char op;
- int num1, dem1, num2, dem2, anum, adem;
- cout << "Please type in the first fraction" << endl;
- typed_fraction(num1, dem1);
- cout << "Please type in the second fraction" << endl;
- typed_fraction(num2, dem2);
- cout << "Please type in the operator, (+, -, *, /) " << endl;
- cin >> op;
- if(op == '+') {
- if(dem1 != dem2) {
- equalize_fraction(num1, dem1, num2, dem2);
- }
- anum = num1 + num2;
- adem = dem1;
- print_answer(op, num1, dem1, num2, dem2, anum, adem);
- } else if(op == '-') {
- if(dem1 != dem2) {
- equalize_fraction(num1, dem1, num2, dem2);
- }
- anum = num1 - num2;
- adem = dem1;
- print_answer(op, num1, dem1, num2, dem2, anum, adem);
- } else if(op == '*') {
- anum = num1 * num2;
- adem = dem1 * dem2;
- print_answer(op, num1, dem1, num2, dem2, anum, adem);
- } else if(op == '/') {
- anum = num1 * dem2;
- adem = dem1 * num2;
- print_answer(op, num1, dem1, num2, dem2, anum, adem);
- } else {
- cout << "Invalid operator " << op << " please use (+, -, *, /)" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement