【数据结构】顺序表的查找(11)

news/2024/10/18 12:21:33/

静态查找表

1 顺序表的查找

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <stdbool.h>
typedef long KeyType;
#define key number
struct ElemType
{long number;char name[9];int politics;int chinese;int math;int English;int physics;int chemistry;int biology;int total;
};
struct StaticTable
{struct ElemType * elem;int length;
};
void visit(struct ElemType c)
{printf("% -8ld %8s%5d%5d%5d%5d%5d%5d%5d%5d\n", c.number, c.name, c.politics, c.chinese, c.math,c.English, c.physics, c.chemistry, c.biology, c.total);
}
void InputFromFile(FILE * f, struct ElemType * c)
{fscanf(f, "%ld%s%d%d%d%d%d%d%d", &c->number, c->name, &c->politics, &c->chinese, &c->math,&c->English, &c->physics, &c->chemistry, &c->biology);
}
void InputKey(KeyType * k)
{scanf("%ld", k);
}
//静态查找表(顺序表)基本操作
//1 由数据文件构造静态顺序查找表
void create_SeqFormFile(struct StaticTable * St, char * filename)
{int i;FILE * f;f = fopen(filename, "r");fscanf(f, "%d", &St->length);St->elem = (struct ElemType * ) calloc(St->length + 1, sizeof (struct ElemType));if (!St->elem)exit(-1);for(i = 1; i <= St->length; i++){InputFromFile(f, &St->elem[i]);}fclose(f);
}
//2 重建静态查找表为按关键字非降序排列
void Ascend(struct StaticTable * St)
{int i, j, k;for (i = 1; i < St->length; i++){k = i;St->elem[0] = St->elem[i];for (j = i + 1; j <= St->length; j++){if (St->elem[j].key < St->elem[0].key){k = j;St->elem[0] = St->elem[j];}}if (k != i){St->elem[k] = St->elem[i];St->elem[i] = St->elem[0];}}
}
//3 组合(构建+排序)
void create_ordFormFile(struct StaticTable * St, char * filename)
{create_SeqFormFile(St, filename);Ascend(St);
}//4 查找
int search_Seq(struct StaticTable St, KeyType key)
{int i;St.elem[0].key = key;for (i = St.length; St.elem[i].key != key; --i){}return i;
}//5 折半查找
int search_bin(struct StaticTable St, KeyType key)
{int mid;int low = 1;int high = St.length;while (low <= high){mid = (low + high) / 2;if (key == St.elem[mid].key)return mid;else if (key < St.elem[mid].key)high = mid - 1;elselow = mid + 1;}return 0;
}
//6 遍历输出
void Traverse(struct StaticTable St, void (* visit)(struct ElemType))
{int i;struct ElemType * p = ++St.elem;for (i = 1; i <= St.length; i++){visit(*p++);}
}
//7 销毁
bool destroy(struct StaticTable * St)
{free(St->elem);St->elem = NULL;St->length = 0;return true;
}int main() {struct StaticTable St;int i;long s;char filename[13];printf("Enter file name: \n");scanf("%s", filename);create_SeqFormFile(&St, filename);for (i = 1; i <= St.length; i++){St.elem[i].total = St.elem[i].politics + St.elem[i].chinese + St.elem[i].English+ St.elem[i].math + St.elem[i].physics + St.elem[i].chemistry + St.elem[i].biology;}Traverse(St, visit);printf("Enter id whose searched:");InputKey(&s);i = search_Seq(St, s);if (i)visit(St.elem[i]);elseprintf("Not found!");destroy(&St);return 0;
}

数据文件

5
179328 何芳 85 89 98 100 93 80 47
179325 陈红 85 86 88 100 92 90 45
179326 路华 78 75 90 80 95 88 37
179327 张三 82 90 78 98 84 96 40
179324 李四 76 85 94 57 77 69 44

http://www.ppmy.cn/news/742958.html

相关文章

【Access】win 10 / win 11:Access 下载、安装、使用教程(「管理信息系统」实践专用软件)

目录 一、前言 二、卸载 Office 三、下载 Office Tool Plus 四、安装 Office&#xff08;内含 Access&#xff09; &#xff08;1&#xff09;启动 Office Tool Plus &#xff08;2&#xff09;部署 &#xff08;3&#xff09;安装 Office&#xff08;内含 Access&#…

中运量71路线路图_中运量公交71路正式开通(附全线完整换乘信息)

原标题&#xff1a;中运量公交71路正式开通(附全线完整换乘信息) (原标题&#xff1a;中运量公交71路正式开通(附全线完整换乘信息)) 原标题&#xff1a;今天中运量公交71路正式开通(附全线完整换乘信息)值得你收藏&#xff01; 据上海长宁&#xff0c;凌晨四点半&#xff0c;7…

C++ Primer Plus笔记: 2023.07.05

1.在C中&#xff0c;每个表达式都有值。例如下面的表达式&#xff1a; x 20 ;C将赋值表达式的值定义为左侧成员的值&#xff0c;因此这个表达式的值为20。 由于赋值表达式有值&#xff0c;因此可以编写下面的语句: maids (cooks 4) 3 ;表达式cooks 4的值为4&#xff0c;…

【Linux】文件描述符(下篇)

文章目录 &#x1f4d6; 前言1. 文件描述符fd的分配规则2. 重定向的本质3. 缓冲区的理解3.1 感受缓冲区的存在&#xff1a;3.2 正式认识缓冲区&#xff1a;综合例题&#xff1a; 4. 模拟实现C语言的文件操作5. 完善之前实现的shell5.1 程序替换&#xff0c;会影响曾经子进程打开…

mac,macbook 连接蓝牙耳机播放音乐断断续续

个人的情况是, mac本连的网线,用的无线鼠标, 屋里80多号人都在用笔记本,应该也有好多开着无线的东西 解决方法: mac 或macbook 连接蓝牙耳机播放音乐断断续续的原因, 在网上找了好多方法,试了各种方法,用各种命令,都完全没有效果 终于在晚上回家的时候灵机一动, 怕不是因为无线…

基于 R 对卫星图像进行无监督 kMeans 分类

一、前言 本文将向您展示如何使用 R 对卫星图像执行非常基本的 kMeans 无监督分类。我们将在 Sentinel-2 图像的一小部分上执行此操作。 Sentinel-2 是由欧洲航天局发射的一颗卫星&#xff0c;其数据可在此处免费访问。 我要使用的图像显示了 Neusiedl 湖的北部&#xff08;奥地…

win10蓝牙断断续续问题

修改无线网络的频带&#xff1a; 网络-》配置-》高级-》首选频带-》5Ghz频带&#xff0c;确定。 win10上电脑蓝牙耳机有两个模式。handfree和stereo&#xff0c;handfree是可以通话的&#xff0c;但是音质奇差&#xff1b;stereo是立体声模式&#xff0c;音效很好&#xff0c…

visual studio如何配置远程调试

要在Visual Studio中配置远程调试&#xff0c;可以按照以下步骤进行操作&#xff1a; 连接远程服务器&#xff1a; 首先&#xff0c;您需要使用远程连接工具&#xff08;例如PuTTY或SSH&#xff09;连接到远程服务器。在连接到远程服务器后&#xff0c;确保您具有在远程服务器…