【数据结构与算法】字典代码示例

来源:互联网 发布:淘宝低价包邮怎么赚钱 编辑:程序博客网 时间:2024/06/05 04:20

问题一、写一个程序, 该程序从一个文本文件中读入名字和电话号码, 然后将其存入一个字典。该程序需包含如下功能: 显示单个电话号码、 显示所有电话号码、 增加新电话号码、 删除电话号码、 清空所有电话号码?


字典代码:

function Dictionary() {  this.datastore = new Array();  this.load = function (url, callBack) {    var that = this;    var xmlhttp;    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari      xmlhttp = new XMLHttpRequest();    }    else {// code for IE6, IE5      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");    }    xmlhttp.onreadystatechange = function () {      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {        var arr = xmlhttp.responseText.split("\n");        for (var i = 0, len = arr.length; i < len; i++) {          if ( arr[i] == "" ) continue;          var _arr = arr[i].split(/\s{2,}/);          var name = _arr[0];          var tel = _arr[1];          that.add(name, tel);        }      }    }    xmlhttp.open("GET", url, false);    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");    xmlhttp.send(null);  }  this.add = function (key, value) {    this.datastore[key] = value;  }  this.find = function (key) {    return this.datastore[key];  }  // 删除电话号码  this.remove = function (key) {    delete this.datastore[key];  }  // 显示单个电话号码  this.show = function (key) {    var person = this.find(key);    console.log("显示单个联系人:"+key + " 联系电话: " + this.datastore[key]);  }  // 显示所有电话号码  this.showAll = function () {    var _keys = Object.keys(this.datastore).sort();    for (var k in _keys) {      var key = _keys[k];      console.log("联系人:"+key + " 联系电话: " + this.datastore[key]);    }  }  // 清空所有电话号码  this.clear = function () {    this.datastore = [];  }  // 为什么不使用 length 属性? 这是因为当键的类型为字符串时, length 属性就不管用了  this.count = function () {    return Object.keys(this.datastore).length;  }}

tel.txt文件:

Tom     12000000000Make     12000000000Jot     12000000000Lso     12000000000Degl     12000000000Long     12000000000Feng     12000000000


客户端:

var dict = new Dictionary();dict.load("tel.txt");dict.add("Baixin", "10000000000");//dict.clear();dict.showAll();dict.show("Baixin");

输出结果:




问题二、使用 Dictionary 类写一个程序, 该程序用来存储一段文本中各个单词出现的次数。 该程序显示每个单词出现的次数, 但每个单词只显示一次。 比如下面一段话“ the brown fox jumped over the blue fox”, 程序的输出应为:
the: 2
brown: 1
fox: 2
jumped: 1
over: 1
blue: 1



字典代码:

function Dictionary() {  this.datastore = new Array();  this.add = function (key) {    var _value = this.find(key);    if ( _value ) {      this.datastore[key] = _value + 1;    } else {      this.datastore[key] = 1;    }  }  this.print = function( str ) {    if ( str == "") return;    var arr = str.split(/\s{1,}/);    for( var i = 0, len = arr.length; i < len; i++ ) {      this.add(arr[i]);    }  }  this.find = function (key) {    return this.datastore[key];  }  this.remove = function (key) {    delete this.datastore[key];  }  this.showAll = function () {    var _keys = Object.keys(this.datastore);    for (var k in _keys) {      var key = _keys[k];      console.log(""+key + ": " + this.datastore[key]);    }  }}

客户端:
var dict = new Dictionary();dict.print("the brown fox jumped over the blue fox");dict.showAll();

输出结果:





2 0
原创粉丝点击