• [C++]ATM基础版


    头文件

     1 #pragma once
     2 #ifndef QUEUE_H_
     3 #define QUEUE_H_
     4 class Customer {
     5 private:
     6     long arrive;
     7     int processtime;
     8 public:
     9     Customer() { arrive = processtime = 0; }
    10     void set(long when);
    11     long when() const { return arrive; }
    12     int ptime() const { return processtime; }
    13 };
    14 typedef Customer Item;
    15 class Queue
    16 {
    17 private:
    18     struct Node
    19     {
    20         Item item;
    21         struct Node* next;
    22     };
    23     enum { Q_SIZE = 10 };
    24     Node* front;
    25     Node* rear;
    26     int items;
    27     const int qsize;
    28     Queue(const Queue& q):qsize(0){}
    29     Queue& operator=(const Queue& q) { return *this; }
    30 public:
    31     Queue(int qs = Q_SIZE);
    32     ~Queue();
    33     bool isempty()const;
    34     bool isfull()const;
    35     int queuecount()const;
    36     bool enqueue(const Item& item);
    37     bool dequeue(Item& item);
    38 };
    39 #endif

    类方法实现文件

     1 #include "pch.h"
     2 #include "queue.h"
     3 #include <cstdlib>
     4 Queue::Queue(int qs) :qsize(qs) {
     5     front = rear = nullptr;
     6     items = 0;
     7 }
     8 Queue::~Queue() {
     9     Node* temp;
    10     while (front!=nullptr)
    11     {
    12         temp = front;
    13         front = front->next;
    14         delete temp;
    15     }
    16 }
    17 bool Queue::isempty()const {
    18     return items == 0;
    19 }
    20 bool Queue::isfull()const {
    21     return items == qsize;
    22 }
    23 int Queue::queuecount()const {
    24     return items;
    25 }
    26 bool Queue::enqueue(const Item& item) {
    27     if (isfull())
    28         return false;
    29     Node* add = new Node;
    30     add->item = item;
    31     add->next = nullptr;
    32     items++;
    33     if (front == nullptr)
    34         front = add;
    35     else
    36         rear->next = add;
    37     rear = add;
    38     return true;
    39 }
    40 bool Queue::dequeue(Item& item) {
    41     if (front == nullptr)
    42         return false;
    43     item = front->item;
    44     items--;
    45     Node* temp;
    46     temp = front;
    47     front = front->next;
    48     delete temp;
    49     if (items == 0)
    50         rear = nullptr;
    51     return true;
    52 }
    53 void Customer::set(long when) {
    54     processtime = std::rand() % 3 + 1;
    55     arrive = when;
    56 }

    主函数调用类

     1 #include "pch.h"
     2 #include <iostream>
     3 #include <string>
     4 #include <cstring>
     5 #include <cstdlib>
     6 #include <ctime>
     7 #include "queue.h"
     8 using namespace std;
     9 const int MIN_PER_HR = 60;
    10 bool newcustomer(double x);
    11 int main(void) {
    12     srand(time(0));
    13     cout << "Case Study: Bank of Heather Automatic Teller
    ";
    14     cout << "Enter maximum size of queue: ";
    15     int qs;
    16     cin >> qs;
    17     Queue line(qs);
    18     cout << "Enter the number of simulation hours: ";
    19     int hours;
    20     cin >> hours;
    21     long cyclelimit = MIN_PER_HR * hours;
    22     cout << "Enter the average number of customers per hour: ";
    23     double perhour;
    24     cin >> perhour;
    25     double min_per_cust;
    26     min_per_cust = MIN_PER_HR / perhour;
    27     Item temp;
    28     long turnaways = 0;
    29     long customers = 0;
    30     long served = 0;
    31     long sum_line = 0;
    32     int wait_time = 0;
    33     long line_wait = 0;
    34     for (int cycle = 0; cycle < cyclelimit; cycle++) {
    35         if (newcustomer(min_per_cust)) {
    36             if (line.isfull())
    37                 turnaways++;
    38             else {
    39                 customers++;
    40                 temp.set(cycle);
    41                 line.enqueue(temp);
    42             }
    43         }
    44         if (wait_time <= 0 && !line.isempty()) {
    45             line.dequeue(temp);
    46             wait_time = temp.ptime();
    47             line_wait += cycle - temp.when();
    48             served++;
    49         }
    50         if (wait_time > 0)
    51             wait_time--;
    52         sum_line += line.queuecount();
    53     }
    54     if (customers > 0) {
    55         cout << "customer accepted: " << customers << endl;
    56         cout << "  customers served: " << served << endl;
    57         cout << "         turnaways: " << turnaways << endl;
    58         cout << "average queue size: ";
    59         cout.precision(2);
    60         cout.setf(ios_base::fixed, ios_base::floatfield);
    61         cout << (double)sum_line / cyclelimit << endl;
    62         cout << " average wait time: " << (double)line_wait / served << " minutes
    ";
    63     }
    64     else
    65         cout << "No customers!
    ";
    66     cout << "Done!
    ";
    67     return 0;
    68 }
    69 bool newcustomer(double x) {
    70     return rand()*x / RAND_MAX < 1;
    71 }

    IDE:VS2017

  • 相关阅读:
    2.采用字符的移位方式实现字符文本加密解密。
    用自己的算法实现startsWith和endsWith功能。
    采用多种算法,模拟摇奖:从1-36中随机抽出8个不重复的数字
    输入5个数用冒泡排序进行从小到大排列
    题目六:控制台输入年龄,根据年龄输出不同的提示
    题目五:控制台输出三角形和菱形
    题目四:控制台输出九九乘法表
    百马百担
    classNum 表示学生的班号,例如“class05”。 有如下List  List list = new ArrayList();
    已知有十六支男子足球队参加2008 北京奥运会。写一个程序,把这16 支球队随机分为4 个组。采用List集合和随机数     2008 北京奥运会男足参赛国家:  科特迪瓦,阿根廷,澳大利亚,塞尔维亚,荷兰,尼日利亚、日本,美国,中国,新西 兰,巴西,比利时,韩国,喀麦隆,洪都拉斯,意大利
  • 原文地址:https://www.cnblogs.com/lightmonster/p/10419777.html
Copyright © 2020-2023  润新知