int PostfixCheck(char equation[])	//This method checks either Postfix Equation is valid or not, incase of valid, it returns 1
{
	int i;
	Stack S;
	S=createStack();
	for(i=0;equation[i]!='\0';i++)
	{
		if(equation[i]!='+' && equation[i]!='-'  && equation[i]=='/'  && equation[i]=='*')
		{
			PushIntoStack(equation[i],S);
		}
		else if(equation[i]=='+' || equation[i]=='-'  || equation[i]=='/'  || equation[i]=='*')
		{
			Pop_Stack(S);
			Pop_Stack(S);
			PushIntoStack(equation[i],S);
		}
		else
			PushIntoStack(equation[i],S);
	}
	Pop_Stack(S);
	if(IsEmpty_Stack(S)==0)
		return 1;
	else
		return 0;
}
Esempio n. 2
0
void Calc(Linklist* operator, Linklist* numbers)
{
  int num1 = Pop_Stack(numbers);
  int num2 = Pop_Stack(numbers);

  switch(Pop_Stack(operator))
  {
  case '+':
      Push_Stack(numbers, num2 + num1);
      break;
  case '-':
      Push_Stack(numbers, num2 - num1);
      break;
  case '*':
      Push_Stack(numbers, num2 * num1);
      break;
  case '/':
      Push_Stack(numbers, num2 / num1);
      break;
  default:
      printf("error\n");

  }
}
Esempio n. 3
0
int main(int argc, const char* argv[])
{

    Linklist operator;
    Init_Stack(&operator);
    Push_Stack(&operator, '#'); // avoid for empty stack..

    Linklist numbers;
    Init_Stack(&numbers);

    char in = 0;
    while(scanf("%c", &in) && in != '\n' && in != '\r')
    {
        // printf("in:%c\n", in);
        if (in == ' ')
        {
            continue;
        }
        if (Is_Operator(in))
        {
            if (in == '+' || in == '-' || in == '*' || in == '/')
            {
                if (Operator_Level(Top_Stack(operator)) < Operator_Level(in))    // lower than in
                {
                    // printf("compare: %c < %c success, push\n", Top_Stack(operator), in);
                    Push_Stack(&operator, in);
                }
                else
                {
                    // printf("compare: %c >= %c success, calc\n", Top_Stack(operator), in);
                    Calc(&operator, &numbers);
                    Push_Stack(&operator, in);
                }
            }
            else if (in == '(')
            {
                Push_Stack(&operator, in);
            }
            else if (in == ')')
            {
                while (Top_Stack(operator) != '(')
                {
                    Calc(&operator, &numbers);
                }
                Pop_Stack(&operator);
            }
            else
            {
              printf("nothing:%c\n", in);
            }
        }
        else
        {
            Push_Stack(&numbers, in - '0');
        }
    }
    while(Top_Stack(operator) != '#')
    {
      Calc(&operator, &numbers);
    }
    printf("%d\n", Pop_Stack(&numbers));



    return 0;
}