You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
4.9 KiB

#include<stdbool.h>
#include<limits.h>
#include<math.h>
#include<stdlib.h>
#include<conio.h>
#include<Windows.h>
#include<stdio.h>
#include<string.h>
unsigned int counts = 0;
/*结构体定义*/
typedef struct student {
char no[21];
char name[11];
bool fame;
unsigned short int age;
char oth[151];
struct student * next;
}student;
struct student * first = NULL;//第一个
struct student * loc = NULL;//当前(一般为NULL)
bool del(char *);//删除单个
bool edit(char*);//编辑单个
student * search_by_no(char *);//按学号查找
void search_by_name(char *);//按名字搜索
bool check_password(void);//检查密码
void run(void);//程序开始接入点
bool load(void);//从文件加载数据
void fgets_nn(char * , int, FILE *);//丢弃换行符的整行char*输入
bool check_no(char *);//检查学号是否冲突
int main() {
run();
system("pause");
return 0;
}
bool del(char * no) {
student *ed = search_by_no(no);
if (ed == NULL)
return false;
student temp;
if (ed == first) {
temp = *first;
free(first);
first = temp.next;
return true;
}
student *lu = first;
while (lu->next != NULL) {
if (strlen(lu->next->no) == strlen(no) && !strcmp(lu->next->no, no)) {
break;
}
lu = lu->next;
}
temp = *lu->next;
free(lu->next);
lu->next = temp.next;
counts--;
save();
return true;
}
bool edit(char * no) {
student *l = search_by_no(no);
if (l == NULL)
return false;
printf("请输入学生学号:\n");
while (1) {
char no_temp[21];
fgets_nn(no_temp, 21, stdin);
if (!strcmp(no_temp, "\0")) {
printf("学号不能为空:\n");
continue;
}
if (!(strlen(l->no) == strlen(no_temp) && !strcmp(l->no, no_temp)))
if (!check_no(no_temp))
printf("学号已存在!\n");
else {
int i=0;
for ( i = 0; i < 21; i++)
l->no[i] = no_temp[i];
break;
}
else
break;
}
printf("请输入学生姓名:\n");
do {
fgets_nn(l->name, 11, stdin);
} while (!strcmp(l->name, "\0"));
while (1) {
printf("请输入学生性别(1.男 2.女):\n");
char t = getch();
if (t == '1') {
l->fame = true;
break;
}
else if (t == '2') {
l->fame = false;
break;
}
else {
printf("输入不正确\n");
continue;
}
}
while (1) {
printf("请输入学生年龄:\n");
if (scanf("%hd", &l->age) != 1) {
printf("输入不正确\n");
while (getchar() != '\n')continue;
continue;
}
break;
}
printf("备注:\n");
while (getchar() != '\n')continue;
fgets_nn(l->oth, 151, stdin);
save();
return true;
}
student * search_by_no(char * no) {
student *l = first;
while (l != NULL) {
if (strlen(l->no) == strlen(no) && !strcmp(l->no, no)) {
return l;
}
l = l->next;
}
return NULL;
}
void search_by_name(char * name) {
student *l = first;
unsigned int c = 0;
system("cls");
while (l != NULL) {
if (strlen(l->name) == strlen(name) && !strcmp(l->name, name)) {
print_1(l);
printf("\n");
c++;
}
l = l->next;
}
if (c == 0) {
printf("未检索到内容!\n");
}
else {
printf("\n已检索到%d条名字为%s的学生\n\n", c, name);
}
}
void run(void) {
int n=3;
while (n--) {
if (check_password()) {
load();
show_menu();
}
else if(n!=0){
printf("密码错误,你还有%d次机会!\n",n);
}
}
printf("身份验证失败,正在退出系统\n");
exith();
}
bool check_password(void) {
FILE * password;
password = fopen("password", "r");
if (password == NULL) {
printf("第一次进入系统,请先设置密码(最长16位,最短6位):\n");
password = fopen("password", "w");
system("attrib password +s +h +r");
char pw[17];
char pw2[17];
while (1) {
fgets_nn(pw, 17, stdin);
if (strlen(pw) < 6) {
system("cls");
printf("密码至少设置6位:\n");
continue;
}
system("cls");
printf("请确认密码:\n");
fgets_nn(pw2, 17, stdin);
if (strcmp(pw, pw2)||(strlen(pw)!=strlen(pw2))) {
system("cls");
printf("密码确认错误,请重新设置密码:\n");
continue;
}
break;
}
fprintf(password, "%s", pw);
fclose(password);
system("cls");
return true;
}
char pw[17];
fgets_nn(pw, 17, password);
if (feof(password)) {
fclose(password);
system("attrib password -s -h -r");
remove("password");
return check_password();
}
printf("请输入密码:\n");
char pwi[17];
fgets_nn(pwi, 17, stdin);
system("cls");
if (!strcmp(pw, pwi))
return true;
else
return false;
}
bool load(void) {
FILE * data;
data = fopen("data", "r");
if (data == NULL)
return false;
student temp;
while (1) {
fgets_nn(temp.no, 21, data);
if (feof(data)){
break;
}
fgets_nn(temp.name, 11, data);
fscanf(data, "%hhu", &temp.fame);
fscanf(data, "%hd", &temp.age);
fgetc(data);//去除留下的\n
fgets_nn(temp.oth, 151, data);
creat(temp);
counts++;
}
fclose(data);
return true;
}
void fgets_nn(char *buffer, int max, FILE * file) {
fgets(buffer, max, file);
int i=0;
for (i = 0; i < max; i++) {
if (buffer[i] == '\n') {
buffer[i] = '\0';
return;
}
}
}
bool check_no(char * no) {
student *l = first;
while (l != NULL) {
if (strlen(l->no) == strlen(no) && !strcmp(l->no, no)) {
return false;
}
l = l->next;
}
return true;
}