2015年7月10日金曜日

開発環境

Data Structures and Algorithms With Javascript (Michael McMillan(著)、O'Reilly Media)のChapter 5(Queues)、Exercises 4.(No. 3595)を解いてみる。

Exercises 4.(No. 3595)

JavaScript(Emacs)

/*jslint         browser : true, continue : true,
  devel  : true, indent  : 4,    maxerr   : 50,
  newcap : true, nomen   : true, plusplus : false,
  regexp : true, sloppy  : true, vars     : false,
  white  : false
*/

/*global print, readline, quit */
var Queue = function () {
        this.data_store = [];
    },
    Patient = function (name, code) {
        this.name = name;
        this.code = code;
    },
    ed = new Queue(),
    control,
    name,
    code,
    seen;

Queue.prototype.enqueue = function (element) {
    this.data_store.push(element);
};
Queue.prototype.dequeue = function () {
    return this.data_store.shift();
};
Queue.prototype.front = function () {
    return this.data_store[0];
};
Queue.prototype.back = function () {
    return this.data_store[this.data_store.length - 1];
};
Queue.prototype.toString = function () {
    var result = '',
        i,
        max;
    for (i = 0, max = this.data_store.length; i < max - 1; i += 1) {
        result += this.data_store[i] + ',';
    }
    result += this.data_store[i];
    return result;
};
Queue.prototype.isEmpty = function () {
    return this.data_store.length === 0 ? true : false;
};
Queue.prototype.count = function () {
    return this.data_store.length;
};

ed.dequeue = function () {
    var entry = 0,
        i,
        max;

    for (i = 0, max = this.data_store.length; i < max; i += 1) {
        if (this.data_store[i].code > this.data_store[entry].code) {
            entry = i;
        }
    }
    return this.data_store.splice(entry, 1);
};

ed.toString = function () {
    var result = '',
        i,
        max;

    for (i = 0, max = this.data_store.length; i < max; i += 1) {
        result += this.data_store[i].name + ' code: '
            + this.data_store[i].code + '\n';
    }
    return result;
};

print('control: ');
control = readline();
if (control === 'quit') {
    quit();
}
do {
    if (control === 'a') {
        print('name: ');
        name = readline();
        print('code: ');
        code = readline();
        ed.enqueue(new Patient(name, code));
    } else if (control === 'b') {
        seen = ed.dequeue();
        print('Patient being treated: ' + seen[0].name);
    } else if (control === 'c') {
        print('Patients waiting to be seen:');
        print(ed.toString());
    } else {
        print('Unknown control');
    }
    print('control: ');
    control = readline();
} while (control !== 'quit');

出力結果(Terminal, shell, SpiderMonkey)

$ jslint sample4.js

sample4.js is OK.
$ js sample4.js
control: 
a
name: 
Smith
code: 
5
control: 
a
name: 
Jones
code: 
4
control: 
a
name: 
Fhrenbach
code: 
6
control: 
a
name: 
Brown
code: 
1
control: 
a
name: 
Ingram
code: 
1
control: 
c
Patients waiting to be seen:
Smith code: 5
Jones code: 4
Fhrenbach code: 6
Brown code: 1
Ingram code: 1

control: 
b
Patient being treated: Fhrenbach
control: 
c
Patients waiting to be seen:
Smith code: 5
Jones code: 4
Brown code: 1
Ingram code: 1

control: 
b
Patient being treated: Smith
control: 
c
Patients waiting to be seen:
Jones code: 4
Brown code: 1
Ingram code: 1

control: 
b
Patient being treated: Jones
control: 
c
Patients waiting to be seen:
Brown code: 1
Ingram code: 1

control: 
b
Patient being treated: Brown
control: 
c
Patients waiting to be seen:
Ingram code: 1

control: 
quit
$ 

0 コメント:

コメントを投稿