已知有20个学生记录(包括学号,姓名,成绩) 设计三个查找算法,分别实现按学号,姓名,菜单选择查找方式;c语言
时间: 2023-07-15 22:15:15 浏览: 195
用文件存储10个学生的姓名、学号、手机号、地址,可通过姓名或者学号查找
5星 · 资源好评率100%
以下是按学号查找的示例代码:
```c
#include <stdio.h>
#define N 20
struct student {
int id;
char name[20];
int score;
};
int search_by_id(struct student list[], int n, int target) {
// 二分查找
int left = 0, right = n-1;
while (left <= right) {
int mid = (left + right) / 2;
if (list[mid].id == target) {
return mid;
} else if (list[mid].id < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
int main() {
struct student list[N] = {{1, "Alice", 80}, {3, "Bob", 90}, {5, "Charlie", 85}, {7, "David", 95}, {9, "Emily", 70},
{11, "Frank", 75}, {13, "Grace", 95}, {15, "Henry", 60}, {17, "Ivy", 85}, {19, "Jason", 90},
{2, "Kevin", 85}, {4, "Lucy", 90}, {6, "Maggie", 80}, {8, "Nancy", 70}, {10, "Oscar", 65},
{12, "Peggy", 95}, {14, "Queenie", 85}, {16, "Roger", 80}, {18, "Steven", 60}, {20, "Tom", 70}};
int target_id;
printf("请输入要查找的学号:");
scanf("%d", &target_id);
int index = search_by_id(list, N, target_id);
if (index != -1) {
printf("学号:%d,姓名:%s,成绩:%d\n", list[index].id, list[index].name, list[index].score);
} else {
printf("未找到该学生信息!\n");
}
return 0;
}
```
以下是按姓名查找的示例代码:
```c
#include <stdio.h>
#include <string.h>
#define N 20
struct student {
int id;
char name[20];
int score;
};
int search_by_name(struct student list[], int n, char target[]) {
// 线性查找
for (int i = 0; i < n; i++) {
if (strcmp(list[i].name, target) == 0) {
return i;
}
}
return -1;
}
int main() {
struct student list[N] = {{1, "Alice", 80}, {3, "Bob", 90}, {5, "Charlie", 85}, {7, "David", 95}, {9, "Emily", 70},
{11, "Frank", 75}, {13, "Grace", 95}, {15, "Henry", 60}, {17, "Ivy", 85}, {19, "Jason", 90},
{2, "Kevin", 85}, {4, "Lucy", 90}, {6, "Maggie", 80}, {8, "Nancy", 70}, {10, "Oscar", 65},
{12, "Peggy", 95}, {14, "Queenie", 85}, {16, "Roger", 80}, {18, "Steven", 60}, {20, "Tom", 70}};
char target_name[20];
printf("请输入要查找的姓名:");
scanf("%s", target_name);
int index = search_by_name(list, N, target_name);
if (index != -1) {
printf("学号:%d,姓名:%s,成绩:%d\n", list[index].id, list[index].name, list[index].score);
} else {
printf("未找到该学生信息!\n");
}
return 0;
}
```
以下是菜单选择查找方式的示例代码:
```c
#include <stdio.h>
#include <string.h>
#define N 20
struct student {
int id;
char name[20];
int score;
};
int search_by_id(struct student list[], int n, int target) {
// 二分查找
int left = 0, right = n-1;
while (left <= right) {
int mid = (left + right) / 2;
if (list[mid].id == target) {
return mid;
} else if (list[mid].id < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
int search_by_name(struct student list[], int n, char target[]) {
// 线性查找
for (int i = 0; i < n; i++) {
if (strcmp(list[i].name, target) == 0) {
return i;
}
}
return -1;
}
int main() {
struct student list[N] = {{1, "Alice", 80}, {3, "Bob", 90}, {5, "Charlie", 85}, {7, "David", 95}, {9, "Emily", 70},
{11, "Frank", 75}, {13, "Grace", 95}, {15, "Henry", 60}, {17, "Ivy", 85}, {19, "Jason", 90},
{2, "Kevin", 85}, {4, "Lucy", 90}, {6, "Maggie", 80}, {8, "Nancy", 70}, {10, "Oscar", 65},
{12, "Peggy", 95}, {14, "Queenie", 85}, {16, "Roger", 80}, {18, "Steven", 60}, {20, "Tom", 70}};
int choice, target_id;
char target_name[20];
printf("请选择查找方式(1为按学号查找,2为按姓名查找):");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("请输入要查找的学号:");
scanf("%d", &target_id);
int index = search_by_id(list, N, target_id);
if (index != -1) {
printf("学号:%d,姓名:%s,成绩:%d\n", list[index].id, list[index].name, list[index].score);
} else {
printf("未找到该学生信息!\n");
}
break;
case 2:
printf("请输入要查找的姓名:");
scanf("%s", target_name);
int index2 = search_by_name(list, N, target_name);
if (index2 != -1) {
printf("学号:%d,姓名:%s,成绩:%d\n", list[index2].id, list[index2].name, list[index2].score);
} else {
printf("未找到该学生信息!\n");
}
break;
default:
printf("输入有误!\n");
break;
}
return 0;
}
```
阅读全文