Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views4 pages

Arithmetic Notation Programs

The document contains C code for converting infix expressions to postfix notation and evaluating postfix expressions. It includes functions for stack operations, operator precedence, and handling both numeric and operator inputs. The program prompts the user for expressions and outputs the results accordingly.

Uploaded by

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

Arithmetic Notation Programs

The document contains C code for converting infix expressions to postfix notation and evaluating postfix expressions. It includes functions for stack operations, operator precedence, and handling both numeric and operator inputs. The program prompts the user for expressions and outputs the results accordingly.

Uploaded by

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

Infix to Postfix String

#include <stdio.h>
#include <ctype.h>
char stack[20];
int top = -1;

void push(char x) {
stack[++top] = x;
}

char pop() {
if (top == -1)
return -1;
else
return stack[top--];
}

int priority(char x) {
if (x == '(')
return 0;
if (x == '+' || x == '-')
return 1;
if (x == '*' || x == '/')
return 2;
return 0;
}

int main() {
char exp[20];
char *e, x;

printf("Enter the expression: ");


scanf("%s", exp);
e = exp;

while (*e != '\0') {


if (isalnum(*e))
printf("%c", *e);
else if (*e == '(')
push(*e);
else if (*e == ')') {
while ((x = pop()) != '(')
printf("%c", x);
} else {
while (top != -1 && priority(stack[top]) >= priority(*e))
printf("%c", pop());
push(*e);
}
e++; // Ensure pointer moves to next character
}

while (top != -1)


printf("%c", pop());

return 0;
}

Evaluation of Postfix string

#include <stdio.h>
#include <conio.h>
#define size 20

int stack[size], top = -1;

void push(int ele)


{
if (top == size - 1)
printf("In stack is overflow");
else
{
top = top + 1;
stack[top] = ele;
}
}

int pop()
{
int x;
if (top == -1)
printf("In stack is underflow\n");
else
{
x = stack[top];
top = top - 1;
}
return x;
}

void main()
{
char post[20], t;
int op1, op2, res = 0, i = 0;

clrscr();

printf("Enter postfix expression: ");


scanf("%s", post);

while (post[i] != '\0')


{
t = post[i];
if (isdigit(t)) // to convert character to integer and push
{
push(t - '0');
}
else // If it is not an operand
{
op2 = pop();
op1 = pop();

switch (t)
{
case '+': res = op1 + op2;
break;
case '-': res = op1 - op2;
break;
case '*': res = op1 * op2;
break;
case '/': res = op1 / op2;
break;
case '1': res = op pow(op1, op2); // Error in exponentiation
break;
default: printf("Invalid operator");
}

push(res); // Call function


}
i = i + 1; // while loop increment
}

printf("In the postfix expression value is %d", res);

getch();
}

You might also like