Skip to content

Commit

Permalink
fixed infix to postfix probably, bugs in eval i think
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Nelson committed Feb 19, 2011
1 parent a4b1369 commit 463de2b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
all:
gcc *.c -o infix
gcc *.c -ggdb -o infix
debug:
gcc *.c -ggdb -o infix
clean:
Expand Down
50 changes: 38 additions & 12 deletions postfix.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

char *infixToPostfix(char *infixStr){
char inCopy[strlen(infixStr)];
strcpy(inCopy,infixStr);

char *postfixString = malloc(sizeof(char)*strlen(infixStr)*2);
char *postfixString = malloc(sizeof(char)*strlen(infixStr));
postfixString[0] = '\0';

char *token = strtok(inCopy," ");
Expand All @@ -23,22 +25,29 @@ char *infixToPostfix(char *infixStr){
} else if(isLeftParen(token)){
stackPush(&convoStack, token);
} else if(isOperator(token)){
char *peek = stackPeek(&convoStack);
while(isOperator(peek)){
int sp = stackPrecedence(peek);
int ip = inputPrecedence(token);
if(sp >= ip){
strcat(postfixString,stackPop(&convoStack));
stackPush(&convoStack,token);
if(!stackIsEmpty(&convoStack)){
char *peek = stackPeek(&convoStack);
while(isOperator(peek)){
int sp = stackPrecedence(peek);
int ip = inputPrecedence(token);
if(sp >= ip){
strcat(postfixString,stackPop(&convoStack));
strcat(postfixString," ");
} else
break;
if(!stackIsEmpty(&convoStack)){
peek = stackPeek(&convoStack);
} else {
peek = "\0";
}
}
peek = stackPeek(&convoStack);
}
stackPush(&convoStack,token);
} else if(isRightParen(token)){
char *peek = stackPeek(&convoStack);
while(isOperator(peek)){
strcat(postfixString,stackPop(&convoStack));
stackPush(&convoStack,token);
strcat(postfixString,stackPop(&convoStack));
strcat(postfixString," ");
peek = stackPeek(&convoStack);
}
if(isLeftParen(stackPeek(&convoStack))){
Expand All @@ -47,6 +56,13 @@ char *infixToPostfix(char *infixStr){
}
token = strtok(NULL," ");
}
while(!stackIsEmpty(&convoStack)){
char *tok = stackPop(&convoStack);
if(isOperator(tok)){
strcat(postfixString,tok);
strcat(postfixString," ");
}
}
return postfixString;
}

Expand Down Expand Up @@ -140,11 +156,21 @@ int applyOperator(int num1, int num2, char *opr){
result = num1 % num2;
break;
case '^':
result = num1 ^ num2;
result = ipow(num1, num2);
break;
default:
result = 0;
}
return result;
}

int ipow(int a, int b){
int res = 1;
int i;
for(i = 0; i < b; ++i){
res *= a;
}
return res;
}


11 changes: 5 additions & 6 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

int main()
{
char test[] = " ( 10 + 2 ) * 3 + 2 * ( 6 / 3 + 2 ^ 2 ) ";
char test[] = " ( 10 + 2 ) * 3 + ( 3 * 4 )";
puts(test);
puts(infixToPostfix(test));

char postTest[] = "4 4 + 3 *";
char *postTest = infixToPostfix(test);
puts(postTest);
int i = evaluatePostfix(postTest);
printf("%d\n", i);

/*
char *token = strtok(test, " ");
int a, b, c, d, ip, sp;
Expand All @@ -25,6 +24,6 @@ int main()
sp = stackPrecedence(token);
printf("%s\t%d\t%d\t%d\t%d\t%d\t%d\n",token, a, b, c, d, ip, sp);
token = strtok(NULL," ");
}
} */
return 0;
}

0 comments on commit 463de2b

Please sign in to comment.