コード例 #1
0
ファイル: sim.cpp プロジェクト: jx63king/CSCI211
void run_simulation(Pqueue &arrival_queue, int num_checkers, int break_duration, ostream &os)
{
  struct Checker
  { 
    int money;
    int m_available_time;
    Cust *m_cust;
  }; 
  Checker *checkers = new Checker[num_checkers];//array, it is an array, array always is a pointer to the first element, 
  

  int clock;
  int num_customers=arrival_queue.size();     
  Pqueue shop_Q;
  Pqueue checkout_q;
  for(clock=0; num_customers > 0; clock++)
  {
    while(!arrival_queue.empty() &&  (arrival_queue.time_check() == clock))
    {
      Cust *customer = arrival_queue.dequeue();
      customer->print_entered(cout, clock);
      shop_Q.enqueue(customer, customer->time_done_shopping());
    }
    while(!shop_Q.empty() && (shop_Q.time_check() == clock))
    {
      Cust *cust_done = shop_Q.dequeue();
      cust_done->print_done_shopping(cout, clock);
      checkout_q.enqueue(cust_done, 0);
    }
for(int i=0; i<num_checkers; i++)
{
  /*
      if(checkers[i].m_cust->done_checkout(checkers[i].m_available_time))
      {
        cout << "right yeah" << endl;
      }
      */
cout << "break1" << endl;
cout << checkers[i].m_available_time<< endl;
cout << "clock: " << clock<< endl;
if(checkers[i].m_available_time == clock && checkers[i].m_cust == NULL)
//  if((checkers[i].m_cust->done_checkout(checkers[i].m_available_time)) && checkers[i].m_cust == NULL)
  {
      cout << "break2" << endl;
      checkers[i].m_cust->print(cout);
      if(checkers[i].m_cust->m_robber())
      {
    cout << "checker: " << i << endl;
      cout << "break3" << endl;
      cout << "time: " << clock << endl;
cout << "break4" << endl;
      cout << checkers[i].money<< endl;
      cout << "break5" << endl;
      checkers[i].m_cust->print_done_checkout_robber(os,clock,checkers[i].money,i);
      cout << "break6" << endl;
      checkers[i].money = 0;
      }
      else
      {
      checkers[i].money = checkers[i].money + checkers[i].m_cust->money_earned();
      checkers[i].m_cust->print_done_checkout_shopper(os, clock, checkers[i].money, i);
      }
/*
    if(checkers[i].m_cust->m_robber()==false)
    {
      cout << "break3" << endl;
      checkers[i].money = checkers[i].money + checkers[i].m_cust->money_earned();
      checkers[i].m_cust->print_done_checkout_shopper(os, clock, checkers[i].money, i);
    }
    else
    {
      checkers[i].m_cust->print_done_checkout_robber(os,clock,checkers[i].money,i);
      checkers[i].money = 0;
    } 
    */
   

    //Cust *tmp = checkers[i].m_cust;
    //delete [] tmp;
    //delete [] checkers[i].m_cust;
    num_customers--;
//    checkers[i].m_cust=NULL;
}
}

//cout << "num:" << num_customers << endl;

for(int i=0; i < num_checkers && checkout_q.empty() == false; i++)
{
cout << "break3" << endl;
  Cust *checkout_done=checkout_q.dequeue();
  checkers[i].m_cust = checkout_done;
  checkers[i].m_available_time =  checkers[i].m_cust->checkout_time() + checkers[i].m_available_time;
   // checkers[i].m_cust->done_checkout_time(checkers[i].m_available_time);
  checkers[i].m_cust->print_start_checkout(os, clock, i);
  if(checkers[i].m_cust->m_robber())
    checkers[i].m_available_time = checkers[i].m_available_time+break_duration;
}



cout << "break4" << endl;
cout << "clock: " << clock << endl;
if(clock==1000)
  break;
}
cout << "break5" << endl;
}
コード例 #2
0
ファイル: sim.cpp プロジェクト: Pithers/Class-Work
void run_simulation(Pqueue &arrival_queue, int num_checkers, int break_time, ostream &os)
{
  int clock = 0;
  int customer_time = 0;
  int num_customers = arrival_queue.length();           //get number of customers
  Checker *check_array = new Checker[num_checkers];     //create array of checkers
  Cust* temp_customer = NULL;

  Pqueue shopping_q;
  Pqueue checker_q;

  //Initialize all of the checkers
  for(int i = 0; i < num_checkers; i++)
  {
    check_array[i].money = 100;
    check_array[i].break_left = 0;
    check_array[i].time_finished = 0;
    check_array[i].current_customer = NULL;
  }

  //start simulation
  for(clock = 1; num_customers > 0; clock++)
  {
    //----Entering Store----
    while(!arrival_queue.empty() && arrival_queue.get_priority_of_first() == clock)
    {
      temp_customer = arrival_queue.dequeue();                  //get customer to enter store
      temp_customer->print_entered(os, clock);                  //print that customer entered store
      customer_time = clock + temp_customer->get_items()*2;     //calculate when customer will be done
      shopping_q.enqueue(temp_customer, customer_time);         //que up customers by the time they are done shopping
    }

    //----Shopping----
    while(!shopping_q.empty() && shopping_q.get_priority_of_first() == clock)
    {
      temp_customer = shopping_q.dequeue();                     //get customer to enter checker's line
      temp_customer->print_finish_shop(os, clock);              //print that customer finished shopping
      checker_q.enqueue(temp_customer, 0);                      //place customer on checker q with no priority
    }

    //----See if any Checkers are done----
    for(int i = 0; i < num_checkers; i++)
    {
      if(check_array[i].current_customer != NULL && check_array[i].time_finished <= clock)
      {
        if(check_array[i].current_customer->get_alignment())    //if person is a robber
        {
          check_array[i].current_customer->print_finish_cs(os, clock, i, check_array[i].money);
          check_array[i].money = 0;                            //checker loses all money
          check_array[i].break_left = clock + break_time;
        }
        else                                                    //if person is a customer 
        {
          check_array[i].current_customer->print_finish_cs(os, clock, i, 0);
          check_array[i].money += check_array[i].current_customer->get_items()*3;       //customer gets 3*number of items
        }
        
        temp_customer = check_array[i].current_customer; 
        check_array[i].current_customer = NULL;
        delete temp_customer;
        num_customers--;                                        //a customer has been served and leaves the store
      }
    }

    //----Finding Available Checker----
      for(int i = 0; i < num_checkers; i++)
      {
        if(check_array[i].current_customer == NULL && check_array[i].break_left <= clock && !checker_q.empty())    //if checker is available
        {
          temp_customer = checker_q.dequeue();                                //get cusotomer waiting in line
          check_array[i].current_customer = temp_customer;                    //assign that checker the customer
          if(temp_customer->get_alignment())
          {
            customer_time = clock + 4;                                        //robbers take clock + 4
          }
          else
          {
            customer_time = clock + temp_customer->get_items();               //customers take *1 every item
          }
          check_array[i].time_finished = customer_time;                       //set the time the checker will be finished at
          temp_customer->print_start_cs(os, clock, i);
        }
      }
  }
  for(int i = 0; i < num_checkers; i++)
  {
    os << "registers[" << i << "] = $" << check_array[i].money << endl;
  }
  os << "time = " << clock << endl;
}
コード例 #3
0
ファイル: sim.cpp プロジェクト: nloker1/Store-Simulation
void run_simulation(Pqueue &arrival_queue, int num_checkers, int break_duration, ostream &os)
{
    int holder,i,clock=0,num_customers;
    Checker* checkers = new Checker[num_checkers];
    holder = num_checkers;
    num_customers = arrival_queue.size_of_queue();
    while(holder > 0)
    {
        for(i=0; i < num_checkers; i++)
        {
            checkers[i].cash_in_reg = 500;
            checkers[i].break_duration = break_duration;
            checkers[i].break_done = 0;
            checkers[i].available = true;
            checkers[i].done_checking = 0;
            checkers[i].cust_pointer = NULL;
        }
        holder--;
    }

    Cust* cust=NULL;
    int num_items, done_shopping;
    Pqueue shopping_q;
    Pqueue checkout_q;
    for(clock = 0; 0 < num_customers; clock++)
    {

        while(clock == arrival_queue.get_priority())
        {
            cust = arrival_queue.dequeue();
            cust -> print_entered(os, clock);

            num_items = cust -> num_items();
            done_shopping = (cust -> arrival_time()) + (num_items*3);
            shopping_q.enqueue(cust, done_shopping);

        }

        while(clock == shopping_q.get_priority())
        {
            cust = shopping_q.dequeue();
            cust -> print_done_shopping(os,clock);
            checkout_q.enqueue(cust, 0);
        }

        for(i=0; i<num_checkers; i++)
        {
            if(clock == checkers[i].done_checking)
            {
                if(checkers[i].cust_pointer != NULL && (checkers[i].cust_pointer -> get_m_type()))
                {
                    // os << "where is homer " << endl;
                    checkers[i].cash_in_reg += (checkers[i].cust_pointer->num_items()*5);
                    checkers[i].cust_pointer -> print_checkout_shopper(os, clock, i);
                    delete checkers[i].cust_pointer;
                    checkers[i].cust_pointer = NULL;
                    num_customers--;
                    checkers[i].available = true;
                }

                else if(checkers[i].cust_pointer != NULL && (!checkers[i].cust_pointer -> get_m_type()))
                {
                    //os << "weee" << endl;
                    checkers[i].cust_pointer -> print_checkout_robber(os, clock, i, checkers[i].cash_in_reg);


                    checkers[i].cash_in_reg = 0;
                    delete checkers[i].cust_pointer;
                    checkers[i].cust_pointer = NULL;
                    num_customers --;
                    checkers[i].available = false;
                    checkers[i].break_done = clock + checkers[i].break_duration;
                }
            }
            if (checkers[i].break_done == clock)
                checkers[i].available = true;
        }



        for(i=0; i<num_checkers; i++)
        {
            while(!checkout_q.empty() && checkers[i].available)
            {
                checkers[i].break_done = 0;
                checkers[i].cust_pointer = checkout_q.dequeue();
                checkers[i].available = false;
                if(checkers[i].cust_pointer -> get_m_type())
                {
                    checkers[i].done_checking = (clock + (checkers[i].cust_pointer -> num_items()) * 2);
                    // os << "Debugging what is done_checking time? " << endl;
                    //os << checkers[i].done_checking << endl;
                }
                else if(!(checkers[i].cust_pointer -> get_m_type()))
                {
                    checkers[i].available = false;
                    checkers[i].done_checking = clock + 7;
                }
                checkers[i].cust_pointer -> print_started_checkout(os, clock, i);
            }
        }
    }
    for(i=0; i<num_checkers; i++)
    {
        os << "registers[" << i << "] = $" << checkers[i].cash_in_reg << endl;
    }
    os << "time = " << clock << endl;

}