Upload files to "DS/C/Lab/Week6"
This commit is contained in:
parent
5b8ed261ff
commit
8de2a08e83
3 changed files with 132 additions and 75 deletions
|
@ -6,85 +6,110 @@
|
|||
char stack[MAX_SIZE];
|
||||
int top = -1;
|
||||
|
||||
void push(char item) {
|
||||
if (top >= MAX_SIZE - 1) {
|
||||
void push(char item)
|
||||
{
|
||||
if (top >= MAX_SIZE - 1)
|
||||
{
|
||||
printf("Stack Overflow\n");
|
||||
return;
|
||||
}
|
||||
stack[++top] = item;
|
||||
}
|
||||
|
||||
char pop() {
|
||||
if (top < 0) {
|
||||
char pop()
|
||||
{
|
||||
if (top < 0)
|
||||
{
|
||||
printf("Stack Underflow\n");
|
||||
return -1;
|
||||
}
|
||||
return stack[top--];
|
||||
}
|
||||
|
||||
int precedence(char symbol) {
|
||||
switch (symbol) {
|
||||
case '^': return 3;
|
||||
case '*':
|
||||
case '/': return 2;
|
||||
case '+':
|
||||
case '-': return 1;
|
||||
default: return 0;
|
||||
int precedence(char symbol)
|
||||
{
|
||||
switch (symbol)
|
||||
{
|
||||
case '^':
|
||||
return 3;
|
||||
case '*':
|
||||
case '/':
|
||||
return 2;
|
||||
case '+':
|
||||
case '-':
|
||||
return 1;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void reverse(char *str) {
|
||||
void reverse(char *str)
|
||||
{
|
||||
int len = strlen(str);
|
||||
int i, j;
|
||||
char temp;
|
||||
for (i = 0, j = len - 1; i < j; i++, j--) {
|
||||
for (i = 0, j = len - 1; i < j; i++, j--)
|
||||
{
|
||||
temp = str[i];
|
||||
str[i] = str[j];
|
||||
str[j] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
int isalnum(char c) {
|
||||
int isalnum(char c)
|
||||
{
|
||||
return ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
|
||||
}
|
||||
|
||||
void infixToPrefix(char infix[], char prefix[]) {
|
||||
void infixToPrefix(char infix[], char prefix[])
|
||||
{
|
||||
char symbol, temp[2];
|
||||
int i, j = 0;
|
||||
|
||||
|
||||
reverse(infix);
|
||||
|
||||
for (i = 0; infix[i] != '\0'; i++) {
|
||||
|
||||
for (i = 0; infix[i] != '\0'; i++)
|
||||
{
|
||||
symbol = infix[i];
|
||||
if (isalnum(symbol)) {
|
||||
if (isalnum(symbol))
|
||||
{
|
||||
prefix[j++] = symbol;
|
||||
} else if (symbol == ')') {
|
||||
}
|
||||
else if (symbol == ')')
|
||||
{
|
||||
push(symbol);
|
||||
} else if (symbol == '(') {
|
||||
while ((temp[0] = pop()) != ')') {
|
||||
}
|
||||
else if (symbol == '(')
|
||||
{
|
||||
while ((temp[0] = pop()) != ')')
|
||||
{
|
||||
prefix[j++] = temp[0];
|
||||
}
|
||||
} else {
|
||||
while (top > -1 && precedence(stack[top]) > precedence(symbol)) {
|
||||
}
|
||||
else
|
||||
{
|
||||
while (top > -1 && precedence(stack[top]) > precedence(symbol))
|
||||
{
|
||||
prefix[j++] = pop();
|
||||
}
|
||||
push(symbol);
|
||||
}
|
||||
}
|
||||
|
||||
while (top > -1) {
|
||||
|
||||
while (top > -1)
|
||||
{
|
||||
prefix[j++] = pop();
|
||||
}
|
||||
prefix[j] = '\0';
|
||||
|
||||
|
||||
reverse(prefix);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main()
|
||||
{
|
||||
char infix[MAX_SIZE], prefix[MAX_SIZE];
|
||||
printf("Enter infix expression: ");
|
||||
fgets(infix, MAX_SIZE, stdin);
|
||||
infix[strcspn(infix, "\n")] = 0; // Remove newline if present
|
||||
gets(infix);
|
||||
infixToPrefix(infix, prefix);
|
||||
printf("Prefix expression: %s\n", prefix);
|
||||
return 0;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue