Welcome to Dream.In.Code
Getting C++ Help is Easy!

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




Infix to postfix and postfix evaluation

 
Reply to this topicStart new topic

Infix to postfix and postfix evaluation, Codes about a calculator program that contains infix to postfix and po

bhe_0317
post 16 Jul, 2007 - 05:53 PM
Post #1


New D.I.C Head

*
Joined: 16 Jul, 2007
Posts: 2


My Contributions


CODE

#include <stdio.h>
#include <string.h>
#include "Item.h"
#include "STACK.h"
main(int argc, char *argv[])
  { char *a = argv[1]; int i, N = strlen(a);
    STACKinit(N);
    for (i = 0; i < N; i++)
      {
        if (a[i] == ')')
          printf("%c ", STACKpop());
        if ((a[i] == '+') || (a[i] == '*'))
          STACKpush(a[i]);
        if ((a[i] >= '0') && (a[i] <= '9'))
          printf("%c ", a[i]);
      }
    return 0;
    printf("\n");
  }

for STACK.h:

void STACKinit(int);
int STACKempty();
void STACKpush(Item);
Item STACKpop();

for Item.h

typedef char* Item;
#define eq(A,B) strcmp(A,B)==0)

for STACK.c:

#include <stdlib.h>
#include "Item.h"
#include "STACK.h"
static Item *s;
static int N;
void STACKinit(int maxN)
  { s = malloc(maxN*sizeof(Item)); N = 0; }
int STACKempty()
  { return N == 0; }
void STACKpush(Item item)
  { s[N++] = item; }
Item STACKpop()
  { return s[--N]; }

for Makefile:

CC=gcc
CFLAGS=-g -W -Wall -I.
HEADERS=STACK.h Item.h
OBJECTS=inftopost.o STACK.o

PROGNAME=inftopost

$(PROGNAME): $(OBJECTS)
    $(CC) $(CFLAGS) $(OBJECTS) -o $(PROGNAME)

inftopost.o: inftopost.c $(HEADERS)
    $(CC) -c $(CFLAGS) inftopost.c -o inftopost.o

STACK.o: STACK.c $(HEADERS)
    $(CC) $(CFLAGS) $(OBJECTS) -o STACK.o


.PHONY: clean ctags

tags: ctags


ctags:
    ctags -R

clean:
    rm -rf $(OBJECTS) $(PROGNAME)



my program doesn't produce an executable file.. pls. help me..
User is offlineProfile CardPM

Go to the top of the page

enpey
post 16 Jul, 2007 - 06:14 PM
Post #2


D.I.C Head

**
Joined: 2 May, 2007
Posts: 59



Thanked 1 times
My Contributions


Including the errors when trying to compile would make it easier...

And perhaps the purpose of the code...

bhe_0317, it is good coding practice to comment your code, so as others looking at your code know what is happening, and also if/when you come back and use the code or need to modify it down the track you can pick up what is going on again quickly..
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 17 Jul, 2007 - 04:16 AM
Post #3


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


enpey is correct - please post the exact errors you receive upon compilation.
User is online!Profile CardPM

Go to the top of the page

Topher84
post 17 Jul, 2007 - 05:17 AM
Post #4


D.I.C Head

Group Icon
Joined: 4 Jun, 2007
Posts: 232



Dream Kudos: 25
My Contributions


Here is my infix to postfix in c++... its a function i made which reads in 1 line at a time from a file.. you can mod it to just read whatever you have and it should work fine smile.gif this IS somewhat of a "brute force" attempt on the process because you can assign a # value to each operand and then check for < or > which would reduce code but this WORKS at least heh

CODE

