Programming help?

I am currently taking a course on data structures and algorithms it's a fun, challenging course and I have hit an obstacle in an assignment, wondering if anyone could help me out
In c++ I am to create my own implementation of a Stack class and write a program that reads infix arithmetic expressions and converts them to postfix for the compiler to evaluate them. My problem is with precedence. I've gotten 3+5*2/4-1 to convert to 35241/*+- and 3-5/2*4+1 to convert to 35241*/-+ Here's the code:
Code:
#include<iostream>
#include<iomanip>
#include<string>
#include<cstdlib>
using namespace std;
 
const int MAX_SIZE=100;
 
class Stack
{
private:
    char array[100];
    int top;
public:
    Stack(){
        top=0;
    }
    void push(int val);
    char pop();
    int size();
    char topValue();
    bool isEmpty();
    void print();
};
void Stack::push(int val)
{
    if(size()>99)
    {
        cout << "Stack Overflow" << endl;
    }
    else
    {
        array[top]=val;
        top++;
    }
}
void Stack::print()
{
    for(int i=0;i<top;i++)
    {
        cout << array[i];
    }
}
 
char Stack::pop()
{
    if(isEmpty())
    {
        cout << "Stack Underflow" << endl;
        return '0';
    }
    else
    {
        top--;
        return array[top];
    }
}
 
bool Stack::isEmpty()
{
    if(top<=0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
int Stack::size()
{
    return top;
}
 
char Stack::topValue()
{
    return top;
}
void main()
{
    Stack s;
    //bool divide=false;
    string exp="";
    //string punctuate="";
    string popped="";
    string result;
    int value;
    cout << "Enter an Expression: ";
    cin >> exp;
    cout << endl;
    for(int i=0;i<exp.length();i++)
    {
            if(ispunct(exp[i]))
            {
                if(exp[i]=='*'||exp[i]=='/')
                {
                    s.pop();
                    popped=exp[i]+popped;
                   
                }
                if(exp[i]=='+')
                {
                    s.push(exp[i]);
                    popped=popped+exp[i];
                }
                if(exp[i]=='-')
                {
                    s.push(exp[i]);
                    popped=popped+exp[i];
                }
            }
            else
            {
                result = result+exp[i];
            }
        }
   
    cout << result+popped << endl;
   
    system("Pause");
}
I can't find a way to always order addition before subtraction, and go to the left most symbol first and print that, ex: 3/5*2=352/* and 3*5/2 is 352*/
 

Hellwig

Banned
Short Bussed
BUH!??? :lulz:



















I tried programming a space invaders clone a few yrs back using C++ & OpenGL. I learned my lesson - i truly suck at math and coding - sorry, no help here.
I'll post the code sometime if any1 wants a good laugh. :facepalm:Got the player ship to respond to key presses by moving and shooting at the aliens, got the title screen, background, & alien ship bitmaps up, but i realized i was wasting my time even trying, and quit.
:woo:

I assume u know about http://www.gamedev.net/page/index.html & http://www.gamasutra.com/ etc, etc. Prolly gonna have to troll thru 4 what u r looking for.
 

SAF

Imperator
Administrator
Would add my thoughts, but that's not until next semester for me.

On a similar note, if anyone has any suggestions on how to make Oracle any less nervewracking, I'm all ears.
 
SAF your going to be pissed when you learn how much easier and more proficient stacks are from ordering arrays frOm least to greatest and vise versa, as well as taking a word you type and have it printed backwards lol.
 
BUH!??? :lulz:



















I tried programming a space invaders clone a few yrs back using C++ & OpenGL. I learned my lesson - i truly suck at math and coding - sorry, no help here.
I'll post the code sometime if any1 wants a good laugh. :facepalm:Got the player ship to respond to key presses by moving and shooting at the aliens, got the title screen, background, & alien ship bitmaps up, but i realized i was wasting my time even trying, and quit.
:woo:

I assume u know about http://www.gamedev.net/page/index.html & http://www.gamasutra.com/ etc, etc. Prolly gonna have to troll thru 4 what u r looking for.
I have to see that code! Post it!
 
Best program I ever made read a maze out of x's and o's the o's were a path and x's were the walls, the program would follow the path through the o's using recursion and backtracking until it reached the * which was a pot of gold, I was so happy when it worked lol, made me really love this class
 

Hellwig

Banned
Short Bussed
Part 2:

*Some pages may be dupli. or even previous versions of the same program. It's too painful for me to delve much further into it all and relive painful memories. :(
 

Attachments

  • IFS_PART2.pdf
    4.1 MB · Views: 8
Here is the finished program, see if you can find any bugs now...aside from starting the input as anything but a numeric value, and using anything below 0 or past 9 for the expression.
 

Attachments

  • stack.rar
    2.7 KB · Views: 4
Well I am having trouble figuring out what to do after queue is created in my next assignment. Try it out and let me know what you come up

image.jpg
image.jpg
 

Hellwig

Banned
Short Bussed
:( Hmm - my AWESOME space invaders clone pdf's show only 6 views combined, and 2 of em were me!

I take it my "expertise" is no longer desired? :Cry:

:lol:
 
I do
:( Hmm - my AWESOME space invaders clone pdf's show only 6 views combined, and 2 of em were me!

I take it my "expertise" is no longer desired? :Cry:

:lol:
i downloaded it to my iPod and PC and was looking through it to see if I understood any of it. I don't
 

Hellwig

Banned
Short Bussed
Yeah, most of it is just standard win32/GL application window creation etc. Didn't even need the GL stuff for what I eventually ended up wiv. :( Anyone who hasn't used GL b4 would be fucking LOST!
:lulz:

Anyways like I said, sorry but I'm prertty much done wiv dat shit 4eva and don't wish to revisit it all. I wouldn't even know where to start nowadays. My only (obvious, I know) advice is to make sure u master your compilers debugging features to the fucking max, esp. the step-thru etc.
:beer:
 
Top