Conversions
Conversions
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;
}
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;
}
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();
}