0

I am learning data structures and C and make a Postfix calculator as an exercise. the calculator works fine. but it is not able to get the equation from the user, For now, I defined a expression in the code itself.

What i want is, the user can enter expression one by one so it will give the value until he enters "Stop". How can i do that ??

This is my code

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

// Stack type
struct Stack
{
int top;
unsigned capacity;
int* array;
};

// Stack Operations
struct Stack* createStack( unsigned capacity )
{
struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));

if (!stack) return NULL;

 stack->top = -1;
stack->capacity = capacity;
stack->array = (int*) malloc(stack->capacity * sizeof(int));

if (!stack->array) return NULL;

 return stack;
 }

int isEmpty(struct Stack* stack)
{
return stack->top == -1 ;
}

int peek(struct Stack* stack)
{
return stack->array[stack->top];
}

int pop(struct Stack* stack)
{
if (!isEmpty(stack))
    return stack->array[stack->top--] ;
return '$';
}

void push(struct Stack* stack,int op)
{
stack->array[++stack->top] = op;
}



int evaluatePostfix(char* exp)
{
struct Stack* stack = createStack(strlen(exp));
int i;

if (!stack) return -1;

for (i = 0; exp[i]; ++i)
{
    if(exp[i]==' ')continue;


    else if (isdigit(exp[i]))
    {
        int num=0;

        while(isdigit(exp[i]))
        {
        num=num*10 + (int)(exp[i]-'0');
            i++;
        }
        i--;

        push(stack,num);
    }

    else
    {
        int val1 = pop(stack);
        int val2 = pop(stack);

        switch (exp[i])
        {
        case '+': push(stack, val2 + val1); break;
        case '-': push(stack, val2 - val1); break;
        case '*': push(stack, val2 * val1); break;
        case '/': push(stack, val2/val1); break;

        }
    }
}
return pop(stack);
}

int main()
{
char exp[] = "100 200 + 2 / 5 * 7 +";
printf ("%d", evaluatePostfix(exp));
return 0;
}

I want to change this a a user input expression

char exp[] = "100 200 + 2 / 5 * 7 +";

How can i do it. any leads ???

  • Do you mean your question is how to take `exp` from the user? – Rishikesh Raje Dec 04 '18 at 06:17
  • Yes, @RishikeshRaje Here i defined an expression, i want to take that exp from the user, – Sivarathan Dec 04 '18 at 06:20
  • Use `fgets` to read a string from the user. Determining whether the user entered "Stop" is a little trickier than it should be due to the fact that there is no standardized non-case-sensitive string compare. So you'll need to write a function that checks the first four bytes of the string to see if they match "Stop", "stop", "STOP" or any other version of the word "stop" – user3386109 Dec 04 '18 at 06:25
  • On the other hand, you could be nasty and force the user to type the word "Stop" exactly like that. In that case you can use `strcmp`, but [beware the newline that `fgets` puts in the buffer](https://stackoverflow.com/questions/2693776). – user3386109 Dec 04 '18 at 06:33

0 Answers0