Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <string>
- #include <iostream>
- #include <vector>
- using std::vector;
- using std::string;
- using std::cout;
- using std::cin;
- enum Type
- {
- TYPE_OPERATOR,
- TYPE_NUMBER,
- MAX_TYPES
- };
- struct Equation
- {
- Type type;
- int num;
- char op;
- int order;
- };
- int add(int x, int y)
- {
- return x + y;
- }
- int multiply(int x, int y)
- {
- int z = 0;
- for (int index = 0; index < y; index++)
- {
- z += x;
- }
- return z;
- }
- bool replay()
- {
- string userInput;
- cout << "\nDo you want to calculate something else?\n";
- do
- {
- cout << "Please enter \"yes\" to calculate something else or \"no\" to quit : ";
- getline(cin, userInput);
- if (cin.fail())
- {
- cin.clear();
- cin.ignore(32767, '\n');
- }
- if (userInput == "Yes" || userInput == "YES" || userInput == "yes" || userInput == "y" || userInput == "Y")
- return true;
- else if (userInput == "No" || userInput == "NO" || userInput == "no" || userInput == "n" || userInput == "N")
- return false;
- } while (true);
- }
- string getStringEquation() // Forces the user to enter an equation with no letters in it
- {
- cout << "Enter an Equation\n";
- bool testL; // Used to test if the equation has a letter in it
- string equation;
- do
- {
- testL = false;
- getline(cin, equation);
- for (int index = 0; index < static_cast<int>(equation.length()); index++)
- {
- if (isalpha(equation[index]))
- {
- testL = true;
- break;
- }
- }
- if (!testL)
- return equation;
- } while (true);
- }
- int getNum(char sign)
- {
- switch (sign)
- {
- case '0':
- return 0;
- case '1':
- return 1;
- case '2':
- return 2;
- case '3':
- return 3;
- case '4':
- return 4;
- case '5':
- return 5;
- case '6':
- return 6;
- case '7':
- return 7;
- case '8':
- return 8;
- case '9':
- return 9;
- }
- }
- bool isNum(char sign)
- {
- switch (sign)
- {
- case '0':
- return true;
- case '1':
- return true;
- case '2':
- return true;
- case '3':
- return true;
- case '4':
- return true;
- case '5':
- return true;
- case '6':
- return true;
- case '7':
- return true;
- case '8':
- return true;
- case '9':
- return true;
- default:
- return false;
- }
- }
- int getFullNum(int &index, string equation)
- {
- int num = 0;
- int tempNum;
- for (int x; index < equation.length(); index++)
- {
- if (isNum(equation[index]))
- {
- tempNum = getNum(equation[index]);
- num = num * 10;
- num = num + tempNum;
- }
- else
- return num;
- }
- }
- void convertEquation(vector<Equation> &equation, string stringEquation)
- {
- stringEquation += ' ';
- equation.resize(1);
- if (stringEquation[0] == '-')
- {
- equation[0].op = '-';
- equation[0].order = 1;
- equation[0].type = TYPE_OPERATOR;
- }
- else
- {
- equation[0].op = '+';
- equation[0].order = 1;
- equation[0].type = TYPE_OPERATOR;
- }
- int order = 1;
- for (int index = 0, eIndex = 1; index < stringEquation.length(); index++)
- {
- if (isNum(stringEquation[index]))
- {
- equation.resize(equation.size() + 1);
- equation[eIndex].num = getFullNum(index, stringEquation);
- equation[eIndex].order = order;
- equation[eIndex].type = TYPE_NUMBER;
- eIndex++;
- }
- if (stringEquation[index] == '+')
- {
- equation.resize(equation.size() + 1);
- equation[eIndex].op = '+';
- equation[eIndex].order = order;
- eIndex++;
- }
- if (stringEquation[index] == '-' && index > 0)
- {
- equation.resize(equation.size() + 1);
- equation[eIndex].op = '-';
- equation[eIndex].order = order;
- eIndex++;
- }
- if (stringEquation[index] == '*')
- {
- equation.resize(equation.size() + 1);
- equation[eIndex].op = '*';
- equation[eIndex].order = order;
- eIndex++;
- }
- if (stringEquation[index] == '/')
- {
- equation.resize(equation.size() + 1);
- equation[eIndex].op = '/';
- equation[eIndex].order = order;
- eIndex++;
- }
- if (stringEquation[index] == '^')
- {
- equation.resize(equation.size() + 1);
- equation[eIndex].op = '^';
- equation[eIndex].order = order;
- eIndex++;
- }
- if (stringEquation[index] == '(')
- {
- if (index - 1 > 0) // Bounds test
- if (stringEquation[index - 1] != '(' && stringEquation[index - 1] != ')' && stringEquation[index - 1] != '+' && stringEquation[index - 1] != '-' && stringEquation[index - 1] != '*' && stringEquation[index - 1] != '^' && stringEquation[index - 1] != '/')
- {
- equation.resize(equation.size() + 1);
- equation[eIndex].op = '*';
- equation[eIndex].order = order;
- eIndex++;
- }
- order++;
- }
- if (stringEquation[index] == ')')
- {
- if (index + 2 < stringEquation.length()) // Bounds test, added an extra char to stringEquation
- if (stringEquation[index + 1] != ')' && stringEquation[index + 1] != '+' && stringEquation[index + 1] != '-' && stringEquation[index + 1] != '*' && stringEquation[index + 1] != '^' && stringEquation[index + 1] != '/')
- {
- equation.resize(equation.size() + 1);
- equation[eIndex].op = '*';
- equation[eIndex].order = order;
- eIndex++;
- }
- order--;
- }
- }
- }
- void printEquation(const vector<Equation> equation)
- {
- for (auto value : equation)
- {
- if (value.type == TYPE_NUMBER)
- cout << value.num << " " << value.order << "\n";
- else if (value.type == TYPE_OPERATOR)
- cout << value.op << " " << value.order << "\n";
- }
- }
- int getHighestOrder(const vector<Equation> equation)
- {
- int order = 0;
- for (auto value : equation)
- {
- if (value.type == TYPE_OPERATOR && value.order > order)
- order = value.order;
- }
- return order;
- }
- int calculateEquation(vector<Equation> equation)
- {
- int solution;
- int highestOrder = getHighestOrder(equation);
- vector<Equation> equation2;
- equation2.resize(1);
- int e2Index = 0;
- do
- {
- for (auto value : equation)
- {
- if (value.type == TYPE_NUMBER && value.order == highestOrder)
- {
- equation2.resize(equation2.size() + 1);
- equation2[e2Index].num = value.num;
- e2Index++;
- }
- else if (value.type == TYPE_OPERATOR && value.order == highestOrder)
- {
- equation2.resize(equation2.size() + 1);
- equation2[e2Index].op = value.op;
- e2Index++;
- }
- }
- for (int index = 0; index < equation2.size(); index++)
- {
- if (equation2[index].type == TYPE_OPERATOR)
- {
- if (equation2[index].op == '*')
- {
- }
- }
- }
- highestOrder--;
- } while (highestOrder);
- return solution;
- }
- int main()
- {
- vector<Equation> equation;
- do
- {
- convertEquation(equation, getStringEquation());
- printEquation(equation);
- cout << "This equals : " << calculateEquation(equation) << "\n";
- } while (replay());
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement