while (!isEmpty(s))
{
*c = pop(s);
strcat(postfix, c);
strcat(postfix, " ");
}
This is where the crash happens specifically at the *c line. When i display the stack it has a value of 43.00000 which means it contains the '+' in ASCI code. I also used these 3 lines above this specific one in two loops which gave no error.
float pop(stack *s)
{
float x = s->top->data;
node *temp = s->top;
s->top = s->top->next;
free(temp);
return x;
}
Here is the pop function. Also this works in vscode and the crashes happen in codeblocks only
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
typedef struct node
{
float data;
struct node *next;
} node;
typedef struct
{
node *top;
} stack;
node *newNode(float x)
{
node *n = malloc(sizeof(node));
n->data = x;
n->next = NULL;
return n;
}
stack *init()
{
stack *s = malloc(sizeof(stack));
s->top = NULL;
return s;
}
float pop(stack *s)
{
float x = s->top->data;
node *temp = s->top;
s->top = s->top->next;
free(temp);
return x;
}
void push(stack *s, float x)
{
node *n = newNode(x);
if (s->top == NULL)
s->top = n;
else
{
n->next = s->top;
s->top = n;
}
}
float peek(stack *s)
{
float x = s->top->data;
return x;
}
int isEmpty(stack *s)
{
return (s->top == NULL);
}
void display(stack *s)
{
stack *temp = init();
while (!isEmpty(s))
push(temp, pop(s));
while (!isEmpty(temp))
{
printf("%f ", peek(temp));
push(s, pop(temp));
}
printf("\n");
}
int priority(char op)
{
if (op == '+' || op == '-')
return 1;
if (op == '*' || op == '/')
return 2;
if (op == '^')
return 3;
else
return 0;
}
int isOperator(char op[5])
{
if (!strcmp(op, "+") || !strcmp(op, "-") || !strcmp(op, "*") || !strcmp(op, "/") || !strcmp(op, "^") || !strcmp(op, "(") || !strcmp(op, ")"))
return 1;
else
return 0;
}
int isDigit(char digit[20])
{
int i = 0;
if (digit[0] == '-')
i++;
for (i; digit[i] != '\0'; i++)
if (!isdigit(digit[i]) && digit[i] != '.')
return 0;
return 1;
}
char *infixToPostfix(char *infix)
{
char postfix[100] = "", tokens[20][2000], *temptok, *c;
int i = 0;
temptok = strtok(infix, " ");
strcpy(tokens[i++], temptok);
while (temptok != NULL)
{
temptok = strtok(NULL, " ");
if (temptok != NULL)
strcpy(tokens[i++], temptok);
}
int ntok = i;
stack *s = init();
for (i = 0; i < ntok; i++)
{
if (isDigit(tokens[i]) && strcmp(tokens[i], "-"))
{
strcat(postfix, tokens[i]);
strcat(postfix, " ");
}
else if (isOperator(tokens[i]))
{
if (isEmpty(s))
push(s, tokens[i][0]);
else if (tokens[i][0] == '(')
push(s, tokens[i][0]);
else if (tokens[i][0] == ')')
{
while (peek(s) != '(')
{
*c = pop(s);
strcat(postfix, c);
strcat(postfix, " ");
}
pop(s);
}
else
{
while (!isEmpty(s) && priority(peek(s)) >= priority(tokens[i][0]) && tokens[i][0] != '(')
{
*c = pop(s);
strcat(postfix, c);
strcat(postfix, " ");
}
push(s, tokens[i][0]);
}
}
}
display(s);
system("pause");
while (!isEmpty(s))
{
*c = pop(s);
strcat(postfix, c);
strcat(postfix, " ");
}
strcpy(infix, postfix);
return infix;
}
int main()
{
char infixExpr[256] = "";
printf("Enter an expression you want to evaluate or Ctrl+Z to exit: ");
while (fgets(infixExpr, 255, stdin) != NULL)
{
replaceNewLineBySpace(infixExpr);
char *postfix = infixToPostfix(infixExpr);
printf("Postfix : %s\n", postfix);
float result = evaluatePostfix(postfix);
printf("Result: %f\n", result);
system("pause");
// exit(1);
}
return 0;
}