仿学生管理系统
简单的增删改查,链表的排序还不会,勿喷,代码数最长的一次,{:10_265:}#include <stdio.h>#include <stdlib.h>
#include <string.h>
typedef struct _node
{
char name;
char id;
char score;
struct _node *next;
} Node;
typedef struct _list
{
Node *head;
} List;
//--1--插入新的学生(头插法)
void AddStudentByHead(List *list);
//尾插法V1
void AddStudentByEndV1(List *list);
//尾插法V2只能单独使用
void AddStudentByEndV2(List *list);
void ChangeStudent(List *list, char *input);
void Input(Node *p);
int Search(List *list, char *input);
void Delete(List *list, char *input);
void PrintStudent(List *list);
void ClearList(List *list);
Node *PrintFlage(List *list, char *input);
int main(void)
{
List list;
list.head = NULL;
char code;
int c;
char input1;
char input2;
char input3;
int flag = 0;
Node *temp;
while (1)
{
printf("欢迎使用学生成绩管理系统\n");
printf("----1.插入新的学生-----\n");
printf("----2.查找已有学生----\n");
printf("----3.更改学生--------\n");
printf("----4.删除已有学生----\n");
printf("----5.显示已有学生----\n");
printf("----6.退出系统-------\n");
printf("请输入指令代码(1-6)\n");
do
{
scanf("%c", &code);
} while (code>54||code<49);
switch (code)
{
case '1':
printf("请选择插入学生的方法(1-头插法 2-尾插法V1):");
scanf("%d", &c);
switch (c)
{
case 1:
AddStudentByHead(&list);
break;
case 2:
AddStudentByEndV1(&list);
break;
default:
printf("输入指令错误\n");
}
break;
case '2':
printf("请输入你要查找的学生:");
scanf("%s", input1);
flag = (Search(&list, input1));
if (flag > 0)
{
temp = PrintFlage(&list, input1);
printf("学生姓名:%s\t学号%s\t分数:%s\t", temp->name, temp->id, temp->score);
}
else
{
printf("不好意思,查无此人\n");
}
break;
case '3':
printf("请输入你要更改的学生名字:");
scanf("%s", input2);
flag = Search(&list, input2);
temp = PrintFlage(&list, input2);
if (flag > 0)
{
printf("当前的学生信息为:\n");
printf("学生姓名:%s\t学号%s\t分数:%s\t", temp->name, temp->id, temp->score);
printf("请输入您要更改的学生信息\n");
ChangeStudent(&list, input2);
}
else
{
printf("不好意思,你要更改的学生名字尚未存在\n");
}
break;
case '4':
printf("请输入你要删除的学生名字:");
scanf("%s", input3);
if (Search(&list, input3) > 0)
{
Delete(&list, input3);
}
else
{
printf("不好意思,你要删除的学生名字尚未存在\n");
}
break;
case '5':
PrintStudent(&list);
break;
case '6':
if (!list.head)
{
printf("还没有信息呢\n");
}
goto END;
default:
printf("输入指令错误\n");
break;
}
printf("^__^\n");
printf("\n\n");
}
END:
ClearList(&list), printf("删库跑路成功^__^\n");
system("pause");
return 0;
}
//--1--插入新的学生
void AddStudentByHead(List *list)
{
Node *p, *temp;
p = (Node *)malloc(sizeof(Node));
Input(p);
if (list->head)
{
temp = list->head;
list->head = p;
p->next = temp;
}
else
{
list->head = p;
p->next = NULL;
}
}
//尾插法
void AddStudentByEndV1(List *list)
{
Node *p;
p = (Node *)malloc(sizeof(Node ));
Input(p);
Node *last;
last = list->head;
if (last)
{
while (last->next)
{
last = last->next;
}
last->next = p;
p->next=NULL;
}
else
{
list->head = p;
p->next = NULL;
}
}
//尾插法
/*
void AddStudentByEndV2(List *list)
{
Node *p;
p = (Node *)malloc(sizeof(Node ));
Input(p);
static Node *tail;
if (list->head)
{
tail=list->head;
tail->next = p;
p->next = NULL;
}
else
{
list->head = p;
p->next = NULL;
}
tail = p;
}
*/
void ChangeStudent(List *list, char *input)
{
Node *p;
for (p = list->head; p; p = p->next)
{
if (!strcmp(p->name, input))
{
Input(p);
break;
}
}
}
void Input(Node *p)
{
printf("请输入姓名:");
scanf("%s", p->name);
printf("请输入学号(2位数字):");
scanf("%s", p->id);
printf("请输入学生分数(小于1000):");
scanf("%s", p->score);
}
int Search(List *list, char *input)
{
int flag = -1; //标志
Node *p;
for (p = list->head; p; p = p->next)
{
if (!strcmp(p->name, input))
{
flag = 1;
break;
}
}
return flag;
}
Node *PrintFlage(List *list, char *input)
{
Node *p;
for (p = list->head; p; p = p->next)
{
if (!strcmp(p->name, input))
{
break;
}
}
return p;
}
void Delete(List *list, char *input)
{
Node *p, *q = NULL;
for (p = list->head; p; q = p, p = p->next)
{
if (!strcmp(p->name,input))
{
if (q)
{
q->next = p->next;
}
else
{
list->head = p->next;
}
free(p);
break;
}
}
}
void PrintStudent(List *list)
{
Node *p;
for (p = list->head; p; p = p->next)
{
printf("学生姓名:%s\t学号:%s\t分数:%s\t\n", p->name, p->id, p->score);
}
}
//--6--删库跑路
void ClearList(List *list)
{
Node *p, *q;
for (p = list->head; p; p = q)
{
q = p->next;
free(p);
}
}
还是@zltzlt 的功能全
这是Ta的代码(Python)
import pickle
from os.path import isfile
from os import remove
def main():
running = True
while running:
menu()
code = '9'
count = 0
while code not in :
if count == 0:
code = input("请输入:")
else:
code = input("输入有误!请重新输入:")
count += 1
if code == '0':
running = False
elif code == '1':
insert()
elif code == '2':
search()
elif code == '3':
delete()
elif code == '4':
modify()
elif code == '5':
sort()
elif code == '6':
total()
elif code == '7':
show()
else:
input("感谢您使用学生信息查询系统程序,请按 <Enter> 键退出程序!")
def menu():
print('''
╔———————————————学生信息管理系统————————————————╗
=============== 功能菜单 ================
1 录入学生信息
2 查找学生信息
3 删除学生信息
4 修改学生信息
5 排序
6 统计学生总人数
7 显示所有学生信息
0 退出系统
==========================================
说明:通过数字选择菜单
╚——————————————————————————————————————————————╝
''')
def insert():
continue_ = 'y'
while continue_ == 'y':
ID = input("请输入 ID(如 1001):")
name = input("请输入名字:")
chinese = input("请输入语文成绩:")
while not is_float(chinese):
chinese = input("输入无效!请重新输入语文成绩:")
chinese = float(chinese)
math = input("请输入数学成绩:")
while not is_float(math):
math = input("输入无效!请重新输入数学成绩:")
math = float(math)
english = input("请输入英语成绩:")
while not is_float(english):
english = input("输入无效!请重新输入英语成绩:")
english = float(english)
score = [{'id': ID, 'name': name,
'chinese': chinese, 'math': math, 'english': english}]
if not isfile("students.pkl"):
file = open("students.pkl", "ab")
pickle.dump(score, file)
file.close()
else:
file = open("students.pkl", "rb+")
score_list = pickle.load(file)
score_list.extend(score)
file.close()
file = open("students.pkl", "wb")
pickle.dump(score_list, file)
file.close()
continue_ = input("是否继续添加?(y/n)")
while continue_ not in ['y', 'n']:
continue_ = input("输入无效!是否继续添加?(y/n)")
else:
print("学生信息录入完毕!")
def search():
continue_ = 'y'
while continue_ == 'y':
if not isfile("students.pkl"):
print("没有任何数据!")
return
way = input("按ID查输入1;按姓名查输入2:")
while way not in ['1', '2']:
way = input("输入无效!按ID查输入1;按姓名查输入2:")
if way == '1':
students = pickle.load(open("students.pkl", "rb"))
ID = input("请输入学生 ID:")
result = []
for each in students:
if each['id'] == ID:
result.append(each)
else:
students = pickle.load(open("students.pkl", "rb"))
name = input("请输入学生姓名:")
result = []
for each in students:
if each['name'] == name:
result.append(each)
result_str = ''
result_str += 'ID'.center(6)
result_str += '姓名'.center(12)
result_str += '语文'.center(10)
result_str += '数学'.center(10)
result_str += '英语'.center(10)
result_str += '总成绩'.center(11)
for i in result:
result_str += "\n"
result_str += i['id'].center(6)
result_str += i['name'].center(12)
result_str += str(i['chinese']).center(12)
result_str += str(i['math']).center(12)
result_str += str(i['english']).center(12)
result_str += str(i['chinese'] + i['math'] +
i['english']).center(13)
print(result_str)
continue_ = input("是否继续查询 (y/n)?")
while continue_ not in ['y', 'n']:
continue_ = input("输入无效!是否继续查询 (y/n)?")
def delete():
if not isfile("students.pkl"):
print("没有学生信息!")
return
continue_ = 'y'
while continue_ == 'y':
show()
ID = input("请输入要删除的学生ID:")
students = pickle.load(open("students.pkl", "rb"))
result = []
for each in students:
if each['id'] == ID:
result.append(each)
if not result:
print(f"没有找到ID为 {ID} 的学生信息...")
show()
else:
for i in result:
students.remove(i)
if not students:
remove("students.pkl")
print(f"ID 为 {ID} 的学生已成功删除!不可继续删除!")
return
else:
pickle.dump(students, open("students.pkl", "wb"))
print(f"ID 为 {ID} 的学生已成功删除!")
continue_ = input("是否继续删除 (y/n)?")
while continue_ not in ['y', 'n']:
continue_ = input("输入无效!是否继续删除 (y/n)?")
def modify():
if not isfile("students.pkl"):
print("没有学生信息!")
return
show()
continue_ = 'y'
while continue_ == 'y':
ID = input("请输入要修改的学生ID:")
students = pickle.load(open("students.pkl", "rb"))
result = []
for each in students:
if each['id'] == ID:
result.append(each)
if not result:
print(f"没有找到ID为 {ID} 的学生信息...")
show()
elif len(result) != 1:
print("ID 有相同的情况!")
else:
students.remove(result)
name = input("请输入名字:")
chinese = input("请输入语文成绩:")
while not is_float(chinese):
chinese = input("输入无效!请重新输入语文成绩:")
chinese = float(chinese)
math = input("请输入数学成绩:")
while not is_float(math):
math = input("输入无效!请重新输入数学成绩:")
math = float(math)
english = input("请输入英语成绩:")
while not is_float(english):
english = input("输入无效!请重新输入英语成绩:")
english = float(english)
score = {'id': ID, 'name': name,
'chinese': chinese, 'math': math, 'english': english}
students.append(score)
pickle.dump(students, open("students.pkl", "wb"))
continue_ = input("是否继续修改 (y/n)?")
while continue_ not in ['y', 'n']:
continue_ = input("输入无效!是否继续修改 (y/n)?")
def sort():
if not isfile("students.pkl"):
print("没有学生信息!")
return
show()
reverse = input("请选择(0升序;1降序):")
while reverse not in ['0', '1']:
reverse = input("输入无效!请选择(0升序;1降序):")
reverse = bool(int(reverse))
students = pickle.load(open("students.pkl", "rb"))
way = input("请选择排序方式"
"(1按语文成绩排序;"
"2按数学成绩排序;"
"3按英语成绩排序;"
"0按总成绩排序):")
while way not in ['0', '1', '2', '3']:
way = input("输入无效!请选择排序方式"
"(1按语文成绩排序;"
"2按数学成绩排序;"
"3按英语成绩排序;"
"0按总成绩排序):")
if way == '0':
def condition(x):
return x['chinese'] + x['math'] + x['english']
elif way == '1':
def condition(x):
return x['chinese']
elif way == '2':
def condition(x):
return x['math']
else:
def condition(x):
return x['english']
result = sorted(students, key=condition, reverse=reverse)
result_str = ''
result_str += 'ID'.center(6)
result_str += '姓名'.center(12)
result_str += '语文'.center(10)
result_str += '数学'.center(10)
result_str += '英语'.center(10)
result_str += '总成绩'.center(11)
for i in result:
result_str += "\n"
result_str += i['id'].center(6)
result_str += i['name'].center(12)
result_str += str(i['chinese']).center(12)
result_str += str(i['math']).center(12)
result_str += str(i['english']).center(12)
result_str += str(i['chinese'] + i['math'] +
i['english']).center(13)
print(result_str)
def total():
try:
print("一共有",
str(len(pickle.load(open("students.pkl", "rb")))), "名学生!")
except FileNotFoundError:
print("没有学生信息!")
def show():
if not isfile("students.pkl"):
print("没有学生信息!")
return
result = pickle.load(open("students.pkl", "rb"))
result_str = ''
result_str += 'ID'.center(6)
result_str += '姓名'.center(12)
result_str += '语文'.center(10)
result_str += '数学'.center(10)
result_str += '英语'.center(10)
result_str += '总成绩'.center(11)
for i in result:
result_str += "\n"
result_str += i['id'].center(6)
result_str += i['name'].center(12)
result_str += str(i['chinese']).center(12)
result_str += str(i['math']).center(12)
result_str += str(i['english']).center(12)
result_str += str(i['chinese'] + i['math'] +
i['english']).center(13)
print(result_str)
def is_float(number):
try:
float(number)
except ValueError:
return False
else:
return True
if __name__ == '__main__':
try:
main()
except BaseException as e:
print("啊哦,出错了!")
print(e)
input("请按 <Enter> 键退出程序!")
wuqramy 发表于 2020-3-7 14:32
还是@zltzlt 的功能全
这是Ta的代码(Python)
哈哈,用一下他的主界面就好看多了
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _node
{
char name;
char id;
char score;
struct _node *next;
} Node;
typedef struct _list
{
Node *head;
} List;
//--1--插入新的学生(头插法)
void AddStudentByHead(List *list);
//尾插法V1
void AddStudentByEndV1(List *list);
//尾插法V2只能单独使用
void AddStudentByEndV2(List *list);
void ChangeStudent(List *list, char *input);
void Input(Node *p);
int Search(List *list, char *input);
void Delete(List *list, char *input);
void PrintStudent(List *list);
void ClearList(List *list);
Node *PrintFlage(List *list, char *input);
int main(void)
{
List list;
list.head = NULL;
char code;
int c;
char input1;
char input2;
char input3;
int flag = 0;
Node *temp;
while (1)
{
printf("\n\
╔———————————————学生信息管理系统————————————————╗\n\
\n\
=============== 功能菜单 ================\n\
\n\
1 录入学生信息\n\
2 查找学生信息\n\
4 删除学生信息\n\
3 修改学生信息\n\
5 显示所有学生信息\n\
6 退出系统\n\
==========================================\n\
说明:通过数字选择菜单\n\
╚——————————————————————————————————————————————╝\n");
printf("请输入指令代码(1-6)\n");
do
{
scanf("%c", &code);
} while (code>54||code<49);
switch (code)
{
case '1':
printf("请选择插入学生的方法(1-头插法 2-尾插法V1):");
scanf("%d", &c);
switch (c)
{
case 1:
AddStudentByHead(&list);
break;
case 2:
AddStudentByEndV1(&list);
break;
default:
printf("输入指令错误\n");
}
break;
case '2':
printf("请输入你要查找的学生:");
scanf("%s", input1);
flag = (Search(&list, input1));
if (flag > 0)
{
temp = PrintFlage(&list, input1);
printf("学生姓名:%s\t学号%s\t分数:%s\t", temp->name, temp->id, temp->score);
}
else
{
printf("不好意思,查无此人\n");
}
break;
case '3':
printf("请输入你要更改的学生名字:");
scanf("%s", input2);
flag = Search(&list, input2);
temp = PrintFlage(&list, input2);
if (flag > 0)
{
printf("当前的学生信息为:\n");
printf("学生姓名:%s\t学号%s\t分数:%s\t", temp->name, temp->id, temp->score);
printf("请输入您要更改的学生信息\n");
ChangeStudent(&list, input2);
}
else
{
printf("不好意思,你要更改的学生名字尚未存在\n");
}
break;
case '4':
printf("请输入你要删除的学生名字:");
scanf("%s", input3);
if (Search(&list, input3) > 0)
{
Delete(&list, input3);
}
else
{
printf("不好意思,你要删除的学生名字尚未存在\n");
}
break;
case '5':
PrintStudent(&list);
break;
case '6':
if (!list.head)
{
printf("还没有信息呢\n");
}
goto END;
default:
printf("输入指令错误\n");
break;
}
printf("^__^\n");
printf("\n\n");
}
END:
ClearList(&list), printf("删库跑路成功^__^\n");
system("pause");
return 0;
}
//--1--插入新的学生
void AddStudentByHead(List *list)
{
Node *p, *temp;
p = (Node *)malloc(sizeof(Node));
Input(p);
if (list->head)
{
temp = list->head;
list->head = p;
p->next = temp;
}
else
{
list->head = p;
p->next = NULL;
}
}
//尾插法
void AddStudentByEndV1(List *list)
{
Node *p;
p = (Node *)malloc(sizeof(Node ));
Input(p);
Node *last;
last = list->head;
if (last)
{
while (last->next)
{
last = last->next;
}
last->next = p;
p->next=NULL;
}
else
{
list->head = p;
p->next = NULL;
}
}
//尾插法
/*
void AddStudentByEndV2(List *list)
{
Node *p;
p = (Node *)malloc(sizeof(Node ));
Input(p);
static Node *tail;
if (list->head)
{
tail=list->head;
tail->next = p;
p->next = NULL;
}
else
{
list->head = p;
p->next = NULL;
}
tail = p;
}
*/
void ChangeStudent(List *list, char *input)
{
Node *p;
for (p = list->head; p; p = p->next)
{
if (!strcmp(p->name, input))
{
Input(p);
break;
}
}
}
void Input(Node *p)
{
printf("请输入姓名:");
scanf("%s", p->name);
printf("请输入学号(2位数字):");
scanf("%s", p->id);
printf("请输入学生分数(小于1000):");
scanf("%s", p->score);
}
int Search(List *list, char *input)
{
int flag = -1; //标志
Node *p;
for (p = list->head; p; p = p->next)
{
if (!strcmp(p->name, input))
{
flag = 1;
break;
}
}
return flag;
}
Node *PrintFlage(List *list, char *input)
{
Node *p;
for (p = list->head; p; p = p->next)
{
if (!strcmp(p->name, input))
{
break;
}
}
return p;
}
void Delete(List *list, char *input)
{
Node *p, *q = NULL;
for (p = list->head; p; q = p, p = p->next)
{
if (!strcmp(p->name,input))
{
if (q)
{
q->next = p->next;
}
else
{
list->head = p->next;
}
free(p);
break;
}
}
}
void PrintStudent(List *list)
{
Node *p;
for (p = list->head; p; p = p->next)
{
printf("学生姓名:%s\t学号:%s\t分数:%s\t\n", p->name, p->id, p->score);
}
}
//--6--删库跑路
void ClearList(List *list)
{
Node *p, *q;
for (p = list->head; p; p = q)
{
q = p->next;
free(p);
}
} 大佬牛批
果断收藏 感谢分享{:5_95:}学习了 厉害
页:
[1]