• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

数据结构设计--哈希表词频统计检索+职工电话号码查询系统

武飞扬头像
梦·源·启
帮助1

目录

一、哈希表词频统计

1.环境

2.语言

3.功能

4.源代码

二、职工电话号码查询系统

1.环境

2.语言

3.功能

4.源代码


一、哈希表词频统计

1.环境

vs code

2.语言

c

3.功能

一篇英文文章(单词个数不超过1000个)存储在一个文本文件中,请设计程序,完成以下功能:

(1)从文件中读取英文单词(不区分大小写),过滤掉所有的标点,统计文章中各单词出现的次数。

(2)单词查找:

用哈希表作为查找表(自行设计哈希函数),分别实现基于线性探测法的单词查找和基于链地址法的单词查找,并分别计算其ASL。

(3)单词插入:

若查找失败,向哈希表中插入该单词。

4.源代码

  1.  
    #include<iostream>
  2.  
    #include<fstream>
  3.  
    #include<cstring>
  4.  
    #include<string>
  5.  
    #include<windows.h>
  6.  
    #include<conio.h>
  7.  
    #include<cmath>
  8.  
    #include<time.h>
  9.  
    #include<stdlib.h>
  10.  
    using namespace std;
  11.  
     
  12.  
    //常量的定义
  13.  
    const int MaxSize = 1000; //文章单词最大容量
  14.  
    const char* file = "D:\\下载2\\English.txt"; //待检索文件
  15.  
    int sum; //单词总数
  16.  
     
  17.  
    // 词频顺序存储结构--线性探测法
  18.  
    struct WordFrequency
  19.  
    { //词频
  20.  
    string word; //单词
  21.  
    int frequency; //频率
  22.  
    int key; //关键码
  23.  
    }WF[MaxSize];
  24.  
     
  25.  
    typedef WordFrequency datatype; //为数据类型定义一个新的名字
  26.  
     
  27.  
    //词频链式存储结构--链地址法
  28.  
    struct Node
  29.  
    {
  30.  
    datatype data; //数据域
  31.  
    Node* next; //指针域
  32.  
    };
  33.  
     
  34.  
    //读取文件函数声明
  35.  
    int StatisticalWord(string word); //统计文章词频
  36.  
    void StatisticsData(); //数据统计
  37.  
    int WordTransitionKey(string word); //将单词转换为唯一关键码
  38.  
     
  39.  
    //Hash函数声明
  40.  
    void HashMenu(); //哈希表菜单
  41.  
    void OpenHashLocateMenu(); //线性探测法哈希查找菜单
  42.  
    void OpenHashLocate(); //线性探测法哈希查找
  43.  
    void LinkHashLocate(); //链地址法哈希查找
  44.  
    void LinkHashWordLocateMenu(); //线性探测法哈希查找菜单
  45.  
     
  46.  
    void MajorMenu(); //主菜单
  47.  
     
  48.  
    //统计文章词频,把字符添加到结构体数组中
  49.  
    int StatisticalWord(string word)
  50.  
    {
  51.  
    for (int i=0; i<MaxSize; i )
  52.  
    {
  53.  
    if (WF[i].word == word)
  54.  
    {
  55.  
    WF[i].frequency ; //词频
  56.  
    return i; //退出函数
  57.  
    }
  58.  
    }
  59.  
    //单词如果不重复,添加到哈希表中
  60.  
    WF[sum].word = word; //添加单词
  61.  
    WF[sum].frequency = 1; //词频置为一
  62.  
    WF[sum].key = WordTransitionKey(word); //添加关键码
  63.  
    sum ; //单词总数
  64.  
    return 0;
  65.  
    }
  66.  
     
  67.  
    //将单词转换为关键字--随机数法
  68.  
    int WordTransitionKey(string word)
  69.  
    {
  70.  
    int a[23] = {0,2,3,5,7,11,13,17,19,23,27,29,31,37,41,47,51,67,87,101,111,113,117};//用随机数法
  71.  
    int sumkey = 0;
  72.  
    for (int i = 0; i < int(word.size()); i )
  73.  
    {
  74.  
    sumkey = int(word[i]); //每个字符的ASCLL值相加
  75.  
    }
  76.  
    sumkey = int('a') * a[int(word.size())];//用a的ASCLL*一个随机的素数
  77.  
    return sumkey;
  78.  
    }
  79.  
     
  80.  
    //读取文件,并进行词频统计
  81.  
    void StatisticsData()
  82.  
    {
  83.  
    system("cls"); //清屏
  84.  
    ifstream fin; //文件读操作,存储设备读取到内存中
  85.  
    fin.open(file); //关联文件file
  86.  
    char ch; //用于获取字符
  87.  
    string word; //用于存放单词
  88.  
    int count = 0, min; //count用于标记单词个数,min用于标记最小的单词
  89.  
    for (int i = 0; fin.get(ch); i )
  90.  
    { //读取文件内容,并去除符号
  91.  
    if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
  92.  
    {
  93.  
    if (word == "\0") //word为空,放入第一个字母
  94.  
    word = ch;
  95.  
    else
  96.  
    word = ch; //word不为空,拼接字母组成单词
  97.  
    }
  98.  
    else
  99.  
    {
  100.  
    if (word != "\0")
  101.  
    { //判断之前的word里面是否有单词
  102.  
    count ; //有单词,总数
  103.  
    if (count>MaxSize)
  104.  
    {
  105.  
    cout << "文章单词超出统计上限,系统已退出" << endl;
  106.  
    fin.close(); //关闭文件
  107.  
    exit(0); //退出程序
  108.  
    system("pause"); //暂停
  109.  
    }
  110.  
    StatisticalWord(word); //存放到结构体数组里面--哈希数组
  111.  
    }
  112.  
    word = "\0"; //重置word,用于存放新单词
  113.  
    }
  114.  
    }
  115.  
    fin.close(); //关闭文件
  116.  
    }
  117.  
     
  118.  
    //线性探测法哈希表类
  119.  
    class HashTable
  120.  
    {
  121.  
    public:
  122.  
    HashTable(); //构造函数,初始化空散列表
  123.  
    ~HashTable() {}; //删除函数
  124.  
    int Insert(datatype a); //插入
  125.  
    int Search(string word); //查找
  126.  
    datatype Get(int a);
  127.  
    void Print(); //输出
  128.  
    private:
  129.  
    int H(int k); //哈希函数(散列函数)
  130.  
    datatype ht[MaxSize]; //散列表
  131.  
    };
  132.  
     
  133.  
    //初始化散列函数
  134.  
    HashTable::HashTable()
  135.  
    {
  136.  
    for (int i = 0; i < MaxSize; i )
  137.  
    {
  138.  
    ht[i].key = 0; //关键码初始化
  139.  
    ht[i].word = "";
  140.  
    ht[i].frequency = 0; // 0表示该散列单元为空
  141.  
    }
  142.  
    }
  143.  
     
  144.  
    //哈希函数,除留余数法
  145.  
    int HashTable::H(int k)
  146.  
    {
  147.  
    return k % MaxSize;
  148.  
    }
  149.  
     
  150.  
    //输出函数
  151.  
    void HashTable::Print()
  152.  
    {
  153.  
    system("cls"); //清屏
  154.  
    cout << "单词总数为:" << sum << endl;
  155.  
    for (int i = 0; i < sum; i )
  156.  
    {
  157.  
    cout << "单词:"<<WF[i].word << "\t" <<"词频:"<<WF[i].frequency << endl;
  158.  
    }
  159.  
    system("pause"); //暂停
  160.  
    }
  161.  
     
  162.  
    //查找函数
  163.  
    int HashTable::Search(string word)
  164.  
    {
  165.  
    int key = WordTransitionKey(word); //将单词转化为关键码
  166.  
    int i = H(key); //计算散列地址,设置比较的起始位置
  167.  
    while (ht[i].key != 0)
  168.  
    {
  169.  
    if (ht[i].word == word)
  170.  
    return i; //查找成功
  171.  
    else
  172.  
    i = (i 1) % MaxSize; //向后探测一个位置
  173.  
    }
  174.  
    return 0; //查找失败
  175.  
    }
  176.  
     
  177.  
    //插入函数
  178.  
    int HashTable::Insert(datatype f_word_key)
  179.  
    {
  180.  
    int key = WordTransitionKey(f_word_key.word);//将单词转化为关键码
  181.  
    int i = H(key); //计算散列地址,设置比较的起始位置
  182.  
    while (ht[i].key != 0)
  183.  
    { //寻找空的散列单元
  184.  
    i = (i 1) % MaxSize; //向后探测一个位置
  185.  
    }
  186.  
    ht[i].key = key; //关键码赋值
  187.  
    ht[i].word = f_word_key.word; //单词赋值
  188.  
    ht[i].frequency = f_word_key.frequency; //词频赋值
  189.  
    return i; //返回插入位置
  190.  
    }
  191.  
     
  192.  
    //获取单词以及频率
  193.  
    datatype HashTable::Get(int a)
  194.  
    {
  195.  
    return ht[a];
  196.  
    }
  197.  
     
  198.  
    //链地址法哈希表类
  199.  
    class HashTableLink
  200.  
    {
  201.  
    public:
  202.  
    HashTableLink(); //构造函数,初始化散列表
  203.  
    ~HashTableLink(); //删除函数,释放同义词子表结点
  204.  
    int Insert(datatype fword); //插入
  205.  
    Node* Search(string word); //查找
  206.  
    void Print(); //输出
  207.  
    private:
  208.  
    int H(int k); //散列函数
  209.  
    Node* ht[MaxSize]; //开散列表
  210.  
    };
  211.  
     
  212.  
    //构造函数
  213.  
    HashTableLink::HashTableLink()
  214.  
    {
  215.  
    for (int i = 0; i < MaxSize; i )
  216.  
    ht[i] = NULL; //链式存储结构指针置空
  217.  
    }
  218.  
     
  219.  
    //析构函数,释放空间
  220.  
    HashTableLink :: ~HashTableLink()
  221.  
    {
  222.  
    Node* p = NULL, * q = NULL;
  223.  
    for (int i = 0; i < MaxSize; i )
  224.  
    {
  225.  
    p = ht[i];
  226.  
    q = p; //用来储存p
  227.  
    while (p != NULL)
  228.  
    { //p非空
  229.  
    p = p->next; //p后移
  230.  
    delete q; //删除q
  231.  
    q = p;
  232.  
    } //while
  233.  
    }
  234.  
    }
  235.  
     
  236.  
    //除留余数法-散列函数
  237.  
    int HashTableLink::H(int k)
  238.  
    {
  239.  
    return k % MaxSize;
  240.  
    }
  241.  
     
  242.  
    //输出到屏幕
  243.  
    void HashTableLink::Print()
  244.  
    {
  245.  
    system("cls"); //清屏
  246.  
    cout << "单词总数为:" << sum << endl;
  247.  
    for (int i = 0; i < sum; i )
  248.  
    {
  249.  
    cout << "单词:"<<WF[i].word << "\t" <<"词频:"<<WF[i].frequency<< endl;
  250.  
    }
  251.  
    system("pause"); //暂停
  252.  
    }
  253.  
     
  254.  
    //链地址查找函数
  255.  
    Node* HashTableLink::Search(string word)
  256.  
    {
  257.  
    int k = WordTransitionKey(word); //转化为关键码
  258.  
    int j = H(k); //计算散列地址
  259.  
    Node* p = ht[j]; //指针p初始化
  260.  
    while (p != NULL)
  261.  
    { //p非空
  262.  
    if (p->data.word == word)
  263.  
    return p; //已找到返回指针
  264.  
    else
  265.  
    p = p->next; //p后移
  266.  
    } //while
  267.  
    return NULL; //未找到返回空指针
  268.  
    }
  269.  
     
  270.  
    //插入函数(前插法)--链地址
  271.  
    int HashTableLink::Insert(datatype fword)
  272.  
    {
  273.  
    int k = WordTransitionKey(fword.word); //转化为关键码
  274.  
    fword.key = k; //给关键码赋值
  275.  
    int j = H(k); //计算散列地址
  276.  
    Node* p = Search(fword.word); //调用查找函数
  277.  
    if (p != NULL) //p非空,表示该内容已经插入过了
  278.  
    return -1; //已存在元素k,无法插入
  279.  
    else
  280.  
    { //p为空,表示该内容未插入
  281.  
    p = new Node; //生成新节点
  282.  
    p->data.key = fword.key; //关键码赋值
  283.  
    p->data.frequency = fword.frequency; //词频赋值
  284.  
    p->data.word = fword.word; //单词赋值
  285.  
    p->next = ht[j]; //新节点插入ht[j]
  286.  
    ht[j] = p; //更新节点
  287.  
    return 1; //插入成功标志
  288.  
    }
  289.  
    }
  290.  
     
  291.  
    //哈希表菜单
  292.  
    void HashMenu()
  293.  
    {
  294.  
    while(true)
  295.  
    {
  296.  
    system("cls"); //清屏
  297.  
    cout << "*******************基于哈希表的英文单词词频统计和检索系统*******************" << endl;
  298.  
    cout << "1.线性探测法哈希查找" << endl;
  299.  
    cout << "2.链地址法哈希查找" << endl;
  300.  
    cout << "3.返回上一级" << endl;
  301.  
    cout << "请按相应的数字键进行选择:" << endl;
  302.  
    int n;
  303.  
    cin >> n;
  304.  
    switch (n){
  305.  
    case 1 :
  306.  
    OpenHashLocate(); //线性探测法哈希查找
  307.  
    break;
  308.  
    case 2 :
  309.  
    LinkHashLocate(); //链地址法哈希查找
  310.  
    break;
  311.  
    case 3 :
  312.  
    return; //退出函数
  313.  
    default:
  314.  
    cout << "输入的不是有效符号,请重新输入" << endl;
  315.  
    system("pause");
  316.  
    }
  317.  
    }
  318.  
    return;
  319.  
    }
  320.  
     
  321.  
    //线性探测法哈希查找菜单
  322.  
    void OpenHashLocateMenu()
  323.  
    {
  324.  
    HashTable HT;
  325.  
    for (int i = 0; i < sum; i )
  326.  
    HT.Insert(WF[i]); //把数据插入到哈希表中
  327.  
    double a = (double)sum / MaxSize; //装填因子
  328.  
    system("cls"); //清屏
  329.  
    cout << "*******************基于哈希表的英文单词词频统计和检索系统*******************" << endl;
  330.  
    cout << "---线性探测单词查找---" << endl;
  331.  
    cout << "请输入要查找的单词:";
  332.  
    string word;
  333.  
    cin >> word;
  334.  
    int i = HT.Search(word); //获取散列地址
  335.  
    if (i) //查找成功的ASL
  336.  
    {
  337.  
    cout << "此单词为:" << HT.Get(i).word << endl;
  338.  
    cout << "此单词的词频:" << HT.Get(i).frequency << endl;
  339.  
    double ans=(double)(1 (1.00)/ (1 - a)) / 2;
  340.  
    printf("查找成功时ASL=%.2lf\n",ans);
  341.  
    }
  342.  
    else//查找失败,把单词加入到哈希表当中 ,并输出失败的ASL
  343.  
    {
  344.  
    WF[sum].word=word;
  345.  
    WF[sum].frequency=1;
  346.  
    WF[sum].key=WordTransitionKey(word);
  347.  
    HT.Insert(WF[sum]);
  348.  
    sum ;
  349.  
    cout << "查找失败" << endl;
  350.  
    double ans=(double)(1 1/pow(1-a,2))/2;
  351.  
    printf("查找失败时ASL=%.2lf\n",ans);
  352.  
    }
  353.  
    system("pause");
  354.  
    }
  355.  
     
  356.  
    //线性探测法哈希查找
  357.  
    void OpenHashLocate()
  358.  
    {
  359.  
    HashTable HT;
  360.  
    for (int i = 0; i < sum; i )
  361.  
    HT.Insert(WF[i]); //把数据插入到哈希表中
  362.  
    while(true)
  363.  
    {
  364.  
    system("cls");
  365.  
    cout << "*******************基于哈希表的英文单词的词频统计和检索系统*******************" << endl;
  366.  
    cout << "---基于线性探测法的哈希查找---" << endl;
  367.  
    cout << "1.词频统计" << endl;
  368.  
    cout << "2.单词查找" << endl;
  369.  
    cout << "3.返回上一级" << endl;
  370.  
    cout << "请按相应的数字键进行选择:" << endl;
  371.  
    int n;
  372.  
    cin >> n;
  373.  
    switch (n)
  374.  
    {
  375.  
    case 1 :
  376.  
    HT.Print(); //词频统计
  377.  
    break;
  378.  
    case 2 :
  379.  
    OpenHashLocateMenu(); //线性探测法的哈希查找菜单
  380.  
    break;
  381.  
    case 3 :
  382.  
    return;
  383.  
    default:
  384.  
    cout << "输入的不是有效符号,请重新输入" << endl;
  385.  
    system("pause");
  386.  
    }
  387.  
    }
  388.  
    }
  389.  
     
  390.  
    //链地址法哈希查找
  391.  
    void LinkHashLocate()
  392.  
    {
  393.  
    HashTableLink HT;
  394.  
    for (int i = 0; i < sum; i )
  395.  
    HT.Insert(WF[i]); //把数据插入到哈希表
  396.  
    while(true)
  397.  
    {
  398.  
    system("cls"); //清屏
  399.  
    cout << "*******************基于哈希表的英文单词词频统计和检索系统*******************" << endl;
  400.  
    cout << "---基于链地址法的哈希查找---" << endl;
  401.  
    cout << "1.词频统计" << endl;
  402.  
    cout << "2.单词查找" << endl;
  403.  
    cout << "3.返回上一级" << endl;
  404.  
    cout << "请按相应的数字键进行选择:" << endl;
  405.  
    int n;
  406.  
    cin >> n;
  407.  
    switch (n)
  408.  
    {
  409.  
    case 1:
  410.  
    HT.Print(); //词频统计
  411.  
    break;
  412.  
    case 2:
  413.  
    LinkHashWordLocateMenu(); //单词查找菜单
  414.  
    break;
  415.  
    case 3:
  416.  
    return; //退出函数
  417.  
    default:
  418.  
    cout << "输入的不是有效符号,请重新输入" << endl;
  419.  
    system("pause");
  420.  
    }
  421.  
    }
  422.  
    }
  423.  
     
  424.  
    //链地址法哈希查找菜单
  425.  
    void LinkHashWordLocateMenu()
  426.  
    {
  427.  
    HashTableLink HT;
  428.  
    for (int i = 0; i < sum; i )
  429.  
    HT.Insert(WF[i]); //把数据插入到哈希表
  430.  
    double a= (double)sum / MaxSize;//散列表的装填因子
  431.  
    system("cls");
  432.  
    cout << "*******************基于哈希表的英文单词词频统计和检索系统*******************" << endl;
  433.  
    cout << "---链地址单词查找---" << endl;
  434.  
    cout << "请输入要查找的单词:";
  435.  
    string word;
  436.  
    cin >> word;
  437.  
    HT.Search(word);
  438.  
    Node* p = HT.Search(word); //返回目标指针
  439.  
    if (p != NULL)
  440.  
    {
  441.  
    cout << "此单词为:" << p->data.word << endl;
  442.  
    cout << "此单词的词频:" << p->data.frequency<< endl;
  443.  
    double ans=1 (double)(a)/2;
  444.  
    printf("查找成功时ASL=%.2lf\n", ans);
  445.  
    } //if
  446.  
    else
  447.  
    {
  448.  
    WF[sum].word=word;
  449.  
    WF[sum].frequency=1;
  450.  
    WF[sum].key=WordTransitionKey(word);
  451.  
    HT.Insert(WF[sum]);
  452.  
    sum ;
  453.  
    cout << "查找失败" << endl;
  454.  
    double e=2.718281;
  455.  
    double ans=(double)(a pow(e,-a));
  456.  
    printf("查找失败时ASL=%.2lf\n",ans);
  457.  
    }
  458.  
    system("pause"); //暂停
  459.  
    }
  460.  
    //主菜单
  461.  
    void MajorMenu()
  462.  
    {
  463.  
    while (true)
  464.  
    {
  465.  
    cout << "*******************基于哈希表的英文单词词频统计和检索系统*******************" << endl;
  466.  
    cout << "---菜单---" << endl;
  467.  
    cout << "1.基于哈希表的查找" << endl;
  468.  
    cout << "2.退出系统" << endl;
  469.  
    cout << "请按相应的数字键进行选择:" << endl;
  470.  
    int n;
  471.  
    cin >> n;
  472.  
    switch (n)
  473.  
    {
  474.  
    case 1:
  475.  
    HashMenu();
  476.  
    break;
  477.  
    case 2:
  478.  
    cout << "系统已退出" << endl;
  479.  
    return;
  480.  
    default:
  481.  
    cout << "输入的不是有效符号,请重新输入" << endl;
  482.  
    system("cls"); //清屏
  483.  
    system("pause"); //暂停
  484.  
    }
  485.  
    }
  486.  
    }
  487.  
    //主函数
  488.  
    int main()
  489.  
    {
  490.  
    ifstream fin;
  491.  
    fin.open(file);//获取文件English.txt
  492.  
    if (!fin.is_open())
  493.  
    {
  494.  
    cout << "EngLish.txt文件不存在,请检查文件名或者目录下文件是否存在..." << endl;
  495.  
    system("pause");
  496.  
    return 0;
  497.  
    }
  498.  
    else
  499.  
    {
  500.  
    cout << "EngLish.txt文件加载中..." << endl;
  501.  
    Sleep(1000);//延时函数,延时1秒
  502.  
    }
  503.  
    StatisticsData(); //数据统计
  504.  
    MajorMenu();//主菜单
  505.  
    return 0;
  506.  
    }
学新通

二、职工电话号码查询系统

1.环境

vs code

2.语言

c

3.功能

设计散列表,实现职工电话号码查询系统,要求:

(1) 每个记录包括下列内容:

(电话号码(11位) 、工号(5位) 、姓名、性别、院系名)总记录数不少于300条。

(2)从文件中读入各记录,分别以电话号码和用户名为关键字建立散列表(散列函数要求使用除留余数法,采用开放定址法处理冲突)

(3)查找并显示给定电话号码的记录,并输出查找长度。

(4)查找并显示给定用户名的记录(若有同名记录,则输出全部同名用户信息),并输出查找长度。

(5) 从文件读入各记录,以工号为关键字建立散列表(散列函数要求使用除留余数法,采用链地址法处理冲突)。

(6) 查找并显示给定院系的所有职工信息。

4.源代码

  1.  
    #include<bits/stdc .h>
  2.  
    #include<algorithm>
  3.  
    using namespace std;
  4.  
    const int MAXSIZE=300;
  5.  
    int n;//读入数据数
  6.  
    typedef struct
  7.  
    {
  8.  
    string tel;//电话
  9.  
    string num;//工号
  10.  
    string name;//姓名
  11.  
    string sex;//性别
  12.  
    string col;//院系
  13.  
    }Hash;
  14.  
     
  15.  
    typedef Hash Elemtype;//重新定义一个类型
  16.  
     
  17.  
    typedef struct LNode
  18.  
    {
  19.  
    Elemtype data;
  20.  
    LNode *next;
  21.  
    }*LinkList;
  22.  
     
  23.  
    Hash ht[MAXSIZE];//定义结构体线性表
  24.  
     
  25.  
    //初始化单链表
  26.  
    void InitList(LinkList h[MAXSIZE])
  27.  
    {
  28.  
    for(int i=0;i<MAXSIZE;i )
  29.  
    {
  30.  
    h[i]=new LNode;
  31.  
    h[i]->next=NULL;
  32.  
    }
  33.  
    }
  34.  
     
  35.  
    //初始化线性表
  36.  
    void init()
  37.  
    {
  38.  
    n=0;
  39.  
    return ;
  40.  
    }
  41.  
     
  42.  
    //将文件中的数据逐条读入顺序表L中
  43.  
    void ReadData(Hash ht[])
  44.  
    {
  45.  
    int i=0;
  46.  
    init(); //初始化顺序表
  47.  
    ifstream infile("D:\\下载2\\edg.txt"); //以输入的方式(读文件的方式)打开文件d:\book.txt
  48.  
    while(!infile.eof()) //只要文件没读完,就读一行数据到顺序表L中的第i个分量中
  49.  
    {
  50.  
    infile>>ht[i].tel;
  51.  
    infile>>ht[i].num;
  52.  
    infile>>ht[i].name;
  53.  
    infile>>ht[i].sex;
  54.  
    infile>>ht[i].col;
  55.  
    i ;
  56.  
    }
  57.  
    n=i; //修改顺序表的长度
  58.  
    infile.close (); //关闭文件
  59.  
    }
  60.  
     
  61.  
    int H(int x)//除留余数法
  62.  
    {
  63.  
    return x%MAXSIZE;
  64.  
    }
  65.  
     
  66.  
    int KEY(string x)//求关键字
  67.  
    {
  68.  
    int a[16]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,51};//减少冲突
  69.  
    int ans=0;
  70.  
    for(int i=0;i<x.size();i )
  71.  
    ans =int(x[i]);
  72.  
    return (a[x.size()]*ans MAXSIZE)%MAXSIZE;
  73.  
    }
  74.  
     
  75.  
    void name_create(int op)//开放寻址法电话号码/姓名建表
  76.  
    {
  77.  
    Hash a[MAXSIZE];
  78.  
    int ans=0;
  79.  
    for(int i=0;i<n;i )
  80.  
    {
  81.  
    int key;
  82.  
    if(op==1)
  83.  
    key=H(KEY(ht[i].tel));
  84.  
    else if(op==2)
  85.  
    key=H(abs(KEY(ht[i].name)));
  86.  
    int f=key;
  87.  
    while(a[key].name!="")//如果当前有冲突,向下一个继续探测
  88.  
    {
  89.  
    key ;
  90.  
    if(key==MAXSIZE-1) key=0;
  91.  
    if(f==key) break;//再次探测这个位置,跳出循环
  92.  
    }
  93.  
    a[key]=ht[i]; //插入哈希表中,也就是将结构体数组赋值
  94.  
    }
  95.  
    cout<<"输出开放寻址法所建的哈希表:"<<endl;
  96.  
    for(int i=0;i<MAXSIZE;i )
  97.  
    {
  98.  
    if(a[i].name!="")
  99.  
    {
  100.  
    cout<<a[i].tel<<" "<<a[i].num<<" "<<a[i].name<<" "<<a[i].sex<<" "<<a[i].col<<endl;
  101.  
    }
  102.  
    }
  103.  
    cout<<endl;
  104.  
    return ;
  105.  
    }
  106.  
     
  107.  
    void tel_search()//按电话号码查找
  108.  
    {
  109.  
    cout<<"请输入一个电话号码:";
  110.  
    string x;
  111.  
    cin>>x;
  112.  
    cout<<endl;
  113.  
    int ans=0;
  114.  
    int p=0;
  115.  
    for(int i=0;i<n;i )
  116.  
    {
  117.  
    if(ht[i].tel==x)
  118.  
    {
  119.  
    p ;//设置访问标志
  120.  
    cout<<ht[i].tel<<" "<<ht[i].num<<" "<<ht[i].name<<" "<<ht[i].sex<<" "<<ht[i].col<<endl;
  121.  
    }
  122.  
    ans ;//继续查找,长度 1
  123.  
    if(p) break; //如果查到,退出循环
  124.  
    }
  125.  
    if(p)
  126.  
    cout<<"查找长度为:"<<ans<<endl;
  127.  
    else cout<<"未查找到该用户!"<<endl;
  128.  
    return ;
  129.  
    }
  130.  
     
  131.  
    void name_search()
  132.  
    {
  133.  
    cout<<"请输入一个姓名:";
  134.  
    string x;
  135.  
    cin>>x;
  136.  
    cout<<endl;
  137.  
    int ans=0;
  138.  
    int p=0;
  139.  
    int q=0;
  140.  
    for(int i=n-1;i>=0;i--)
  141.  
    {
  142.  
    if(ht[i].name==x)
  143.  
    {
  144.  
    q=i;
  145.  
    break;
  146.  
    }
  147.  
    }
  148.  
    for(int i=0;i<n;i )
  149.  
    {
  150.  
    if(ht[i].name==x)
  151.  
    {
  152.  
    p ;//设置访问标志
  153.  
    cout<<ht[i].tel<<" "<<ht[i].num<<" "<<ht[i].name<<" "<<ht[i].sex<<" "<<ht[i].col<<endl;
  154.  
    }
  155.  
    }
  156.  
    if(p)
  157.  
    cout<<"查找长度为:"<<q 1<<endl;
  158.  
    else cout<<"未查找到该用户!"<<endl;
  159.  
    return ;
  160.  
    }
  161.  
     
  162.  
    void num_create()//基于链地址法工号创建哈希表
  163.  
    {
  164.  
    LinkList h[MAXSIZE];
  165.  
    InitList(h);
  166.  
    int ans=0;
  167.  
    for(int i=0;i<n;i )
  168.  
    {
  169.  
    int key=H(KEY(ht[i].num));
  170.  
    LinkList p=new LNode;
  171.  
    p=h[abs(key)];
  172.  
    while(p->next!=NULL) p=p->next;//让p指向尾节点 ,用尾插法 建表
  173.  
    LinkList q=new LNode;
  174.  
    q->data.name=ht[i].name;//复制姓名
  175.  
    q->data.col=ht[i].col;//复制学院
  176.  
    q->data.num=ht[i].num;//复制工号
  177.  
    q->data.sex=ht[i].sex;//复制性别
  178.  
    q->data.tel=ht[i].tel;//复制电话
  179.  
    p->next=q;//尾插发
  180.  
    q->next=NULL;//将表尾置空
  181.  
    }
  182.  
    cout<<"输出链地址法建表职工信息:"<<endl;
  183.  
    for(int i=0;i<MAXSIZE;i )
  184.  
    {
  185.  
    LinkList p=new LNode;
  186.  
    if(h[i]->next==NULL) continue;
  187.  
    else
  188.  
    {
  189.  
    p=h[i]->next;
  190.  
    while(p)
  191.  
    {
  192.  
    cout<<p->data.col<<" "<<p->data.name<<" "<<p->data.num<<" "<<p->data.sex<<" "<<p->data.tel<<endl;
  193.  
    p=p->next;
  194.  
    }
  195.  
    }
  196.  
    }
  197.  
    return ;
  198.  
    }
  199.  
     
  200.  
    void query()//输出给定院系职工信息
  201.  
    {
  202.  
    cout<<"请输入一个院系:";
  203.  
    string op;
  204.  
    cin>>op;
  205.  
    cout<<"该院系所有的职工为:"<<endl;
  206.  
    for(int i=0;i<n;i )
  207.  
    {
  208.  
    if(ht[i].col==op)
  209.  
    {
  210.  
    cout<<ht[i].tel<<" "<<ht[i].num<<" "<<ht[i].name<<" "<<ht[i].sex<<" "<<ht[i].col<<endl;
  211.  
    }
  212.  
    }
  213.  
    return ;
  214.  
    }
  215.  
     
  216.  
    void menu()
  217.  
    {
  218.  
    cout<<"***********欢迎使用职工电话号码查询系统*************"<<endl;
  219.  
    cout<<"***********1.开放寻址法以电话建立哈希表*************"<<endl;
  220.  
    cout<<"***********2.开放寻址法以姓名建立哈希表*************"<<endl;
  221.  
    cout<<"***********3.查询电话号码记录并输出长度*************"<<endl;
  222.  
    cout<<"***********4.查找用户名并且输出查找长度*************"<<endl;
  223.  
    cout<<"***********5.链地址法以工号来建立哈希表*************"<<endl;
  224.  
    cout<<"***********6.查找显示给定院系的职工信息*************"<<endl;
  225.  
    }
  226.  
     
  227.  
    int main()
  228.  
    {
  229.  
    ReadData(ht);//读取信息
  230.  
    while(1)
  231.  
    {
  232.  
    menu();
  233.  
    int op;
  234.  
    cout<<"请输入一个序号:";
  235.  
    cin>>op;
  236.  
    puts("");//换行
  237.  
    switch(op)
  238.  
    {
  239.  
    case 1:name_create(op);
  240.  
    break;
  241.  
    case 2:name_create(op);
  242.  
    break;
  243.  
    case 3:tel_search();
  244.  
    break;
  245.  
    case 4:name_search();
  246.  
    break;
  247.  
    case 5:num_create();
  248.  
    break;
  249.  
    case 6:query();
  250.  
    break;
  251.  
    default:printf("序号错误!");
  252.  
    break;
  253.  
    }
  254.  
    }
  255.  
    return 0;
  256.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfiejkc
系列文章
更多 icon
同类精品
更多 icon
继续加载