本程序是大一C语言的练习,使用了链表,文件读写等。现在分享出来,有需要的同学可以看看。觉得还不错的话,点赞支持一下吧!
执行效果图:
文件目录:
源代码:
#include
#include
#include
#include
typedef struct people
{
int num;//用户编号
char ID[20] = "\0";//身份证;
char name[10]= "\0" ;//姓名
int sex;//性别,0-男,1-女
int age;//年龄
char phone[12]= "\0" ;//手机号
int condition;//风险,0-低风险,1-中风险 ,2-高风险
float temp;//温度
struct people* next;
}*People, peo;
typedef struct Ad
{
int Id;
char Key[20];
struct Ad* next;
}Admi;
Admi* tail = NULL;//保存账号信息的尾节点
int welcome(Admi** Admihead);//欢迎页面
void menu();//菜单
void help();//帮助信息
void printSingle(People p);//打印单个人员信息
People seacher(People head, char* id);//按照身份证查找,存在返回相应地址
People addPeople(People head);//增加人员信息
void change(People cur);//修改
People delByNum(People head,int num);//删除
void showAll(People head);//显示当前人员信息
int save(People head);//保存当前人员信息
People readInfo(People head);//读取文件信息
void get_account(int* account);//自动生成账号
void CreateAd(Admi** head);//创建账号
int Login(Admi* head);//登录
void saveAd(Admi* head);//退出程序保存信息
Admi* getAd();//读取账号信息
// 定义一些全局变量
int num = 0; //编号
int size = 0; //统计人数
const char filename[30] = { "疫情防控登记单.csv" };
int USER_CT = 1000;//账号编辑
//-----------------------------主函数----------------------
int main()
{
People head = NULL;//人员信息
Admi* Admihead = NULL;//账号
int choice,t;
Admihead = getAd();//读取账号信息
while (1)//登陆界面
{
if (welcome(&Admihead)==1)break;
system("pause");
system("cls");
}
system("pause");
system("cls");
while (1)//功能区
{
People tmp=NULL;
char id[20] = {"\0"};
menu();
printf("请输入功能序号:\n");
scanf("%d", &choice);
switch (choice)
{
case 0: help();
; break;//帮助信息
case 1:
head = readInfo(head);
; break; //刷新人员信息
case 2:
printf("请输入身份证号:\n");
scanf("%s", id);
tmp = seacher(head, id);
if (tmp) printSingle(tmp);
else printf("该人员信息不存在!\n");
break;//查询人员信息
case 3:
printf("请输入要更改人员的身份证号:\n");
scanf("%s", id);
tmp = seacher(head, id);
if (tmp) change(tmp);
else printf("该人员信息不存在!\n");
; break;//修改人员信息
case 4:
head=addPeople(head);
; break;//增加人员信息
case 5:
printf("请输入要删除的编号:\n");
scanf("%d", &t);
head=delByNum(head, t);
; break;//按编号删除信息
case 6:
showAll(head);
; break;//显示当前信息
case 7:
save(head);
; break;//保存当前人员信息
case 8:
saveAd(Admihead);
printf("成功退出!欢迎下次使用!\n");
return 0;; break; //退出系统
default:printf("请输入正确的序号(0~8)!\n");
}
system("pause");
system("cls");
}
return 0;
}
//-----------------------------函数区---------------------
void menu()
{
printf(" **********************************************************\n\n");
printf(" * 疫情防控管理系统 *\n");
printf(" **********************************************************\n\n");
printf("*************************系统功能菜单***********************\n");
printf(" ------------------------- ------------------------\n");
printf(" *****************************************************\n");
printf(" * 0.系统帮助及说明 * * 1.刷新人员信息 *\n");
printf(" *****************************************************\n");
printf(" * 2.查询人员信息 * * 3.修改人员信息 *\n");
printf(" *****************************************************\n");
printf(" * 4.增加人员信息 * * 5.按编号删除信息 *\n");
printf(" *****************************************************\n");
printf(" * 6.显示当前信息 * * 7.保存当前人员信息 *\n");
printf(" *****************************************************\n");
printf(" * 8.退出系统 *\n");
printf(" **************************\n");
printf(" ------------------------- ------------------------\n");
}
void help()
{
printf("-------------------帮助信息-------------------\n\n");
printf("1.输入序号1~8、并回车,选择相应功能。\n");
printf("2.身份证默认18位。\n");
printf("3.性别,年龄信息从身份证中自动获取。\n");
printf("4.退出程序之前要保存当前信息。\n");
printf("5.由于格式问题,在文件中直接更改的话可能导致部分数据乱码。\n");
}
People seacher(People head, char* id)
{
while (head)
{
if (head&&strcmp(head->ID, id) == 0)
{
return head;
}
head = head->next;
}
return NULL;//不存在
}
People addPeople(People head)
{
peo tmp;
printf("请输入姓名:\n");
scanf("%s", tmp.name);
printf("请输入身份证号:\n");
scanf("%s", tmp.ID);
if (strlen(tmp.ID) != 18)
{
printf("身份证不符合规范(18位),请重新输入!\n");
return head;
}
printf("请输入手机号:\n");
scanf("%s", tmp.phone);
printf("请输入风险等级:\n");
printf("0-低风险,1-中风险 ,2-高风险\n");
scanf("%d",&tmp.condition);
printf("请输入温度:\n");
scanf("%f", &tmp.temp);
if (tmp.ID[16] % 2)tmp.sex = 0;
else tmp.sex = 1;
char year[5] = { "\0" };
strncat(year, tmp.ID + 6, 4);
time_t timep;
struct tm* ti;
time(&timep);
ti = gmtime(&timep);
int age =1900+ti->tm_year- atoi(year);
tmp.age = age;
tmp.num = num;
if (seacher(head, tmp.ID))
{
printf("录入失败!该人员信息已经存在。\n");
return head;
}
People p = (People)malloc(sizeof(peo));
size++;
num++;
*p = tmp;//赋值
p->next = NULL;//
//采用按照编号插入
People cur = head,pre=NULL;
if (NULL == head)//插在头部
{
head = p;
return head;
}
while (cur)
{
if (cur->num > p->num)//满足插入条件
{
if (NULL == pre)//插在头部
{
p->next = cur;
head = p;
}
else
{
pre->next = p;
p->next = cur;
}
return head;//返回
}
else//迭代
{
pre = cur;
cur = cur->next;
}
}
//插在末尾
pre->next = p;
return head;
}
void printSingle(People p)
{
char sex[2][4] = { "男","女"};
char con[3][10] = { "低风险","中风险","高风险" };
printf("编号 姓名 性别 年龄 身份证号 手机号码 体温 风险等级 \n");
printf("%-8d%-10s%-7s%-6d%-23s%-20s%-8.1f%-8s\n",p->num,p->name,sex[p->sex],p->age,p->ID,p->phone,p->temp,con[p->condition]);
}
void change(People cur)
{
printSingle(cur);
printf("------------- 请输入更改后的信息 -------------\n");
printf("请输入姓名:\n");
scanf("%s", cur->name);
printf("请输入身份证号:\n");
scanf("%s", cur->ID);
printf("请输入手机号:\n");
scanf("%s", cur->phone);
printf("请输入风险等级:\n");
printf("0-低风险,1-中风险 ,2-高风险\n");
scanf("%d", &cur->condition);
printf("请输入温度:\n");
scanf("%f", &cur->temp);
if (cur->ID[16] % 2)cur->sex = 0;
else cur->sex = 1;
char year[5] = { "\0" };
strncat(year, cur->ID + 6, 4);
time_t timep;
struct tm* ti;
time(&timep);
ti = gmtime(&timep);
cur->age = 1900 + ti->tm_year - atoi(year);
}
People delByNum(People head, int num)
{
People tmp = head, pre = NULL;
while (tmp)
{
if (tmp->num == num)
{
if (!pre)
{
head = tmp->next;
free(tmp);
}
else
{
pre->next = tmp->next;
free(tmp);
}
printf("删除成功!\n");
return head;//返回
}
else
{
pre = tmp;
tmp = tmp->next;
}
}
printf("删除失败!不存在该编号人员信息!\n");
return head;
}
void showAll(People head)
{
People p = head;
int low = 0,mid = 0,high = 0;
char sex[2][4] = { "男","女" };
char con[3][10] = { "低风险","中风险","高风险" };
printf("编号 姓名 性别 年龄 身份证号 手机号码 体温 风险等级 \n");
while (p)
{
printf("%-8d%-10s%-7s%-6d%-23s%-20s%-8.1f%-8s\n", p->num, p->name, sex[p->sex], p->age, p->ID, p->phone, p->temp, con[p->condition]);
if (p->condition == 0)low++;
else if (p->condition == 1)mid++;
else high++;
p = p->next;
}
printf("\n\n统计结果为:低风险%d人,中风险%d人,高风险%d人\n", low, mid, high);
}
int save(People head)
{
FILE* fp=fopen(filename,"w");
if (!fp)
{
printf("保存失败!\n");
return 0;
}
People p = head;
fprintf(fp, "编号,姓名,性别,年龄,身份证号,手机号码,体温,风险等级\n");
while (p->next)
{
fprintf(fp, "%d,%s,%d,%d,%s,%s,%0.2f,%d\n", p->num, p->name, p->sex, p->age, p->ID, p->phone, p->temp, p->condition);
p = p->next;
}
fprintf(fp, "%d,%s,%d,%d,%s,%s,%0.2f,%d", p->num, p->name, p->sex, p->age, p->ID, p->phone, p->temp, p->condition);
fclose(fp);
printf("保存成功!");
return 1;
}
People readInfo(People head)
{
FILE* fp = fopen(filename, "r");
if (!fp)
{
printf("文件读取失败!\n");
return head;
}
char buf[100] = { "\0" };
char sin[30] = { "\0" };
fscanf(fp,"%s" ,buf);//吸收掉标题
while (!feof(fp))
{
int i = 0,flag=0;
People p=(People)malloc(sizeof(peo));
p->next = NULL;
buf[0] = '\0';
fscanf(fp, "%s", buf);//读取信息
while (buf[i] && buf[i] != '\n')
{
int j = 0;
while (buf[i]&&buf[i] != ','&&buf[i]!='\n')
{
sin[j] = buf[i];
i++, j++;
}
sin[j] = '\0';//
flag++,i++;//
if (1 == flag)
{
p->num = atoi(sin);
if (num <= p->num)num = p->num + 1;//更新num编号
}
else if (2 == flag)
{
strcpy(p->name, sin);
}
else if (3 == flag)
{
p->sex = atoi(sin);
}
else if (4 == flag)
{
p->age = atoi(sin);
}
else if (5 == flag)
{
strcpy(p->ID, sin);
}
else if (6 == flag)
{
strcpy(p->phone, sin);
}
else if (7 == flag)
{
p->temp=atof(sin);
}
else if (8 == flag)
{
p->condition = atoi(sin);
sin[0] = '\0';
break;
}
sin[0] = '\0';
}
flag = 0;
//加入链表
//采用按照编号插入
if (p->condition != 0 && p->condition != 1 && p->condition != 2)
{
free(p);
break;//最后不符合的
}
People cur = head, pre = NULL;
if (NULL == head)//插在头部
{
head = p;
}
else//其他
{
while (cur)
{
if (cur->num == p->num)//已经存在
{
free(p);
break;
}
else if (cur->num > p->num)//满足插入条件
{
if (NULL == pre)//插在头部
{
p->next = cur;
head = p;
}
else
{
pre->next = p;
p->next = cur;
}
break;//返回
}
else//迭代
{
pre = cur;
cur = cur->next;
}
}
if (!cur)pre->next = p;//插在末尾
}
}
//关闭文件
fclose(fp);
printf("刷新成功!\n");
return head;
}
int welcome(Admi** head)
{
printf("\n\n");
printf(" 欢 迎 使 用 疫 情 防 控 管 理 系 统 ! \n\n");
printf(" 祝 您 使 用 愉 快 !\n\n\n");
printf(" 1.创 建 管 理 员 账 号\n\n");
printf(" 2.登 录 账 号\n\n");
printf(" 其他.退 出 程 序\n\n");
int c;
scanf("%d", &c);
if (1 == c)
{
CreateAd(head);
return 0;
}
else if(2==c)
{
if (Login(*head))return 1;
else return 0;
}
else if (3 == c)
{
printf("欢迎下次使用!\n");
exit(0);
}
}
//-----------------------------------------------------------------------------------------------
void get_account(int* account)//自动生成账号
{
*account = ++USER_CT;
}
void CreateAd(Admi** head)//创建账号
{
int account;
char key[12], tmp[12];
get_account(&account);
printf("您的账号为:%d", account);
printf("请输入您的密码(不超过十位):\n");
scanf("%s", key);
system("cls");
printf("请再次输入您的密码,以确认无误。\n");
scanf("%s", tmp);
while (strcmp(tmp, key) != 0)
{
printf("抱歉!两次输入不一致,请检查后重新输入:\n");
printf("请输入您的密码(不超过十位):\n");
scanf("%s", key);
system("cls");
printf("请再次输入您的密码,以确认无误。\n");
scanf("%s", tmp);
}
printf("创建成功!\n");
printf("请牢记您的账号:%d\n", account);
printf("密码:%s\n", key);
Admi* p = (Admi*)malloc(sizeof(Admi));
p->next = NULL;
p->Id = account;
strcpy(p->Key, key);
if (tail)
{
tail->next = p;
tail = p;
}
else
{
*head = p;
tail = p;
}
return;
}
int Login(Admi* head)
{
int account;
char key[12];
int flag = 0;
printf("请输入账号:\n");
scanf("%d", &account);
printf("请输入密码:\n");
scanf("%s", key);
while (head)
{
if (head->Id == account && strcmp(head->Key, key) == 0)
{
flag = 1;
break;
}
head = head->next;
}
if (flag)
{
printf("登陆成功!\n");
return 1;
}
else
{
printf("登陆失败!\n");
return 0;
}
//system("pause");
}
void saveAd(Admi* head)//退出程序保存信息
{
FILE* fp;
fp = fopen("账号密码.txt", "w");
if (fp == NULL)
{
printf("账号信息保存失败!\n");
return;
}
fprintf(fp, "%d\n", USER_CT);
while (head)
{
fprintf(fp, "%d %s\n", head->Id, head->Key);
head = head->next;
}
fclose(fp);
system("pause");
}
Admi* getAd()//读取账号信息
{
FILE* fp;
fp = fopen("账号密码.txt", "a+");
if (fp == NULL)
{
printf("账号信息读取失败!\n");
exit(1);
return NULL;
}
Admi* tmp, * head = NULL;
if (fscanf(fp, "%d\n", &(USER_CT)) == EOF)//
{
USER_CT = 1000;
fclose(fp);
return NULL;
}
while (!feof(fp))
{
tmp = (Admi*)malloc(sizeof(Admi));
tmp->next = NULL;
fscanf(fp, "%d %s\n", &(tmp->Id), tmp->Key);
if (head == NULL)
{
head = tmp;
tail = tmp;
}
else
{
tail->next = tmp;
tail = tmp;
}
}
fclose(fp);
return head;
}