C stack implementation throws bad access error - c

Am building a stack in C. Stack occasionally fails with a EXC_BAD_ACCESS error on the if check in my isEmpty() function, and I'm not sure why.
Typedefs & definitions:
#define NodeSize sizeof(StackNode)
struct stackNode {
int data;
struct stackNode *nextPtr;
};
typedef struct stackNode StackNode;
typedef StackNode *StackNodePtr;
Stack operation functions
void push(StackNodePtr *topPtr, int value) {
// Declare a new node and set it to the top
StackNode *node;
node = malloc(sizeof(NodeSize));
node->data = value;
node->nextPtr = *topPtr;
// Reassign the pointer to the top
*topPtr = node;
printStack(*topPtr);
}
int pop(StackNodePtr *topPtr) {
StackNode *node = *topPtr;
if (isEmpty(node)) {
return 0;
}
// Grab the current top node and reset the one beneath it to the top pointer.
*topPtr = node->nextPtr;
printStack(*topPtr);
return node->data;
}
int isEmpty(StackNodePtr topPtr) {
if (topPtr == NULL || topPtr->nextPtr == NULL) { // BAD_ACCESS
return 1;
}
return 0;
}
void printStack(StackNodePtr topPtr) {
StackNode *iter = topPtr;
// If the stack is immediately empty, just print that.
if (!isEmpty(iter->nextPtr)) {
// Iterate over each element in the stack and print its data
for (;isEmpty(iter); iter = iter->nextPtr) {
printf("%d ", iter->data);
}
printf("NULL");
} else {
printf("NULL");
}
printf("\n");
}
void evaluatePostfixExpression(char *expr) {
// Create the stack and insert an initial parenthesis
StackNode *head;
head = malloc(NodeSize);
for (int i = 0; expr[i] != '\0'; i++) {
char token = expr[i];
// If the current character is a digit
if (token >= '0' && token <= '9') {
push(&head, token - '0');
// If it's an operator
} else if (token == '+' || token == '-' || token == '*' || token == '/' || token == '^' || token == '%') {
int x = pop(&head);
int y = pop(&head);
int result = calculate(y, x, token);
push(&head, result);
}
}
int output = pop(&head);
printf("The value of the expression is: %d\n", output);
}
int calculate(int op1, int op2, char operator) {
switch (operator) {
case '+':
return op1 + op2;
case '-':
return op1 - op2;
case '*':
return op1 * op2;
case '/':
return op1 / op2;
case '%':
return op1 % op2;
}
if (operator == '^') {
int result = 1;
for (int i = 0; i < op2; i++) {
result *= op1;
}
return result;
}
return 0;
}
Even just a hint in the right direction would be appreciated.
Request for context:
int main() {
char postfix[255] = "54*81+4/+";
evaluatePostfixExpression(postfix);
return 0;
}
void evaluatePostfixExpression(char *expr) {
// Create the stack and insert an initial parenthesis
StackNode *head;
head = malloc(NodeSize);
for (int i = 0; expr[i] != '\0'; i++) {
char token = expr[i];
// If the current character is a digit
if (token >= '0' && token <= '9') {
push(&head, token - '0');
// If it's an operator
} else if (token == '+' || token == '-' || token == '*' || token == '/' || token == '^' || token == '%') {
int x = pop(&head);
int y = pop(&head);
int result = calculate(y, x, token);
push(&head, result);
}
}
int output = pop(&head);
printf("The value of the expression is: %d\n", output);
}
int calculate(int op1, int op2, char operator) {
switch (operator) {
case '+':
return op1 + op2;
case '-':
return op1 - op2;
case '*':
return op1 * op2;
case '/':
return op1 / op2;
case '%':
return op1 % op2;
}
if (operator == '^') {
int result = 1;
for (int i = 0; i < op2; i++) {
result *= op1;
}
return result;
}
return 0;
}

It looks like the expression
if (topPtr == NULL || topPtr->nextPtr == NULL)
is giving you an error because topPtr doesn't point to a valid node, nor it's null. So topPtr->nextPtr will give an error, due to the fact that topPtr is not a valid pointer.
According to your code, your head->nextPtr is not initialized to NULL, so this seems to be the cause of the problem.
By the way: Why do you initialize your head variable with an empty node? As far as I can see, you could just use head = NULL and it would work.
Last, but not least: you should free your node variable in the pop() function, in order to avoid a memory leak.
Hope it helps.

Related

Postfix Expression - Checking for Spaces

So I am trying to add an if statement in my main function to check if there is some whitespace. If so it would just go on to the next number/operator in line. So for example, if I would type 2 3 4 * +, I should get 14. However, when I run the code I would get a random number. When I do it with no spaces such as 234*+, then I would get 14. Could anyone tell me what I'm doing wrong and how I would fix this?
#include <ctype.h>
#include <stdio.h>
#define MAX 20
typedef struct stack {
int data[MAX];
int top;
} stack;
int evaluate(char x, int op1, int op2) {
if (x == '+')
return (op1 + op2);
if (x == '-')
return (op1 - op2);
if (x == '*')
return (op1 * op2);
if (x == '/')
return (op1 / op2);
if (x == '%')
return (op1 % op2);
}
void init(stack *s) {
s->top = -1;
}
void push(stack *s, int x) {
s->top = s->top + 1;
s->data[s->top] = x;
}
int pop(stack *s) {
int x;
x = s->data[s->top];
s->top = s->top - 1;
return (x);
}
int main() {
stack s;
char x;
int op1, op2, val;
init(&s);
printf("Enter a Postfix Expression: ");
while ((x = getchar()) != '\n') {
if (isdigit(x))
push(&s, x - 48); //x-48 for removing the effect of ASCII
if (isspace(x))
continue;
else {
op2 = pop(&s);
op1 = pop(&s);
val = evaluate(x, op1, op2);
push(&s, val);
}
}
val = pop(&s);
printf("\nValue of expression=%d",val);
return 0;
}
First of all, char x needs to be replaced with int x. The value returned by getchar won't always fit in a char. But that's not the problem you asked about.
If the character is a digit, you treat it as a digit and a operator. This
if(isdigit(x))
push(&s,x-48); //x-48 for removing the effect of ASCII
if (isspace(x))
continue;
else
{
op2=pop(&s);
op1=pop(&s);
val=evaluate(x,op1,op2);
push(&s,val);
}
should be
if (isdigit(x)) {
push(&s, x-48);
}
else if (isspace(x)) {
continue;
}
else {
int op2 = pop(&s);
int op1 = pop(&s);
int val = evaluate(x, op1, op2);
push(&s, val);
}
or just
if (isdigit(x)) {
push(&s, x-48);
}
else if (!isspace(x)) {
int op2 = pop(&s);
int op1 = pop(&s);
int val = evaluate(x, op1, op2);
push(&s, val);
}
You shouldn't count on a trailing line feed. So you should really have the following:
while ( ( x = getchar() ) != EOF ) {
if (isspace(x))
continue;
if (isdigit(x)) {
push(&s, x-48);
} else {
int op2 = pop(&s);
int op1 = pop(&s);
int val = evaluate(x, op1, op2);
push(&s, val);
}
}
Finally, you should handle someone entering 23++, 234+ or 23!.

How to evaluate binary tree (inorder)?

