# Thread: Infix to Postfix Help?

1. ## Infix to Postfix Help?

Hey guys, i have a project to convert an Infix Expression to Postfix Expression or the Reverse Polish Notation.

I'm almost done but my code only reads single digits..

For example, if i input 12+3, it converts to 123+ and gives 5 as a result.

How do i read the number '12' as one so it can be 12 3 + = 15?

Here's my code..if you find something wrong with it please tell me too so i can fix it..

Code:
```QUEUE head=NULL;

void push(STACK *top, char c)
{
STACK temp;
temp=(stacks*)malloc(sizeof(stacks));
temp->x= c;
temp->next = *top;
*top=temp;
}

void pop(STACK *top)
{
STACK temp;

if(!isEmpty(*top))
{
temp=*top;

if(temp->x!='(')
{
}

*top=(*top)->next;
free(temp);
}

}

void intPush(STACK2 *top, float a)
{
STACK2 temp;
temp=(stacks2*)malloc(sizeof(stacks2));

if(temp==NULL)
{
printf("no memory!");
}

temp->x= a;
temp->next = *top;
*top=temp;
}

float intPop(STACK2 *top)
{
STACK2 temp;
float valuePopped;

if(*top!=NULL)
{
temp=*top;
valuePopped=(*top)->x;
*top=(*top)->next;
free(temp);
}

return valuePopped;

}

{
QUEUE p;
QUEUE temp;

temp = (queues*) malloc(sizeof(queues));

{
temp->x = x;
temp->next = NULL;
}

else
{
while(p->next!=NULL)
{
p=p->next;
}

temp->x = x;
temp->next = NULL;
p->next = temp;
}
}

void toRPN(STACK *top, char *x, STACK2 *top2)
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleTextAttribute(hOut,FGI);

printf("\n\nIn Postfix Notation\n ");

while(*x!='\0')
{
if(*x=='(')
{
push(&(*top),*x);
}

else if(*x==')')
{
while(!isEmpty(*top) && (*top)->x!='(')
{
pop(&(*top));

}

if((*top)->x=='(')
{
pop(&(*top));
}
}

else if(isdigit(*x))
{
}

else
{
if(*x=='+' || *x=='-')
{
while(!isEmpty(*top) && priority((*top)->x)>0)
{
pop(&(*top));
}

push(&(*top),*x);
}

else if(*x=='*' || *x=='/' || *x=='%')
{
while(!isEmpty(*top) && priority((*top)->x)>1)
{
pop(&(*top));
}

push(&(*top),*x);
}
}

x++;
}

while(!isEmpty(*top))
{
pop(&(*top));
}

}

{

float x;
float y;
float result;

HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

while(p!=NULL)
{
SetConsoleTextAttribute(handle,FCI);
printf("%c",p->x);

if(isalnum(p->x))
{
x = (float)atof(&(p->x));
intPush(&(*top),x);
}

else if(p->x!='(')
{
y=intPop(&(*top));
x=intPop(&(*top));
result = calculate(x,p->x,y);

intPush(&(*top),result);
}

p=p->next;
}

return (*top)->x;
}

int priority(char c)
{
if(c=='*' || c=='/' || c=='%')
return 2;
else if(c=='+' || c=='-')
return 1;
else
return 0;
}

bool isEmpty(STACK top)
{
if(top==NULL)
return true;
else
return false;
}

float calculate(float x, char op, float y)
{
if(op=='+')
return x + y;
else if(op=='-')
return x - y;
else if(op=='*')
return x * y;
else if(op=='/')
return x / y;
else if(op=='%')
return float((int)x % int(y));
else
return -1;
}```

2. Your using isdigit, then push that one digit on the stack. You can use strtol or strtof (if you want to accept floats) once you pass the is digit test. With these function you can parse the string x and update it with the endptr argument that these function have.

But you might still get into problem if someone enters say: -12

3. Originally Posted by Subsonics
Your using isdigit, then push that one digit on the stack. You can use strtol or strtof (if you want to accept floats) once you pass the is digit test. With these function you can parse the string x and update it with the endptr argument that these function have.

But you might still get into problem if someone enters say: -12
Sir, can i ask how to use strtof and how to apply it in my code?

4. Well suppose you have reached the point where you check the string for digits, then doing something like this:

Code:
```    if(isdigit(*x)) {
char *endp = NULL;
float f = strtof(x, &endp);
//push f to stack
x = endp;
}```
This might not just drop in to your current code, but you get the idea. One thing I noticed was that you increment x at the end, with this step you would need to skip that step since: x = endp; will update x to the next token.

Code:
`char a[6] = { '1','2','+','3','+','4'};`
, how do i turn '1' and '2' into just "12" then push it to the stack? I tried using strcat() and made changes to my code, it didnt have any errors at compile time but it crashed at runtime -_-

6. If you make that a[7] and add a terminating zero, then you can do what I just showed you!

Code:
`char *x = a;`

7. rp.h
colors.h
rpnmain.cpp
rpnsource.cpp

This is my whole program so far..i undid the changes i made..so its functioning again now but only reads single characters..

8. Why are you showing this? It's not related to your problem. Try this if you still don't get what it is I'm trying to tell you.

Code:
```#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
if(argc < 2) return 0;

char *str = argv[1];
char *endp = NULL;
float f = strtof(str, &endp);
str = endp;

printf("%f, %s\n", f, str);

return 0;
}```
It demonstrates the idea in a standalone program. Just add an argument that starts with a number and have characters follow immediately, example: ./a.out 123hello