Exemple #1
0
int main () {
    DoubleLinkList A;
    int customerCounter = 1;
    int minNumber = 1;
    int totalSimulationTime = 0, maxIntervalEnterQueue = 0, maxIntervalServePerson = 0, timeUntilNextCustomer = 0;
    bool validInput = false;
    string trash;
    // Gather user input information. Prompt again if invalid input, i.e. non-numeric.

    while(!validInput) {
        cout << "Enter the length of time simulation should run (minutes): ";

        cin >> totalSimulationTime;

        if (!cin.eof() && cin.good())
            validInput = true;
        else {
            cout << "Invalid input. Please try again." << endl;
            cin.clear();
            cin >> trash; //remove the bad input from the cin stream by throwing it into the trash string
        }
    }

    validInput = false;

    while(!validInput) {
        cout << "Enter maximum interval between individual people entering the queue (minutes): ";
        cin >> maxIntervalEnterQueue;

        if (!cin.eof() && cin.good())
            validInput = true;
        else {
            cout << "Invalid input. Please try again." << endl;
            cin.clear();
            cin >> trash; //remove the bad input from the cin stream by throwing it into the trash string
        }
    }

    validInput = false;

    while(!validInput) {
        cout << "Enter maximum interval of time to serve an individual person in the queue (minutes): ";
        cin >> maxIntervalServePerson;

        if (!cin.eof() && cin.good())
            validInput = true;
        else {
            cout << "Invalid input. Please try again." << endl;
            cin.clear();
            cin >> trash; //remove the bad input from the cin stream by throwing it into the trash string
        }
    }

    cout << endl;

    maxIntervalEnterQueue--; // account for rand() mod
    maxIntervalServePerson--; // account for rand() mod

    timeUntilNextCustomer = 1+rand()%(maxIntervalEnterQueue); // initialize time until next customer

    A.putCustomer(customerCounter, 1, maxIntervalServePerson); // initialize first customer

    cout << "Minute # | Customer # | Entry Minute | Service Time Remaining | Wait Time remaining | Minutes remaining until Service is finished" << endl;

    for(; minNumber<=totalSimulationTime; minNumber++) {

        if (A.getFirstCustomer() != NULL) { // check if any customers in line
            if (A.getFirstCustomer()->getTimeToServe() == 0)
                A.dequeue();

            if (A.getFirstCustomer() != NULL) // check if any customers in line
                A.getFirstCustomer()->decrementTimeToServe();
        }

        if (timeUntilNextCustomer == 0) { // new customer! add to queue and reset timer
            timeUntilNextCustomer = 1 + rand()%(maxIntervalEnterQueue);
            A.putCustomer(++customerCounter, minNumber, maxIntervalServePerson);
        }

        A.getListReverse(minNumber); // print out customers in line and relevant information
        timeUntilNextCustomer--;
    }
    return 0;
}
int main () {
  DoubleLinkList A;
  double nr, udata;
  bool cont = true;
  char sel;
  Node* nodePtr = NULL;
  while (!cin.eof()&&cin.good()&&cont) {
    cout << "Enter your function: Add, Delete, DeleteToEnd(T), Exit, Find, ListForward, ListReverse(R): ";
    cin >> sel;
    if (!cin.eof()) {
      switch (sel) {		
      case 'A': case 'a': {
	cout << "Enter a double-precision number or control-z to end ";
	cin >> nr;
	if (!cin.eof() && cin.good())
	  cont = A.putNode(nr);
	else
	  cont = false;
	break;
      }
      case 'D': case 'd': { // assume that thye current found node is to be deleted
	if (nodePtr != NULL) {
	  A.deleteNode (nodePtr);
	  nodePtr = NULL;
	  cout << "Delete was successful" << endl;
	}
	else
	  cout << "Valid Find not run previously." << endl;
	break;
      }			
      case 'T': case 't': {
	if (nodePtr != NULL) {
	  if (A.DeleteToEnd(nodePtr)) {
	    cout << "Dete to end was successfull" << endl;
	  }
	  else
	    cout << "Delete to end failed." << endl;
	}
	else
	  cout << "Valid Find not run previously." << endl;
	break;
      }
      case 'E': case 'e': {
	cont = false;
	break;
      }
      case 'F': case 'f': {
	cout << "Please Enter User Data value to find: ";
	cin >> udata;
	if (!cin.eof() && cin.good()) {
	  nodePtr = A.findNode(udata);
	  if (nodePtr == NULL)
	    cout << "Data not found." << endl;
	  else
	    cout << "Your data was found: " << nodePtr->getOneNode() << endl;
	}
	else 
	  cont = false;
	break;
      }
      case 'L': case 'l': {
	cout << "Forward List of nodes" << endl;
	A.getListForward();
	cout << endl;
	break;
      }
      case 'R': case 'r': {
	cout << "Reverse List of nodes" << endl;
	A.getListReverse();
	cout << endl;
	break;
      }
      default: {
	cout << "Invalid funciton entered." << endl;
      }
      }
    }
  }
  return 0;
}