I am trying to evaluate a binary tree (inorder). The result always remains
the same or giving an unexpected answer. I do not know where the problem is, can
anyone help me? First I have converted postfix expression to expression tree than evaluate expression tree. I shall be very thankful.
The result is unexpected when I run this program. This the header file.
#define POST2EXPTREE_H_INCLUDED
#define MAX 100
struct node
{
char ch;
struct node *left;
struct node *right;
} *stack[MAX];
typedef struct node node;
void push(node *str);
node *pop();
void convert(char exp[]);
void display(node *temp);
#endif
#include <stdio.h>
#include <stdlib.h>
#include"post2expTree.h"
#define SIZE 100
int top = -1;
void push(node *str)
{
if (top >= MAX-1)
printf("Stack is Full ");
else
{
stack[top] = str;
top++;
}
}
node *pop()
{
node *exp;
if (top < 0)
printf("Stack is Empty ");
else
exp = stack[--top];
return exp;
}
void convert(char exp[])
{
node *op1, *op2;
node *temp;
int i;
for (i=0;exp[i]!='\0';i++)
if (exp[i] >= 'a'&& exp[i] <= 'z'|| exp[i] >= 'A' && exp[i] <= 'Z' ||isalnum(exp[i]))
{
temp = (node*)malloc(sizeof(node));
temp->ch = exp[i];
temp->right = NULL;
temp->left = NULL;
push(temp);
}
else if (exp[i] == '+' || exp[i] == '-' || exp[i] == '*' || exp[i] == '/' || exp[i] == '%'
|| exp[i] == '^')
{
op1 = pop();
op2 = pop();
temp = (node*)malloc(sizeof(node));
temp->ch = exp[i];
temp->right = op1;
temp->left = op2;
push(temp);
}
}
void display(node *temp)
{
if (temp != NULL)
{
display(temp->left);
printf("%c", temp->ch);
display(temp->right);
}
}
int evaluate(node *temp)
{
int left,right,value;
if ((temp->ch) >= 0 || (temp->ch <=9))
{
return temp->ch;
}
else
{
left = evaluate(temp -> left);
right = evaluate(temp -> right);
switch(temp->ch)
{
case '+':
value = left + right;
break;
case '-':
value = left - right;
break;
case '*':
value = left * right;
break;
case '/':
value = left / right;
break;
case '%':
value = left % right;
break;
case '^':
value = left ^ right;
break;
}
temp->ch = value;
}
return value;
}
int top = -1;
void push(node *str)
{
if (top >= MAX-1)
printf("Stack is Full ");
else
{
stack[top] = str;
top++;
}
}
Here top is initialized to -1, and the very first push() wrongly accesses stack[-1]; correct that by changing the else block to stack[++top] = str;.
node *pop()
{
node *exp;
if (top < 0)
printf("Stack is Empty ");
else
exp = stack[--top];
return exp;
}
Here top is decremented before accessing the stack off by one; correct that by changing the else block to exp = stack[top--];.
int evaluate(node *temp)
{
int left,right,value;
if ((temp->ch) >= 0 || (temp->ch <=9))
{
return temp->ch;
Here you forgot that you store the operands as single digits '0' to '9', not as values 0 to 9. Correct that by changing to:
int left, right, value = temp->ch-'0';
if (value >= 0 && value <= 9)
{
return value;

semantical error with parenthesis balancing here

I've written a C code to check for simple parenthesis balancing which prints NO and YES if balanced, not balanced respectively.
The problem is that I'm getting NO for all inputs. Hence I'm thinking that its a semantic error but cannot find it (I have been trying for 2 days :p)
Could someone please help me here? thanks!
#include <stdio.h>
#include <stdlib.h>
typedef struct stack {
char s[1000];
int top;
} STACK;
void create(STACK *sptr) {
sptr->top = -1;
}
int isEmpty(STACK *sptr) {
if (sptr->top == -1)
return 1;
else
return 0;
}
void push(STACK *sptr, char data) {
sptr->s[++sptr->top] = data;
}
char pop(STACK *sptr) {
if (isEmpty(sptr) == 0)
return sptr->s[sptr -> top];
else
return '$';
}
char *isBalanced(char *s, STACK *sptr) {
char *y, *n;
int i = 0;
y = (char*)malloc(sizeof(char) * 4);
y[0] = 'Y';
y[1] = 'E';
y[2] = 'S';
y[3] = '\0';
n = (char*)malloc(sizeof(char) * 3);
n[0] = 'N';
n[1] = 'O';
n[2] = '\0';
while (s[i] != '\0') {
if (s[i] == '(' || s[i] == '{' || s[i] == '[') {
push(sptr, s[i]);
} else {
char x = pop(sptr);
switch (s[i]) {
case ')':
if (x != '(')
return n;
break;
case '}':
if (x != '{')
return n;
break;
case ']':
if (x != '[')
return n;
break;
}
}
++i;
}
if (isEmpty(sptr))
return y;
else
return n;
}
int main() {
STACK *sptr = (STACK *)malloc(sizeof(STACK));
char c[21];
int ch;
do {
printf("enter sequence:");
scanf("%s", c);
char *msg = isBalanced(c, sptr);
printf("%s", msg);
printf("choice?:");
scanf("%d", &ch);
} while(ch);
}
updated code:
#include <stdio.h>
#include <stdlib.h>
typedef struct stack {
char s[1000];
int top;
} STACK;
void create(STACK *sptr) {
sptr->top = -1;
}
int isEmpty(STACK *sptr) {
if (sptr->top == -1)
return 1;
else
return 0;
}
void push(STACK *sptr, char data) {
sptr->s[++sptr->top] = data;
}
char pop(STACK *sptr) {
if (isEmpty(sptr) == 0)
return sptr->s[sptr->top--];
else
return '$';
}
int isBalanced(char *s, STACK *sptr) {
int i = 0;
while (s[i] != '\0') {
if (s[i] == '(' || s[i] == '{' || s[i] == '[') {
push(sptr, s[i]);
} else {
char x = pop(sptr);
switch (s[i]) {
case ')':
if (x != '(')
return 0;
break;
case '}':
if (x != '{')
return 0;
break;
case ']':
if (x != '[')
return 0;
break;
}
}
++i;
}
if (isEmpty(sptr))
return 1;
else
return 0;
}
int main() {
STACK *sptr = malloc(sizeof(STACK));
char c[21];
int ch;
do {
printf("enter sequence:");
scanf("%s", c);
printf("%s", (isBalanced(c, sptr) ? "YES" : "NO"));
printf("choice?:");
scanf("%d", &ch);
} while(ch);
}
Here are some major issues in your code:
you do not properly initialize the stack with create(sptr) before use, preferably in main() where it is defined. Your code has undefined behavior. It prints NO by chance, probably because sptr->top has an initial value of 0, which makes the stack non-empty.
you should only pop from the stack when you encounter a closing delimiter ), ] or }.
you should prevent potential buffer overflow by telling scanf() the maximum number of characters to read into c: scanf("%20s", c). Furthermore you should test the return value to avoid undefined behavior at end of file.
Note also that the STACK can be made a local variable to avoid heap allocation and potential allocation failure, which would cause undefined behavior since it is not tested.
Here is a corrected version:
#include <stdio.h>
typedef struct stack {
char s[1000];
int top;
} STACK;
void create(STACK *sptr) {
sptr->top = -1;
}
int isEmpty(STACK *sptr) {
if (sptr->top == -1)
return 1;
else
return 0;
}
void push(STACK *sptr, char data) {
sptr->s[++sptr->top] = data;
}
char pop(STACK *sptr) {
if (isEmpty(sptr) == 0)
return sptr->s[sptr->top--];
else
return '$';
}
int isBalanced(char *s, STACK *sptr) {
int i;
for (i = 0; s[i] != '\0'; i++) {
switch (s[i]) {
case '(':
case '{':
case '[':
push(sptr, s[i]);
break;
case ')':
if (pop(sptr) != '(')
return 0;
break;
case '}':
if (pop(sptr) != '{')
return 0;
break;
case ']':
if (pop(sptr) != '[')
return 0;
break;
}
}
return isEmpty(sptr);
}
int main() {
STACK s, *sptr = &s;
char c[100];
int ch;
do {
printf("Enter sequence: ");
if (scanf(" %99[^\n]", c) != 1)
break;
create(sptr);
printf("%s\n", isBalanced(c, sptr) ? "YES" : "NO");
printf("Choice? ");
if (scanf("%d", &ch) != 1)
break;
} while (ch);
return 0;
}
In your pop function you are not decrementing the top thus your isEmpty function always returns false.
Hence you return no always.
char* isBalanced(char* s, STACK* sptr)
{
....
if(isEmpty(sptr)) return y;
else return n;
}
The following is the correct implementation:
char pop(STACK* sptr)
{
if(isEmpty(sptr) == 0)
return sptr->s[sptr->top--];
else
return '$';
}
I assume that the idea is this: an opening paren is always permitted (in the analyzed text), but a closing paren must match the last opened. This is why a stack is required. Also, in the end, if the stack is not empty that means that some parens were not closed.
So, you should use the stack, but only when you encounter parens - either opening or closing.
In the function isBalanced(), the main cycle could be this:
while (s[i] != '\0') {
if ( s[i] == '(' || s[i] == '{' || s[i] == '[' ) {
// opening - remember it
push(sptr, s[i]);
} else if ( s[i] == ')' || s[i] == '}' || s[i] == ']' ) {
// closing - it should match
if ( ! popmatch(sptr, s[i]) )
return n;
}
++i;
}
The rest of the function is ok. Now, I modified the the pop() function: renamed to popmatch, because it should check if the argument matches the top of the stack. If it does, pop the stack and return OK. So the function would be:
char popmatch(STACK* sptr, char c) {
// an empty stack can not match
if (isEmpty(sptr))
return 0;
// not empty, can check for equality
if ( c =! sptr->s[sptr->top] )
return 0;
// does not match!
// ok, it matches so pop it and return ok
sptr->top--;
return 1;
}
Please note that the code mirrors pretty well the "analysis"; when the analysis is short and clear, and the code follows the analysis, the result often is short and clear too.
I would add the flags to see which one does not match
unsigned match(const char *f)
{
int p = 0,s = 0,b = 0;
while(*f)
{
switch(*f++)
{
case '(':
p++;
break;
case ')':
p -= !!p;
break;
case '[':
s++;
break;
case ']':
s -= !!s;
break;
case '{':
b++;
break;
case '}':
b -= !!b;
break;
default:
break;
}
}
return (!p) | ((!s) << 1) | ((!b) << 2);
}
Not the stack version but just for the idea. It is rather easy to add push and pop

Going from infix to postfix

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "stack.h"
#define MAX_EQU_LEN 100
static int prec(char operator)
{
switch (operator)
{
case '*':
return 5;
case '/':
return 4;
case '%':
return 3;
case '+':
return 2;
case '-':
return 1;
default:
break;
}
return 0;
}
static int isNumeric(char* num)
{
if(atoi(num) == 0)
{
return 0;
}
return 1;
}
char* infix_to_postfix(char* infix)
{
char* postfix = malloc(MAX_EQU_LEN);
stack* s = create_stack();
s->size = strlen(infix);
node* tempPtr = s->stack;
unsigned int i;
char symbol,next;
for(i = 0; i < s->size ; i++)
{
symbol = *((infix + i));
tempPtr = s->stack;
if(isNumeric(&symbol) != 1)
{
strcat(postfix, &symbol);
}
else if(symbol == '(')
{
push(s, symbol);
}
else if(symbol == ')')
{
while(s->size != 0 && top(s) != '(')
{
next = tempPtr->data;
pop(s);
strcat(postfix, &next);
tempPtr = s->stack;
if(tempPtr->data == '(')
{
pop(s);
}
}
}
else
{
while(s->size != 0 && prec(top(s)) > prec(symbol))
{
next = tempPtr->data;
pop(s);
strcat(postfix, &next);
push(s,next);
}
}
while(s->size != 0)
{
next = tempPtr->data;
pop(s);
strcat(postfix, &next);
}
}
return postfix;
}
int evaluate_postfix(char* postfix) {
//For each token in the string
int i,result;
int right, left;
char ch;
stack* s = create_stack();
node* tempPtr = s->stack;
for(i=0;postfix[i] < strlen(postfix); i++){
//if the token is numeric
ch = postfix[i];
if(isNumeric(&ch)){
//convert it to an integer and push it onto the stack
atoi(&ch);
push(s, ch);
}
else
{
pop(&s[i]);
pop(&s[i+1]);
//apply the operation:
//result = left op right
switch(ch)
{
case '+': push(&s[i],right + left);
break;
case '-': push(&s[i],right - left);
break;
case '*': push(&s[i],right * left);
break;
case '/': push(&s[i],right / left);
break;
}
}
}
tempPtr = s->stack;
//return the result from the stack
return(tempPtr->data);
}
This file is part of a program that uses a stack struct to perform an infix to postfix on an input file. The other functions have been tested and work fine but when I try to add this part and actually perform the operations the program segmentation faults. A debugger says it occurs in the infix_to_postfix function however it doesn't say which line and I can't figure out where. Does anyone know why this would seg fault?
You've done a few things wrong:
if(isNumeric(&symbol) != 1)
The function isNumeric() expects a null terminated string as input, not a pointer to a single character.
strcat(postfix, &symbol);
Here the same thing applies.
strcat(postfix, &next);
I'm guessing this is wrong too. If you want to turn a single character into a string, you can do this:
char temp[2] = {0};
temp[0] = symbol;
strcat(postfix, temp);
static int isNumeric(char* num)
{
if(atoi(num) == 0)
{
return 0;
}
return 1;
}
What if the string is "0"? Consider using strtol instead because it offers a more powerful means of testing the success of the outcome.
An unrelated stylistic note: your first function looks overcomplicated to me. Although it's perfectly possible that the way I'd do it is also overcomplicated.
static int prec(char operator)
{
switch (operator)
{
case '*':
return 5;
case '/':
return 4;
case '%':
return 3;
case '+':
return 2;
case '-':
return 1;
default:
break;
}
return 0;
}
If a function performs a simple mapping from one set to another, It could usually be performed more simply (and faster) as an array lookup. So I'd start with a string of the input characters.
char *operators = "*" "/" "%" "+" "-";
Note that the compiler will concatenate these to a single string value with a null-terminator.
int precedence[] = { 5, 4, 3, 2, 1, 0 };
Then testing whether a char is an operator is:
#include <string.h>
if (strchr(operators, chr))
...;
And getting the precedence becomes:
p = precedence[strchr(operators, chr) - operators];
If there are more values to associate with the operator, I'd consider using an X-Macro to generate a table and a set of associated enum values to use as symbolic indices.

Infix to Postfix Algorithm in C

I'm solve an exercise in which one of the functions has to translate infix notation to postfix notation. Here follows my whole code
#include<stdio.h>
#define MAX 100
char stack[MAX];
int top;
void compact(char Descomp[], char Compac[]);
void init_stack();
int push(char Elem);
int desempilha(char *Elem);
int priority(char Operator);
int arity(char Exp[], int position);
int translate_pos(char exp[], char exp_pos[]);
int main()
{
char Exp[MAX]; /* stores the expression read from stdin */
char Exp_compact[MAX]; /* stores expression without spaces */
char Exp_pos[MAX]; /* stores the expression after the translation for postfix*/
int indicator; /* indicate if an error occurred, 0 for NO ERROR and -1 for ERROR*/
indicator = 0;
printf("\nType the expression: ");
gets(Exp);
compact(Exp, Exp_compact);
indicator = translate_pos(Exp_compact, Exp_pos);
puts(Exp_pos);
return indicator;
}
/* compact function delete spaces within the expression read from stdin */
void compact(char Descomp[], char Compac[])
{
int i;
int j;
i = 0;
j = 0;
while(Descomp[j] != '\0')
{
if(Descomp[j] != ' ')
{
Compac[i] = Descomp[j];
i++;
}
j++;
}
}
/* initiate the stack by setting top = -1 */
void init_stack()
{
top = -1;
}
/* puts the element Elem in the stack */
int push(char Elem)
{
if(top == MAX - 1) /* Stack is full */
return -1;
top++;
stack[top] = Elem;
return 0;
}
/* remove the element in stack[top] and puts it in &Elem*/
int pop(char *Elem)
{
if(top == -1) /* stack is empty */
return -1;
*Elem = stack[top];
top--;
return 0;
}
/* Return the priority of an operator */
int priority(char Operator)
{
switch(Operator)
{
case '+': return 1;
case '-': return 1;
case '*': return 2;
case '/': return 2;
case '^': return 3;
case '(': return 4;
case ')': return 5;
default : return 0;
}
}
/* returns the arity of CONSTANTS + - * / and ^, for ( an ) is merely symbolic */
int arity(char Exp[], int position)
{
if(priority(Exp[position]) == 1)
{
if( (position == 0) || ( (priority(Exp[position - 1]) >= 1) && (priority(Exp[position - 1]) <= 3) ))
return 1;
else
return 2;
}
else if( (priority(Exp[position]) > 1) && (priority(Exp[position]) <= 4))
return 2;
else
return priority(Exp[position]);
}
/* reads an infix expression and returns postfix expression */
int translate_pos(char exp[], char exp_pos[])
{
int i;
int j;
int ind;
char trash;
i = 0;
j = 0;
ind = 0;
trash = ' ';
init_stack();
while(exp[i]!= '\0')
{
if(arity(exp, i) == 0)
{
exp_pos[j] = exp[i];
j++;
}
if(arity(exp, i) == 1)
{
switch(exp[i])
{
case '-':
{
exp_pos[j] = exp_pos[i];
j++;
}
case '+': trash = exp_pos[i];
}
}
if(arity(exp, i) == 2)
{
while((top != -1) && (priority(stack[top]) <= priority(exp[i])))
{
ind = pop(&exp_pos[j]);
j++;
}
ind = push(exp[i]);
}
if(priority(exp[i]) == 4)
{
ind = push(exp[i]);
}
if(priority(exp[i]) == 5)
{
while( (top != -1) && (stack[top] != '('))
{
ind = pop(&exp_pos[j]);
j++;
}
if(stack[top] == '(')
ind = pop(&trash);
}
i++;
}
while(top != -1)
{
ind = pop(&exp_pos[j]);
j++;
}
return ind;
}
The algorithm I used to translate the expression is
while there is token to be read;
read the token;
if token is a constant
push it to Exp_Postfix;
if token is '('
push it to stack
if token is ')'
pop from the stack all symbols until '(' be find and remove '(' from the stack
if token is an operator and its arity is 2
pop all operators with less or equal priority than the token and store then in the Exp_Postfix;
push token to the stack;
if token is an operator and its arity is 1
if token is '-'
push it to Exp_postfix;
if token is '+'
pass to the next token;
pop all remaining symbols in the stack and push then, in order, to the Exp_Postfix;
I compiled the .c archive using
gcc -Wall archive.c -o archive
and executed it. I give the expression
5+(6*9^14)
It the returned expression was
5
I do not now if the error is in my code or in solution to the problem.
There are an awful lot of problems, here, for instance:
compact() and translate_pos() leave Exp_compact and Exp_pos, respectively, without a terminating \0, so you're getting garbage printed out.
your arity() function is returning 2 for an opening parenthesis.
in the first switch statement of translate_pos(), you're missing break statements.
Your priority comparison in translate_pos() when arity is 2 is back to front.
When you're comparing operator precedence, you should treat an opening parenthesis specially, since it should have the lowest precedence when on the top of the stack.
You should have a lot more else keywords in translate_pos().
You're using gets(), which was always bad, and now has actually been removed from C.
I haven't exhaustively tested it, but here's a correct version that seems to work for all the test inputs I tried:
#include <stdio.h>
#include <ctype.h>
#include <assert.h>
/* Function prototypes */
void compact(char Descomp[], char Compac[]);
void init_stack();
int push(char Elem);
int desempilha(char *Elem);
int priority(char Operator);
int arity(char Exp[], int position);
int translate_pos(char exp[], char exp_pos[]);
/* Stack variables */
#define MAX 100
char stack[MAX];
int top;
int main(void) {
char Exp[MAX];
char Exp_compact[MAX] = {0};
char Exp_pos[MAX] = {0};
int indicator = 0;
printf("\nType the expression: ");
fgets(Exp, MAX, stdin);
compact(Exp, Exp_compact);
indicator = translate_pos(Exp_compact, Exp_pos);
puts(Exp_pos);
return indicator;
}
/* compact function delete spaces within the expression read from stdin */
void compact(char Descomp[], char Compac[]) {
int i = 0;
int j = 0;
while ( Descomp[j] ) {
if ( !isspace(Descomp[j]) ) {
Compac[i++] = Descomp[j];
}
j++;
}
}
/* initiate the stack by setting top = -1 */
void init_stack() {
top = -1;
}
/* puts the element Elem in the stack */
int push(char Elem) {
if (top == MAX - 1) /* Stack is full */
return -1;
stack[++top] = Elem;
return 0;
}
/* remove the element in stack[top] and puts it in &Elem*/
int pop(char *Elem) {
if (top == -1) /* stack is empty */
return -1;
*Elem = stack[top--];
return 0;
}
/* Return the priority of an operator */
int priority(char Operator) {
switch (Operator) {
case '+':
return 1;
case '-':
return 1;
case '*':
return 2;
case '/':
return 2;
case '^':
return 3;
case '(':
return 4;
case ')':
return 5;
default:
return 0;
}
}
/* returns the arity of OPERATORS + - * / and ^,
* for ( an ) is merely symbolic */
int arity(char Exp[], int position) {
if ( priority(Exp[position]) == 1 ) {
if ( (position == 0) ||
((priority(Exp[position - 1]) >= 1) &&
(priority(Exp[position - 1]) <= 3)) ) {
return 1;
} else {
return 2;
}
} else if ( (priority(Exp[position]) > 1) &&
(priority(Exp[position]) <= 3) ) {
return 2;
} else {
return priority(Exp[position]);
}
}
/* reads an infix expression and returns postfix expression */
int translate_pos(char exp[], char exp_pos[]) {
int i = 0, j = 0, ind = 0;
char trash = ' ';
init_stack();
while ( exp[i] ) {
if ( arity(exp, i) == 0 ) {
exp_pos[j++] = exp[i];
} else if ( arity(exp, i) == 1 ) {
switch (exp[i]) {
case '-':
exp_pos[j++] = exp[i];
break;
case '+':
trash = exp_pos[i];
break;
default:
assert(0);
}
} else if (arity(exp, i) == 2) {
while ( (top != -1) &&
(priority(stack[top]) >= priority(exp[i])) &&
stack[top] != '(' ) {
ind = pop(&exp_pos[j++]);
}
ind = push(exp[i]);
} else if ( priority(exp[i]) == 4 ) {
ind = push(exp[i]);
} else if ( priority(exp[i]) == 5 ) {
while ( (top != -1) && (stack[top] != '(') ) {
ind = pop(&exp_pos[j++]);
}
if ( (top != - 1) && stack[top] == '(') {
ind = pop(&trash);
}
}
i++;
}
while (top != -1) {
ind = pop(&exp_pos[j++]);
}
return ind;
}
Gives the following output for various test cases:
paul#local:~/src/c/postfix$ ./postfix
Type the expression: 1+2
12+
paul#local:~/src/c/postfix$ ./postfix
Type the expression: (1+2)
12+
paul#local:~/src/c/postfix$ ./postfix
Type the expression: 1+2*3
123*+
paul#local:~/src/c/postfix$ ./postfix
Type the expression: (1+2)*3
12+3*
paul#local:~/src/c/postfix$ ./postfix
Type the expression: (3+4)*4/2
34+4*2/
paul#local:~/src/c/postfix$ ./postfix
Type the expression: 5+(6*9^14)
56914^*+
paul#local:~/src/c/postfix$
I'd suggest comparing my code to yours and trying to understand the individual differences to see where you were going wrong.

Resources