Assignment 6
Assignment 6
ASSIGNMENT-6
Course: BTECH(CSE)
Section: B1
}
i++;
}
printf("%d",stack[top]);
return 0;
}
Question-2: Infix to prefix
#include<iostream>
#include<string.h>
using namespace std;
int SIZE=20;
int pre(char c)
{
switch(c)
{
case '+': return 1;
case '-': return 1;
case '*': return 2;
case '/': return 2;
default: return 0;
}
}
if (*top == -1)
{
cout << "Stack is empty" << endl;
return 0;
}
else
{
return arr[(*top)--];
}
}
void reversestring(char str[],int n)
{
int start=0;
int end=n-1;
while(start<end)
{
swap(str[start],str[end]);
start++;
end--;
}
}
int main()
{
char infix[SIZE],prefix[SIZE];
char stack[SIZE];
int top=-1;
int j=0;
cout<<"Enter an infix ";
cin>>infix;
int len=strlen(infix);
reversestring(infix,len);
for(int i=0;i<len;i++)
{
if(infix[i]=='(')
{
infix[i]=')';
}
else if(infix[i]==')')
{
infix[i]='(';
}
}
for(int i=0;i<len;i++)
{
char c=infix[i];
if(c>='a'&&c<='z')
{
prefix[j]=c;
j++;
}
else if(c=='(')
{
push(stack,&top,c);
}
else if(c==')'){
while(top!=-1&& stack[top]!='(')
{
prefix[j]=pop(stack,&top);
j++;
}
pop(stack,&top);
}
else{
while(top!=-1&&pre(stack[top])>=pre(c))
{
prefix[j]=pop(stack,&top);
j++;
}
push(stack,&top,c);
}
}
while(top!=-1){
prefix[j]=pop(stack,&top);
j++;
}
prefix[j]='\0';
reversestring(prefix,j);
cout<<"Prefix: "<<prefix<<endl;
return 0;
}
Question-3: Postfix to prefix
#include<stdio.h>
#include<string.h>
int SIZE=20;
void push(char arr[][SIZE],int *top,char* x)
{
if(*top==SIZE-1)
{
printf("Stack is full");
}
else{
strcpy(arr[++(*top)],x);
}
}
char* pop(char arr[][SIZE], int *top)
{
if (*top == -1)
{
printf("Stack is empty\n");
return NULL;
}
else
{
return arr[(*top)--];
}
}
int isoperator(char c)
{
return(c=='+'||c=='-'||c=='*'||c=='/');
}
int main()
{
char postfix[SIZE],stack[SIZE][SIZE];
int top=-1;
printf("Postfix expression");
scanf("%s",postfix);
int len=strlen(postfix);
for(int i=0;i<len;i++){
if(!isoperator(postfix[i]))
{
char oper[2]={postfix[i],'\0'};
push(stack,&top,oper);
}
else
{
char oper1[SIZE],oper2[SIZE],result[SIZE];
int j=0;
char *temp1 = pop(stack, &top);
char *temp2 = pop(stack, &top);
strcpy(oper1, temp1);
strcpy(oper2, temp2);
result[0] = postfix[i];
result[1] = '\0';
strcat(result, oper2);
strcat(result, oper1);
push(stack, &top, result);
}
}
printf("Prefix expression is:%s",(pop(stack,&top)));
return 0;
}
Question-4: Prefix to postfix
#include <stdio.h>
#include <string.h>
int SIZE=100;
void push(char stack[SIZE][SIZE], int *top, const char *value)
{
if (*top >= SIZE - 1)
{
printf("Stack is full\n");
return;
}
strcpy(stack[++(*top)], value);
}
char stack[SIZE][SIZE];
int top=-1;
char *value;
char op1[SIZE],op2[SIZE],result[SIZE];
int i=strlen(prefix)-1;
while(i>=0)
{
if(isalnum(prefix[i])){
char operand[2];
operand[0] = prefix[i];
operand[1] = '\0';
push(stack, &top, operand);
}
else if(isoperator(prefix[i]))
{
pop(stack,&top,op1);
pop(stack,&top,op2);
result[0]='(';
strcpy(result+1,op1);
int len=strlen(result);
result[len]=' ';
result[len+1]=prefix[i];
result[len+2]=' ';
strcpy(result+len+3,op2);
result[len + 3 + strlen(op2)] = ')';
result[len + 3 + strlen(op2) + 1] = '\0';
push(stack, &top, result);
}
i--;
}
printf("infix is :%s",stack[top]);
return 0;
}