Skip to content

Instantly share code, notes, and snippets.

@meldsza
Created August 26, 2018 12:37
Show Gist options
  • Save meldsza/68a81496cde1dd43905d60836fa2688e to your computer and use it in GitHub Desktop.
Save meldsza/68a81496cde1dd43905d60836fa2688e to your computer and use it in GitHub Desktop.
/**
Infix to postfix
Write a C program to convert and print a given valid parenthesized infix arithmetic expression to postfix expression. The expression consists of single character operands and +,-, *, / operators.
Constraints: only four operators used +, -, *, / .
Input format:
Input consists of a string which consists of infix expression.
The output consists of an postfix expression. If the input In-fix expression consists of an operator other than Arithmetic operators mentioned above print "Invalid Input".
Refer sample input and output for formatting specifications.
[All text in bold corresponds to the input and the rest corresponds to output.]
Output format:
The output consists of postfix expression.
Sample Input-Output :
Enter the infix expression
a+b
The postfix expression is
ab+
Sample Input-Output :
Enter the infix expression
a+b-(c*d)
The postfix expression is
ab+cd*-
Additional Sample TestCases
Sample Input and Output 1 :
Enter the infix expression
a+b
The postfix expression is
ab+
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#define MAX 100
struct {
char items[MAX];
int top;
} typedef Stack;
int precedence(char sym)
{
switch(sym){
case '(':
case ')': return 0;
case '+':
case '-': return 1;
case '/':
case '*': return 2;
case '$':
case '^': return 3;
default: return -1;
}
}
int isEmpty(Stack *s)
{
return (s->top == -1);
}
void push(Stack*s, char item)
{
//printf("Pushed %c\n", item);
s->items[++s->top] = item;
}
char pop(Stack *s)
{
if(isEmpty(s))
return -1;
return s->items[s->top--];
}
Stack* create_stack(void)
{
Stack *s = malloc(sizeof(Stack));
s->top=-1;
return s;
}
int getTop(Stack *s)
{
if(isEmpty(s))
return -1;
return s->items[s->top];
}
int convert(char* infix, char* postfix)
{
Stack* s = create_stack();
char temp=0;
int pos=0,i=0;
for(i=0;infix[i]!=0;i++)
{
//printf("%c\n",infix[i]);
switch(infix[i])
{
case'(': push(s, infix[i]); break;
case')':
temp = pop(s);
while(temp!='(')
{
if(isEmpty(s))
return 0;
postfix[pos++] =temp;
temp = pop(s);
}
break;
case '+':
case '-':
case '*':
case '/':
case '$':
while((!isEmpty(s)) && (precedence(getTop(s)) >= precedence(infix[i])) && precedence(infix[i]) >0)
{
temp=pop(s);
postfix[pos++]=temp;
}
push(s, infix[i]);
break;
default:
if(isalnum(infix[i]))
postfix[pos++]=infix[i];
else
return 0;
}
}
//printf("%c",getTop(s));
while(!isEmpty(s))
postfix[pos++]=pop(s);
return 1;//sucess
}
int main()
{
char infix[100], postfix[100]={0};
puts("Enter the infix expression");
gets(infix);
int f=convert(infix,postfix);
if(f)
{
puts("The postfix expression is");
puts(postfix);
}
else
{
puts("Invalid input");
}
return 0;
}
/**
Suffix-Postfix
Write a C program to evaluate a valid suffix/postfix expression using the stack. Assume that suffix/postfix expression is read as a single line consisting of non –negative single digit operands and binary arithmetic operators. The arithmetic operators are +, -,/,*,^, ($).
Input-Output Format:
Input will be in String format which represents postfix expression.
The output will be an Integer value obtained after converting postfix Expression to Infix and executed.
If the input postfix expression consists of an operator other than Arithmetic operators mentioned above print "Invalid Input".
Refer sample input and output for formatting specifications.
[All text in bold corresponds to the input and the rest corresponds to output.]
Sample Input-Output:
Enter the postfix expression 62/3-42*+ Result after evaluation is 8.00
Additional Sample TestCases
Sample Input and Output 1 :
Enter the postfix expression
62/3-42*+
Result after evaluation is 8.00
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#define MAX 100
struct {
float items[MAX];
int top;
} typedef Stack;
float operation(float op1, char sym, float op2)
{
switch(sym){
case '+': return op1+op2;
case '-': return op1-op2;
case '/': return op1/op2;
case '*': return op1*op2;
case '$': case'^': return pow(op1,op2);
default: return -1;
}
}
void push(Stack*s, float item)
{
s->items[++s->top] = item;
}
float pop(Stack *s)
{
return s->items[s->top--];
}
Stack* create_stack(void)
{
Stack *s = malloc(sizeof(Stack));
s->top=-1;
return s;
}
int main()
{
char input[100];
puts("Enter the postfix expression");
gets(input);
Stack* s = create_stack();
char sym = input[0];
int i;
float op1,op2,temp;
for(i=0;input[i]!=0;i++)
{
sym = input[i];
if(isdigit(sym))
{
push(s,sym-'0');
}
else
{
op2 = pop(s);
op1 = pop(s);
temp = operation(op1,sym,op2);
if(temp == -1)
{
puts("Invalid input");
return 0;
}
else
push(s,temp);
}
}
printf("Result after evaluation is %.2f\n",pop(s));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment