I am trying to make a function to convert from prefix to infix implementation in C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define type char* //type of element in the stack
#define max 100
typedef struct {
int top;
type array[max];
} stack;
stack *initialize() {
stack *s = malloc(sizeof(stack));
if (!s)
return NULL;
s->top = 0;
return s;
}
void push(stack *s, type x) {
s->array[s->top++] = x;
}
type pop(stack *s) {
return s->array[--s->top];
}
type isfull(stack *s) {
return s->top >= max;
}
type isempty(stack *s) {
return !s->top;
}
type peek(stack *s) {
return s->array[s->top - 1];
}
int isoperator(char *x) {
if (x[0] == '+' || x[0] == '*' || x[0] == '/' || x[0] == '-' || x[0] == '^')
return 1;
else
return 0;
}
void error() {
printf("error");
exit(-1);
}
char *prefix_to_infix(char *pre) {
char op1[20], op2[20], temp[2];
char *fin = malloc(30);
stack *s = initialize();
int i;
for (i = strlen(pre) - 1; i >= 0; i--) {
temp[0] = pre[i];
temp[1] = '\0';
if (temp[0] == ' ')
continue;
if (temp[0] >= '0' && temp[0] <= '9') {
push(s, temp);
} else
if (isoperator(temp)) {
if (!isempty(s)) {
strcpy(op1, pop(s));
} else
error();
if (!isempty(s)) {
strcpy(op2, pop(s));
} else
error();
strcpy(fin, "(");
strcat(fin, op1);
strcat(fin, temp);
strcat(fin, op2);
strcat(fin, ")");
push(s, fin);
}
}
if (isempty(s))
error;
strcpy(fin, pop(s));
return fin;
}
int main() {
char *s = prefix_to_infix("-78");
printf("%s", s);
return 0;
}
The output should be (7-8) but the output is (---)
when I pop from the stack it gives '-' I don't know how because I only push numbers in the stack