Find below my sample code for given problem statement.
#include "stdio.h"
#define MAX_EXPRESSION_SIZE 400
typedef enum { OPERAND = 0, OPERATOR, BRACKET, STR } datatype_t;
typedef union {
char ch;
char str[MAX_EXPRESSION_SIZE];
} data_t;
typedef struct node {
datatype_t datatype;
data_t data;
struct node *prev;
struct node *next;
} node_t;
typedef struct {
node_t* pHead;
node_t* pTail;
} stack_t;
node_t* createnewelement(datatype_t datatype, data_t data) {
node_t *newnode = (node_t*)malloc(sizeof(node_t));
newnode->datatype = datatype;
newnode->data = data;
newnode->prev = NULL;
newnode->next = NULL;
return newnode;
}
void pushtostack(stack_t* stack, node_t* newelement) {
if(stack->pHead) {
stack->pTail->next = newelement;
newelement->prev = stack->pTail;
stack->pTail = newelement;
} else {
stack->pHead = newelement;
stack->pTail = newelement;
}
}
node_t* popfromstack(stack_t* stack) {
node_t *lastelement = NULL;
if(stack->pHead == NULL) {
printf("Empty list\n");
return NULL;
} else {
lastelement = stack->pTail;
stack->pTail = stack->pTail->prev;
if(stack->pTail == NULL) {
stack->pHead = NULL;
}
return lastelement;
}
}
void getRPN(char* expression, char* rpn) {
int exp_len = strlen(expression);
stack_t stack = { 0 };
node_t* tempn = NULL;
node_t* tempn1 = NULL;
node_t* tempn2 = NULL;
node_t* tempn3 = NULL;
node_t* tempn4 = NULL;
datatype_t tempdt = 0;
data_t tempd = { 0 };
int len = 0;
int i = 0;
for(i = 0; i < exp_len; i++) {
if((expression[i] == ' ') || (expression[i] == '\t')) {
//ignore whitespace
continue;
} else if((expression[i] == '+') || (expression[i] == '-') || (expression[i] == '*')
|| (expression[i] == '/') || (expression[i] == '^')) {
tempdt = OPERATOR;
tempd.ch = expression[i];
pushtostack(&stack, createnewelement(tempdt, tempd));
} else if(expression[i] == '(') {
tempdt = BRACKET;
tempd.ch = expression[i];
pushtostack(&stack, createnewelement(tempdt, tempd));
} else if(expression[i] == ')') {
tempn1 = popfromstack(&stack); //operand 2
tempn2 = popfromstack(&stack); //operator
tempn3 = popfromstack(&stack); //operand 1
tempn4 = popfromstack(&stack); //bracket ignore
tempdt = STR;
len = 0;
if(tempn3->datatype == OPERAND) {
tempd.str[0] = tempn3->data.ch;
tempd.str[1] = '\0';
len++;
} else {
strcpy(tempd.str, tempn3->data.str);
len = strlen(tempd.str);
}
if(tempn1->datatype == OPERAND) {
tempd.str[len] = tempn1->data.ch;
tempd.str[len + 1] = '\0';
len++;
} else {
strcat(tempd.str, tempn1->data.str);
len = strlen(tempd.str);
}
tempd.str[len] = tempn2->data.ch;
tempd.str[len + 1] = '\0';
pushtostack(&stack, createnewelement(tempdt, tempd));
free(tempn1);
free(tempn2);
free(tempn3);
free(tempn4);
} else {
tempdt = OPERAND;
tempd.ch = expression[i];
pushtostack(&stack, createnewelement(tempdt, tempd));
}
}
tempn = popfromstack(&stack);
strcpy(rpn, tempn->data.str);
free(tempn);
}
int main() {
char** expressions = NULL;
char rpn[MAX_EXPRESSION_SIZE] = { 0 };
int exp_count = 0;
int result = 0;
int i = 0;
//get abd validate number of expressions
scanf("%d", &exp_count);
getchar(); //to eliminate new line character from input stream
if(exp_count <= 0) {
printf("Invalid number of expressions\n");
return 0;
}
//get expressions
expressions = (char**)malloc(sizeof(char*)*exp_count);
for(i = 0; i < exp_count; i++) {
expressions[i] = (char*)malloc(sizeof(char)*MAX_EXPRESSION_SIZE);
gets(expressions[i]);
while(expressions[i][0] == '\0') {
gets(expressions[i]);
}
}
//get and print RPN for each expression
for(i = 0; i < exp_count; i++) {
getRPN(expressions[i], rpn);
printf("%s\n", rpn);
free(expressions[i]);
}
free(expressions);
return 0;
}
Compile the file with above code and run as:
a.exe < input.txt > output.txt [on windows]
a.out < input.txt > output.txt [on linux]
Here,
a.exe or a.out are the executables generated after compilation
input.txt is the file containing input, e.g., for given example in problem input.txt will be:
3
(a+(b*c))
((a+b)*(z+x))
((a+t)*((b+(a+c))^(c+d)))
output.txt is the output file containing result, for above input output.txt will contain:
abc*+
ab+zx+*
at+bac++cd+^*
-Pradeep