0% found this document useful (0 votes)
29 views

Conversions

The document contains code for converting between infix, postfix, and prefix notations. It includes classes with methods for: 1. Pushing and popping operators and operands onto a stack. 2. Checking operator precedence to determine evaluation order when converting infix to postfix or prefix. 3. Converting between the notations in both directions by iterating through the expression and handling operators and operands accordingly.

Uploaded by

ayesha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Conversions

The document contains code for converting between infix, postfix, and prefix notations. It includes classes with methods for: 1. Pushing and popping operators and operands onto a stack. 2. Checking operator precedence to determine evaluation order when converting infix to postfix or prefix. 3. Converting between the notations in both directions by iterating through the expression and handling operators and operands accordingly.

Uploaded by

ayesha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Assignment #02

Infix to postfix

#include<iostream>
#include<conio.h>
using namespace std;
class conversion{
public:
int top;
char stack[100];
conversion(){
top=-1;
}
public : void push(char ch){
++top;
stack[top]=ch;
// cout<<stack[top];
}
public: char pop(){

if (isempty()){
cout<<"empty";
}else{
char c;
c=stack[top];
top=top-1;
return c;
}
}
public :int isempty(){

if(top==-1){
return 1;
}
else{
return 0;
}

}
public: int gettop(){

char c;
c= stack[top];
return c;
}

public :int precedence(char symbol){ // set precedence


switch(symbol){
case '^':
return 3;
case '*':
case '/':
return 2;
case '+':
case '-':
return 1;
default:
return 0;}
}
void calculation(){
int i=0;
int j=0;
char infix[100];
char postfix[100];
char symbol,next;
cout<<"enter the expression";
cin>>infix;
while(infix[i]!='\0'){ // run loop until null value
symbol=infix[i];
switch(symbol){
case '(':
push(symbol);
break;
case ')':
while((next=pop())!='('){ // pop all
characters until ( bracket
postfix[j]=next;
j++;
}
break;
case '+':
case '-':
case '*':
case '/':
case '^':
while(top!=-1 &&
precedence(gettop())>= precedence(symbol))
postfix[j++]=pop();
push(symbol);
break;
default:
postfix[j++]=symbol;
}
i++;
}
while(!isempty()){ // display remaining elements in stack
postfix[j++]=pop();
}
int n=0; // display final results
while(postfix[n]!='\0'){
cout<<postfix[n];
n++;
}
}
};

int main(){
conversion s;
s.calculation();
getch();
}

Infix to prefix

#include<iostream>
#include<string.h>
using namespace std;
class conversion{
public:
int top;
char stack[100];
conversion(){
top=-1;
}
public : void push(char ch){
++top;
stack[top]=ch;
// cout<<stack[top];
}
public: char pop(){

if (isempty()){
cout<<"empty";
}else{
char c;
c=stack[top];
top=top-1;
return c;
}
}
public :int isempty(){

if(top==-1){
return 1;
}
else{
return 0;
}

}
public: int gettop(){

char c;
c= stack[top];
return c;
}

public :int precedence(char symbol){ // set precedence


switch(symbol){
case '^':
return 3;
case '*':
case '/':
return 2;
case '+':
case '-':
return 1;
default:
return 0;}
}
void calculation(){
int i=0;
int j=0;
char infix[100];
char postfix[100];
char symbol,next;
cout<<"enter the expression";
cin>>infix;
strrev(infix);
cout<<infix<<endl;
while(infix[i]!='\0'){ // run loop until null value
symbol=infix[i];
cout<<symbol<<endl;
switch(symbol){
case '(':
push(symbol);
break;
case ')':
while((next=pop())!='('){ // pop all
characters until ( bracket
postfix[j]=next;
j++;
}
break;
case '+':
case '-':
case '*':
case '/':
case '^':
while(top!=-1 && precedence(gettop())>
precedence(symbol)) //check precedece til brackets
postfix[j++]=pop();
push(symbol);
break;
default:
postfix[j++]=symbol;
}
i++;
}
while(!isempty()){ // display remaining elements in stack
postfix[j++]=pop();
}
int n=0;
strrev(postfix);
cout<<postfix<<endl;
}
};
int main(){
conversion s;
s.calculation();
}

Postfix to infix

#include<iostream>
#include<string>
using namespace std;
class conversion{
public:
int top;
string stack[20];
conversion(){
top=-1;
}
public : void push(char ch){
string s;
s+=ch;
++top;
stack[top]=s;
}
public : void push(string ch){
++top;
stack[top]=ch;
}
public: string pop(){

if (isempty()){
cout<<"empty";
}else{
string c;
c=stack[top];
top=top-1;
return c;
}
}
public :int isempty(){

if(top==-1){
return 1;
}
else{
return 0;
}

}
bool isOperand(char c)
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
return true;
}
else {
return false;
}
}
public: string gettop(){

string c;
c=stack[top];
return c;
}
void calculation(){
string operator1,operator2;
int i=0;
int j=0;
string postfix;
cout<<"Enter postfix the expression to convert into
infix:";
cin>>postfix;

if(isOperand(postfix[1])&&isOperand(postfix[0])&&isOper
and(postfix[2])&&!isOperand(postfix[postfix.length()-1])){
int n=0;
for(int i=0;i<postfix.length();i++)
{
char ch='\0';
ch+=postfix[i];
if(isOperand(postfix[i])){
push(ch);
}
else{
string s="";
s+=ch;
operator1=gettop();
pop();
operator2=gettop();
pop();
push("("+operator2+s+operator1+")");
}
}
cout<<gettop()<<endl;
}else{
cout<<"your enterd infix is wrong.."<<endl;
}
}
};

int main(){
conversion s;
s.calculation();
}

Prefix to infix

#include<iostream>
#include<string.h>
using namespace std;
class conversion{
public:
int top;
string stack[20];
conversion(){
top=-1;
}
public : void push(char ch){
string s;
s+=ch;
++top;
stack[top]=s;
}
public : void push(string ch){
++top;
stack[top]=ch;
}
public: string pop(){

if (isempty()){
cout<<"empty";
}else{
string c;
c=stack[top];
top=top-1;
return c;
}
}
public :int isempty(){

if(top==-1){
return 1;
}
else{
return 0;
}

}
bool isOperand(char c)
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
return true;
}
else {
return false;
}
}
public: string gettop(){

string c;
c=stack[top];
return c;
}
void calculation(){
string operator1,operator2;
string postfix="";
cout<<"Enter postfix the expression to convert into
infix:";
cin>>postfix;
int len=postfix.length();
cout<<postfix<<endl;

if(!isOperand(postfix[0])&&isOperand(postfix[postfix.leng
th()-1])){
int n=0;
for(int i=postfix.length()-1;i>=0;i--)
{
char ch='\0';
ch+=postfix[i];
if(isOperand(postfix[i])){
push(ch);
}
else{
string s="";
s+=ch;
operator1=gettop();
pop();
operator2=gettop();
pop();
push("("+operator1+s+operator2+")");
}
}
cout<<gettop()<<endl;
}else{
cout<<"your enterd infix is wrong.."<<endl;
}
}
};

int main(){
conversion s;
s.calculation();
}

Prefix to postfix

#include<iostream>
#include<string>
using namespace std;
class conversion{
public:
int top;
string stack[20];
conversion(){
top=-1;
}
public : void push(char ch){
string s;
s+=ch;
++top;
stack[top]=s;
}
public : void push(string ch){
++top;
stack[top]=ch;
}
public: string pop(){

if (isempty()){
cout<<"empty";
}else{
string c;
c=stack[top];
top=top-1;
return c;
}
}
public :int isempty(){

if(top==-1){
return 1;
}
else{
return 0;
}

}
bool isOperand(char c)
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
return true;
}
else {
return false;
}
}
public: string gettop(){

string c;
c=stack[top];
return c;
}
void calculation(){
string operator1,operator2;
int i=0;
int j=0;
string postfix;
cout<<"Enter postfix the expression to convert into
infix:";
cin>>postfix;

if(!isOperand(postfix[0])&&isOperand(postfix[postfix.leng
th()-1])){
int n=0;
for(int i=postfix.length()-1;i>=0;i--)
{
char ch='\0';
ch+=postfix[i];
if(isOperand(postfix[i])){
push(ch);
}
else{
string s="";
s+=ch;
operator1=gettop();
pop();
operator2=gettop();
pop();
push("("+operator2+operator1+s+")");
}
}
cout<<gettop()<<endl;
}else{
cout<<"your enterd infix is wrong.."<<endl;
}
}
};

int main(){
conversion s;
s.calculation();
}
Postfix to prefix

#include<iostream>
#include<string>
using namespace std;
class conversion{
public:
int top;
string stack[20];
conversion(){
top=-1;
}
public : void push(char ch){
string s;
s+=ch;
++top;
stack[top]=s;
}
public : void push(string ch){
++top;
stack[top]=ch;
}
public: string pop(){

if (isempty()){
cout<<"empty";
}else{
string c;
c=stack[top];
top=top-1;
return c;
}
}
public :int isempty(){

if(top==-1){
return 1;
}
else{
return 0;
}

}
bool isOperand(char c)
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
return true;
}
else {
return false;
}
}
public: string gettop(){

string c;
c=stack[top];
return c;
}
void calculation(){
string operator1,operator2;
int i=0;
int j=0;
string postfix;
cout<<"Enter postfix the expression to convert into
infix:";
cin>>postfix;
if(isOperand(postfix[1])&&isOperand(postfix[0])&&isOper
and(postfix[2])&&!isOperand(postfix[postfix.length()-1])){
int n=0;
for(int i=0;i<postfix.length();i++)
{
char ch='\0';
ch+=postfix[i];
if(isOperand(postfix[i])){
push(ch);
}
else{
string s="";
s+=ch;
operator1=gettop();
pop();
operator2=gettop();
pop();
push("("+s+operator2+operator1+")");
}
}
cout<<gettop()<<endl;
}else{
cout<<"your enterd infix is wrong.."<<endl;
}
}
};

int main(){
conversion s;
s.calculation();
}

You might also like