Reverse Polish Calculator help!
Oct 11, 2013 at 9:18pm UTC
Hello!
So I have my Reverse Polish calculator compiling and everything but for the assignment I need to handle a few exceptions that I can't seem to get.
First off I'm trying to make the program exit if the user enters only "0"
but since the input i'm using is string, I cant figure out how to code
"If the first node is 0 and the next node = NULL, return true"
Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
#include<iomanip>
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<sstream>
using namespace std;
class Stack
{
private :
double data;
Stack *top;
Stack *next;
int counter;
public :
Stack()
{
top = NULL;
next = NULL;
}
bool isEmpty()
{
return (top == NULL);
}
bool full()
{
return 0;
}
void push(double input)
{
Stack *q = new Stack;
q->data=input;
q->next=top;
top=q;
counter++;
}
double pop()
{
if (isEmpty())
{
cout << "Too many operators!" << endl;
}
else
{
counter--;
Stack *q = top->next;
double value = top->data;
delete top;
top = q;
return value;
}
}
int count()
{
cout << "There are " << counter << "elements on the stack" ;
}
void print()
{
cout << top->data << endl;
}
bool isOperator(const string& input)
{
string ops[] = {"-" , "+" , "*" , "/" };
for (int i = 0; i < 4; i++)
{
if (input == ops[i])
{
return true ;
}
}
return false ;
}
void performOp(const string& input, Stack& stack)
{
double fVal, sVal, result;
int error = 0;
sVal = stack.pop();
fVal = stack.pop();
if (input == "-" )
{
stack.push(fVal - sVal);
}
else if (input == "+" )
{
stack.push(fVal + sVal);
}
else if (input == "*" )
{
stack.push(fVal * sVal);
}
else if (input == "/" && sVal!=0)
{
stack.push(fVal / sVal);
}
if (input == "/" && sVal == 0)
{
cout << "Division by zero error" << endl;
error = 1;
}
if (error == 0)
{
stack.print();
}
}
bool quitter(const string& input, Stack *top, Stack *next)
{ //This is the function I cant seem to get working
if (top == 0 && next == NULL)
{
return true ;
}
}
};
int main()
{
Stack a;
string input;
cout << "Reverse Polish Calculator! Enter Calculation or Enter 0 to quit.\n" ;
cout << "Enter a Reverse Polish Expression: " ;
while (true )
{
cin >> input;
double num;
if (a.quitter(input))//check for quit
{
cout << "QUITTER!" ;
return 0;
}
else if (istringstream(input) >> num)
{
a.push(num);
}
else if (a.isOperator(input))
{
a.performOp(input, a);
}
else
{
cout << "Invalid input" << endl;
}
}
system("PAUSE" );
return 0;
}
Oct 11, 2013 at 9:31pm UTC
I got it to accept this but when i try calculations I get this error
Unhandled exception at 0x00E18019 in RPN.exe: 0xC0000005: Access violation reading location 0x00000000.
1 2 3 4 5 6 7 8
bool quitter(const string& input)
{
if (top->data == 0 && top->next == NULL)
{
return true ;
}
}
I also swapped:
1 2 3 4 5
if (a.quitter(input))//check for quit
{
cout << "QUITTER!" ;
return 0;
}
to under else if (a.isOperator(input)) function in main
Last edited on Oct 11, 2013 at 9:36pm UTC
Topic archived. No new replies allowed.