string Utility::InfixToPostfixOperation(string& strInput)
{
    vector<string> strvInput;         //vector to hold the referenced Split(strInput)
    stack<char> cStack;                 //stack of characters (Operators)
    char cStackToPostfix = ' ';         //character to add(pop) to postfix result
    string strPostfixResult = " ";   //string to hold postfix notation result
    int nSize = strvInput.size();     //length of the infix notation expression
    vector<string>::iterator Itv;     //vector Iterator
    
    strvInput = Split(strInput);

    for(Itv = strvInput.begin(); Itv != strvInput.end(); Itv++)
    {
        string strTemp =* Itv;
        if(strTemp == "*" || strTemp == "/" || strTemp == "+" || strTemp == "-" || strTemp == "=" || strTemp == "(" || strTemp == ")")
        {
            if(strTemp == "=")
            {
                cStack.push('=');
            }//end if
            else if(strTemp == "-" && !cStack.empty())
            {
                if(cStack.top() == '=' || cStack.top() == '*' || cStack.top() == '/' || cStack.top() == '+' || cStack.top() == ')' || cStack.top() == '(')
                {
                    if(cStack.top() == '(')
                    {
                        cStack.push('-');
                    }
                    else if(cStack.top() == '*' || cStack.top() == '/' || cStack.top() == '+')
                    {
                        strPostfixResult += " ";
                        strPostfixResult += cStack.top();
                        cStack.pop();
                        cStack.push('-');
                    }
                    else
                    {
                        cStack.push('-');
                    }
                }//end while
            }//end else if
            else if(strTemp == "+" && !cStack.empty())
            {
                if(cStack.top() == '=' || cStack.top() == '*' || cStack.top() == '/' || cStack.top() == '(' || cStack.top() == '(')
                {
                    if(cStack.top() == '(')
                    {
                        cStack.push('+');
                    }
                    else if(cStack.top() == '*' || cStack.top() == '/')
                    {
                        strPostfixResult += " ";
                        strPostfixResult += cStack.top();
                        cStack.pop();
                        cStack.push('+');
                    }
                    else
                    {
                        cStack.push('+');
                    }
                }//end while
            }//end else if
            else if(strTemp == "/" && !cStack.empty())
            {
                if(cStack.top() == '+' || cStack.top() == '-' || cStack.top() == '=' || cStack.top() == '*' || cStack.top() == '(' || cStack.top() == ')')
                {
                    if(cStack.top() == '(')
                    {
                        cStack.push('/');
                    }
                    else if(cStack.top() == '*')
                    {
                        strPostfixResult += " ";
                        strPostfixResult += cStack.top();
                        cStack.pop();
                        cStack.push('/');
                    }
                    else
                    {
                        cStack.push('/');
                    }
                }
            }//end else if
            else if(strTemp == "*" && !cStack.empty())
            {
                if(cStack.top() == '/' || cStack.top() == '+' || cStack.top() == '-' || cStack.top()== '=' || cStack.top() == '(' || cStack.top() == ')')
                {
                    cStack.push('*');
                }
            }//end else if
            else if(strTemp == "(")
            {
                cStack.push('(');
            }
            else if(strTemp == ")")
            {
                while(!cStack.empty() && cStack.top() != '(')
                {
                    strPostfixResult += " ";
                    strPostfixResult += cStack.top();
                    strPostfixResult += " ";
                    cStack.pop();
                }
                if(!cStack.empty())
                {
                    cStack.pop();
                }
            }
        }//end if
        if(strTemp != "*" && strTemp != "/" && strTemp != "+" && strTemp != "-" && strTemp != "=" && strTemp != "(" && strTemp != ")")
        {
            strPostfixResult += " ";
            strPostfixResult += strTemp;
        }//end if
    }//end for
    while(!cStack.empty())
    {
        if(cStack.top() == '(' || cStack.top() == ')')
        {
            cout << "Invalid Infix Notation Expression... Parantheses Mismatch";
            if(cStack.top() == '(')
            {
                cout << "\nMissing Right Parantheses" << endl << endl;
                cout << "\nAttempted ";
            }//end if
            else if(cStack.top() == ')')
            {
                cout << " \nMissing Left Parantheses" << endl << endl;
                cout << "\nAttempted ";
            }//end else if
        }//end if
        if(cStack.top() == '(' || cStack.top() == ')')
        {
            cStack.pop();
        }//end if
        strPostfixResult += " ";
        strPostfixResult += cStack.top();
        strPostfixResult += " ";
        cStack.pop();
    }
    return strPostfixResult;
}//end InfixToPostfix


maybe this will help some


note: here is my split function too:

CODE

vector<string> Utility::Split(const string &strInput, const string &strDelims)
{

    string strTokens;
    vector<string> parsedString;
    string strTemp = strInput;

    int nCol = 0;
    
    while(!strTemp.empty())
    {
        StripBoth(strTemp);
        StripBoth(strTemp, '\t');
        nCol = (int)strTemp.find_first_of(strDelims); //find the position of the first delimiter

        if(nCol == string::npos) //if a delimiter is NOT found
        {
             strTokens = strTemp; //set string to Token (can't parse it so send it back)
             strTemp = "";          //set the remaining string to nothing
        }//END if
        else if(nCol != string::npos) //if a delimiter IS found
        {
            if(nCol == 0)
            {
                nCol = 1;
            }//end if
            strTokens = strTemp.substr(0,nCol); //set strTemp to the string from 0 to the position of the 1st delimiter
            //strTemp = strTemp.substr(nCol+1); //set strTemp to the remaining string
            strTemp = strTemp.substr(nCol);
        }//END else
        parsedString.push_back(strTokens); //put token in vector
    }//END while
    return parsedString;
}//END split


This post has been edited by Topher84: 17 Jul, 2007 - 05:21 AM
User is offlineProfile CardPM

Go to the top of the page

Trogdor
post 17 Jul, 2007 - 06:19 AM
Post #5


D.I.C Addict

Group Icon
Joined: 6 Oct, 2006
Posts: 517



Thanked 3 times

Dream Kudos: 125
My Contributions


Nice, but i dont think copypasting your program is going to help him very much to learn to code.
On that line of thought, i doubt that the first thing that he posted was his own either.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 17 Jul, 2007 - 06:34 AM
Post #6


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


Very true. I'm sure Topher is aware of the site policy against providing source code until an effort has been shown, and our preference for guidance as opposed to simply providing a full solution. No one learns that way.
User is online!Profile CardPM

Go to the top of the page

Topher84
post 17 Jul, 2007 - 09:13 AM
Post #7


D.I.C Head

Group Icon
Joined: 4 Jun, 2007
Posts: 232



Dream Kudos: 25
My Contributions


QUOTE(Amadeus @ 17 Jul, 2007 - 07:34 AM) *

Very true. I'm sure Topher is aware of the site policy against providing source code until an effort has been shown, and our preference for guidance as opposed to simply providing a full solution. No one learns that way.


my bad.. i thought that was his code.. oops on my end .. sorry!

Note: man it took me a while to dig that up too! haha oh well i know where i have it now in my old college class stuff smile.gif ha

This post has been edited by Topher84: 17 Jul, 2007 - 09:15 AM
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 17 Jul, 2007 - 09:16 AM
Post #8


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


No worries at all my friend - it is natural to assume that the posted code was his - I would have myself. I merely meant to point out that you are well aware of the site policies, and have upheld them on numerous occasions. smile.gif
User is online!Profile CardPM

Go to the top of the page

Topher84
post 17 Jul, 2007 - 09:20 AM
Post #9


D.I.C Head

Group Icon
Joined: 4 Jun, 2007
Posts: 232



Dream Kudos: 25
My Contributions


QUOTE(Amadeus @ 17 Jul, 2007 - 10:16 AM) *

No worries at all my friend - it is natural to assume that the posted code was his - I would have myself. I merely meant to point out that you are well aware of the site policies, and have upheld them on numerous occasions. smile.gif


Eh my professor pointed out this site and i got a few helpful tips/tricks out of it during some of my classes so I thought i'd contribute some.. it just pisses me off when people just ask for others to do their homework and almost "demand" its given to them by the end of the day. Personally, i did NOT do great my first round of intro classes but now i think im pretty good at c++ and studying has paid off A LOT! just wish people would take the time to learn rather than the easy route to just get it done and waiting till the last minute.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 06:12AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month