Advertisement
aod6060

Fraction Program... ( This is a really ugly version)

May 6th, 2012
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. /*
  2.     This is a simple Fraction Math Program
  3.    
  4.     This is a really retarded version of it
  5. */
  6.  
  7. #include <iostream>
  8.  
  9. using namespace std;
  10.  
  11. // This function allows for typing in a fraction
  12. void typed_fraction(int& num, int& dem) {
  13.     cin >> num;
  14.     cout << "__" << endl;
  15.     cin >> dem;
  16. }
  17.  
  18. // This will make both fraction have the same denomenator
  19. void equalize_fraction(int& n1, int& d1, int& n2, int& d2) {
  20.     int temp1 = d1, temp2 = d2;
  21.    
  22.     n1 *= temp2;
  23.     d1 *= temp2;
  24.    
  25.     n2 *= temp1;
  26.     d2 *= temp1;
  27. }
  28.  
  29. // This simply prints of the answer
  30. void print_answer(char op, int& n1, int& d1, int& n2, int& d2, int& n3, int& d3) {
  31.     if(n3 == d3) {
  32.         cout << "1";
  33.     } else {
  34.         cout << n3 << "/" << d3;
  35.     }
  36.    
  37.     cout << " = " << n1 << "/" << d1 << " " << op << " "<< n2 << "/" << d2 << endl;
  38. }
  39.  
  40. int main() {
  41.     unsigned char op;
  42.     int num1, dem1, num2, dem2, anum, adem;
  43.    
  44.     cout << "Please type in the first fraction" << endl;
  45.     typed_fraction(num1, dem1);
  46.    
  47.     cout << "Please type in the second fraction" << endl;
  48.     typed_fraction(num2, dem2);
  49.    
  50.     cout << "Please type in the operator, (+, -, *, /) " << endl;
  51.    
  52.     cin >> op;
  53.    
  54.     if(op == '+') {
  55.        
  56.         if(dem1 != dem2) {
  57.             equalize_fraction(num1, dem1, num2, dem2);
  58.         }
  59.        
  60.         anum = num1 + num2;
  61.         adem = dem1;
  62.        
  63.         print_answer(op, num1, dem1, num2, dem2, anum, adem);
  64.        
  65.     } else if(op == '-') {
  66.        
  67.         if(dem1 != dem2) {
  68.             equalize_fraction(num1, dem1, num2, dem2);
  69.         }
  70.        
  71.         anum = num1 - num2;
  72.         adem = dem1;
  73.         print_answer(op, num1, dem1, num2, dem2, anum, adem);
  74.        
  75.     } else if(op == '*') {
  76.        
  77.         anum = num1 * num2;
  78.         adem = dem1 * dem2;
  79.        
  80.         print_answer(op, num1, dem1, num2, dem2, anum, adem);
  81.     } else if(op == '/') {
  82.        
  83.         anum = num1 * dem2;
  84.         adem = dem1 * num2;
  85.        
  86.         print_answer(op, num1, dem1, num2, dem2, anum, adem);
  87.        
  88.     } else {
  89.         cout << "Invalid operator " << op << " please use (+, -, *, /)" << endl;
  90.     }
  91.    
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement