Thread: need help for stack problem

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    15

    need help for stack problem

    can anyone help me with creating the h file.please help me.
    this is about garage parking:

    There is only one entrance/exit to the garage. If someone comes to retrieve a car that is not first out of exit, all cars blocking its path must be moved out (hence the stack). After the car is taken out, all other cars are restored in order they were taking out. Each line of the input file contains an "A" for arrival or "D" for departure, a license plate number (6 characters) and the time of arrival or departure.
    The program should print a message whenever a car arrives or departs. When a car arrives, the message should specify the time of arrival and whether or not there is room for the car in the garage (10 car limits).
    sample output:

    Car with license no. DT3172 arriveg at 8:01 AM car parked

    Car with license no. BFD111 arriving at 12:52 PM no room available

    Car with license no. BFD111 deparing at 1:33 PM no such car parked here

    Car with license no. DT3172 departing at 2:59 PM
    times moved: 2
    time parked: 6 hours and 58 minutes

    Note: when calculating the time parked, first convert the time of arrival and the time of departure to military time.

    Note: use stack

    Code:
    #include <iostream>
    #include <fstream>
    #include "garage.h"
    #include <stack>
    
    
    using namespace std;
    
    const int MAX = 10;
    
    void main()
    {
    	ifstream infile;
    	stack garage;
    	char AorD;
    	char AorP[2];
    	char plate [MAX];
    	int hour;
    	int min;
    
    	infile.open  ("garagepark.txt");
    
    	if (infile.fail ())
    	{
    		cout << "Inut file open failed." <<endl;
    		exit(1);
    	}
    
    	infile >> AorD>>plate>>hour>>min>>AorP;
    
    	while (infile)
    	{
    		if (AorD == 'A')
    			garagepark.Push;
    		else
    			if (AorD == 'D')
    				garagepark.Pop;
    			infile >> AorD>>plate>>hour>>min>>AorP;
    	}
    	infile.close();
    
    }
    Code:
    #include <iostream>
    #include "garage.h"
    #include <string>
    
    using namespace std;
    
    string MAXCAR;
    
    GarageStack::GarageStack()
    {
    	
    	top= -1;
    }
    bool  GarageStack::IsEmpty()const
    {
    	if(top ==-1)
    	{
    		return true;
    	}
    	return false;
    }
    
    bool  GarageStack::IsFull()const
    {
    	if (top==MAXCAR-1)
    	{
    		return true;
    	}
    	return false;
    }
    
    void  GarageStack::Push(char AorD, char tag[], int milhours, int milmin, char AorP[])
    {
    	if(!IsFull())
    	{
    		top++;
    		spaces[top].arrival= AorD;
    		for (int i=0; i<10; i++)
    		spaces[top].tag[i] = tag[i];
    		spaces[top].hours = milhours;
    		spaces[top].minutes = milmin;
    
    		for (int k=0; k<2; k++)
    			spaces[top].ampm[k]= AorP[k];
    
    		cout<<"Car with license number: ";
    		int i=0;
    		while(i<10 && spaces[top].tage[i]!='\0')
    		{
    			cout<<spaces[top].tag[i];
    			i++;
    		}
    		spaces[top].moved=0;
    		cout<< "arriving at: "<<spaces[top].minutes:cout<<spaces[top].minutes;
    		cout<<" Car is Parked "<<endl<<endl;
    	}
    	else
    	{
    		cout<<" Car with license number: ";
    		for (int i=0; i<10; i++)
    			cout<<tag[i];
    		cout<<"\nNo Room Available"<<endl<<endl;
    	}
    }
    
    void  GarageStack::Pop(char AorD, char tag[], int milhours, int milmin, char AorP[])
    {
    	if(!IsEmpty())
    	{
    		bool match=false;
    		int i= top;
    		while(!match && i>0)
    		{
    			match = true;
    			int j=0;
    			while (match && j<10)
    			{
    				if (spaces[i].tag[j] !=tag[j])
    				{
    					spaces[i].moved++;
    					match=flase;
    				}
    				j++;
    			}
    			if (!match)
    				i--;
    		}
    		if (match)
    		{
    			int diffhours;
    			int diffminutes;
    			
    			if(AorP[0]=='P' && Hr !=12)
    				Hr+=12;
    			if (spaces[i].ampm[0]=='P' && soaces[i].hours !=12)
    				spaces[i].hours+=12;
    			diffhours = Hr - spaces[i].hours;
    			diffminutes = Min - spaces[i].minutes;
    
    			if(diffminutes < 0 )
    			{
    				diffhours = diffhours - 1;
    				diffminutes = diffminutes = 60;
    			}
    
    			cout<< "Car with license number: ";
    			int i=0;
    			while ( i<10 && spaces[i].tag[i]!='\0')
    			{
    				cout<<spaces[i].tag[i];
    				i++;
    			}
    			cout<<" departing at: "<<spaces[top].hours<<":";
    			(spaces[top].minutes)<10?cout<<'0'<<spaces[top].minutes:cout<<spaces[top].minutes;
    			cout<<endl;
    			cout<<"Time parked: "<<diffhours<<" Hours and "<<diffminutes<<" minutes "<<endl;
    			cout<<"Time the car was moved: "<<spaces[i].numx<<endl;
    
    			int z;
    
    			cout<<endl;
    			for (z = top; z>=i; z--)
    			{
    				tempspaces[top-z] = spaces[z];
    			}
    			for (z = i; z<=(top-1); z++)
    				spaces[z] = tempspaces[top-1-z];
    			top = top -1;
    		}
    		else
    			cout<<"There is no such car."<<endl;
    	}
    	else 
    		cout<<"The garage is Empty"<<endl;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Presumably you have to declare the GarageStack class. What do you have so far?

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    15

    GarageStack

    this is for GarageStack, but I don't know it is correct or not?

    Code:
    #ifndef GARAGE_H
    #define GARAGE_H
    
    
    const int MAX_CARS=10;
    
    
    class GarageStack
    {
    public:
    	bool IsEmpty() const;
    
    	bool IsFull() const;
    
    	void Push(/* in */ int newItem);
    
    	//int Top() const;
    
    	void Pop();
    
    	IntStack();
    
    private:
    	struct car
    	{
    		char firstLetter;
    		char plate;
    		//i need a time spot here
    		//and here one for arrival and one for departure.
    		int timesMoved;
    	};
    	car data[MAX_CARS];
    	int top;
    };
    #endif

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Looks like you still have a little more updating to do. Look at the function definitions in GarageStack.cpp. Your function declarations in GarageStack.h should match them with the same parameters and return type.

    Also look at the names of the variables used in the cpp file. The ones that aren't funcion parameters are probably member variables. Make sure you have a match in your header.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    can you show me what I have to do? thanks

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Add declaration of GarageStack::GarageStack() into class declaration.
    Than read all functions in your c-file and add their declaration (if missing) to the class declaration
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> can you show me what I have to do?
    If we show you, then we are doing the assignment for you. Is there something you didn't understand in my instructions?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    can you tell me what I do with stack garage; in cpp what I have to use?
    and the other thing is garagepark.push and pop.
    can you help me with these? thanks

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Look at the function definitions in GarageStack.cpp. Your function declarations in GarageStack.h should match them with the same parameters and return type. Can you tell us what the return type and parameters are supposed to be for Push?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Your push() and pop() are way way too complicated for a stack.

    The isempty() test is good, but apart from that, you only need this

    Code:
    push( item ) { stack[stackptr++] = item; }
    pop ( ) { return stack[--stackptr]; }
    All that logic you have in there has to go somewhere else.

    So if you remove a car from the stack, you can do these things
    - push it back to the stack you just took it from
    - push it back to another stack of cars
    - finish with it

    Like removing cars requires two stacks (that's a big hint)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    please need more help I am very confuse?

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Get a bunch of paper plates, library index cards, post-it notes or whatever, then write down your car registrations on each one.

    Then use the rules for a stack (add and remove from one end only) to add and remove them from the garage.

    Observe how each piece of paper moves

    Then do the same in software.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Great advice, Salem.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> please need more help I am very confuse?
    I asked a specific question. If you can answer my question, I might be able to give you more help.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    for push or pop do I have to use this:
    Push(AorD,plate,hour,min,AorP);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM