Help with c++ program. The following code takes an infix expression and converts it to postfix. When I compile the code it returns "Segmentation fault". However I don't know what causes this.
#include
#include
#include
using namespace std;
template
class Stack
{
public:
Stack();//creates the stack
bool isempty(); // returns true if the stack is empty
T gettop();//returns the front of the list
void push(T entry);//add entry to the top of the stack
void pop();//remove the top of the stack
private:
vector stack;
}; // Stack
template
Stack::Stack()
{
}
template
bool Stack::isempty()
{ if (stack.size() == 0)
return true;
else
return false;
}
template
T Stack::gettop()
{ return stack[stack.size()-1];
}
template
void Stack::push(T entry)
{
stack.push_back(entry);
}
template
void Stack::pop()
{
stack.pop_back();
}
int main()
{
Stack S;
S.push('[');
string y;
string x = "(A+B)*C-(D/(J+D))]";
int index=0;
char s=x[index];
while(s!=']')
{
if(s=='(');
{
S.push(s);
}
if(s=='*' or s=='/')
{
while(S.gettop()=='*' or S.gettop()=='/')
{
y += S.gettop();
S.pop();
}
S.push(s);
}
if(s=='+' or s=='-')
{
while(S.gettop()=='+' or S.gettop()=='-'
or S.gettop()=='*' or S.gettop()=='/')
{
y += S.gettop();
S.pop();
}
S.push(s);
}
if(s==')' or s==']')
{
while(S.gettop()!='(' or S.gettop()!='[')
{
y += S.gettop();
S.pop();
}
S.pop();
}
if(s!='*' or s!='/' or s!='+' or s!='-')
{
y += s;
}
index++;
s=x[index];
}
cout< }
Segmentation fault problem is solved and modified code is given below
#include<iostream>
#include<string>
#include<vector>
#include<ctype.h>
using namespace std;
template<class T>
class Stack
{
public:
Stack();//creates the stack
bool isempty(); // returns true if the stack is empty
T gettop();//returns the front of the list
void push(T entry);//add entry to the top of the stack
void pop();//remove the top of the stack
private:
vector<T> stack;
}; // Stack
template<class T>
Stack<T>::Stack()
{
}
template<class T>
bool Stack<T>::isempty()
{ if (stack.size() == 0)
return true;
else
return false;
}
template<class T>
T Stack<T>::gettop()
{ return stack[stack.size()-1];
}
template<class T>
void Stack<T>::push(T entry)
{
stack.push_back(entry);
}
template<class T>
void Stack<T>::pop()
{
stack.pop_back();
}
int main()
{
Stack<char> S;
S.push('[');
string y="";
string x = "(A+B)*C-(D/(J+D))]";
int index=0;
char s=x[index];
while(s!=']')
{
if(s=='(')
{
S.push(s);
}
if(s=='*' or s=='/')
{
while(S.gettop()=='*' or S.gettop()=='/')
{
y += S.gettop();
S.pop();
}
S.push(s);
}
if(s=='+' or s=='-')
{
while(S.gettop()=='+' or S.gettop()=='-' or S.gettop()=='*' or
S.gettop()=='/')
{
y += S.gettop();
S.pop();
}
S.push(s);
}
if(s==')' or s==']')
{
while(S.gettop()!='(')
{
y += S.gettop();
S.pop();
}
S.pop();
}
if(isalpha(s))
{
y += s;
}
index++;
s=x[index];
}
while(!S.isempty())
{
y+=S.gettop();
S.pop();
}
cout<<y;
}
Help with c++ program. The following code takes an infix expression and converts it to postfix....
stack.h template class Stack{ public: Stack(int max = 10); ~Stack() {delete [] stack;} bool isEmpty() const { return top == -1; } bool isFull() const { return top == MaxStackSize; } T peek() const; void push(const T& x); void pop(); private: int top; int MaxTop; T * stack; } source.cpp What is printed by the following program segment? Stack s; int n; s.push(4); s.push(6); s.push(8); while(!s.isEmpty()) { n = s.peek(); cout << n << ‘ ‘; s.pop(); } cout<< endl;...
This code in C converts infix to postfix and evaluates it. The problem is that it only evaluates one digit expressions. I need to fix it so that it can evaluate 2 digits expressions as well. #include <stdio.h> #include <ctype.h> #include <string.h> #include <math.h> #define SIZE 100 char s[SIZE]; int top=-1; void infixToPostfix(char *infix, char *postfix); void postfixEvaluation(char *postfix); void push(char elem){ s[++top]=elem; } char pop(){ return(s[top--]); } int pr(char elem){ // Order of precedence switch (elem) { case '(':...
Help me to fix this code in C language. This code converts infix expressions to postfix and then evaluate the expression. Right now, it works with single digits. I need to modify it so that it can evaluate expressions with also 2 digits, example: (60+82)%72. Additionally I need to display an error when the parenthesis don't match like (89+8(. I have muted some line that would print the postfix expression with a space like: 22 8 + when the input...
Must be written in C++ Please
Lab 11 Due Date: April 25, 2019 Total Points: 15 points The purpose of this lab is to implement and test a static and dy namic stack Part 1: Static Stack In this part, you are going to design a stack of characters. Assume a simple static array implementation. Complete the code below as specified by the comments below const int MAX- 5 // define an alias for the element type class Stack private:...
i want similar for this code to solve two questions : 1- Write a program to convert a postfix expression to infix expression 2-Write a program to convert an infix expression to prefix expression each question in separate code ( so will be two codes ) #include <iostream> #include <string> #define SIZE 50 using namespace std; // structure to represent a stack struct Stack { char s[SIZE]; int top; }; void push(Stack *st, char c) { st->top++; st->s[st->top] = c;...
I need to modify my C++ code so it can get the min value of the stack code is as follows: #include <iostream> using namespace std; #define MAX_SIZE 100 class Stack { private: int S[MAX_SIZE]; int top; public: Stack() { top = -1; } void push(int x) { if (top == MAX_SIZE - 1) { cout << "Stack is Full." << endl; return; } S[++top] = x; } void pop() { if (top == -1) { cout << "Stack is...
Given the following C code. Develop a evaluate post fix function that calculates the converted post fix. #include <stdio.h> #include <ctype.h> #define max 10 char s[100]; int top=-1; void push(char); char pop(); int precede(char); main(){ char infix[100],postfix[100],ch; int i=0,j=0; // Read infix expression from user printf("Enter infix expression:"); scanf("%s",infix); // evaluate each char in infix expression while((ch=infix[i++])!='\0'){ // if it is number, add it to postfix expression if(isalnum(ch)){ ...
USING THE C++ CODE BELOW, CAN YOU PLEASE SOLVE THE QUESTION AND
EXPLAIN THE STEPS.
class StaticStack
{
private:
int *stack;
int capacity;
int top; // index of the top
element
public:
StaticStack(int size); //
constructor
~StaticStack();
bool isFull();
bool isEmpty();
void push(int);
int pop();
};
#include "StaticStack.h"
#include <iostream>
using namespace std;
StaticStack::StaticStack(int size)
{
stack = new int[size]; //
constructing a size sized array
capacity = size;
top = -1; // empty stack
}
StaticStack::~StaticStack()
{
delete...
I have added a little Code but I need help with the rest. /** A class of stacks whose entries are stored in a chain of nodes. Implement all methods in MyStack class Main Reference : text book or class notes Do not change or add data fields */ package PJ2; public class MyStack<T> implements StackInterface<T> { // Data fields private Node<T> topNode; // references the first node in the chain private int numberOfEntries; public...
I need help fixing my code. My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...