Advertisement
Okiko

Calculator, only uses + and - for all operations. [ WIP ]

Mar 1st, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.56 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <string>
  3. #include <iostream>
  4. #include <vector>
  5.  
  6. using std::vector;
  7. using std::string;
  8. using std::cout;
  9. using std::cin;
  10.  
  11. enum Type
  12. {
  13.     TYPE_OPERATOR,
  14.     TYPE_NUMBER,
  15.     MAX_TYPES
  16. };
  17.  
  18. struct Equation
  19. {
  20.     Type type;
  21.     int num;
  22.     char op;
  23.     int order;
  24. };
  25.  
  26. int add(int x, int y)
  27. {
  28.     return x + y;
  29. }
  30.  
  31. int multiply(int x, int y)
  32. {
  33.     int z = 0;
  34.     for (int index = 0; index < y; index++)
  35.     {
  36.         z += x;
  37.     }
  38.     return z;
  39. }
  40.  
  41. bool replay()
  42. {
  43.     string userInput;
  44.     cout << "\nDo you want to calculate something else?\n";
  45.     do
  46.     {
  47.         cout << "Please enter \"yes\" to calculate something else or \"no\" to quit : ";
  48.         getline(cin, userInput);
  49.         if (cin.fail())
  50.         {
  51.             cin.clear();
  52.             cin.ignore(32767, '\n');
  53.         }
  54.         if (userInput == "Yes" || userInput == "YES" || userInput == "yes" || userInput == "y" || userInput == "Y")
  55.             return true;
  56.         else if (userInput == "No" || userInput == "NO" || userInput == "no" || userInput == "n" || userInput == "N")
  57.             return false;
  58.     } while (true);
  59. }
  60.  
  61. string getStringEquation()          // Forces the user to enter an equation with no letters in it
  62. {
  63.     cout << "Enter an Equation\n";
  64.     bool testL;                     // Used to test if the equation has a letter in it
  65.     string equation;
  66.     do
  67.     {
  68.         testL = false;
  69.         getline(cin, equation);
  70.         for (int index = 0; index < static_cast<int>(equation.length()); index++)
  71.         {
  72.             if (isalpha(equation[index]))
  73.             {
  74.                 testL = true;
  75.                 break;
  76.             }
  77.         }
  78.         if (!testL)
  79.             return equation;
  80.     } while (true);
  81. }
  82.  
  83. int getNum(char sign)
  84. {
  85.     switch (sign)
  86.     {
  87.         case '0':
  88.             return 0;
  89.         case '1':
  90.             return 1;
  91.         case '2':
  92.             return 2;
  93.         case '3':
  94.             return 3;
  95.         case '4':
  96.             return 4;
  97.         case '5':
  98.             return 5;
  99.         case '6':
  100.             return 6;
  101.         case '7':
  102.             return 7;
  103.         case '8':
  104.             return 8;
  105.         case '9':
  106.             return 9;
  107.     }
  108. }
  109.  
  110. bool isNum(char sign)
  111. {
  112.     switch (sign)
  113.     {
  114.     case '0':
  115.         return true;
  116.     case '1':
  117.         return true;
  118.     case '2':
  119.         return true;
  120.     case '3':
  121.         return true;
  122.     case '4':
  123.         return true;
  124.     case '5':
  125.         return true;
  126.     case '6':
  127.         return true;
  128.     case '7':
  129.         return true;
  130.     case '8':
  131.         return true;
  132.     case '9':
  133.         return true;
  134.     default:
  135.         return false;
  136.     }
  137. }
  138.  
  139. int getFullNum(int &index, string equation)
  140. {
  141.     int num = 0;
  142.     int tempNum;
  143.     for (int x; index < equation.length(); index++)
  144.     {
  145.         if (isNum(equation[index]))
  146.         {
  147.             tempNum = getNum(equation[index]);
  148.             num = num * 10;
  149.             num = num + tempNum;
  150.         }
  151.         else
  152.             return num;
  153.     }
  154. }
  155.  
  156. void convertEquation(vector<Equation> &equation, string stringEquation)
  157. {
  158.     stringEquation += ' ';
  159.     equation.resize(1);
  160.     if (stringEquation[0] == '-')
  161.     {
  162.         equation[0].op = '-';
  163.         equation[0].order = 1;
  164.         equation[0].type = TYPE_OPERATOR;
  165.     }
  166.     else
  167.     {
  168.         equation[0].op = '+';
  169.         equation[0].order = 1;
  170.         equation[0].type = TYPE_OPERATOR;
  171.     }
  172.    
  173.     int order = 1;
  174.     for (int index = 0, eIndex = 1; index < stringEquation.length(); index++)
  175.     {
  176.         if (isNum(stringEquation[index]))
  177.         {
  178.             equation.resize(equation.size() + 1);
  179.             equation[eIndex].num = getFullNum(index, stringEquation);
  180.             equation[eIndex].order = order;
  181.             equation[eIndex].type = TYPE_NUMBER;
  182.             eIndex++;
  183.         }
  184.         if (stringEquation[index] == '+')
  185.         {
  186.             equation.resize(equation.size() + 1);
  187.             equation[eIndex].op = '+';
  188.             equation[eIndex].order = order;
  189.             eIndex++;
  190.         }
  191.         if (stringEquation[index] == '-' && index > 0)
  192.         {
  193.             equation.resize(equation.size() + 1);
  194.             equation[eIndex].op = '-';
  195.             equation[eIndex].order = order;
  196.             eIndex++;
  197.         }
  198.         if (stringEquation[index] == '*')
  199.         {
  200.             equation.resize(equation.size() + 1);
  201.             equation[eIndex].op = '*';
  202.             equation[eIndex].order = order;
  203.             eIndex++;
  204.         }
  205.         if (stringEquation[index] == '/')
  206.         {
  207.             equation.resize(equation.size() + 1);
  208.             equation[eIndex].op = '/';
  209.             equation[eIndex].order = order;
  210.             eIndex++;
  211.         }
  212.         if (stringEquation[index] == '^')
  213.         {
  214.             equation.resize(equation.size() + 1);
  215.             equation[eIndex].op = '^';
  216.             equation[eIndex].order = order;
  217.             eIndex++;
  218.         }
  219.         if (stringEquation[index] == '(')
  220.         {
  221.             if (index - 1  > 0)             // Bounds test
  222.                 if (stringEquation[index - 1] != '(' && stringEquation[index - 1] != ')' && stringEquation[index - 1] != '+' && stringEquation[index - 1] != '-' && stringEquation[index - 1] != '*' && stringEquation[index - 1] != '^' && stringEquation[index - 1] != '/')
  223.                 {
  224.                     equation.resize(equation.size() + 1);
  225.                     equation[eIndex].op = '*';
  226.                     equation[eIndex].order = order;
  227.                     eIndex++;
  228.                 }
  229.             order++;
  230.         }
  231.         if (stringEquation[index] == ')')
  232.         {
  233.             if (index + 2 < stringEquation.length())    // Bounds test, added an extra char to stringEquation
  234.                 if (stringEquation[index + 1] != ')' && stringEquation[index + 1] != '+' && stringEquation[index + 1] != '-' && stringEquation[index + 1] != '*' && stringEquation[index + 1] != '^' && stringEquation[index + 1] != '/')
  235.                 {
  236.                     equation.resize(equation.size() + 1);
  237.                     equation[eIndex].op = '*';
  238.                     equation[eIndex].order = order;
  239.                     eIndex++;
  240.                 }
  241.             order--;
  242.         }
  243.     }
  244. }
  245.  
  246. void printEquation(const vector<Equation> equation)
  247. {
  248.     for (auto value : equation)
  249.     {
  250.         if (value.type == TYPE_NUMBER)
  251.             cout << value.num << " " << value.order << "\n";
  252.         else if (value.type == TYPE_OPERATOR)
  253.             cout << value.op << " " << value.order << "\n";
  254.     }
  255. }
  256.  
  257. int getHighestOrder(const vector<Equation> equation)
  258. {
  259.     int order = 0;
  260.     for (auto value : equation)
  261.     {
  262.         if (value.type == TYPE_OPERATOR && value.order > order)
  263.             order = value.order;
  264.     }
  265.     return order;
  266. }
  267.  
  268. int calculateEquation(vector<Equation> equation)
  269. {
  270.     int solution;
  271.     int highestOrder = getHighestOrder(equation);
  272.     vector<Equation> equation2;
  273.     equation2.resize(1);
  274.     int e2Index = 0;
  275.     do
  276.     {
  277.         for (auto value : equation)
  278.         {
  279.             if (value.type == TYPE_NUMBER && value.order == highestOrder)
  280.             {
  281.                 equation2.resize(equation2.size() + 1);
  282.                 equation2[e2Index].num = value.num;
  283.                 e2Index++;
  284.             }
  285.             else if (value.type == TYPE_OPERATOR && value.order == highestOrder)
  286.             {
  287.                 equation2.resize(equation2.size() + 1);
  288.                 equation2[e2Index].op = value.op;
  289.                 e2Index++;
  290.             }
  291.         }
  292.         for (int index = 0; index < equation2.size(); index++)
  293.         {
  294.             if (equation2[index].type == TYPE_OPERATOR)
  295.             {
  296.                 if (equation2[index].op == '*')
  297.                 {
  298.                    
  299.                 }
  300.             }
  301.         }
  302.         highestOrder--;
  303.     } while (highestOrder);
  304.    
  305.     return solution;
  306. }
  307.  
  308. int main()
  309. {
  310.     vector<Equation> equation;
  311.     do
  312.     {
  313.         convertEquation(equation, getStringEquation());
  314.         printEquation(equation);
  315.         cout << "This equals : " << calculateEquation(equation) << "\n";
  316.     } while (replay());
  317.     return 0;
  318. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement