Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is the C program and I know what the logic is, n=but I don\'t know how to i

ID: 3839493 • Letter: T

Question

This is the C program and I know what the logic is, n=but I don't know how to implement it, cu=ould someone else help me?

The Bashemin Parking Garage contains a single lane that holds up to ten cars. There is only a single entrance/exit to the garage at one end of the lane. If a customer arrives to pick up a car that is not nearest the exit, all cars blocking its path are moved out, the customer’s car is driven out, and the other cars are restored in the same order that they were in originally. Write a C program that processes a group of input lines. Each input line contains an ‘A’ for arrival or a ‘D’ for departure, and a license plate number. Cars are assumed to arrive and depart in the order specified by the input. The program should print a message whenever a car arrives or departs. When a car arrives, the message should specify whether or not there is room for the car in the garage. If there is no room, the car leaves without entering the garage. When a car departs, the message should include the number of times that the car was moved out of the garage to allow other cars to depart (hint: use stack to simulate the process). The main program and data types are given for your use, on the course/assignments website.

Explanation / Answer

const unsigned MAX_SPOTS = 5;

class Car

{

public:

    string plate;

    int moves;    

    Car( string p = "", int m = 0 ) : plate(p), moves(m) {}

   bool operator == ( const Car& c ) const

    {

        return plate == c.plate;

    }

    bool operator != ( const Car& c ) const

    {

        return plate != c.plate;

    }

} ;

class Garage

{

public:

    deque< Car > park_q;

    string park_name;

    bool is_here( const Car& c )

    {

        deque< Car>::iterator it;

        for( it = park_q.begin(); it != park_q.end(); ++it )

        {

            if( it->plate == c.plate ) return true;

        }

        return false;

    }

   bool arrive( const Car& c )

    {

        if( park_q.size() < MAX_SPOTS )

        {

            park_q.push_front( c );

            cout << c.plate << " arrived in " << park_name << endl;

            return true;

        }

        else return false;

    }

    bool depart( const Car& c )

    {

        if( park_q.size() > 0 && is_here( c ) )

        {

            Car end_car;

            deque< Car >::iterator it = park_q.end(), i;

            if( (end_car = *(--it)) == c )

            {

               for( i= park_q.begin(); i != park_q.end(); ++i ) ++i->moves;

            }

            else

            {

                while( end_car != c )

               {

                    for( i = park_q.begin(); i != park_q.end(); ++i ) ++i->moves;

                    end_car.moves = (--i)->moves;

                    park_q.push_front( end_car );

                    park_q.pop_back();

                    it = park_q.end();

                    end_car = *(--it);

                }

               for( i = park_q.begin(); i != park_q.end(); ++i ) ++i->moves;

             it = park_q.end();

                --it;

            }

             cout << it->plate << " departed from " << park_name

                 << " after " << (it->moves) << " move"

                 << ((it->moves == 1) ? "." : "s.") << endl;

           park_q.pop_back();

            return true;

        }

        return false;

    }

    bool depart_street( const Car& c )

    {

        if( park_q.size() > 0 && is_here( c ) )

        {

            deque< Car >::iterator it = park_q.begin();

            int pos = 0;

            while( it->plate != c.plate ) ++it, ++pos;

            cout << c.plate << " departed from " << park_name

                 << " after " << (it->moves + 1) << " move"

                 << ((it->moves + 1 == 1) ? "." : "s.") << endl;

            park_q.erase( it );

            for( it = park_q.begin(); pos > 0 ; ++it ) ++it->moves, --pos;

            return true;

       }

        else return false;

    }

} ;

void car_depart( Garage g[], const Car& c )

{

if( !g[1].depart( c ) )

    {

        if( !g[2].depart( c ) )

        {

            if( !g[3].depart_street( c ) )

                cout << c.plate << " is NOT HERE!" << endl;

        }

        else if( g[3].park_q.size() )

        {

            deque< Car >::iterator i;

            for( i = g[3].park_q.begin(); i != g[3].park_q.end(); ++i ) ++i->moves;

g[2].park_q.push_front( *(--i) );

            g[3].park_q.pop_back();

        }

    }

    else if( g[3].park_q.size() )

    {

        deque< Car >::iterator i;

       for( i = g[3].park_q.begin(); i != g[3].park_q.end(); ++i ) ++i->moves;

      g[1].park_q.push_front( *(--i) );

        g[3].park_q.pop_back();

   }

}

bool car_arrive( Garage g[], const Car& c )

{

   if( !g[1].arrive( c ) )

    {

        if( !g[2].arrive( c ) )

        {

           if( !g[3].arrive( c ) )

            {

                cout << "For " << c.plate

                    << ", there is NO MORE PARKING SPACE!" << endl;

               return false;

            }

        }

    }

    return true;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote