2015年6月19日金曜日

開発環境

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

Exercises 5.(No. 2421)

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  : true
*/

var List = function () {
        this.list_size = 0;
        this.pos = 0;
        this.data_store = [];
    };

List.prototype.append = function (element) {    
    this.data_store[this.list_size] = element;
    this.list_size += 1;
};
List.prototype.find = function (element) {
    var i,
        max;
    
    for (i = 0, max = this.data_store.length; i < max; i += 1) {
        if (this.data_store[i] === element) {
            return i;
        }
    }
    return -1;
};
List.prototype.remove = function (element) {
    var found_at = this.find(element);

    if (found_at > -1) {
        this.data_store.splice(found_at, 1);
        this.list_size -= 1;
        return true;
    }
    return false;
};
List.prototype.length = function () {
    return this.list_size;
};
List.prototype.toString = function () {
    return this.data_store;
};
List.prototype.insert = function (element, after) {
    var insert_pos = this.find(after);
    if (insert_pos > -1) {
        this.data_store.splice(insert_pos + 1, 0, element);
        this.list_size += 1;
        return true;
    }
    return false;
};
List.prototype.clear = function () {
    this.data_store = [];
    this.list_size = this.pos = 0;
};
List.prototype.contains = function (element) {
    var i,
        max;
    for (i = 0, max = this.data_store.length; i < max; i += 1) {
        if(this.data_store[i] === element) {
            return true;
        }
    }
    return false;
};
List.prototype.moveTo = function (pos) {
    this.pos = pos;
};
List.prototype.getElement = function () {
    return this.data_store[this.pos];
};
List.prototype.previous = function () {
    this.pos -= 1;
    return this.data_store[this.pos];
};
List.prototype.next = function () {
    var element = this.data_store[this.pos];
    this.pos += 1;
    return element;
};
List.prototype.hasNext = function () {
    if (this.pos > this.list_size - 1) {
        return false;
    }
    return true;
};
List.prototype.hasPrevious = function () {
    if (this.ps <= 0) {
        return false;
    }
    return true;
};
List.prototype.front = function () {
    this.pos = 0;
};
List.prototype.end = function () {
    this.pos = this.list_size - 1;
};
List.prototype.currPos = function () {
    return this.pos;
};
List.prototype.displayList = function () {
    this.front();
    while (this.hasNext()) {
        print(this.next().toString());
    }
};

var movies,
    movieList = new List(),
    customers = new List(),
    Customer = function (name, movie) {
        this.name = name;
        this.movie = movie;
    },
    rentedMovieList = new List(),
    checkOut = function (name, movie, movieList, customerList,
                         rentedMovieList) {        
        var c;        
        if (movieList.contains(movie)) {
            c = new Customer(name, movie);
            customerList.append(c);
            movieList.remove(movie);
            rentedMovieList.append(movie);
            print('rented movie list');
            rentedMovieList.displayList();
        } else {
            print(movie + ' is not available.');
        }
    },
    checkIn = function (movie, movieList, rentedMovieList) {
        if (rentedMovieList.contains(movie)) {
            rentedMovieList.remove(movie);
            movieList.append(movie);
        }
    },
    createAttr = function () {
        var arr = ['The Shawshank Redemption',
                   'The Godfather',
                   'The Godfather: Part II',
                   'Pulp Fiction',
                   'The Good, the Bad and the Ugly'];
        return arr;
    },
    i,
    max;    

Customer.prototype.toString = function () {
    return this.name + ', ' + this.movie;
};

movies = createAttr();
for (i = 0, max = movies.length; i < max; i += 1) {
    movieList.append(movies[i]);
}

checkOut("Jane Doe", "The Godfather", movieList, customers, rentedMovieList);
checkOut('Pulp Fiction', 'The Godfather: Part II', movieList, customers,
         rentedMovieList);
checkOut("Jane Doe", "The Godfather", movieList, customers, rentedMovieList);
checkOut("Jane Doe", "The Good, the Bad and the Ugly", movieList, customers,
         rentedMovieList);
print('check in --------------------------------------------------');
checkIn('The Godfather', movieList, rentedMovieList);
print('movie list');
movieList.displayList();
print('rented movie list');
rentedMovieList.displayList();

出力結果(Terminal, shell, SpiderMonkey)

$ js sample5.js
rented movie list
The Godfather
rented movie list
The Godfather
The Godfather: Part II
The Godfather is not available.
rented movie list
The Godfather
The Godfather: Part II
The Good, the Bad and the Ugly
check in --------------------------------------------------
movie list
The Shawshank Redemption
Pulp Fiction
The Godfather
rented movie list
The Godfather: Part II
The Good, the Bad and the Ugly
$

0 コメント:

コメントを投稿

Comments on Google+: