Я создаю функцию, которая получает от пользователя инфиксное выражение и возвращает его значение. Введением будет постфикс.
Пожалуйста, вы можете найти ошибку, которую я сделал в коде, потому что, когда я пытаюсь вставить числа и операторы, это не работает. Я попытался ввести 1 + 2, и ничего не появилось. PS: я еще не делал оценку.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct
{
int items[100],top;
} stack;
void initialize (stack*s)
{
s->top=0;
}
int pop (stack*s)
{
int out =s->items[s->top];
s->top--;
return out;
}
int push(stack*s,int x)
{
s->top++;
s->items[s->top]=x;
}
int top(stack*s)
{
return s->items[s->top=-1];
}
int isEmpty(stack*s)
{
if(s->top==0)
return 1;
else
return 0;
}
void infixToPostfix (char *infix,char *postfix)
{
stack s;
int ch;
while(isEmpty(&s)==0)
{
if( ch >0&& ch <=9)
printf("%c", ch );
else
switch( ch )
{
case'(':
push(&s,ch);
case'+':
push(&s,ch);
case'-':
push(&s,ch);
case'*':
push(&s,ch);
case'/':
push(&s,ch);
case')':
pop(&s);
}
}
}
int main()
{
char Iexp[256]="",Pexp[256]="";
printf("Enter the expression");
scanf("%s",&Iexp);
infixToPostfix(Iexp,Pexp);
printf("Postfix : %s\n",Pexp);
return 0;
}