Advertisement
Ashanmaril

Fraction Calculator

Jan 17th, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.17 KB | None | 0 0
  1. //
  2. //  Fraction.cpp
  3. //  CS115-Assignment1
  4. //
  5. //  Created by Hayden Lueck on 2015-01-13.
  6. //  Copyright (c) 2015 Hayden Lueck. All rights reserved.
  7. //
  8. //  Create a calculator that adds, subtracts, multiplies, and divides fractions
  9. //
  10.  
  11. #include <iostream>
  12. #include <string>
  13.  
  14. using namespace std;
  15.  
  16. /** FUNCTION PROTOTYPING **/
  17. void fractionCalc(); //main algorithm
  18. void assignValues(string, int&, int&, int&, int&, char&); //for assigning values (integers and operator)
  19. bool isProperFormat(string); //checks if input is in proper format
  20. bool isOperator(char); //checks if the user gave a proper operator (+, -, *, or /)
  21. bool isNotZero(char); //makes sure a char isn't 0, used to avoid diving by 0 error
  22. void addFrac(int, int, int, int); //adds 2 fractions together
  23. void subtractFrac(int, int, int, int); //subtracts 2 fractions
  24. void multiplyFrac(int, int, int, int); //multiplies 2 fractions
  25. void divideFrac(int, int, int, int); //divides 2 fractions
  26.  
  27. /** MAIN FUNCTION **/
  28. int main() {
  29.     fractionCalc(); //call to fractionCalc function
  30.     return 0;
  31. }
  32.  
  33. void fractionCalc()
  34. {
  35.     int frac1num, frac1den, frac2num, frac2den; //integer declarations
  36.     char oprtr; //character delcarion
  37.     bool quit = false; //boolean declaration for quitting
  38.     string equation; //string declaration
  39.    
  40.     do{
  41.         cout << "Enter a common fraction problem (q to quit): "; //sentence to print to screen at the beginning of each loop
  42.         getline(cin, equation); //get string form user until enter is pressed, set it to string, equation
  43.        
  44.         if(isProperFormat(equation)) //checks if sentence is format: x/y [operator] a/b
  45.         {
  46.             assignValues(equation, frac1num, frac1den, frac2num, frac2den, oprtr); // if passes check, assignes values
  47.            
  48.             if(oprtr == '+') //in case of addition
  49.                 addFrac(frac1num, frac1den, frac2num, frac2den); //call to addFrac function
  50.            
  51.             if(oprtr == '-') //in case of subtraction
  52.                 subtractFrac(frac1num, frac1den, frac2num, frac2den); //call to subtractFrac function
  53.            
  54.             if(oprtr == '*') //in case of multiplication
  55.                 multiplyFrac(frac1num, frac1den, frac2num, frac2den); //call to multiplyFrac function
  56.            
  57.             if(oprtr == '/') //in case of division
  58.                 divideFrac(frac1num, frac1den, frac2num, frac2den); //call to divideFrac function
  59.            
  60.         }else if (equation == "q" || equation == "Q") //if string given is sumply "q" or "Q"
  61.             quit = true; //set boolean flag for quitting to "true"
  62.        
  63.     }while(!quit); //loop until quit flag is set to true
  64.    
  65. }
  66.  
  67. bool isProperFormat(string equation)
  68. {
  69.     /** CONDITIONS **/
  70.     if(isdigit(equation[0]) && //first char must be integer from 0-9
  71.        equation[1] == '/' && //second char must be a forwardslash
  72.        isdigit(equation[2]) && //third char must be a digit
  73.        isNotZero(equation[2]) && //third char cannot be 0 (to avoid dividing by 0)
  74.        isspace(equation[3]) && //fourth char must be a space
  75.        isOperator(equation[4]) && //fifth char must be an operator (+, -, *, /)
  76.        isspace(equation[5]) && //sixth char must be a space
  77.        isdigit(equation[6]) && //seventh char must be a digit
  78.        equation[7] == '/' && //eighth char must be a forwardslash
  79.        isdigit(equation[8]) && //ninth char must be a digit
  80.        isNotZero(equation[8])) //ninth char cannot be 0 (to avoid dividing by 0)
  81.     {
  82.         return true; //if all conditions passed, it is in the proper format
  83.     }
  84.     else //if not in proper format
  85.     {
  86.         if(!isOperator(equation[4]) && equation != "q" && equation != "Q") //check if operator was wrong
  87.             cout << "Error: Invalid operator" << endl; //tell user the operator is invalid
  88.        
  89.         if(equation != "q" && equation != "Q") //if it didn't pass and it wasn't because the user quit
  90.             cout << "Cannot solve this problem." << endl; //give error
  91.        
  92.         return false; //equation isn't proper format, return flase
  93.     }
  94. }
  95.  
  96. bool isOperator(char character)
  97. {
  98.     if(character == '+' || //character is + or
  99.        character == '-' || //character is - or
  100.        character == '*' || //character is * or
  101.        character == '/')   //character is /
  102.     {
  103.         return true; //the character is a valid operator
  104.     }
  105.     else //otherwise
  106.     {
  107.         return false; //the character isn't a valid operator
  108.     }
  109. }
  110.  
  111. bool isNotZero(char number)
  112. {
  113.     if(number != '0') //if character is not equal to 0
  114.         return true; //character is not zero
  115.     else //otherwise
  116.         return false; //character is zero
  117. }
  118.  
  119. void assignValues(string str, int& frac1num, int& frac1den, int& frac2num, int& frac2den, char& oprtr)
  120. {
  121.     frac1num = str[0] - '0'; //set value of fraction 1 numerator to first value of given string
  122.     frac1den = str[2] - '0'; //set value of fraction 1 denominator to third value of given string
  123.     frac2num = str[6] - '0'; //set value of fraction 2 numerator to seventh value of given string
  124.     frac2den = str[8] - '0'; //set value of fraction 2 denominator to ninth value of given string
  125.     oprtr = str[4]; //set operator to fifth value of given string
  126. }
  127.  
  128. void addFrac(int frac1num, int frac1den, int frac2num, int frac2den)
  129. {
  130.     if(frac1den == frac2den) //if denominators of 2 given fractions are the same
  131.     {
  132.         cout << "The result of the addition is " << frac1num + frac2num << "/" << frac1den << endl; //print out solution
  133.     }
  134.     else
  135.     {
  136.         int denominator = frac1den * frac2den; //set common denominator
  137.         int numerator1 = frac1num * frac2den; //multiply fraction 1 numerator by fraction 2 denominator
  138.         int numerator2 = frac2num * frac1den; //multiply fraction 2 numerator to fraction 1 denominator
  139.        
  140.         cout << "The result of the addition is " << numerator1 + numerator2 << "/" << denominator << endl; //print out solution
  141.        
  142.     }
  143. }
  144.  
  145. void subtractFrac(int frac1num, int frac1den, int frac2num, int frac2den)
  146. {
  147.     if(frac1den == frac2den) //if denominators of 2 given fractions are the same
  148.     {
  149.         cout << "The result of the subtraction is " << frac1num - frac2num << "/" << frac1den << endl; //print out solution
  150.     }
  151.     else
  152.     {
  153.         int denominator = frac1den * frac2den; //set common denominator
  154.         int numerator1 = frac1num * frac2den; //multiply fraction 1 numerator by fraction 2 denominator
  155.         int numerator2 = frac2num * frac1den; //multiply fraction 2 numerator to fraction 1 denominator
  156.        
  157.         cout << "The result of the subtraction is " << numerator1 - numerator2 << "/" << denominator << endl; //print out solution
  158.        
  159.     }
  160. }
  161.  
  162. void multiplyFrac(int frac1num, int frac1den, int frac2num, int frac2den)
  163. {
  164.     cout << "The result of the multiplication is " << frac1num * frac2num << "/" << frac1den * frac2den << endl; //multiply numerators, multiply denominators, print out solution (num/den)
  165. }
  166.  
  167. void divideFrac(int frac1num, int frac1den, int frac2num, int frac2den)
  168. {
  169.     cout << "The result of the division is " << frac1num * frac2den << "/" << frac1den * frac2num << endl; //cross multiply, print out solution
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement