All pastes #804055 Raw Edit

Doug

public cpp v1 · immutable
·published 2007-12-03 01:58 UTC
rendered paste body
//Authour: Doug Cox//Purpose: Inventory control with pointers//Last Updated: 30 November 2007#include <iostream>#include <iomanip>using namespace std;const int maxInventory = 500;     //  Maximum amount of widgets                                  //  allowed in inventoryconst double warnPercent = 0.10;  //  Percentage of maximum                                  //  not to fall beneathconst int warnLevel = static_cast<int>(maxInventory*warnPercent);const char helpChar = 'H';        //  Helpconst char quitChar = 'Q';        //  Quitconst char listChar = 'L';        //  Listconst char reqChar = 'R';         //  Requestconst char shipChar = 'S';        //  Shipmentstruct pending {  int amount;                     //  Amount in pending list  string name;                    //  Company name  pending *next;                  //  path to next item};char getCmd();                    //  Returns a char variableint getAPosInt();                 //  Returns a positive integer//  Counts total pending requests and determines sumvoid countPending(pending *head, int &count, int &maxPending);//  Prints out total accepted, total rejected, total sent, maximum ever in pending and stock levelvoid printStats(int totalAccepted, int totalRejected, int totalSent, int maxPending, int inStock, pending *head, int &count);void helpCmd();                   //  Help function                                  //  - lists all valid commands//  Request function - fills customer requests for widgetsvoid reqCmd(int &inStock, int &totalSent, int &maxPending, pending *&head, pending *&last, int &totalAccepted);//  Shipment function - ships new stockvoid shipCmd(int &inStock, int &totalSent, int &totalAccepted, int &totalRejected, pending *&head);//  List function - lists all stockvoid listCmd(int inStock, pending *head, int &maxPending, int &count);int main() {  int inStock = 0;  int maxPending = 0;  int totalAccepted = 0;  int totalRejected = 0;  int totalSent = 0;  int count = 0;  pending *head = 0;  pending *last = 0;  char cmd;  helpCmd();  cmd = getCmd();  while (cin && cmd != quitChar) {    switch (cmd) {    case helpChar:    case helpChar:      helpCmd();      break;    case reqChar:      reqCmd(inStock, totalSent, maxPending, head, last, totalAccepted);      break;    case shipChar:      shipCmd(inStock, totalSent, totalAccepted, totalRejected, head);      break;    case listChar:      listCmd(inStock, head, maxPending, count);      break;    default:      cout << "Invalid command\n";      break;    }    if (inStock < warnLevel) {      cout << "\n\tWarning! The current inventory level is too low.\n"           << "\tOrder a new shipment ASAP.\n\n";    }    cmd = getCmd();  }  printStats(totalAccepted, totalRejected, totalSent, maxPending, inStock, head, count);  cout << "\n==== End of run ====\n\n";  return 0;}char getCmd() {  if (cin) {    char cmd;    cout << "Enter your choice: ";    if (cin >> cmd) {      cin.ignore();      return toupper(cmd);    }    else {      cout << "Invalid command option.\n";    }  }  return quitChar;}int getAPosInt() {}void countPending(pending *head, int &count, int &maxPending) {  int num = 0;  count = 0;  while (head != 0) {    ++count;    num += head->amount;    head = head->next;  }  if (num > maxPending)    maxPending = num;}void printStats(int totalAccepted, int totalRejected, int totalSent, int maxPending, int inStock, pending *head, int &count) {  countPending(head, count, maxPending);  if (head != 0) {    cout << "--------------------\n"         << "--- Number of requests pending:" << setw(8) << count << endl;    while (head != 0) {      cout << "--- " << head->amount << " widgets need to be sent to "           << head->name << ".\n";      head = head->next;      }    cout <<"---------------------\n\n";    }  cout << "==== Statistics ====\n\n"       << "Widgets accepted from supplier:" << setw(7) << totalAccepted << endl       << "Widgets returned to supplier  :" << setw(7) << totalRejected << endl       << "Widgets sent to customers     :" << setw(7) << totalSent << endl       << "Max widgets pending           :" << setw(7) << maxPending << endl       << "Number of widgets in inventory:" << setw(7) << inStock << endl;}void helpCmd() {  cout << "--------------------------------------\n"       << "Choose from the following commands: \n"       << listChar << " --- list the current inventory level;\n"       << reqChar << " --- handle a request form the customer;\n"       << shipChar << " --- handle a shipment from the supplier;\n"       << quitChar << " --- terminate the program;\n"       << helpChar << " --- display this help message.\n"       << "--------------------------------------\n";}void reqCmd(int &inStock, int &totalSent, int &maxPending, pending *&head, pending *&last, int &totalAccepted) {  int num;  string company;  cout << "Enter the name of the customer: ";  getline(cin, company);  cout << "Enter the request amount: ";  cin >> num;  if (num <= inStock && head == 0) {    cout << "--- " << num << " widgets have been sent to " << company << ".\n";    inStock -= num;    totalSent += num;  }  else {    pending *tmp = new pending;    tmp->name = company;    tmp->amount = (num - inStock);    tmp->next = 0;    if (head == 0)      head = tmp;    else      last->next = tmp;    last = tmp;    if (inStock > 0) {      cout << "--- " << (num - (num - inStock)) << " widgets have been sent to " << company << ".\n";      totalSent += (num - (num - inStock));    }    cout  << "--- " << (num - inStock) << " widgets requested by " << company << " have been entered "          << "into a pending list.\n";    inStock -= (num - (num - inStock));  }}void shipCmd(int &inStock, int &totalSent, int &totalAccepted, int &totalRejected, pending *&head) {  int num;  cout << "Enter the shipment amount: ";  cin >> num;  if (head == 0) {    if (num <= maxInventory - inStock) {      inStock += num;     totalAccepted += num;      cout << "--- " << num << " widgets have been stored.\n\n";    }    else {      if ((maxInventory - inStock) == 0) {        cout << "--- " << num << " widgets have been sent back due to"             << " lack of storage space.\n\n";        totalRejected += num;      }      else {        cout << "--- " << (maxInventory - inStock) << " widgets have been stored.\n"             << "--- " << (num - (maxInventory - inStock)) << " widgets have been sent"             << " back due to lack of storage space.\n\n";        totalRejected += num - (maxInventory - inStock);      }      inStock += (maxInventory - inStock);      totalAccepted += (maxInventory - inStock);    }  }  else {    while (head != 0 && num != 0) {      if ((head->amount) >= num) {        cout << "--- " << num << " widgets have been sent to " << head->name << ".\n";        head->amount -= num;        totalSent += num;        totalAccepted += num;        num -= num;        if (head->amount == 0) {          pending *tmp = head->next;          delete head;          head = tmp;        }      }      else {        cout << "--- " << (num - (num - head->amount)) << " widgets have been sent to "             << head->name << ".\n";        totalSent += (num - (num - head->amount));        totalAccepted += (num - (num - head->amount));        num -= (num - (num - head->amount));        pending *tmp = head->next;        delete head;        head = tmp;      }    }   if (num > 0) {      if (num <= (maxInventory - inStock)) {        cout << "--- " << num << " widgets have been stored.\n";        inStock += num;        totalAccepted += num;      }      else {        cout << "--- " << (maxInventory - inStock) << " widgets have "             << "been stored.\n"             << "--- " << (num - (maxInventory - inStock))             << " have been sent back due to lack of storage space.\n";        inStock += (maxInventory - inStock);        totalAccepted += (maxInventory - inStock);        totalRejected += (num - (maxInventory - inStock));      }    }  }}void listCmd(int inStock, pending *head, int &maxPending, int &count) {  cout << "--- Number of widgets in inventory:"       << setprecision(2) << setw(8) << inStock << endl       << "--------------------\n";  if (head == 0) {    cout << "--- No pending requests.\n"         << "--------------------\n";  }  else {    countPending(head, count, maxPending);    cout << "--- Number of requests pending:" << setw(8) << count << endl;    while (head != 0) {      cout << "--- " << head->amount << " widgets need to be sent to "           << head->name << endl;      head = head->next;    }    cout << "--------------------\n";  }}