2024-09-03 00:27:03 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define MAX_SIZE 100
|
|
|
|
|
|
|
|
char stack[MAX_SIZE];
|
|
|
|
int top = -1;
|
|
|
|
|
2024-09-03 11:27:33 +05:30
|
|
|
void push(char item)
|
|
|
|
{
|
|
|
|
if (top >= MAX_SIZE - 1)
|
|
|
|
{
|
2024-09-03 00:27:03 +05:30
|
|
|
printf("Stack Overflow\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
stack[++top] = item;
|
|
|
|
}
|
|
|
|
|
2024-09-03 11:27:33 +05:30
|
|
|
char pop()
|
|
|
|
{
|
|
|
|
if (top < 0)
|
|
|
|
{
|
2024-09-03 00:27:03 +05:30
|
|
|
printf("Stack Underflow\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return stack[top--];
|
|
|
|
}
|
|
|
|
|
2024-09-03 11:27:33 +05:30
|
|
|
int precedence(char symbol)
|
|
|
|
{
|
|
|
|
switch (symbol)
|
|
|
|
{
|
|
|
|
case '^':
|
|
|
|
return 3;
|
|
|
|
case '*':
|
|
|
|
case '/':
|
|
|
|
return 2;
|
|
|
|
case '+':
|
|
|
|
case '-':
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
return 0;
|
2024-09-03 00:27:03 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-03 11:27:33 +05:30
|
|
|
void reverse(char *str)
|
|
|
|
{
|
2024-09-03 00:27:03 +05:30
|
|
|
int len = strlen(str);
|
|
|
|
int i, j;
|
|
|
|
char temp;
|
2024-09-03 11:27:33 +05:30
|
|
|
for (i = 0, j = len - 1; i < j; i++, j--)
|
|
|
|
{
|
2024-09-03 00:27:03 +05:30
|
|
|
temp = str[i];
|
|
|
|
str[i] = str[j];
|
|
|
|
str[j] = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-03 11:27:33 +05:30
|
|
|
int isalnum(char c)
|
|
|
|
{
|
2024-09-03 00:27:03 +05:30
|
|
|
return ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
|
|
|
|
}
|
|
|
|
|
2024-09-03 11:27:33 +05:30
|
|
|
void infixToPrefix(char infix[], char prefix[])
|
|
|
|
{
|
2024-09-03 00:27:03 +05:30
|
|
|
char symbol, temp[2];
|
|
|
|
int i, j = 0;
|
2024-09-03 11:27:33 +05:30
|
|
|
|
2024-09-03 00:27:03 +05:30
|
|
|
reverse(infix);
|
2024-09-03 11:27:33 +05:30
|
|
|
|
|
|
|
for (i = 0; infix[i] != '\0'; i++)
|
|
|
|
{
|
2024-09-03 00:27:03 +05:30
|
|
|
symbol = infix[i];
|
2024-09-03 11:27:33 +05:30
|
|
|
if (isalnum(symbol))
|
|
|
|
{
|
2024-09-03 00:27:03 +05:30
|
|
|
prefix[j++] = symbol;
|
2024-09-03 11:27:33 +05:30
|
|
|
}
|
|
|
|
else if (symbol == ')')
|
|
|
|
{
|
2024-09-03 00:27:03 +05:30
|
|
|
push(symbol);
|
2024-09-03 11:27:33 +05:30
|
|
|
}
|
|
|
|
else if (symbol == '(')
|
|
|
|
{
|
|
|
|
while ((temp[0] = pop()) != ')')
|
|
|
|
{
|
2024-09-03 00:27:03 +05:30
|
|
|
prefix[j++] = temp[0];
|
|
|
|
}
|
2024-09-03 11:27:33 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (top > -1 && precedence(stack[top]) > precedence(symbol))
|
|
|
|
{
|
2024-09-03 00:27:03 +05:30
|
|
|
prefix[j++] = pop();
|
|
|
|
}
|
|
|
|
push(symbol);
|
|
|
|
}
|
|
|
|
}
|
2024-09-03 11:27:33 +05:30
|
|
|
|
|
|
|
while (top > -1)
|
|
|
|
{
|
2024-09-03 00:27:03 +05:30
|
|
|
prefix[j++] = pop();
|
|
|
|
}
|
|
|
|
prefix[j] = '\0';
|
2024-09-03 11:27:33 +05:30
|
|
|
|
2024-09-03 00:27:03 +05:30
|
|
|
reverse(prefix);
|
|
|
|
}
|
|
|
|
|
2024-09-03 11:27:33 +05:30
|
|
|
int main()
|
|
|
|
{
|
2024-09-03 00:27:03 +05:30
|
|
|
char infix[MAX_SIZE], prefix[MAX_SIZE];
|
|
|
|
printf("Enter infix expression: ");
|
2024-09-03 11:27:33 +05:30
|
|
|
gets(infix);
|
2024-09-03 00:27:03 +05:30
|
|
|
infixToPrefix(infix, prefix);
|
|
|
|
printf("Prefix expression: %s\n", prefix);
|
|
|
|
return 0;
|
|
|
|
}
|