帮我用c语言写一个每个函数都在不同文件的运用了结构体的功能是可以增删改查的代码
时间: 2024-03-19 19:43:12 浏览: 14
好的,以下是一个示例程序,其中包含了增加、删除、修改和查询结构体的功能,每个函数分别被定义在不同的文件中:
文件1:struct.h
```c
#ifndef STRUCT_H
#define STRUCT_H
#define MAX_SIZE 100
typedef struct {
int id;
char name[20];
int age;
} Person;
Person people[MAX_SIZE];
int count;
void add_person();
void delete_person(int id);
void update_person(int id);
void search_person(int id);
#endif
```
文件2:add_person.c
```c
#include <stdio.h>
#include "struct.h"
void add_person() {
if (count >= MAX_SIZE) {
printf("The person table is full.\n");
return;
}
Person p;
printf("Enter person's id: ");
scanf("%d", &p.id);
printf("Enter person's name: ");
scanf("%s", p.name);
printf("Enter person's age: ");
scanf("%d", &p.age);
people[count++] = p;
printf("Person added successfully.\n");
}
```
文件3:delete_person.c
```c
#include <stdio.h>
#include "struct.h"
void delete_person(int id) {
for (int i = 0; i < count; i++) {
if (people[i].id == id) {
for (int j = i; j < count - 1; j++) {
people[j] = people[j + 1];
}
count--;
printf("Person deleted successfully.\n");
return;
}
}
printf("Person not found.\n");
}
```
文件4:update_person.c
```c
#include <stdio.h>
#include "struct.h"
void update_person(int id) {
for (int i = 0; i < count; i++) {
if (people[i].id == id) {
printf("Enter person's name: ");
scanf("%s", people[i].name);
printf("Enter person's age: ");
scanf("%d", &people[i].age);
printf("Person updated successfully.\n");
return;
}
}
printf("Person not found.\n");
}
```
文件5:search_person.c
```c
#include <stdio.h>
#include "struct.h"
void search_person(int id) {
for (int i = 0; i < count; i++) {
if (people[i].id == id) {
printf("Person's id: %d\n", people[i].id);
printf("Person's name: %s\n", people[i].name);
printf("Person's age: %d\n", people[i].age);
return;
}
}
printf("Person not found.\n");
}
```
文件6:main.c
```c
#include <stdio.h>
#include "struct.h"
int main() {
int choice, id;
do {
printf("1. Add person\n");
printf("2. Delete person\n");
printf("3. Update person\n");
printf("4. Search person\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
add_person();
break;
case 2:
printf("Enter person's id to delete: ");
scanf("%d", &id);
delete_person(id);
break;
case 3:
printf("Enter person's id to update: ");
scanf("%d", &id);
update_person(id);
break;
case 4:
printf("Enter person's id to search: ");
scanf("%d", &id);
search_person(id);
break;
case 5:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Try again.\n");
}
} while (choice != 5);
return 0;
}
```
这个程序可以在不同的文件中进行函数定义,通过结构体来存储和操作人员信息,并允许用户增加、删除、修改和查询人员信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)