Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,233 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,786 people online right now. Registration is fast and FREE... Join Now!




Postfix evaluation problems(multidigit & printing problems)

 
Reply to this topicStart new topic

Postfix evaluation problems(multidigit & printing problems), need suggestions for working with multidigit numbers and printing the

jah76de
19 Mar, 2007 - 08:27 PM
Post #1

New D.I.C Head
*

Joined: 19 Mar, 2007
Posts: 6


My Contributions
Hi, aspiring programmer here taking a class in Data Structures(taught in C). I have really tried on my own to figure out this code as I like to think I went back to school to actually learn something as opposed to just getting it done to get it done.

I apologize in advance for my messy code, as I have been experimenting with many different ways to do what I need to do.

So, I tried searching for this topic, and though I found a few threads with people asking for postfix evaluation help, it seemed like these were people who just wanted someone else to do their homework assignment for them. I would actually like to understand what I am doing. I am not expecting someone to give me exact code...more like point me in the right direction or suggest something I should try.

Anyway, so when I compile my code, it does work for postfix notation when I use numbers 0-9. But the 2nd part is that I need to be able to evaluate a number greater than 9(double digits or more). This is what I have tried:

1)Concatenating the last 2 popped values on the stack(after using itoa to convert the integer back to a char to be able to be concatenated). I did get this to work, the problem is it concatenates the wrong numbers(i.e. "12 5 +" would end up concatenating 2 and 5, instead of 1 and 2 because of the last in 1st out nature of stacks.)

2)Then I tried a way of using isspace(input) to be used as a condition to concatenate the last 2 popped values off the stack. That didn't work cause in this example again: "12 5 +", 1 and 2 would be combined(and pushed back on the stack as 12), but then it sees a space again after the 5 and would then combine 12 and 5.

3)Another idea I had was string tokenization where I use whitespaces and operators as the delimiters(?) to break the numbers into groups and then concatenate. But how would I say in the code to only concatenate the tokens that only have 2 or more characters?

OK, so my 2nd problem is that the postfix expression I enter, must be printed in the same order.

Anyway, thanks for any help anyone can give me. I'm sorry if my explanation of things is confusing =/

CODE

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>

struct stackNode{
    int data;
    struct stackNode * link;
};

typedef struct stackNode StackNode;
typedef StackNode * StackNodePtr;

void pushStack(StackNodePtr* topNode, int newData);
int popStack(StackNodePtr* topNode);
bool isOperator(char input);
int eval(int int1, char input, int int2);
void push2ndStack(StackNodePtr* printTopNode, char input);
void printStack(StackNodePtr *printTopNode);
//char * concat(StackNodePtr* topNode, char * inputPtr);
char * putArray(char input);



int main()
{
char input;
int num1;
int num2;
int data=0;
int num3=0;
int num4=0;
int result=0;
int tempdata=0;
int intdata=0;
char itoa1[2];
char itoa2[2];
char line[100];



StackNodePtr topNode = NULL;
StackNodePtr printTopNode = NULL;

printTopNode=(StackNode*)malloc(sizeof(StackNode));
topNode=(StackNode*)malloc(sizeof(StackNode));

printf("Please enter sumthin\n");

while((input = getchar() ) != '\n')
{

if(isspace(input))
    {
    continue;
    }
if(!isdigit(input))
    {
    num1=popStack(&topNode);
    num2=popStack(&topNode);
    //_itoa_s(num1, itoa1, 10);
    //_itoa_s(num2, itoa2, 10);
    //printf("%s...%s...\n", itoa1, itoa2);
    //printf("concat=%s\n", strcat(itoa2, itoa1));
    result=eval(num1, input, num2);
    pushStack(&topNode, result);
    }
    else
    {    
    intdata=atoi(&input); //move this up ^
    pushStack(&topNode, intdata);
    }

}

printf("\nThe result is %d\n", topNode->data);

}


void pushStack(StackNodePtr *topNode, int newData)
{
StackNodePtr newNode;

newNode=(StackNode*)malloc(sizeof(StackNode));
if(newNode != NULL)
    {
    newNode->data = newData;
    newNode->link = *topNode;
    *topNode = newNode;
    }
}

int popStack(StackNodePtr* topNode)
{
StackNodePtr tempNode;
int poppedData;

tempNode=(StackNode*)malloc(sizeof(StackNode));

tempNode = *topNode;
poppedData = (*topNode)->data;
*topNode = (*topNode)->link;
free(tempNode);

return poppedData;

}

bool isOperator(char input)
{
if(input == '+' ||
   input == '-' ||
   input == '*' ||
   input == '/')
    {
    return true;
    }
else
    {
    return false;
    }
}

int eval(int int1, char input, int int2)
{
int results;

    if(input == '+')
    {
    results=int2 + int1;
    }
    if(input == '-')
    {
    results=int2 - int1;
    }
    if(input == '*')
    {
    results=int2 * int1;
    }
    if(input == '/')
    {
    results=int2 / int1;
    }

return results;
}


EDIT: Just realized my lack of comments makes this even more confusing then it already is. Also my commented out "experimental code" doesn't help either. Gonna clean this up tomorrow and re-post.

This post has been edited by jayman9: 20 Mar, 2007 - 09:58 PM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Postfix Evaluation Problems(multidigit & Printing Problems)
20 Mar, 2007 - 07:05 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,859



Thanked: 50 times
Dream Kudos: 550
My Contributions
Processing postfix is really not terribly difficult and you seem to have the basic idea. From your discription it would seem that all you need is a better parser. basicly something that when "isdigit()" return true it will read though until it runs out of digits using a formula such as: value = value * 10 + digit as it turns out I recently added a snippet which does this (though not in a way directly plugglage into your code) and the C standard library has functions like atoi() (ascii to integer), atol() (ascii to long), strtol() (string to long) that can do the conversion for you.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 03:38PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month