Hello Im new to programming and I'm trying to make a RPN calculator in Cpp. I sort of understand how it works but i dont know how to code it. So after searching on youtube i found a video of someone with a Stack calulator im planning to use on my project. I copied everything the program runs but it doesnt display the desired result. I debugged on Visual Studio and it gives me the error {data=0xcccccccccccccccc top=-858993460 size=-858993460 }. The program keeps running but doesnt do what it is supposed to do. any help would be appreciate it.
This is the firs file that defines the stack
#pragma once
// arraystack.h
#define STACK_H
template <class T>
class stack
{
private:
T* data; // Pointer.
int top;
int size;
void resize();
bool needToResize();
public:
stack()
{
size = 5;
data = new T[size];
top = 0;
}
void push(T item);
T peek();
T pop();
bool isEmpty();
};
//#include "arraystack.cpp"
#include <stdexcept> // para manejar la excepcion de fuera de rango.
template <class T>
void stack<T>::push(T item)
{
if (needToResize())
resize();
data[top] = item;
top++;
}
template < class T >
T stack<T>::peek()
{
if (top <= 0)
throw std::out_of_range("Attempting to peek an empty stack.");
return data[top - 1];
}
template<class T>
T stack<T>::pop()
{
if (top <= 0)
throw std::out_of_range("Attempting to pop an empty stack.");
top--;
return data[top];
}
template <class T>
bool stack<T>::needToResize()
{
return (top == size);// si top es igual al size entonce sse necesita resize.
}
template <class T>
void stack<T>::resize()
{
T* newdata = new T[2 * size];
for (int i = 0; i < size; i++)
newdata[i] = data[i];
data = newdata;
size *= 2;
}
template <class T>
bool stack<T>::isEmpty()
{
return (top == 0);
}
This is the second file that defines the calculator
#pragma once
// calculator.h
#include <iostream>
#include <string>
#include "arraystack.h"
using namespace std;
// check if char is a digit.
bool isDigit(char c)
{
return (c <= '0' && c <= '9'); // valor unicode de los numeros en 48 empieza 0.
}
//check if char is an operator
bool isOp(char c)
{
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')' || c == '^');
}
// determine the precedence of an operator
int getPrecendence(char c)
{
switch (c)
{
case '+':
case '-': return 1;
case '*':
case '/': return 2;
case '(':
case ')': return 3;
default: return -1;
}
}
// evaluate an arithmetic expression.
int operate(int val1, int val2, char op)
{
if (op == '+') return val1 + val2;
if (op == '-') return val1 - val2;
if (op == '*') return val1 * val2;
return val1 / val2; // se retorna esta porque es la unica op left. (sin expo)
}
//evaluate a string.
int evaluate(string s)
{
stack<int> vals;
stack<char> ops;
int val = 0;
int pos = 0;
while (pos < s.length())
{
char spot = s[pos];
if (isDigit(spot))
{
val = (val * 10) + (int)(spot - '0');
}
else if (isOp(spot))
{
if (spot == '(')
{
ops.push(spot);
val = 0;
}
else if (vals.isEmpty())
{
vals.push(val);
ops.push(spot);
val = 0;
}
else if (spot == ')')
{
vals.push(val);
while (ops.peek() != '(')
{
spot = ops.pop();
val = vals.pop();
int prev = vals.pop(); // previous on the stack
val = operate(prev, val, spot);
vals.push(val);
}
ops.pop();
vals.pop();
}
else
{
char prev = ops.peek();
if (getPrecendence(spot) > getPrecendence(prev))
{
vals.push(val);
ops.push(spot);
val = 0;
}
else
{
int prevval = vals.pop();
int prevop = ops.pop();
prevval = operate(prevval, val, prevop);
vals.push(prevval);
ops.push(spot);
val = 0;
}
}
}
pos++;
}
while (!ops.isEmpty())
{
int prev = vals.pop();
char spot = ops.pop();
val = operate(prev, val, spot);
}
return val;
}
This is the file that tests
// arraystack.cpp (the tester class)
#include <iostream>
#include "calculator.h"
#include <string>
using namespace std;
int main()
{
cout << evaluate("3 + 4 + 5") << endl; // expected result 12
cout << evaluate("3 * 4 + 5") << endl; // expected result 17
cout << evaluate("3 + 4 * 5") << endl; // expected result 23
cout << evaluate("(3 +4) * 5") << endl; // expected result 35
system("pause");
return 0;
}
The results i get
Results after running program
The errors showing in the visual studio debugger
Visual Studio Debbuging screen
I have no idea of whats going on. Any help would be appreciate it.
The output i get:
-1667666
-1667666
-1667666
-9658676
Press any key to continue . . .
Related
So I am working on some homework for my uni and need to convert a string to a float. For whatever reason g++ is complaining that the 'stof' function doesn't exist. Although I have included the required header. Here is my code, the error is on the line that says
holder = stof(x.substr(0, end_of_num));
#include <iostream>
#include <string>
#include <list>
using namespace std;
float process_func(string x);
bool isPartOfNum(char x);
int main() {
string x;
while (true) {
cout << "input a string" << endl;
getline(cin, x);
cout << process_func(x);
}
return 0;
}
float process_func(string x) {
int end_of_num =0;// used to find last index from num
int negMult = 1; //used to multiply value at end if there was a negative
bool onNum = false; //used to
list <float> numList;
list <char> operList;
if ((x.at(0) < 48 || x.at(0) > 57) && x.at(0) != '-') //check if start of string doesnt have a number or negative symbol
return -1;
if (x.at(0) != '-')
negMult = -1;
float holder;// temp holder for floats
int i = 0;
while (i<x.length()) {
if (isPartOfNum(x.at(i))) {
end_of_num++;
onNum = true;
}
else if (onNum) {
holder = stof(x.substr(0, end_of_num));
numList.push_back(holder); //adds num as float to list
x.erase(0, end_of_num + 1); //+1 removes the space after the number before the operator
end_of_num = 0;
onNum = false;
}
if (x.at(i) == '+' || x.at(i) == '-' || x.at(i) == '*' || x.at(i) == '/') {
operList.push_back(x.at(i));
}
} //at this point both lists should be full of all needed pieces of info
int answer = 0;
int temp;
bool firstOper=true; // used to hold first operation
while (numList.size() >=2) { //requires at least 2 entries for last operation
while (!operList.empty()) {
temp = numList.front();
numList.pop_front();
if (operList.front() == '+') {
if (firstOper) {
answer = temp + numList.front();
numList.pop_front();
firstOper = false;
}
else {
answer += temp;
}
}
else if (operList.front() == '-') {
if (firstOper) {
answer = temp - numList.front();
numList.pop_front();
firstOper = false;
}
else {
answer -= temp;
}
}
else if (operList.front() == '*') {
if (firstOper) {
answer = temp * numList.front();
numList.pop_front();
firstOper = false;
}
else {
answer *= temp;
}
}
else if (operList.front() == '/') {
if (firstOper) {
answer = temp / numList.front();
numList.pop_front();
firstOper = false;
}
else {
answer /= temp;
}
}
operList.pop_front();
}
}
return answer;
}
bool isPartOfNum(char x) {
if ((x >= 48 && x <= 57) || (x == '-' || x == '.'))
return true;
return false;
}
Solved by compiling using c++ 11
I've got a few different functions that on their own work fine. Now I am trying to put them together into one file. My debugger is throwing segmentation faults in the infixToPostfix function.
When I watch the ns variable, it does not show anything getting assigned to it, but it prints out the postfix expression when I actually run the code. When ns passes into PostfixEvaulation, it runs to the top member function of TemplateStack and crashes with:
terminate called after throwing an instance of "std::string"
This only happens when I pass a string with an operator. When I pass a string of just numbers, everything runs fine,
but it still does not seem to call PostfixEvaluation.
#include <iostream>
#include <sstream>
#include <string>
#include <typeinfo>
using namespace std;
//Class Template Stack declaration
template <class T>
class TemplateStack {
public:
typedef T type;
TemplateStack()
{
max_size_ = 50;
TopOfStack = 0;
data_ = new T[max_size_];
} //Default Constructor taking no parameters
void push(T element)
{
if (TopOfStack == max_size_)
throw string("Stack's underlying storage is overflow");
TopOfStack++;
data_[TopOfStack] = element;
}
void pop() {
if (TopOfStack == -1)
throw string("Stack is empty");
TopOfStack--;
}
T top() {
if (TopOfStack == -1)
throw string("Stack is empty");
return data_[TopOfStack];
}
private:
int TopOfStack; //Generic data type for the top element of stack
size_t max_size_;
T* data_;
};
//Function to Evauluate the Postfix notation expresion
int PostfixEvaulation(string input){
//string input;
int operand1, operand2, result,number;
TemplateStack<char>operation;
stringstream temp;
int i=0;
while (i < input.length())
{
if (isdigit(input[i]))
{
operation.push(input[i]);
}
else
{
operand2 = operation.top();
temp << operation.top();
operation.pop();
operand1 = operation.top();
temp << operation.top();
operation.pop();
switch(operand1,operand2)
{
case '+': result=operand1 + operand2;
break;
case '-': result=operand1 - operand2;
break;
case '*': result=operand1 * operand2;
break;
case '/': result=operand1 / operand2;
break;
}
operation.push(result);
}
i++;
}
cout << "The result is: "<<temp.str()<<endl;
//cin.ignore(numeric_limits<streamsize>::max(), '\n');
return 0;
}
//Function to return precedence of operators
int prec(char c)
{
if(c == '^')
return 3;
else if(c == '*' || c == '/')
return 2;
else if(c == '+' || c == '-')
return 1;
else
return -1;
}
//Function: Convert Infix to Postfix
void infixToPostfix(string s)
{
TemplateStack<char> st;
st.push('N');
int l = s.length();
string ns;
for (int i = 0; i < l; i++) {
// If the scanned character is an operand, add it to output string.
if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z')) {
ns += s[i];
}
// If the scanned character is an ‘(‘, push it to the stack.
else if (s[i] == '(') {
st.push('(');
}
// If the scanned character is an ‘)’, pop and to output string from the stack
// until an ‘(‘ is encountered.
else if (s[i] == ')') {
while (st.top() != 'N' && st.top() != '(') {
char c = st.top();
st.pop();
ns += c;
}
if (st.top() == '(') {
char c = st.top();
st.pop();
}
}
//If an operator is scanned
else {
while (st.top() != 'N' && prec(s[i]) <= prec(st.top())) {
char c = st.top();
st.pop();
ns += c;
}
st.push(s[i]);
}
}
//Pop all the remaining elements from the stack
while (st.top() != 'N') {
char c = st.top();
st.pop();
ns += c;
}
cout << "Here is the ns variable: "<< ns << endl;
PostfixEvaulation(ns);//Call the PostFixEvaluationFunction with the ns (postfix notation) variab
}
//Function: User inputs Expression
void GetInput(){
string input;
cout << "Enter Infix Expression: ";
getline(cin, input);
infixToPostfix(input);
}
int main()
{
GetInput();
}
I'm trying to transmission parameters onto my function to excute
Here's my stack.h file
#ifndef stackarray_h
#define stackarray_h
#include<iostream>
#include<string>
using namespace std;
const int MAX_SIZE = 100;
typedef string Type;
class Stack{
private:
Type item[MAX_SIZE];
int top;
public:
Stack();
bool isEmpty()const;
void push(Type value);
void pop();
Type getTop();
};
Stack::Stack(){
top = -1;
}
bool Stack::isEmpty()const{
if (top == -1)
return true;
else
return false;
}
void Stack::push(Type value){
item[++top] = value;
}
void Stack::pop(){
if (top != -1){
top = top - 1;
}
}
Type Stack::getTop(){
return item[top];
}
#endif
Here's my source.cpp file. My problem that i can't transmission parameters string into stack to my push function.
#include<iostream>
using namespace std;
#include<string>
#include<fstream>
#include"stack.h"
void readFile(char* file,string &str);
void convertFromInfixToPostfix(string &str);
int evaluatingPostfix(string str);
int convert(int value);
string convertToString(int value);
int main(){
string str;
readFile("Text.txt",str);
cout<<str<<endl;
convertFromInfixToPostfix(str);
cout << "Output: ";
cout << str << endl;
cout << "Result: ";
cout << evaluatingPostfix(str) << endl;
return 0;
}
void readFile(char* file,string &str){
ifstream read(file);
if (!read.is_open()){
cout << "Error!.File wasn't opened." << endl;
}
else
{
getline(read, str);
}
read.close();
}
void convertFromInfixToPostfix(string &str){
Stack _stack;
string output="";
int i = 0;
while (i < str.length()){
if (str[i] == '(' || str[i] == '+' || str[i] == '-'
|| str[i] == '*' || str[i] == '/' || str[i] == '%'){
_stack.push(str[i]);
}
else {
//excute character )
if (str[i] == ')'){
while (!_stack.isEmpty()){
char top = _stack.getTop();
output += top;
_stack.pop();
}
}else
//excute character digit
output = output + str[i];
}
// excute end of string
if (_stack.getTop() == '('){
_stack.pop();
}
i++;
}
str = output;
}
int evaluatingPostfix(string str){
int result;
Stack _stack;
int op1, op2;
int i = 0;
int index;
string temp;
char next_ch;
while (i < str.length()){
next_ch = str[i];
if (next_ch == '+' || next_ch == '-'
|| next_ch == '*' || next_ch == '/'
){
//excute operator
op1 = _stack.getTop();
_stack.pop();
// convert op1 to integer
op1 = convert(op1);
op2 = _stack.getTop();
_stack.pop();
// convert op2 to integer
op2 = convert(op2);
if (next_ch == '+'){
index = op2 + op1;
// convert index from integer to char
temp = convertToString(index);
_stack.push(temp);
}
if (next_ch == '-'){
index = op2 - op1;
// convert index from integer to char
temp = convertToString(index);
_stack.push(temp);
}
if (next_ch == '*'){
index = op2 * op1;
// convert index from integer to char
temp = convertToString(index);
_stack.push(temp);
}
if (next_ch == '/'){
index = op2 / op1;
// convert index from integer to char
temp = convertToString(index);
_stack.push(temp);
}
}
else {
// excute integer
_stack.push(next_ch);
}
i++;
}
// push the result onto the _stack
result = _stack.getTop();
// _stack is empty
_stack.pop();
return result;
}
int convert(int value){
int result;
result = value - 48;
return result;
}
string convertToString(int value){
string result ;
int n = value;
do{
n = n % 10;
result = result + (char)(n + 48);
} while (n>0);
return result;
}
There are error:
Error 4 error C2664: 'void Stack::push(stackItemType)' : cannot
convert argument 1 from 'std::string' to 'stackItemType'
At line _stack.push(temp);
I'm sorry because my english isn't good.
I'm writing a program that uses stacks to evaluate infix expressions read in from a file. Here is the code:
ptStack.h
#ifndef STACK
#define STACK
#include <cstdlib>
#include <iostream>
using namespace std;
template <class Item>
class Stack
{
public:
// CONSTRUCTOR
Stack( ) {top = NULL; count = 0;} // Inline
// MODIFIER MEMBER FUNCTIONS
void push( const Item& entry);
Item pop( );
// CONSTANT MEMBER FUNCTIONS
int size( ) {return count;} // Inline
bool is_empty( ) {return count == 0;} // Inline
private:
// DATA MEMBERS
struct Node
{
Item element;
Node *next;
};
Node *top;
int count;
};
#endif
ptStack.cpp
#include <cassert>
#include "ptStack.h"
using namespace std;
// MODIFIER MEMBER FUNCTIONS
template <class Item>
void Stack<Item>::push(const Item& entry)
{
Node *temp;
temp = new Node;
temp->element = entry;
temp->next = top->next;
top->next = temp;
count++;
}
template <class Item>
Item Stack<Item>::pop( )
{
Item value;
Node *temp;
value = top->next->element;
temp = top->next;
top->next = top->next->next;
delete temp;
count--;
return value;
}
infix.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "ptStack.h"
using namespace std;
// PRECONDITION: op must be an operator
// POSTCONDITION: returns precedence of operator
int pr(char op)
{
int precedence = 0;
switch (op)
{
case '+':
case '-': precedence = 1;
case '*':
case '/': precedence = 2;
default : precedence = 0;
}
return precedence;
}
// PRECONDITIONS: optr is one of the following: + - * /
// opnd1 and opnd2 are numbers from 1-9
// POSTCONDITIONS: returns the result of the chosen mathematical operation
int apply(char optr, int opnd1, int opnd2)
{
int result;
switch (optr)
{
case '+': result = opnd2+opnd1;
case '-': result = opnd2-opnd1;
case '*': result = opnd2*opnd1;
default : result = opnd2/opnd1;
}
return result;
}
int main()
{
Stack<int> numbers;
Stack<char> operators;
char ch, optr;
int num, opnd1, opnd2, prec = 0, newprec;
ifstream in_file; // input file
char in_file_name[20]; // name of input file (20 letter max)
cout << "Enter input file name: ";
cin >> in_file_name;
in_file.open(in_file_name); // opens file to read equations
while (!in_file.eof())
{
cout << "Expression: ";
while(in_file >> ch)
{
if (ch == ' ')
{}
else
{
num = ch - '0';
if((num < 10) && (num > 0))
{
cout << num << " ";
numbers.push(num);
}
else if((ch == '+') || (ch == '-') || (ch == '*') || (ch == '/'))
{
cout << ch << " ";
newprec = pr(ch);
if(newprec >= prec)
{
prec = newprec;
operators.push(ch);
}
else if(newprec < prec)
{
optr = operators.pop( );
opnd1 = numbers.pop( );
opnd2 = numbers.pop( );
num = apply(optr, opnd1, opnd2);
numbers.push(num);
operators.push(ch);
}
}
}
if(in_file.peek() == '\n')
break;
}
num = operators.size();
while(num != 0)
{
optr = operators.pop( );
opnd1 = numbers.pop( );
opnd2 = numbers.pop( );
num = apply(optr, opnd1, opnd2);
numbers.push(num);
num = operators.size( );
}
num = numbers.pop( );
cout << endl << "Value = " << num << endl << endl;
}
return 0;
}
It looks like everything should work but when I compile it, I get this error message.
> g++ -c ptStack.cpp
> g++ infix.cpp ptStack.o
Undefined first referenced
symbol in file
_ZN5StackIcE4pushERKc /var/tmp//ccwhfRAZ.o
_ZN5StackIiE4pushERKi /var/tmp//ccwhfRAZ.o
_ZN5StackIcE3popEv /var/tmp//ccwhfRAZ.o
_ZN5StackIiE3popEv /var/tmp//ccwhfRAZ.o
ld: fatal: symbol referencing errors. No output written to a.out
I've been able to pinpoint the errors to the callings of the push and pop member functions in the main of the infix file. I tried defining them inline in the header file like the size function and it compiles just fine using g++ infix.cpp ptStack.h, but then I get a segmentation fault when I run it.
Does anyone know how to fix either of these errors? Thanks in advance.
Just compile all the .cpp files with g++
g++ ptStack.cpp infix.cpp
I'm working on a program for a computer science class. I have everything completed and separated into the interface/implementation and for some reason, the program loops infinitely and says "Stack is empty!" after the peek() function is called.
I've tried inserting cout << statements to see if I can pinpoint the issue, however, no luck. I would appreciate it if someone else could take a look at it.
Thank You
Header
#ifndef STACK_H
#define STACK_H
/////////////////////////////////////////////////////
////Includes/////////////////////////////////////////
/////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <stdlib.h>
#include <iomanip>
#include <sstream>
#include <stdio.h>
/////////////////////////////////////////////////////
using namespace std;
/*-------------------------------------------------------------------------------------------------------------*/
template <typename Object>
class Stack
{
private:
class stackListNode
{
public:
Object data;
stackListNode *next;
private:
//Nothing to declare-->placeholder since class
//sets data members to private initially
};
stackListNode *top;
public:
/////////////////////////////////////////////////
//Constructor Function//////////////////////////
Stack() {top = NULL;}
/////////////////////////////////////////////////
//Rest of functions defined inline for simplicity
void push(char token) // Push token onto the stack and create new node for top of stack
{
stackListNode *newNode = new stackListNode;
newNode->data = token;
newNode->next = top;
top = newNode;
}
int pop()
{
if(empty())
{
cout << "Stack is empty!\n" << endl;
return NULL;
}
stackListNode *temp = top;
top = temp->next;
}
char peek()
{
if(empty())
{
cout << "Stack is empty!\n" << endl;
return NULL;
}
return top->data;
}
int empty()
{
return top == NULL;
}
};
#endif
Main File:
/////////////////////////////////////////////////////
////Includes/////////////////////////////////////////
/////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <string>
#include "Stack.h"
/////////////////////////////////////////////////////
using namespace std;
int precendence(char stack_beginning); //ensures order of operations.
int precendence(char*myArray);
void InFixToPostfix(ifstream& in_file);
double EvaluatePostfix(double first_operand, double second_operand, char*myArray);
int main()
{
////VARS/////////////////////////////////////////////
string absolutePath;
cout << endl;
cout << "Please type in the name of the file you would to open \n";
cin >> absolutePath;
ifstream in_file;
in_file.open(absolutePath.c_str());
if(!in_file)
{
cout << "failed to open input file\n" ;
return 1 ;
}
else
{
InFixToPostfix(in_file); //kicks off program
}
}
void InFixToPostfix(ifstream& in_file)
{
string infix;
int right_parentheses =0;
int left_parentheses =0;
while(getline(in_file, infix))
{
char myArray[infix.size()];
strcpy(myArray, infix.c_str());
for(int i = 0; i < sizeof(myArray); i++)
{
if(myArray[i] == '(')
{
right_parentheses++;
}
if(myArray[i] == ')')
{
left_parentheses++;
}
}
if(right_parentheses!=left_parentheses)
{
cout << endl;
cout << "There is a typo in one of the expressions in your file \n";
cout << "Please fix it and rerun the program \n";
exit(1);
}
for(int i = 0; i < sizeof(myArray); i++)
{
//int number = int(myArray[i]);
//deferences the pointer and reads each char in the array
//as an int rather than a character.
//int number = myArray[i];
if(isxdigit(myArray[i]) > 0) //function used to check for hexidecimal values (i.e. int's)
{
goto exit_out;
}
else if(myArray[i] == '(' || myArray[i] == ')' || myArray[i] == '+' || myArray[i] == '-' || myArray[i] == '*' || myArray[i] == '/' || myArray[i] == '\\' || myArray[i] == '\n')
{
goto exit_out;
}
else if(myArray[i] == char(32)) //checks to see if there is a space
{
goto exit_out;
}
else
{
cout << endl;
cout << "There is an invalid character in the file\n";
exit(1);
}
exit_out:;
}
////////Declares a STRING Stack////////////////////////////////
Stack<char> stack_string;
////////Declares an Int Stack/////////////////////////////////
Stack<int> stack_int;
//////////////////////////////////////////////////////////////
for(int i = 0; i < sizeof(myArray); i++)
{
int number = isxdigit(myArray[i]);
if(number > 0)
{
cout << number;
//outputs the number b/c it is an operand
}
if(myArray[i] == '(')
{
stack_string.push(myArray[i]);
}
if(myArray[i] == ')')
{
//cout << "test";
while(stack_string.peek() != '(')
{
cout << stack_string.peek();
stack_string.pop(); //pops to the peek
}
stack_string.pop(); // if there is a ), pops to the peek
}
if(myArray[i] == '+' || myArray[i] == '-' || myArray[i] == '/' || myArray[i] == '*')
{
if(stack_string.empty())
{
stack_string.push(myArray[i]); //if there's nothing on the stack, pushes the current character
//goto done;
break; //breaks out of the statement
}
char stack_beginning = stack_string.peek();
int stack_top = precendence(stack_beginning);
//int stack_top = precendence(stack_string.peek());
int operatorHierarchy = precendence(myArray);
//must be declared here because i will have been interated through array
if(operatorHierarchy > stack_top)
{
stack_string.push(myArray[i]);
}
else if(operatorHierarchy <= stack_top) //could also be an if
{
dump_out:;
cout << stack_string.peek();
stack_string.pop();
int rerunThroughStack =0;
char new_stack_beginning = stack_string.peek();
rerunThroughStack = precendence(new_stack_beginning);
if(operatorHierarchy < stack_top)
{
stack_string.push(myArray[i]);
goto done; //could break
}
if(stack_string.peek() == '(')
{
stack_string.push(myArray[i]);
goto done;
}
if(stack_string.empty())
{
stack_string.push(myArray[i]);
goto done;
}
goto dump_out;
}
}
done:;
}
cout << stack_string.peek() << endl;
//////////Evaluate Section/////////////////////////////
for(int i = 0; i < sizeof(myArray); i++)
{
if(isxdigit(myArray[i]) > 0) //this is a number
{
stack_int.push(isxdigit(myArray[i]));
goto end;
}
else if(myArray[i] == '*' || myArray[i] == '+' || myArray[i] == '-' || myArray[i] == '/')
{
double first_operand;
first_operand = stack_int.peek(); //fetches first operand on the stack_int
stack_int.pop();
//////////////////
double second_operand;
second_operand = stack_int.peek();
stack_int.pop();
//////////////////
double answer;
answer = EvaluatePostfix(first_operand, second_operand, myArray); //THIS PROBABLY IS NOT RIGHT
stack_int.push(answer);
}
end:;
}
/*
int size_of_stack;
size_of_stack = stack_int.size();
if(size_of_stack == 1)
{
cout << stack_int.peek();
}
*/
}
}
double EvaluatePostfix(double first_operand, double second_operand, char* myArray) //might have to pass array as reference
{
/*
Cycle through the characters passed in through myArray[i];
*/
for(int i = 0; i < sizeof(myArray); i++)
{
if(myArray[i] == '*')
{
double first_answer;
first_answer = (second_operand * first_operand);
return first_answer;
}
else if(myArray[i] == '/')
{
double second_answer;
second_answer = (second_operand / first_operand);
return second_answer;
}
else if(myArray[i] == '+')
{
double third_answer;
third_answer = (second_operand + first_operand);
return third_answer;
}
else if(myArray[i] == '-')
{
double fourth_answer;
fourth_answer = (second_operand - first_operand);
return fourth_answer;
}
/*
else
{
cout << "There must be an error in your file" <<endl;
break;
exit(1);
}
*/
}
}
int precendence(char stack_beginning)
{
int precendence;
if(stack_beginning == '*' || stack_beginning == '/')
{
precendence = 2;
return precendence;
}
//by making it 2, the precendence is dubbed less than +/-
if(stack_beginning == '+' || stack_beginning == '-')
{
precendence = 1;
return precendence;
}
} //by making it 1, the precendence is dubbed greater than */"/"
int precendence(char*myArray)
{
int precendence;
for(int i = 0; i < sizeof(myArray); i++)
{
if(myArray[i] == '*' || myArray[i] == '/')
{
precendence = 2;
return precendence;
}
//by making it 2, the precendence is dubbed less than +/-
if(myArray[i] == '+' || myArray[i] == '-')
{
precendence = 1;
return precendence;
}
} //by making it 1, the precendence is dubbed greater than */"/"
}