can someone look over these c++ files for me...

Discussion in 'OT Technology' started by MP, Oct 23, 2004.

  1. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    can someone look over these c++ files for me...question

    Instead of it printing put when I hit "p" how would I make it print to the screen when it reaches the end of file?

    so like the input can be

    1 2 +
    2
    -


    then it needs to print :\

    I don't get how it would know the difference between

    2

    or

    1 2 +

    or

    1 2 + 3 -

    i'm confused :(

    calc.cpp

    Code:
    using namespace std;
    
    #include <iostream>
    #include <cstdlib>
    #include <cctype>
    #include <string>
    
    #include "dstack.h"
    #include "integer.h"
    
    int main()
    {
        Stack values;
        
        string command;
        
        while (cin >> command && command != "q")
        {
            if (isdigit(command[0]))
            {
                int input = atoi(command.c_str());
                Integer wrapper(input);
                values.push(wrapper);
            }
            else
            {
                Integer x;
                Integer y;
                
                switch (command[0])
                {
                case '+':
                    values.pop(x);
                    values.pop(y);
                    values.push(Integer(y.intValue() + x.intValue()));
                    break;
                
                case '-':
                    values.pop(x);
                    values.pop(y);
                    values.push(Integer(y.intValue() - x.intValue()));
                    break;
                
                case '*':
                    values.pop(x);
                    values.pop(y);
                    values.push(Integer(y.intValue() * x.intValue()));
                    break;
                
                case '/':
                    values.pop(x);
                    values.pop(y);
                    values.push(Integer(y.intValue() /  x.intValue()));
                    break;
                
                case 'p':
                    values.pop(x);
                    x.writeToFile(cout);
                    values.push(x);
                    cout << endl;
                }
            }
        }
    }
    
    dstack.h

    Code:
    #ifndef DSTACK_H
    #define DSTACK_H
    #include "integer.h"
    
    class Stack
    {
        public:
            typedef Integer ItemType;
            
            Stack();
            ~Stack();
            
            bool isFull() const;
            bool isEmpty() const;
            
            void makeEmpty();
            void pop(ItemType& popped);
            void push(const ItemType& toAdd);
            
        private:
            static const int MIN_ITEMS = 4;
            
            int maxItems;
            int numItems;
            ItemType *items;
    };
    
    #endif
    
    dstack.cpp

    Code:
    using namespace std;
    
    #include <cassert>
    
    #include "dstack.h"
    
    Stack::Stack()
    {
        numItems = 0;
        maxItems = MIN_ITEMS;
        items = new ItemType[maxItems];
    }
    
    
    //Destruct
    Stack::~Stack()
    {
        delete[] items;
    }
    
    bool Stack::isFull() const
    {
        return (numItems == maxItems);
    }
    
    bool Stack::isEmpty() const
    {
        return (numItems == 0);
    }
    
    void Stack::pop(ItemType& popped)
    {
        assert(!isEmpty());
        
        popped = items[numItems - 1];
        numItems--;
        
        ItemType *newArr;
        
        if (maxItems /2 >= MIN_ITEMS 
            && numItems < maxItems / 4 
            && (newArr = new ItemType[maxItems / 2]) != NULL)
        {
            maxItems /= 2;
            for (int i = 0; i < numItems; i++)
            newArr[i] = items[i];
            delete[] items;
            items = newArr;
        }
    }
    
    void Stack::makeEmpty()
    {
        numItems = 0;
    }
    
    void Stack::push(const ItemType& toAdd)
    {
        assert(!isFull());
        
        ItemType *newArr;
        
        if (numItems + 1 == maxItems && (newArr = new ItemType[maxItems * 2]) != NULL)
        {
            maxItems *=2;
            for (int i = 0; i < numItems; i++)
            newArr[i] = items[i];
            delete[] items;
            items = newArr;
        }
        
        items[numItems] = toAdd;
        numItems++;
    }
    


    integer.h

    Code:
    
    #ifndef INTEGER_H
    #define INTEGER_H
    #include <iostream>
    
    class Integer
    {
        public:
            typedef int KeyType;
            
            Integer();
            Integer(int z);
            
            int compareTo(const Integer& rhs) const;
            void writeToFile(ostream& os) const;
            int intValue() const;
            
            void readFromFile(istream& is);
            
            static void *clone(const void *toCopy);
            static void destroy(void *toDestroy);
            
        private:
            int value;
    };
    
    #endif
    

    integer.cpp

    Code:
    
    using namespace std;
    
    #include <iostream>
    #include "integer.h"
    
    Integer::Integer()
    {
        value = 0;
    }
    
    Integer::Integer(int z)
    {
        value = z;
    }
    
    int Integer::compareTo(const Integer& rhs) const
    {
        return value - rhs.value;
    }
    
    int Integer::intValue() const
    {
        return value;
    }
    
    void Integer::writeToFile(ostream& os) const
    {
        os << value;
    }
    
    void Integer::readFromFile(istream& is)
    {
        is >> value;
    }
    
    
    void *Integer::clone(const void *toCopy)
    {
        return new Integer(*static_cast<const Integer *>(toCopy));
    }
    
    
    void Integer::destroy(void *toDestroy)
    {
        delete static_cast<Integer *>(toDestroy);
    }
    
     
    Last edited: Oct 24, 2004
  2. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    dstack: using namepace std;
    integer: using namepsace std;

    Hate it when that happens, eh? :embd:
     
  3. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    very much so!!! Now i just gotta figure the rest of the errors out.

    Code:
    integer.h:7: error: redefinition of `class Integer'
    integer.h:7: error: previous definition of `class Integer'
    
     
    Last edited: Oct 23, 2004
  4. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    got it.


    thanks penguinman for pointing out the other typos :wiggle:

    edit: instead of it printing put when I hit "p" how would I make it print to the screen when it reaches the end of file?

    so like the input can be

    1 2 +
    2
    -


    then it needs to print :\

    I don't get how it would know the difference between

    2

    or

    1 2 +

    or

    1 2 + 3 -

    i'm confused :(
     
    Last edited: Oct 24, 2004

Share This Page