diff --git a/9781449364939_files.zip b/9781449364939_files.zip deleted file mode 100644 index a5d069d..0000000 Binary files a/9781449364939_files.zip and /dev/null differ diff --git a/Chapter1/test.js b/Chapter1/test.js new file mode 100644 index 0000000..465f533 --- /dev/null +++ b/Chapter1/test.js @@ -0,0 +1,37 @@ +// function factorial(number){ +// if(number == 1){ +// return number; +// }else{ +// return number * factorial(number-1); +// } +// } +// print(factorial(5)); + +// function compare(num1, num2) { +// return num1 - num2; +// } +// var nums = [3,1,2,100,4,200]; +// nums.sort(compare); +// print(nums); // 1,2,3,4,100,200 + + +// Array.matrix = function(numrows,numcols,initial){ +// var arr = []; +// for(var i=0;i"); + for(var j=0;j 0){ + var v = queue.shift();//从队首移除 + if(v != undefined){ + print("Visited vertex: " + v); + } + for each(var w in this.adj[v]){ + if(!this.marked[w]){ + this.edgeTo[w] = v; + this.marked[w] = true; + queue.push(w); + } + } + } +} + +function pathTo(v){ + var source = 0; + if(!this.hasPathTo(v)){ + return undefined; + } + var psth = []; + for(var i=v;i != source;i=this.edgeTo[i]){ + path.push(i); + } + path.push(s); + return path; +} +function hasPathTo(v){ + return this.marked[v]; +} + +//拓扑排序算法 优先级约束调度 +/*拓扑排序算法与深度优先搜索类似。不同的是,拓扑排序算法不会立即输出已访问的顶 +点,而是访问当前顶点邻接表中的所有相邻顶点,直到这个列表穷尽时,才将当前顶点压 +入栈中。*/ \ No newline at end of file diff --git a/Chapter11/test.js b/Chapter11/test.js new file mode 100644 index 0000000..4b314c1 --- /dev/null +++ b/Chapter11/test.js @@ -0,0 +1,28 @@ +load('Graph.js'); +g = new Graph(5); +g.addEdge(0,1); +g.addEdge(0,2); +g.addEdge(1,3); +g.addEdge(2,4); +// g.showGraph(); + +/* +0-> 1 2 +1-> 0 3 +2-> 0 4 +3-> 1 +4-> 2 +*/ + +// g.dfs(0); +// g.bfs(0); + +var vertex = 4; +var paths = g.pathTo(vertex); +while (paths.length > 0) { + if (paths.length > 1) { + putstr(paths.pop() + '-'); + }else { + putstr(paths.pop()); + } +} diff --git a/Chapter12/CArray.js b/Chapter12/CArray.js new file mode 100644 index 0000000..a5fe9ea --- /dev/null +++ b/Chapter12/CArray.js @@ -0,0 +1,225 @@ +function CArray(numElements){//数组测试平台类 + this.dataStore = []; + this.pos = 0; + this.numElements = numElements;//想要的元素数 + this.insert = insert; + this.toString = toString; + this.clear = clear; + this.setData = setData; + this.swap = swap;//用于交换数组元素 + for(var i=0;i0 & i%10==0){ + retstr += "\n"; + } + } + return retstr; +} + +function swap(arr,index1,index2){ + var temp = arr[index1]; + arr[index1] = arr[index2]; + arr[index2] = temp; +} + +//冒泡排序(最慢的排序算法之一,但也是最容易实现的排序算法) +/*算法会多次在数组中移动,比较相邻的数据,当左侧值大于右侧值时将它们进行互换*/ +function bubbleSort(){ + var numElements = this.dataStore.length; + var temp; + for(var outer=numElements;outer >= 2;--outer){ + for(var inner=0;inner <= outer-1;++inner){ + if(this.dataStore[inner] > this.dataStore[inner+1]){ + swap(this.dataStore,inner,inner+1); + } + } + // print(this.toString());//为了能看到排序过程 + } +} + +//选择排序 +/*选择排序从数组的开头开始,先找到最小的放第一位,然后是次小放第二位*/ +function selectionSort(){ + var min,temp; + for(var outer=0;outer <= this.dataStore.length-2;++outer){ + min = outer; + for (var inner = outer + 1; + inner <= this.dataStore.length-1; ++inner) { + if(this.dataStore[inner] < this.dataStore[min]){ + min = inner; + } + } + swap(this.dataStore,outer,min); + // print(this.toString()); + } +} + +//插入排序 +/*通过将较大的数组元素移动到右侧,为数组左侧的较小元素腾出位置。*/ +function insertionSort(){ + var temp,inner; + for(var outer=1;outer<=this.dataStore.length-1;++outer){ + temp = this.dataStore[outer]; + inner = outer; + while(inner > 0 && (this.dataStore[inner-1] >= temp)){ + this.dataStore[inner] = this.dataStore[inner-1]; + --inner; + } + this.dataStore[inner] = temp; + // print(this.toString()); + } +} + + +//高级排序算法 +//希尔排序 /*核心理念与插入排序不同,它会首先比较距离较远的元素,而非相邻的元素*/ +function shellsort(){ + for(var g=0;g= this.gaps[g] && this.dataStore[j-this.gaps[g]]>temp;j -= this.gaps[g]){ + this.dataStore[j] = this.dataStore[j-this.gaps[g]]; + } + this.dataStore[j] = temp; + } + print("间隔"+ this.gaps[g] + ": " + this.toString()); + } +} +/*动态间隔*/ +function shellsort1(){ + var N = this.dataStore.length; + var h = 1; + while(h < N/3){ + h = 3*h+1; + } + while(h >= 1){ + for(var i=h;i=h && this.dataStore[j] -1) { this.dataStore.splice(foundAt,1); - --this.listSize; + --this.listSize;//数组改变后,listSize减1,以反映列表的最新长度 return true; } return false; } - +//返回列表中元素的个数 +function length(){ + return this.listSize; +} +//显示列表中的元素 function toString() { return this.dataStore; } +//向列表中插入一个元素到after之后 +function insert(element,after){ + var insertPos = this.find(after); + if(insertPos > -1){ + this.dataStore.splice(insertPos,0,element); + ++this.listSize; + return true; + } + return false; +} +//清空列表中所有的元素 +function clear(){ + delete this.dataStore; + this.dataStore = []; + this.listSize = this.pos = 0;//表明这个一个新的空列表 +} +//判断给定值是否在列表中 +function contains(element){ + for(var i=0;i 0){ + --this.pos; + } +} +function next(){ + if(this.pos < this.listSize){ + ++this.pos; + } +} +function currPos(){ + return this.pos; +} +function moveTo(position){ + this.pos = position; +} +//返回列表的当前元素 +function getElement(){ + return this.dataStore[this.pos]; +} var names = new List(); names.append("Cynthia"); names.append("Raymond"); names.append("Barbara"); print(names.toString()); -names.remove("Raymond"); -print(names.toString()); +// names.remove("Raymond"); +// print(names.toString()); +// names.front(); +// print(names.getElement()); +print(names.front()); +names.next(); +// print(names.getElement()); +print(names.currPos()); +print(names.front()); +print(names.length()); + +function displayList(list){ + for(list.front();list.currPos() -1) { + this.dataStore.splice(foundAt,1); + --this.listSize;//数组改变后,listSize减1,以反映列表的最新长度 + return true; + } + return false; +} +//返回列表中元素的个数 +function length(){ + return this.listSize; +} +//显示列表中的元素 +function toString() { + return this.dataStore; +} +//向列表中插入一个元素到after之后 +function insert(element,after){ + var insertPos = this.find(after); + if(insertPos > -1){ + this.dataStore.splice(insertPos,0,element); + ++this.listSize; + return true; + } + return false; +} +//清空列表中所有的元素 +function clear(){ + delete this.dataStore; + this.dataStore = []; + this.listSize = this.pos = 0;//表明这个一个新的空列表 +} +//判断给定值是否在列表中 +function contains(element){ + for(var i=0;i 0){ + --this.pos; + } +} +function next(){ + if(this.pos < this.listSize){ + ++this.pos; + } +} +function currPos(){ + return this.pos; +} +function moveTo(position){ + this.pos = position; +} +//返回列表的当前元素 +function getElement(){ + return this.dataStore[this.pos]; +} +// var movies = read('./films.txt').split("\n"); +// print(movies); +var movies = createArr("films.txt"); +//当读进来的内容被分割成数组后,换行符被替换成空格。 +function createArr(file){ + var arr = read(file).split("\n"); + for(var i=0;i0); + var converted=""; + while(s.length()>0){ + converted+=s.pop(); + } + return converted; +} + +// print(mulBase(13,2)); + +// print(13/2);//6 +// print(Math.floor(13/2));//6.5 + +//测试将数字转换为2进制和8进制 +// var num = 32; +// var base = 2; +// var newNum = mulBase(num, base); +// print(num + " converted to base " + base + " is " + newNum); +// num = 125; +// base = 8; +// var newNum = mulBase(num, base); +// print(num + " converted to base " + base + " is " + newNum); + + +//判断给定字符串是否是回文 +function isPalindrome(word){ + var s = new Stack(); + for(var i=0;i0){ + rword+=s.pop(); + } + if(word == rword){ + return true; + }else{ + return false; + } +} + +//测试 +// var word = "hello"; +// if (isPalindrome(word)) { +// print(word + " is a palindrome."); +// } +// else { +// print(word + " is not a palindrome."); +// } +// word = "racecar" +// if (isPalindrome(word)) { +// print(word + " is a palindrome."); +// } +// else { +// print(word + " is not a palindrome."); +// } + +//阶乘函数的递归定义 +function factorial(n){ + if(n===0){ + return 1; + }else{ + return n * factorial(n-1); + } +} +// print(factorial(5));//120 +//使用栈模拟递归 +function fact(n){ + var s = new Stack(); + while(n>1){ + s.push(n--); + } + var product = 1; + while(s.length()>0){ + product *= s.pop(); + } + return product; +} +print(fact(5));//120 \ No newline at end of file diff --git a/Chapter5/test.js b/Chapter5/test.js new file mode 100644 index 0000000..1a2a5bc --- /dev/null +++ b/Chapter5/test.js @@ -0,0 +1,48 @@ +function Queue(){ + this.dataStore = []; + this.enqueue = enqueue;//向队尾添加一个元素 + this.dequeue = dequeue;//从队首删除一个元素 + this.front =front;//读取队首元素 + this.back = back;//读取队尾元素 + this.toString = toString;//显示队列内的所有元素 + this.empty = empty;//判断队列是否为空 +} +function enqueue(element){ + this.dataStore.push(element); +} +function dequeue(){ + person = this.front(); + this.dataStore.shift(); + return person; +} +function front(){ + return this.dataStore[0]; +} +function back(){ + return this.dataStore[this.dataStore.length-1]; +} +function toString(){ + var retStr = ""; + for(var i=0;i 0) { + print("There are " + maleDancers.count() + +" male dancers waiting to dance."); +} +if (femaleDancers.count() > 0) { + print("There are " + femaleDancers.count() + +" female dancers waiting to dance."); +} \ No newline at end of file diff --git a/Chapter6/chap6-3.txt b/Chapter6/chap6-3.txt index bbd8142..2bd094d 100644 --- a/Chapter6/chap6-3.txt +++ b/Chapter6/chap6-3.txt @@ -19,10 +19,18 @@ function remove(item) { } } -function findPrevious(item) { +// function findPrevious(item) { +// var currNode = this.head; +// while (!(currNode.next == null) && +// (currNode.next.element != item)) { +// currNode = currNode.next; +// } +// return currNode; +// } + +function findPrevious(item){ var currNode = this.head; - while (!(currNode.next == null) && - (currNode.next.element != item)) { + while( (currNode.next != null) && (currNode.next.element != item) ){ currNode = currNode.next; } return currNode; diff --git "a/Chapter6/\345\215\225\351\223\276\350\241\250.js" "b/Chapter6/\345\215\225\351\223\276\350\241\250.js" new file mode 100644 index 0000000..dd7faa3 --- /dev/null +++ "b/Chapter6/\345\215\225\351\223\276\350\241\250.js" @@ -0,0 +1,69 @@ +//设计一个基于对象的链表 + +//Node类用来表示节点 +function Node(element){ + this.element = element;//保存节点上的数据 + this.next = null;//保存指向下一个节点的链接 +} +//LinkedList类提供了插入节点、删除节点、显示列表元素的方法等 +function LList(){ + this.head = new Node("head");//头节点 + this.find = find;//在列表中查找给定的值 + this.insert = insert;//插入节点 + this.remove = remove;//删除节点 + this.display = display;//显示链表中的元素 + this.findPrevious = findPrevious; +} +//find检查每一个节点是否等于待查找数据 +function find(item){ + var currNode = this.head; + while(currNode.element != item){/********************************************/ + currNode = currNode.next; + } + return currNode; +} + +//在已知节点item后面插入元素 +function insert(newElement,item){ + var newNode = new Node(newElement); + var current = this.find(item); + newNode.next = current.next; + current.next = newNode; +} +//显示链表中的元素 +function display(){ + var currNode = this.head; + while(!(currNode.next == null)){ + print(currNode.next.element); + currNode = currNode.next; + } +} + +//删除一个节点 首先要找到待删除节点前面的节点 + +//findPrevious()遍历链表中的元素,检查每一个节点的下一个节点中是否存储着待删除数据, +// 如果找到,返回该节点(即前一个节点) +function findPrevious(item){ + var currNode = this.head; + while(!(currNode.next == null) && (currNode.next.element != item) ){/*************************/ + currNode = currNode.next; + } + return currNode; +} +function remove(item){ + var prevNode = this.findPrevious(item);/***之前遗漏掉了this. 结果一直报错TypeError: currNode is undefined*****/ + if(!(prevNode.element == null)){ + prevNode.next = prevNode.next.next; + } +} + +//测试 +var cities = new LList(); +cities.insert("Conway","head"); +cities.insert("Russellville", "Conway"); +cities.insert("Alma", "Russellville"); +cities.insert("Carlisle", "Russellville"); +cities.display();//Conway Russellville Alma +console.log(); +cities.remove("Carlisle"); +cities.display(); \ No newline at end of file diff --git "a/Chapter6/\345\217\214\345\220\221\351\223\276\350\241\250.js" "b/Chapter6/\345\217\214\345\220\221\351\223\276\350\241\250.js" new file mode 100644 index 0000000..54f7dac --- /dev/null +++ "b/Chapter6/\345\217\214\345\220\221\351\223\276\350\241\250.js" @@ -0,0 +1,78 @@ +function Node(element){ + this.element = element; + this.next = null; + this.previous = null; +} +function LList(){ + this.head = new Node('head'); + this.insert = insert; + this.find = find; + this.remove = remove; + this.findLast = findLast;//查找链表中的最后节点 + this.dispReverse = dispReverse;//反序显示双向链表中的元素 + this.display = display; +} +function find(item){ + var currNode = this.head; + while(currNode.element != item){ + currNode = currNode.next; + } + return currNode; +} + +function insert(newElement,item){ + var newNode = new Node(newElement); + var current = this.find(item); + newNode.next = current.next; + newNode.previous = current; + // current.next.previous = newNode; + current.next = newNode; +} +function remove(item){ + var currNode = this.find(item); + if(!(currNode.next == null)){ + currNode.previous.next = currNode.next; + currNode.next.previous = currNode.previous; + currNode.next = null; + currNode.previous = null; + } +} +//查找链表最后的节点 +function findLast(){ + var currNode = this.head; + while(!(currNode.next == null)){ + currNode = currNode.next; + } + return currNode; +} +// 反序显示双向链表中的元素 +function dispReverse(){ + var currNode = this.head; + currNode = this.findLast(); + while(!(currNode.previous == null)){ + print(currNode.element); + currNode = currNode.previous; + } +} + +function display(){ + var currNode = this.head; + while(!(currNode.next == null)){ + print(currNode.next.element); + currNode = currNode.next; + } +} + + +//测试 +var cities = new LList(); +cities.insert("Conway","head"); +cities.insert("Russellville", "Conway"); +cities.insert("Alma", "Russellville"); +cities.insert("Carlisle", "Russellville"); +cities.display();//Conway Russellville Alma +console.log(); +cities.remove("Carlisle"); +cities.display(); +print(); +cities.dispReverse(); \ No newline at end of file diff --git a/Chapter7/Dictionary.js b/Chapter7/Dictionary.js index 68c4c26..e42fe33 100644 --- a/Chapter7/Dictionary.js +++ b/Chapter7/Dictionary.js @@ -1,41 +1,41 @@ -function Dictionary() { - this.add = add; - this.datastore = new Array(); - this.find = find; - this.remove = remove; - this.showAll = showAll; - this.count = count; - this.clear = clear; +function Dictionary(){ + this.datastore = new Array(); + this.add = add;//添加一个键值对 + this.find = find;//以键作为参数,返回和其关联的值 + this.remove = remove;//同时删除键和与其关联的值 + this.showAll = showAll;//显示字典中的所有键-值对 + this.count = count;//返回字典中元素个数 + this.clear = clear;//情况字典中的元素 } - -function add(key, value) { - this.datastore[key] = value; +function add(key,value){ + this.datastore[key] = value; } - -function find(key) { - return this.datastore[key]; +function find(key){ + return this.datastore[key]; } - -function remove(key) { - delete this.datastore[key]; +function remove(key){ + delete this.datastore[key]; } - -function showAll() { - for each (var key in Object.keys(this.datastore).sort()) { - print(key + " -> " + this.datastore[key]); - } +function showAll(){ + var keys = Object.keys(this.datastore).sort(); //先将键进行排序 + var res = new Dictionary(); + for(var i=0;i " + this.datastore[keys[i]]); + } } - -function count() { - var n = 0; - for each (var key in Object.keys(this.datastore)) { - ++n; - } - return n; +function count(){ + // return Object.keys(this.datastore).length; + var n = 0; + for(var key in this.datastore){ + ++n; + } + return n; } - -function clear() { - for each (var key in Object.keys(this.datastore)) { - delete this.datastore[key]; - } +function clear(){ + for(var key in this.datastore){ + delete this.datastore[key]; + } } + + diff --git a/Chapter7/test.js b/Chapter7/test.js new file mode 100644 index 0000000..5736303 --- /dev/null +++ b/Chapter7/test.js @@ -0,0 +1,19 @@ +//使用Dictionary类 +load("Dictionary.js"); +var pbook = new Dictionary(); +pbook.add("Mike","123"); +pbook.add("Mike","13");//重写了datastore[Mike]的值为13 +pbook.add("David", "345"); +pbook.add("Cynthia", "456"); +pbook.add("Mike", "723"); +pbook.add("Jennifer", "987"); +pbook.add("Danny", "012"); +pbook.add("Jonathan", "666"); +print("Number of entries: " + pbook.count());//Number of entries: 3 +print("David's extension: " + pbook.find("David"));//David's extension: 345 +pbook.remove("David"); +pbook.showAll();//Mike -> 13 + //Cynthia -> 456 +pbook.clear(); +print("Number of entries: " + pbook.count());//Number of entries: 0 +pbook.showAll();//无输出 \ No newline at end of file diff --git "a/Chapter7/\346\265\213\350\257\225length\345\261\236\346\200\247.js" "b/Chapter7/\346\265\213\350\257\225length\345\261\236\346\200\247.js" new file mode 100644 index 0000000..e8dbb3b --- /dev/null +++ "b/Chapter7/\346\265\213\350\257\225length\345\261\236\346\200\247.js" @@ -0,0 +1,13 @@ +var nums = new Array(); +nums[0] = 1; +nums[1] = 2; +print(nums.length); // 显示 2 +print(nums);//1,2 + +//当键的类型为字符串的时候,length属性就不管用了 + +var pbook = new Array(); +pbook["David"] = 1; +pbook["Jennifer"] = 2; +print(pbook.length); // 显示 0 +print(pbook);//无输出 \ No newline at end of file diff --git a/Chapter8/Hash.js b/Chapter8/Hash.js new file mode 100644 index 0000000..4322e89 --- /dev/null +++ b/Chapter8/Hash.js @@ -0,0 +1,130 @@ +// 使用散列表存储数据时,通过一个散列函数将键映射为一个数字,这个数字的范围是0到散列表的长度 +// 需要解决的问题 + // 1.collision碰撞 即使使用一个高效的散列函数,依然存在将两个键映射成同一个值的可能 + // 2.对数组大小常见的限制是:数组长度应该是一个质数 +//散列函数的选择依赖于键值的数据类型,如果键是整型,最简单的散列函数就是以数组的长度对键取余 +// 字符串类型的散列函数,将字符串中每个字符的ASCII码值相加的和除以数组长度 取的余数就是散列值 + // 1.除留取余法 + +function HashTable(){ + this.table = new Array(137); + this.simpleHash = simpleHash;//计算散列值的方法 + this.showDistro = showDistro;//显示散列表数据分布 + this.betterHash = betterHash; + this.put = put;//向散列中插入数据 + this.get = get;//从散列表中读取数据 + this.buildChains = buildChains; +} +function simpleHash(data){ + var total = 0; + for(var i=0;i " + total); + return total % this.table.length; +} +//使用霍纳算法的更好的散列函数 +function betterHash(string){ + const H = 37; + var total = 0; + for(var i=0;i -1) { - this.dataStore.splice(pos,1); - return true; - } - else { - return false; - } +//因为集合中不能包含相同的元素,所以插入前使用 indexOf() 检查新加入的元素在数组中是否存在。如果找到, +// 该方法返回该元素在数组中的位置;如果没有找到,该方法返回 -1 +function add(data){ + if(this.dataStore.indexOf(data) < 0){ + this.dataStore.push(data); + return true; + }else{ + return false; + } } -function size() { - return this.dataStore.length; +function remove(data){ + var pos = this.dataStore.indexOf(data); + if(pos > -1){ + this.dataStore.splice(pos,1); + return true; + }else{ + return false; + } } -function show() { - return "[" + this.dataStore + "]"; +function show(){ + return this.dataStore; } -function contains(data) { - if (this.dataStore.indexOf(data) > -1) { - return true; - } - else { - return false; - } +function contains(data){ + if(this.dataStore.indexOf(data) > -1){ + return true; + }else{ + return false; + } } - -function union(set) { - var tempSet = new Set(); - for (var i = 0; i < this.dataStore.length; ++i) { - tempSet.add(this.dataStore[i]); - } - for (var i = 0; i < set.dataStore.length; ++i) { - if (!tempSet.contains(set.dataStore[i])) { - tempSet.dataStore.push(set.dataStore[i]); - } - } - return tempSet; +function union(set){ + var tempSet = new Set(); + for(var i=0;i set.size()) { - return false; - } - else { - for each (var member in this.dataStore) { - if (!set.contains(member)) { - return false; - } - } - } - return true; -} - -function difference(set) { - var tempSet = new Set(); - for (var i = 0; i < this.dataStore.length; ++i) { - if (!set.contains(this.dataStore[i])) { - tempSet.add(this.dataStore[i]); - } - } - return tempSet; -} - - -// main program - -var cis = new Set(); -var it = new Set(); -cis.add("Clayton"); -cis.add("Jennifer"); -cis.add("Danny"); -it.add("Bryan"); -it.add("Clayton"); -it.add("Jennifer"); -var diff = new Set(); -diff = cis.difference(it); -print(cis.show() + " difference " + it.show() + " -> " + diff.show()); \ No newline at end of file + var tempSet = new Set(); + for (var i = 0; i < this.dataStore.length; ++i) { + if (set.contains(this.dataStore[i])) { + tempSet.add(this.dataStore[i]); + } + } + return tempSet; +} \ No newline at end of file diff --git a/Chapter9/Set1.js b/Chapter9/Set1.js new file mode 100644 index 0000000..d288398 --- /dev/null +++ b/Chapter9/Set1.js @@ -0,0 +1,112 @@ +function Set() { + this.dataStore = []; + this.add = add; + this.remove = remove; + this.size = size; + this.union = union; + this.contains = contains; + this.intersect = intersect; + this.subset = subset; + this.difference = difference; + this.show = show; +} + +function add(data) { + if (this.dataStore.indexOf(data) < 0) { + this.dataStore.push(data); + return true; + } + else { + return false; + } +} + +function remove(data) { + var pos = this.dataStore.indexOf(data); + if (pos > -1) { + this.dataStore.splice(pos,1); + return true; + } + else { + return false; + } +} + +function size() { + return this.dataStore.length; +} + +function show() { + return "[" + this.dataStore + "]"; +} + +function contains(data) { + if (this.dataStore.indexOf(data) > -1) { + return true; + } + else { + return false; + } +} + +function union(set) { + var tempSet = new Set(); + for (var i = 0; i < this.dataStore.length; ++i) { + tempSet.add(this.dataStore[i]); + } + for (var i = 0; i < set.dataStore.length; ++i) { + if (!tempSet.contains(set.dataStore[i])) { + tempSet.dataStore.push(set.dataStore[i]); + } + } + return tempSet; +} + +function intersect(set) { + var tempSet = new Set(); + for (var i = 0; i < this.dataStore.length; ++i) { + if (set.contains(this.dataStore[i])) { + tempSet.add(this.dataStore[i]); + } + } + return tempSet; +} + +function subset(set) { + if (this.size() > set.size()) { + return false; + } + else { + for each (var member in this.dataStore) { + if (!set.contains(member)) { + return false; + } + } + } + return true; +} + +function difference(set) { + var tempSet = new Set(); + for (var i = 0; i < this.dataStore.length; ++i) { + if (!set.contains(this.dataStore[i])) { + tempSet.add(this.dataStore[i]); + } + } + return tempSet; +} + + +// main program + +var cis = new Set(); +var it = new Set(); +cis.add("Clayton"); +cis.add("Jennifer"); +cis.add("Danny"); +it.add("Bryan"); +it.add("Clayton"); +it.add("Jennifer"); +var diff = new Set(); +diff = cis.difference(it); +print(cis.show() + " difference " + it.show() + " -> " + diff.show()); \ No newline at end of file diff --git a/Chapter9/test.js b/Chapter9/test.js new file mode 100644 index 0000000..3268ed1 --- /dev/null +++ b/Chapter9/test.js @@ -0,0 +1,27 @@ +load("Set.js"); +var names = new Set(); +names.add("David"); +names.add("Jennifer"); +names.add("Cynthia"); +names.add("Mike"); +names.add("Raymond"); +if (names.add("Mike")) { + print("Mike added") +}else { + print("Can't add Mike, must already be in set"); +} +print(names.show()); +var removed = "Mike"; +if (names.remove(removed)) { + print(removed + " removed."); +}else { + print(removed + " not removed."); +} +names.add("Clayton"); +print(names.show()); +removed = "Alisa"; +if (names.remove("Mike")) { + print(removed + " removed."); +}else { + print(removed + " not removed."); +} \ No newline at end of file diff --git "a/\350\257\276\345\220\216\347\273\203\344\271\240\351\242\230/Chapter2/test1.js" "b/\350\257\276\345\220\216\347\273\203\344\271\240\351\242\230/Chapter2/test1.js" new file mode 100644 index 0000000..f0a518d --- /dev/null +++ "b/\350\257\276\345\220\216\347\273\203\344\271\240\351\242\230/Chapter2/test1.js" @@ -0,0 +1,26 @@ +// 创建一个记录学生成绩的对象, +// 提供一个添加成绩的方法, +// 以及一个显示学生平均成绩的方法。 +function grade(){ + this.dataStore = [];//记录成绩 + this.add = add;//添加成绩 + this.average = average;//求平均成绩 +} +function add(temp){ + this.dataStore.push(temp); +} +function average(){ + var total = 0; + for(var i=0;ia; + // }); //方法1 + _this.wordStore.sort().reverse(); //方法2 + } + }; + _this.sort = function(rule){ + strategy[rule](); + return _this.wordStore; + } +} +print(new SortWord().sort('asc'));//And,Bryan,Clayton,Cynthia,David,Mike,Raymond,and +print(new SortWord().sort('desc'));//and,Raymond,Mike,David,Cynthia,Clayton,Bryan,And diff --git "a/\350\257\276\345\220\216\347\273\203\344\271\240\351\242\230/Chapter2/test3.js" "b/\350\257\276\345\220\216\347\273\203\344\271\240\351\242\230/Chapter2/test3.js" new file mode 100644 index 0000000..98f1747 --- /dev/null +++ "b/\350\257\276\345\220\216\347\273\203\344\271\240\351\242\230/Chapter2/test3.js" @@ -0,0 +1,29 @@ +// 修改本章前面出现过的 weeklyTemps 对象, +// 使它可以使用一个二维数组来存储每月的有用数据。 +// 增加一些方法用以显示月平均数、 +// 具体某一周平均数和所有周的平均数。 +function weekTemps(){ + this.dataStore = []; + this.add = add; + this.average = average; +} +function add(temp) { + this.dataStore.push(temp); +} +function average() { + var total = 0; + for (var i = 0; i < this.dataStore.length; ++i) { + total += this.dataStore[i]; + } + return total / this.dataStore.length; +} +var thisWeek = new weekTemps(); +thisWeek.add(52); +thisWeek.add(55); +thisWeek.add(61); +thisWeek.add(65); +thisWeek.add(55); +thisWeek.add(50); +thisWeek.add(52); +thisWeek.add(49); +print(thisWeek.average()); // 显示 54.875 \ No newline at end of file diff --git "a/\350\257\276\345\220\216\347\273\203\344\271\240\351\242\230/Chapter2/test4.js" "b/\350\257\276\345\220\216\347\273\203\344\271\240\351\242\230/Chapter2/test4.js" new file mode 100644 index 0000000..3d104d7 --- /dev/null +++ "b/\350\257\276\345\220\216\347\273\203\344\271\240\351\242\230/Chapter2/test4.js" @@ -0,0 +1,9 @@ +// 创建这样一个对象,它将字母存储在一个数组中, +// 并且用一个方法可以将字母连在一起,显示成一个单词。 +var Word = { + letters:['H','e','l','l','o'], + getWord:function(){ + return this.letters.join('');//Hello + } +}; +print(Word.getWord()); \ No newline at end of file pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy