Ai
1 Star 0 Fork 0

ubuntuvim/algorithm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
list_demo.js 2.05 KB
一键复制 编辑 原始数据 按行查看 历史
ubuntuvim 提交于 2015-07-15 10:58 +08:00 . save to git...
//一个基于列表的应用
// 为了展示如何使用列表,我们将实现一个类似 Redbox 的影碟租赁自助查询系统。
// 读取文件的内容(模拟读取数据库)并以换行分割
// var movies = read(films.txt).split("\n");
// 防止查询时候空格的影响,去掉数组元素的空格
// function removeSpace(list) {
// for (var i = 0; i < list.length; i++) {
// list[i] = list[i].trim();
// }
// }
// 去掉元素的空格
// removeSpace(movies);
// 初始化数据
function initData(fileName) {
var arr = read(fileName).split("\n");
for (var i = 0; i < arr.length; i++) {
arr[i] = arr[i].trim();
}
return arr;
}
// 定义一个客户对象
function Customer(name, movie) {
this.name = name;
this.movie = movie;
}
// 显示所有的movie
function displayList(list) {
for (lit.front(); list.currPos() < list.length(); list.next()) {
if (list.getElement() instanceof Customer) {
print(list.getElement()['name'] + ", " + list.getElement()['movie']);
} else {
print(list.getElement());
}
}
}
// 客户查看哪些movie可以租赁
function checkOut(name, movie, filmList, customerList) {
if (moviesList.contains(movie)) { //判断要租赁的movie是否存在
var c = new Customer(name, movie); //
customerList.append(c); // 存放已经出租的movie
filmList.remove(movie); //从可出租的列表中移除
} else {
print(movie + " is not available.");
}
}
// 测试
var movies = initData("films.txt");
// 使用自定义的List类,并初始化值
var moviesList = new List();
for (var i = 0; i < movies.length; i++) {
moviesList.append(movies[i]);
}
var customerList = new List();
print("Available movies: \n");
displayList(moviesList);
putstr("\nEnter your name: ");
var name = readline(); //读取客户名字
putstr("What movie would you like ?");
var movie = readline();
checkOut(name, movie, moviesList, customerList);
print("\n Customer Rentals: \n");
displayList(customerList);
print("\nmovies now available: \n");
displayList(moviesList);
// 测试的时候复制到myListTools.js最后
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/ubuntuvim/algorithm.git
git@gitee.com:ubuntuvim/algorithm.git
ubuntuvim
algorithm
algorithm
master

搜索帮助