Hello there, Stack.
I'm currently learning C++ and trying to create an RPN converter.
But I'm having difficulties.
Hopefully, I'll be able to describe the issues in detail.
For stacking my operators, I'm using an array.
Let's look at the example "5 + 8."
When it comes down to:
else if(infix[i] == '+' or infix[i] == '-' or infix[i] == '*' or infix[i] == '/' or infix[i] == '^'){
while(isp(stack1.top()) >= icp(infix[i])){
postfix += stack1.pop();
}
if(isp(stack1.top()) < icp(infix[i])){
stack1.push(infix[i]);
}
For whatever reason, it will add the operator to the stack but not to the postfix string variable to which I am adding my items.
The output would be something like "5 8."
I checked my pop function and everything appears to be correct, but I'm at a loss.
If you could point me in the correct way, that would be fantastic.
This is my whole code:
#include <iostream>
#include <string>
#define DEFAULT_SIZE 100
using namespace std;
class Stack{
private:
char *array;
int tos, capacity;
public:
//constructors
Stack();
//Destructor
~Stack();
//Methods
void push(char a);
char pop();
char top();
int get_size();
bool is_empty();
bool is_full();
void display();
};
Stack::Stack(){
array = new char[DEFAULT_SIZE];
tos = 0;
capacity = DEFAULT_SIZE;
}
Stack::~Stack(){
delete[] array;
}
void Stack::push(char a){
if(!is_full())
array[tos++] = a;
}
char Stack::pop(){
return array[--tos];
}
char Stack::top(){
return array[tos];
}
int Stack::get_size(){
return tos;
}
bool Stack::is_empty(){
if(tos == 0)
return true;
else
return false;
}
bool Stack::is_full(){
if(tos == capacity)
return true;
else
return false;
}
void Stack::display(){
if (tos == 0)
cout<<"The stack is empty"<<endl;
else{
for (int i=0; i<tos;i++)
cout<<array[i]<<" ";
cout<<endl;
}
}
int isp(char a){
if(a == '^'){
return 3;
}
else if (a == '*' or a == '/'){
return 2;
}
else if(a == '+' or a == '-'){
return 1;
}
else if(a == '('){
return 0;
}
else
return -1;
}
int icp(char a){
if(a == '^'){
return 4;
}
else if (a == '*' or a == '/'){
return 2;
}
else if(a == '+' or a == '-'){
return 1;
}
else if(a == '('){
return 4;
}
}
int main(){
string infix, postfix;
Stack stack1;
cout << "This is a Infix to Postfix Expression converter." << endl;
cout << "Enter your Infix Expression: ";
cin >> infix;
stack1.push('#');
for(int i=0;i<infix.length();i++){
if(isdigit(infix[i]) or isalpha(infix[i])){
postfix += infix[i];
}
else if(infix[i] == '+' or infix[i] == '-' or infix[i] == '*' or infix[i] == '/' or infix[i] == '^'){
while(isp(stack1.top()) >= icp(infix[i])){
postfix += stack1.pop();
}
if(isp(stack1.top()) < icp(infix[i])){
stack1.push(infix[i]);
}
}
}
cout << postfix;
return 0;
}
Also, if you know of any decent C++ RPN converter resource sites, please share them since they would be quite helpful!
I'm using a random algorithm I discovered on Google.