zoukankan      html  css  js  c++  java
  • 《Cracking the Coding Interview》——第8章:面向对象设计——题目3

    2014-04-23 18:10

    题目:设计一个点唱机。

    解法:英文叫Musical Jukebox。这是点唱机么?卡拉OK么?这种题目实在是云里雾里,又没有交流的余地,我索性用一个vector来表示一首曲子吧。不过有一点是肯定的,点播行为和播放行为应该分不同线程操作,这样就不至于让机器一直卡在那儿等你点歌了。

    代码:

     1 // 8.3 Design a data structure to simulate a musical jukebox.
     2 #include <iostream>
     3 #include <queue>
     4 #include <string>
     5 #include <thread>
     6 #include <vector>
     7 using namespace std;
     8 
     9 class JukeBox {
    10 public:
    11     JukeBox() {};
    12     
    13     void start() {
    14         work = new thread(workThread, this);
    15     };
    16 
    17     void addSong(vector<int> &song) {
    18         songs.push_back(song);
    19     }
    20     
    21     void orderSong(int id) {
    22         if (id < 0 || id > (int)songs.size() - 1) {
    23             return;
    24         }
    25         song_list.push(id);
    26     }
    27 
    28     friend void workThread(JukeBox *);
    29 
    30     ~JukeBox() {
    31         while (!song_list.empty()) {
    32             song_list.pop();
    33         }
    34         size_t i;
    35         for (i = 0; i < songs.size(); ++i) {
    36             songs[i].clear();
    37         }
    38         songs.clear();
    39         work->detach();
    40         delete work;
    41     };
    42 private:
    43     queue<int> song_list;
    44     vector<vector<int> > songs;
    45     thread *work;
    46 };
    47 
    48 void workThread(JukeBox *jb) {
    49     size_t i;
    50     while (true) {
    51         if (!jb->song_list.empty()) {
    52             for (i = 0; i < jb->songs[jb->song_list.front()].size(); ++i) {
    53                 cout << jb->songs[jb->song_list.front()][i] << ' ';
    54             }
    55             cout << endl;
    56             jb->song_list.pop();
    57         }
    58     }
    59 };
    60 
    61 int main()
    62 {
    63     JukeBox *jb;
    64     string cmd;
    65     int id;
    66     int i, n;
    67     vector<int> song;
    68 
    69     jb = new JukeBox();
    70     jb->start();
    71     while (cin >> cmd) {
    72         if (cmd == "add") {
    73             cin >> n;
    74             song.resize(n);
    75             for (i = 0; i < n; ++i) {
    76                 cin >> song[i];
    77             }
    78             jb->addSong(song);
    79         } else if (cmd == "order") {
    80             cin >> id;
    81             jb->orderSong(id);
    82         } else if (cmd == "end") {
    83             break;
    84         }
    85     }
    86     delete jb;
    87 
    88     return 0;
    89 }
  • 相关阅读:
    【剑指Offer】6、旋转数组的最小数字
    【剑指Offer】5、用两个栈实现队列
    【剑指Offer】4、重建二叉树
    python面试经典315
    shell面试经典70例
    vim编辑器使用
    bootstrap3基本了解
    Windows Server 2008允许多用户登录远程桌面
    配置远程桌面
    Python-Python及PyCharm的下载与安装
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3683543.html
Copyright © 2011-2022 走看看