管理员头文件
管理员和用户的头文件中的内容类似
但是要在定义使用时体现出区别
相比用户管理员中的内容相对应就会缺失一部分功能
所以管理员部分的头文件会比用户部分的少很多
其中涉及到形式参数是指针的是想通过形式参数的改变引起实际参数的改变
管理员函数功能实现
完整源码
#pragma once
typedef struct Manger_user {
char name[10];
char idcard[20];
char phone[12];
}Muser;
typedef struct Manger_card {
int id;
int password;
int count;
bool islocked;
Manger_user user;
char create_card_datetime[30];
}Mcard;
typedef struct Manger_list {
Mcard* arr;
int arr_size;
int arr_length;
}Mlist,*MPlist;
void init_Muser(Muser* user, const char* name, const char* idcard, const char* phone);
void init_Manger_list(MPlist list);
void init_Mcard(Mcard, int id, int password, Manger_user* user);
bool IsFull(MPlist list);
void Grow(MPlist list);
int Seach(MPlist list, int id);
void register_Manger(MPlist list, int id, int password, Manger_user *user);
int login_Manger(MPlist list, int id, int password);
void destory_Manger(MPlist list);
void active_Manger(MPlist list, const char* idcard);
#define _CRT_SECURE_NO_WARNINGS
#include"Manger.h"
#include <stdlib.h>
#include <cassert>
#include <string.h>
#include <stdio.h>
#include <time.h>
#define Mangercard 10
void init_Muser(Muser* user, const char* name, const char* idcard, const char* phone) {
strcpy(user->idcard, idcard);
strcpy(user->name, name);
strcpy(user->phone, phone);
}
void init_Manger_list(MPlist list) {
list->arr = (Mcard*)malloc(sizeof(Mcard) * Mangercard);
assert(list->arr != NULL);
list->arr_size = 0;
list->arr_length = Mangercard;
}
void init_Mcard(Mcard* card, int id, int password, Manger_user* user) {
card->id = id;
card->password = password;
card->islocked = false;
card->count = 0;
init_Muser(&card->user, user->name, user->idcard, user->phone);
}
bool IsFull(MPlist list) {
assert(list != NULL);
return list->arr_size == list->arr_length;
}
void Grow(MPlist list) {
int newlen = 2 * list->arr_length;
Manger_card* p = (Manger_card*)realloc(list->arr, sizeof(Mcard) * newlen);
assert(p != NULL);
list->arr = p;
list->arr_length = newlen;
}
int Seach(MPlist list, int id) {
for (int i = 0; i < list->arr_size; i++) {
if (list->arr[i].id == id) {
return i;
}
}
return -1;
}
void register_Manger(MPlist list, int id, int password, Manger_user* user) {
if (IsFull(list)) {
Grow(list);
}
int index = Seach(list, id);
if (index != -1&&list->arr[index].user.idcard==user->idcard) {
printf("已注册,请登录\n");
return;
}
Manger_card card;
init_Mcard(&card, id, password, user);
list->arr[list->arr_size++] = card;
printf("注册成功\n");
return;
}
int login_Manger(MPlist list, int id, int password) {
int index = Seach(list, id);
if (index == -1)return -1;
if (list->arr[index].id == id && list->arr[index].password == password && !list->arr[index].islocked) {
list->arr[index].count = 0;
printf("登陆成功\n");
return index;
}
else {
printf("登陆失败\n");
list->arr[index].count++;
if (list->arr[index].count == 3) {
list->arr[index].islocked = true;
printf("卡已被锁请激活\n");
}
return -1;
}
}
void destory_Manger(MPlist list) {
free(list->arr);
list->arr = NULL;
}
void active_Manger(MPlist list, const char* idcard) {
assert(list != NULL);
for (int i = 0; i < list->arr_size; i++) {
if (strcmp(list->arr[i].user.idcard, idcard) == 0) {
list->arr[i].islocked = false;
printf("激活成功,请登录\n");
return;
}
}
printf("该用户不存在\n");
}
管理员的函数实现方式与用户的方式大致相同,只是功能分布有所不同