使用c语言创建一个链表,具有//工号 //姓名 //性别 //出生年月//籍贯//部门的信息,可以进行舔添加员工信息的操作,使用scanf函数输入员工信息,但是工号最大输入只能8位,输入过多则显示错误重新让用户输入工号,性别只能输入男或者女,输入其他的显示错误重新让用户输入性别的代码
时间: 2023-07-07 09:41:49 浏览: 45
好的,以下是创建链表并添加员工信息的C语言代码,满足你的需求:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//定义员工信息结构体
typedef struct Employee {
char id[9]; //工号
char name[20]; //姓名
char sex[5]; //性别
char birth[15]; //出生年月
char hometown[20]; //籍贯
char department[20]; //部门
struct Employee *next; //指向下一个员工信息结构体的指针
} Employee;
//函数声明
void addEmployee(Employee **head);
int checkId(char *id);
int checkSex(char *sex);
int main(void) {
Employee *head = NULL; //定义链表头指针
int choice = 0;
do {
printf("1. 添加员工信息\n");
printf("2. 退出\n");
printf("请选择操作: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addEmployee(&head);
break;
case 2:
printf("谢谢使用!\n");
break;
default:
printf("无效的操作!\n");
break;
}
} while (choice != 2);
return 0;
}
//添加员工信息函数
void addEmployee(Employee **head) {
Employee *newNode = (Employee *)malloc(sizeof(Employee)); //新建员工信息结构体
printf("请输入员工信息:\n");
//输入工号,最多8位
do {
printf("工号(最多8位):");
scanf("%s", newNode->id);
} while (checkId(newNode->id) == 0);
//输入姓名
printf("姓名:");
scanf("%s", newNode->name);
//输入性别,只能输入男或女
do {
printf("性别(男/女):");
scanf("%s", newNode->sex);
} while (checkSex(newNode->sex) == 0);
//输入出生年月
printf("出生年月:");
scanf("%s", newNode->birth);
//输入籍贯
printf("籍贯:");
scanf("%s", newNode->hometown);
//输入部门
printf("部门:");
scanf("%s", newNode->department);
newNode->next = NULL; //新建的员工信息结构体的指针指向NULL
if (*head == NULL) { //如果链表为空,则将新建的员工信息结构体设为链表头
*head = newNode;
} else { //如果链表不为空,则将新建的员工信息结构体添加到链表末尾
Employee *current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
printf("员工信息添加成功!\n");
}
//检查工号是否合法
int checkId(char *id) {
if (strlen(id) > 8) {
printf("工号过长,请重新输入!\n");
return 0;
}
return 1;
}
//检查性别是否合法
int checkSex(char *sex) {
if (strcmp(sex, "男") != 0 && strcmp(sex, "女") != 0) {
printf("性别输入有误,请重新输入!\n");
return 0;
}
return 1;
}
```
以上代码中,`checkId`函数和`checkSex`函数用于检查用户输入的工号和性别是否合法。如果不合法,则在函数内部输出错误信息并返回0,让用户重新输入,直到输入合法为止。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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_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)