I'm supposed to take user input in the form of a prefix expression, convert it to infix and postfix, and then print the results. So far, I've got both working, but I would like to add some parenthesis to the infix expression once its converted. At the moment, I can't think of a way to sneak it in there.
Here's my code so far.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
static char s[50], i;
static char a[50];
void push(int data) {
s[i++] = data;
}
char pop() {
return s[--i];
}
// prefix -> infix
// prefix -> postfix
void prefix() {
puts("Enter a prefix expression:");
fgets(a, 50, stdin);
int a_size = strlen(a);
puts("Prefix -> Infix:");
char *token, delim[] = {" \n"}, match[] = {"*+/-"};
int j = 0, m_size = strlen(match);
token = strtok(a, delim);
while (token != NULL) {
if (isdigit(*token)) {
printf("%d ", atoi(token));
if (i >= 0) printf("%c ", pop());
}
else
while (j < m_size) {
if (*token == match[j]) {
push(*token);
break;
}
j++;
}
token = strtok(NULL, delim);
j = 0;
}
puts("");
puts("Prefix -> Postfix:");
int k = a_size;
while (k >= 0) {
if (a[k] == '\0' || a[k] == ' ') k--;
printf("%c ", a[k--]);
}
puts("");
}
void infix() {
puts("Enter an infix expression:");
fgets(a, 50, stdin);
}
void postfix() {
}
void main(void) {
int c;
puts("1: Prefix");
puts("2: Infix");
puts("3: Postfix");
scanf("%d", &c);
fflush(stdin);
prefix();
/*
switch(c) {
case 1: prefix();
break;
case 2: infix();
break;
case 3: postfix();
break;
}
*/
}