daily pastebin goal
78%
SHARE
TWEET

Untitled

a guest Oct 23rd, 2018 901 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. #include "MadewellStack.h"
  5.  
  6. using namespace std;
  7.  
  8. //returns whether or not the char is a math operator
  9. bool ifOperator(char c)
  10. {
  11.     if (c == '+' || c == '-' || c == '*' || c == '/')
  12.     {
  13.         return true;
  14.     }
  15.     else
  16.     {
  17.         return false;
  18.     }
  19. }
  20.  
  21. //this does the math involved when the two numbers are popped off the stack
  22. double funcMath(double o1, double o2, char oper)
  23. {
  24.     double answer;
  25.     switch (oper) {
  26.     case '+':
  27.         answer = o2 + o1;
  28.         break;
  29.     case '-':
  30.         answer = o2 - o1;
  31.         break;
  32.     case '*':
  33.         answer = o2 * o1;
  34.         break;
  35.     case '/':
  36.         answer = o2 / o1;
  37.         break;
  38.     }
  39.     return answer;
  40. }
  41.  
  42. double iterate(string input, MadewellStack &stack)
  43. {
  44.     for (size_t i = 0; i < (input.length()); i++)
  45.     {
  46.         if (ifOperator(input[i]))
  47.         {
  48.             char oper = input[i];
  49.             double o1 = stack.Top();
  50.  
  51.             try
  52.             {
  53.                 stack.Pop();
  54.             }
  55.             catch (EmptyStack)
  56.             {
  57.                 cerr << "EmptyStack exception thrown" << endl
  58.                     << "Exiting with error code 2" << endl;
  59.                 exit(2);
  60.             }
  61.  
  62.             double o2 = stack.Top();
  63.  
  64.             try
  65.             {
  66.                 stack.Pop();
  67.             }
  68.             catch (EmptyStack)
  69.             {
  70.                 cerr << "EmptyStack exception thrown" << endl
  71.                     << "Exiting with error code 2" << endl;
  72.                 exit(2);
  73.             }
  74.  
  75.             double answer = funcMath(o1, o2, oper);
  76.             stack.Push(answer);
  77.         }
  78.         else
  79.         {
  80.             double dou = double(input[i]);
  81.  
  82.             try
  83.             {
  84.                 stack.Push(dou);
  85.             }
  86.             catch (FullStack)
  87.             {
  88.                 cerr << "FullStack exception thrown" << endl
  89.                     << "Exiting with error code 3" << endl;
  90.                 exit(3);
  91.             }
  92.         }
  93.     }
  94.     return stack.Top();
  95. }
  96.  
  97. // this converts the input so the spaces and the equals sign are removed
  98. string inputConvert(string input)
  99. {
  100.     string convInput = "";
  101.     for (size_t i = 0; i < input.length(); i++)
  102.     {
  103.         if (isdigit(input[i]) || ifOperator(input[i]))
  104.         {
  105.             convInput += input[i];
  106.         }
  107.     }
  108.     return convInput;
  109. }
  110.  
  111.  
  112. int main()
  113. {
  114.     string equation;
  115.     cout << "Enter expression to evaluate or a # to quit. ";
  116.  
  117.     while(getline(cin, equation))
  118.     {
  119.         if (equation[0] == '#')
  120.         {
  121.             break;
  122.         }
  123.  
  124.         string convInput = inputConvert(equation);
  125.  
  126.         MadewellStack* stack = new MadewellStack(convInput.length());
  127.         cout << endl << "Result = " << (iterate(convInput, *stack));
  128.         cout << endl << "Enter expression to evaluate or a # to quit. ";
  129.     }
  130. }
  131.  
  132.  
  133. ######################################################################################
  134. //stack definition, I don't think the problem is here but just to be sure
  135.  
  136. #pragma once
  137. #include "stdafx.h"
  138. #include "MadewellStack.h"
  139.  
  140. int top;
  141. int maxStack;
  142. double* items;
  143.  
  144. MadewellStack::MadewellStack(int max)
  145. {
  146.     maxStack = max;
  147.     top = -1;
  148.     items = new double[maxStack];
  149. }
  150.  
  151. MadewellStack::MadewellStack()
  152. {
  153.     maxStack = 500;
  154.     top = -1;
  155.     items = new double[maxStack];
  156. }
  157.  
  158. bool MadewellStack::IsEmpty() const
  159. {
  160.     return (top == -1);
  161. }
  162.  
  163. bool MadewellStack::IsFull() const
  164. {
  165.     return (top == maxStack - 1);
  166. }
  167.  
  168. void MadewellStack::Push(double newItem)
  169. {
  170.     if (IsFull())
  171.     {
  172.         throw FullStack();
  173.     }
  174.     top++;
  175.     items[top] = newItem;
  176. }
  177.  
  178. void MadewellStack::Pop()
  179. {
  180.     if (IsEmpty())
  181.     {
  182.         throw EmptyStack();
  183.     }
  184.     top--;
  185. }
  186.  
  187. double MadewellStack::Top() const
  188. {
  189.     if (IsEmpty())
  190.     {
  191.         throw EmptyStack();
  192.     }
  193.     return items[top];
  194. }
  195.  
  196. MadewellStack::~MadewellStack()
  197. {
  198.     delete[] items;
  199. }
  200.  
  201. ##################################################################
  202. stack.h
  203.  
  204.  
  205. #pragma once
  206.  
  207. class FullStack
  208. {};
  209.  
  210. class EmptyStack
  211. {};
  212.  
  213. class MadewellStack
  214. {
  215. public:
  216.     MadewellStack();
  217.     MadewellStack(int max);
  218.     ~MadewellStack();
  219.     bool IsEmpty() const;
  220.     bool IsFull() const;
  221.     void Push(double newItem);
  222.     void Pop();
  223.     double Top() const;
  224. private:
  225.     int top;
  226.     int maxStack;
  227.     double* items;
  228. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top