#include <iostream>
#include<string.h>
using namespace std;
//Class Node Linked List
class Node
{
public:
string data;
Node *next;
};
//Stack class
class stack
{
public:
Node * headref = new Node ();
//top = new Node();
//POP ELEMENT FROM STACK
string pop ()
{
string top;
if (headref == NULL)
{
cout << "Stack is empty";
return "EMPTY";
}
else
{
top = headref->data;
headref = headref->next;
return top;
}
return top;
}
//PUSH ELEMENT IN STACK
string push (string element)
{
if (headref == NULL)
{
headref->data = element;
headref->next = NULL;
}
else
{
Node *newnode = new Node ();
newnode->data = element;
newnode->next = headref;
headref = newnode;
}
return "Hello";
}
//RETURNS TOP OF STACK
string top()
{
//Segmentation Fault right here
string temp = headref->data;
return temp;
}
//print stack
void printStack ()
{
while (headref != NULL)
{
cout << headref->data << "\n";
headref = headref->next;
}
}
};
//Converts infix to Postfix
class convert:public stack
{
private:
int precidence (string operatoor)
{
if (operatoor == "*" || operatoor == "/")
{
return 2;
}
if (operatoor == "+" || operatoor == "-")
{
return 1;
}
if (operatoor == "^")
{
return 0;
}
}
public:
string algorithmInfix (string infix)
{
string postfix;
for (int i = 0; i < infix.length (); i++)
{
string temp;
temp.push_back (infix[i]);
if (isalpha (infix[i]) || isdigit (infix[i]))
{
postfix = postfix + infix[i];
}
else
{
if (temp == "(")
{
cout << "( goes in stack";
push (temp);
continue;
}
else if (temp == ")")
{
cout << "Comming out of stack";
while (top () != "(")
{
postfix = postfix + pop ();
}
pop ();
}
if (top () == "" || top () == "("
|| (precidence (temp) >= precidence (top ())))
{
push (temp);
}
else
{
while (precidence (top ()) >= precidence (temp))
{
if (top () == "(" || top () == ")")
{
push (temp);
break;
}
postfix = postfix + pop ();
}
push (temp);
}
}
}
string temp = top ();
while (true)
{
if (temp == "")
{
break;
}
postfix = postfix + pop ();
temp = top ();
}
/*while(top())
postfix = postfix + pop(); */
return postfix;
}
};
//Main
int main ()
{
//stack obj;
convert obj;
string infix;
while (true)
{
cout << "Insert your Infix statement. To Exit type exit!!:";
//cin.ignore();
getline (cin, infix);
if (infix == "exit" || infix == "EXIT" || infix == "Exit")
{
break;
}
//cout<<"here";
string postfix;
postfix = obj.algorithmInfix (infix);
//cout<<"here";
cout << infix << " " << postfix << endl;
}
/*obj.push('5');
obj.push('6');
string top = obj.pop();
cout<<"Poped this element->"<<top<<endl;
obj.push('*'); */
obj.printStack ();
//cout<<"Hello"<<endl;
}
lundi 6 décembre 2021
Program received signal SIGSEGV, Segmentation fault where is the problem the pointer is not null
Inscription à :
Publier les commentaires (Atom)
Aucun commentaire:
Enregistrer un commentaire