#include <stdio.h> #include <stdlib.h> typedef struct { int key; int value; } Record; void create_table(Record *table, int n) { for (int i = 0; i < n; i++) { printf("请输入第%d个关键字值:", i + 1); scanf("%d", &table[i].key); printf("请输入第%d个关键字值对应的记录值:", i + 1); scanf("%d", &table[i].value); } } void binary_search(Record *table, int n, int target) { int left = 0; int right = n - 1; while (left <= right) { int mid = left + (right - left) / 2; if (table[mid].key == target) { printf("找到记录,键值为:%d,记录值为:%d ", table[mid].key, table[mid].value); return; } else if (table[mid].key < target) { left = mid + 1; } else { right = mid - 1; } } printf("未找到记录,键值为:%d ", target); } int main() { int n; int choice; Record table[100]; while (1) { printf("菜单: "); printf("1. 建立顺序查找表并进行顺序查找 "); printf("2. 建立有序顺序查找表并进行二分查找 "); printf("3. 退出程序 "); printf("请输入您的选择:"); scanf("%d", &choice); switch (choice) { case 1: create_table(table, n); break; case 2: binary_search(table, n, getchar()); // 注意这里需要先用getchar()获取用户输入的换行符,否则会报错 break; case 3: exit(0); default: printf("无效的选择,请重新输入。 "); } } return 0; }
时间: 2024-02-14 11:20:06 浏览: 19
这段代码是一个简单的顺序查找和二分查找的实现。其中,create_table函数用于创建一个记录表,binary_search函数用于在有序记录表中进行二分查找。在main函数中,通过菜单的方式选择进行哪种操作。
需要注意的是,在调用binary_search函数时,需要先用getchar()获取用户输入的换行符,否则会导致输入错误。
相关问题
#include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct LNode { ElemType da
`#include <stdio.h>`和`#include <stdlib.h>`是C语言中的两个头文件。`<stdio.h>`是标准输入输出头文件,提供了一些用于输入输出的函数和宏定义。`<stdlib.h>`是标准库头文件,提供了一些常用的函数和宏定义,如内存分配、随机数生成等。
在你的代码中,`typedef int ElemType;`定义了一个类型别名,将`int`类型命名为`ElemType`。
`typedef struct LNode`定义了一个结构体类型`LNode`,结构体是一种自定义的数据类型,可以包含多个不同类型的成员变量。在这里,`LNode`结构体中的成员变量还没有完整定义,因此代码截断了。
#include <stdio.h> #include <stdlib.h> #define MAXSIZE 100 typedef int ElemType; typedef struct
这段代码是C语言中的头文件引用和一些定义的示例。其中,`#include <stdio.h>`和`#include <stdlib.h>`是用来引入标准库函数的头文件。`stdio.h`包含了输入输出相关的函数,如`printf`和`scanf`;`stdlib.h`包含了一些常用的函数,如内存分配函数`malloc`和随机数生成函数`rand`。
`#define MAXSIZE 100`是一个宏定义,用来定义一个常量`MAXSIZE`,它的值为100。
`typedef int ElemType;`是用来给`int`类型起一个别名`ElemType`,方便在代码中使用。
至于代码中的结构体部分,由于你没有提供完整的代码,所以无法给出具体的解释。但一般来说,结构体用于定义一种自定义的数据类型,可以包含多个不同类型的成员变量。
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)