Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Fraction.cpp
- // CS115-Assignment1
- //
- // Created by Hayden Lueck on 2015-01-13.
- // Copyright (c) 2015 Hayden Lueck. All rights reserved.
- //
- // Create a calculator that adds, subtracts, multiplies, and divides fractions
- //
- #include <iostream>
- #include <string>
- using namespace std;
- /** FUNCTION PROTOTYPING **/
- void fractionCalc(); //main algorithm
- void assignValues(string, int&, int&, int&, int&, char&); //for assigning values (integers and operator)
- bool isProperFormat(string); //checks if input is in proper format
- bool isOperator(char); //checks if the user gave a proper operator (+, -, *, or /)
- bool isNotZero(char); //makes sure a char isn't 0, used to avoid diving by 0 error
- void addFrac(int, int, int, int); //adds 2 fractions together
- void subtractFrac(int, int, int, int); //subtracts 2 fractions
- void multiplyFrac(int, int, int, int); //multiplies 2 fractions
- void divideFrac(int, int, int, int); //divides 2 fractions
- /** MAIN FUNCTION **/
- int main() {
- fractionCalc(); //call to fractionCalc function
- return 0;
- }
- void fractionCalc()
- {
- int frac1num, frac1den, frac2num, frac2den; //integer declarations
- char oprtr; //character delcarion
- bool quit = false; //boolean declaration for quitting
- string equation; //string declaration
- do{
- cout << "Enter a common fraction problem (q to quit): "; //sentence to print to screen at the beginning of each loop
- getline(cin, equation); //get string form user until enter is pressed, set it to string, equation
- if(isProperFormat(equation)) //checks if sentence is format: x/y [operator] a/b
- {
- assignValues(equation, frac1num, frac1den, frac2num, frac2den, oprtr); // if passes check, assignes values
- if(oprtr == '+') //in case of addition
- addFrac(frac1num, frac1den, frac2num, frac2den); //call to addFrac function
- if(oprtr == '-') //in case of subtraction
- subtractFrac(frac1num, frac1den, frac2num, frac2den); //call to subtractFrac function
- if(oprtr == '*') //in case of multiplication
- multiplyFrac(frac1num, frac1den, frac2num, frac2den); //call to multiplyFrac function
- if(oprtr == '/') //in case of division
- divideFrac(frac1num, frac1den, frac2num, frac2den); //call to divideFrac function
- }else if (equation == "q" || equation == "Q") //if string given is sumply "q" or "Q"
- quit = true; //set boolean flag for quitting to "true"
- }while(!quit); //loop until quit flag is set to true
- }
- bool isProperFormat(string equation)
- {
- /** CONDITIONS **/
- if(isdigit(equation[0]) && //first char must be integer from 0-9
- equation[1] == '/' && //second char must be a forwardslash
- isdigit(equation[2]) && //third char must be a digit
- isNotZero(equation[2]) && //third char cannot be 0 (to avoid dividing by 0)
- isspace(equation[3]) && //fourth char must be a space
- isOperator(equation[4]) && //fifth char must be an operator (+, -, *, /)
- isspace(equation[5]) && //sixth char must be a space
- isdigit(equation[6]) && //seventh char must be a digit
- equation[7] == '/' && //eighth char must be a forwardslash
- isdigit(equation[8]) && //ninth char must be a digit
- isNotZero(equation[8])) //ninth char cannot be 0 (to avoid dividing by 0)
- {
- return true; //if all conditions passed, it is in the proper format
- }
- else //if not in proper format
- {
- if(!isOperator(equation[4]) && equation != "q" && equation != "Q") //check if operator was wrong
- cout << "Error: Invalid operator" << endl; //tell user the operator is invalid
- if(equation != "q" && equation != "Q") //if it didn't pass and it wasn't because the user quit
- cout << "Cannot solve this problem." << endl; //give error
- return false; //equation isn't proper format, return flase
- }
- }
- bool isOperator(char character)
- {
- if(character == '+' || //character is + or
- character == '-' || //character is - or
- character == '*' || //character is * or
- character == '/') //character is /
- {
- return true; //the character is a valid operator
- }
- else //otherwise
- {
- return false; //the character isn't a valid operator
- }
- }
- bool isNotZero(char number)
- {
- if(number != '0') //if character is not equal to 0
- return true; //character is not zero
- else //otherwise
- return false; //character is zero
- }
- void assignValues(string str, int& frac1num, int& frac1den, int& frac2num, int& frac2den, char& oprtr)
- {
- frac1num = str[0] - '0'; //set value of fraction 1 numerator to first value of given string
- frac1den = str[2] - '0'; //set value of fraction 1 denominator to third value of given string
- frac2num = str[6] - '0'; //set value of fraction 2 numerator to seventh value of given string
- frac2den = str[8] - '0'; //set value of fraction 2 denominator to ninth value of given string
- oprtr = str[4]; //set operator to fifth value of given string
- }
- void addFrac(int frac1num, int frac1den, int frac2num, int frac2den)
- {
- if(frac1den == frac2den) //if denominators of 2 given fractions are the same
- {
- cout << "The result of the addition is " << frac1num + frac2num << "/" << frac1den << endl; //print out solution
- }
- else
- {
- int denominator = frac1den * frac2den; //set common denominator
- int numerator1 = frac1num * frac2den; //multiply fraction 1 numerator by fraction 2 denominator
- int numerator2 = frac2num * frac1den; //multiply fraction 2 numerator to fraction 1 denominator
- cout << "The result of the addition is " << numerator1 + numerator2 << "/" << denominator << endl; //print out solution
- }
- }
- void subtractFrac(int frac1num, int frac1den, int frac2num, int frac2den)
- {
- if(frac1den == frac2den) //if denominators of 2 given fractions are the same
- {
- cout << "The result of the subtraction is " << frac1num - frac2num << "/" << frac1den << endl; //print out solution
- }
- else
- {
- int denominator = frac1den * frac2den; //set common denominator
- int numerator1 = frac1num * frac2den; //multiply fraction 1 numerator by fraction 2 denominator
- int numerator2 = frac2num * frac1den; //multiply fraction 2 numerator to fraction 1 denominator
- cout << "The result of the subtraction is " << numerator1 - numerator2 << "/" << denominator << endl; //print out solution
- }
- }
- void multiplyFrac(int frac1num, int frac1den, int frac2num, int frac2den)
- {
- cout << "The result of the multiplication is " << frac1num * frac2num << "/" << frac1den * frac2den << endl; //multiply numerators, multiply denominators, print out solution (num/den)
- }
- void divideFrac(int frac1num, int frac1den, int frac2num, int frac2den)
- {
- cout << "The result of the division is " << frac1num * frac2den << "/" << frac1den * frac2num << endl; //cross multiply, print out solution
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement