List.h
#ifndef LIST_H
#define MaxLesson 20
int lesson = 0;
//未上课0,准时到1,缺勤2,请假3
enum condition {
none, present, absence, leave
};
typedef struct Student {
char stuID[15];//学号
char name[10];//姓名
char tel[15];//电话
char parentTel[15];//家长的联系方式
int attendance[MaxLesson];//DS课堂考勤记录
int num;//课代表选拔编号
}Stu;
typedef struct Node {
Stu stu;
struct Node* next;
}StuList;
//初始化链表
void InitList(StuList*& L)
{
L = (StuList*)malloc(sizeof(StuList));
L->next = NULL;
}
//销毁链表
void DestroyList(StuList*& L)
{
StuList* pre = L, *p = L->next;
while (p != NULL)
{
free(pre);
pre = p;
p = pre->next;
}
free(pre);
}
//判断链表是否为空
bool ListEmpty(StuList* L)
{
return (L->next == NULL);
}
//求链表长度
int ListLength(StuList* L)
{
int n = 0;
StuList* p = L;
while (p->next != NULL)
{
n++;
p = p->next;
}
return n;
}
//计算某学生到课次数
int AttendCount(Stu stu) {
int count = 0;
for (int i = 0; i < sizeof(stu.attendance) / sizeof(stu.attendance[0]); i++) {
if (stu.attendance[i] == present) {
++count;
}
}
return count;
}
//遍历链表每个元素
void DispList(StuList* L)
{
system("cls");
StuList* p = L->next;
printf("学号\t\t姓名\t\t电话\t\t家长联系方式\t\t到课次数\n");
while (p != NULL)
{
printf("%s\t%s\t\t%s\t%s\t\t%d/%d\n", p->stu.stuID, p->stu.name, p->stu.tel, p->stu.parentTel, AttendCount(p->stu),lesson);
p = p->next;
}
printf("\n");
}
#endif
main.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include "List.h"
//添加学生
void AddStu(StuList*& L) {
system("cls");
int choice;
printf("添加学生方式(1.键盘2.文件):");
scanf_s("%d", &choice);
while (choice != 1 && choice != 2) {
printf("输入有误,请重新输入:");
scanf_s("%d", &choice);
}
switch (choice) {
case 1:
{
system("cls");
int n, len;
StuList* p = L;
printf("请输入你要输入的学生的个数:");
scanf_s("%d", &n);
printf("\n");
getchar();
for (int i = 1; i <= n; i++)
{
while (p->next != NULL) {
p = p->next;
}
StuList* q = (StuList*)malloc(sizeof(StuList));
printf("请输入第%d名学生的学号:", i);
scanf_s("%s", &q->stu.stuID, 15);
printf("请输入第%d名学生的姓名:", i);
scanf_s("%s", &q->stu.name, 10);
printf("请输入第%d名学生的电话:", i);
scanf_s("%s", &q->stu.tel, 15);
printf("请输入第%d名学生的家人的电话:", i);
scanf_s("%s", &q->stu.parentTel, 15);
for (int i = 0; i < MaxLesson; i++) {
q->stu.attendance[i] = none;
}
printf("\n\n");
q->next = NULL;
p->next = q;
p = q;
}
printf("输入完成!\n");
break;
}
case 2:
{
system("cls");
char stuID[15], name[10], tel[15], parentTel[15];
FILE* file;
StuList* p = L;
if ((file = fopen("D:\\stuinfo.txt", "r")) == NULL)
{
printf("未找到文件!\n");
break;
}
while (p->next != NULL) {
p = p->next;
}
fscanf(file, "%[^,],%[^,],%[^,],%s\n", stuID, name, tel, parentTel);
while (!feof(file)) {
StuList* q = (StuList*)malloc(sizeof(StuList));
for (int i = 0; i < MaxLesson; i++) {
q->stu.attendance[i] = none;
}
//非贪婪匹配
fscanf(file, "%[^,],%[^,],%[^,],%s\n", q->stu.stuID, q->stu.name, q->stu.tel, q->stu.parentTel);
printf("-----------------------------------\n");
printf("学号:%s\n", q->stu.stuID);
printf("姓名:%s\n", q->stu.name);
printf("电话:%s\n", q->stu.tel);
printf("家长联系方式:%s\n", q->stu.parentTel);
printf("-----------------------------------\n");
q->next = NULL;
p->next = q;
p = q;
}
fclose(file);
printf("批量添加完成!\n");
break;
}
}
printf("按任意键返回主界面!\n");
getchar();
}
//按姓名或学号删除学生
void DeleteStu(StuList*& L) {
system("cls");
int n;
int choice;
printf("请输入你要删除的学生的方式(1.学号2.姓名):");
scanf_s("%d", &choice);
while (choice != 1 && choice != 2) {
printf("输入有误,请重新输入:");
scanf_s("%d", &choice);
}
printf("请输入你要删除的学生的个数:");
scanf_s("%d", &n);
if (n > ListLength(L) || n < 0) {
printf("删除学生数量有误!\n");
printf("按任意键返回主界面!\n");
getchar();
return;
}
switch (choice) {
case 1:
{
StuList* p;
for (int i = 1; i <= n; i++)
{
char stuID[15];
p = L;
StuList* q;
printf("请输入删除的学生的学号:");
scanf_s("%s", &stuID, 15);
while (p != NULL && p->next != NULL) {
if (strcmp(p->next->stu.stuID,stuID) == 0) {
q = p->next;
p->next = q->next;
p = p->next;
free(q);
printf("删除完成!\n");
printf("按任意键返回主界面!\n");
getchar();
return;
}
else {
p = p->next;
}
}
if (p != NULL && p->next == NULL) {
printf("查无此生!\n");
}
}
break;
}
case 2:
{
StuList* p;
for (int i = 1; i <= n; i++)
{
char name[10];
StuList* q;
p = L;
printf("请输入删除的学生的姓名:");
scanf_s("%s", &name, 10);
while (p != NULL&&p->next!=NULL) {
if (strcmp(p->next->stu.name, name) == 0) {
q = p->next;
p->next = q->next;
p = p->next;
free(q);
printf("删除完成!\n");
printf("按任意键返回主界面!\n");
getchar();
return;
}
else {
p = p->next;
}
}
if (p != NULL && p->next == NULL) {
printf("查无此生!\n");
}
}
break;
}
}
printf("按任意键返回主界面!\n");
getchar();
}
//按姓名和学号查询学生信息
void QueryList(StuList* L) {
system("cls");
int choice;
printf("请输入你要查询学生的方式(1.学号2.姓名):");
scanf_s("%d", &choice);
while (choice != 1 && choice != 2) {
printf("输入有误,请重新输入:");
scanf_s("%d", &choice);
}
switch (choice) {
case 1:
{
char stuID[15];
StuList* p = L->next;
printf("请输入查询的学生的学号:");
scanf_s("%s", &stuID, 15);
while (p != NULL) {
if (strcmp(p->stu.stuID, stuID) == 0) {
printf("-----------------------------------\n");
printf("学号:%s\n", p->stu.stuID);
printf("姓名:%s\n", p->stu.name);
printf("电话:%s\n", p->stu.tel);
printf("家长联系方式:%s\n", p->stu.parentTel);
printf("-----------------------------------\n");
printf("查询完成!\n\n");
break;
}
else {
p = p->next;
}
}
if (p == NULL) {
printf("查无此生!\n\n");
}
break;
}
case 2:
{
char name[10];
StuList* p = L->next;
printf("请输入查询的学生的姓名:");
scanf_s("%s", &name, 10);
while (p != NULL) {
if (strcmp(p->stu.name, name) == 0) {
printf("-----------------------------------\n");
printf("学号:%s\n", p->stu.stuID);
printf("姓名:%s\n", p->stu.name);
printf("电话:%s\n", p->stu.tel);
printf("家长联系方式:%s\n", p->stu.parentTel);
printf("-----------------------------------\n");
printf("查询完成!\n\n");
break;
}
else {
p = p->next;
}
}
if (p == NULL) {
printf("查无此生!\n\n");
}
break;
}
}
printf("按任意键返回主界面!\n");
getchar();
}
//按学号或姓名更改学生信息
void ChangeList(StuList*& L) {
system("cls");
int choice;
printf("请输入你要修改学生的方式(1.学号2.姓名):");
scanf_s("%d", &choice);
while (choice != 1 && choice != 2) {
printf("输入有误,请重新输入:");
scanf_s("%d", &choice);
}
switch (choice) {
case 1:
{
char stuID[15];
StuList* p = L->next;
printf("请输入要修改的学生的原学号:");
scanf_s("%s", &stuID, 15);
while (p != NULL) {
if (strcmp(p->stu.stuID, stuID) == 0) {
int choice1 = 0;
printf("请选择需要修改的内容(1.学号2.姓名3.电话4.家长联系方式):");
scanf_s("%d", &choice1);
switch (choice1)
{
case 1:
{
printf("请输入修改后的学号:");
scanf_s("%s", &p->stu.stuID, 15);
printf("修改完成!\n\n");
break;
}
case 2:
{
printf("请输入修改后的姓名:");
scanf_s("%s", &p->stu.name, 10);
printf("修改完成!\n\n");
break;
}
case 3:
{
printf("请输入修改后的电话:");
scanf_s("%s", &p->stu.tel, 15);
printf("修改完成!\n\n");
break;
}
case 4:
{
printf("请输入修改后的家长联系方式:");
scanf_s("%s", &p->stu.parentTel, 15);
printf("修改完成!\n\n");
break;
}
}
break;
}
else {
p = p->next;
}
}
if (p == NULL) {
printf("查无此生!\n\n");
}
break;
}
case 2:
{
char name[10];
StuList* p = L->next;
printf("请输入修改的学生的原姓名:");
scanf_s("%s", &name, 10);
while (p != NULL) {
if (strcmp(p->stu.name, name) == 0) {
int choice1 = 0;
printf("请选择需要修改的内容(1.学号2.姓名3.电话4.家长联系方式):");
scanf_s("%d", &choice1);
switch (choice1)
{
case 1:
{
printf("请输入修改后的学号:");
scanf_s("%s", &p->stu.stuID, 15);
printf("修改完成!\n\n");
break;
}
case 2:
{
printf("请输入修改后的姓名:");
scanf_s("%s", &p->stu.name, 10);
printf("修改完成!\n\n");
break;
}
case 3:
{
printf("请输入修改后的电话:");
scanf_s("%s", &p->stu.tel, 15);
printf("修改完成!\n\n");
break;
}
case 4:
{
printf("请输入修改后的家长联系方式:");
scanf_s("%s", &p->stu.parentTel, 15);
printf("修改完成!\n\n");
break;
}
}
break;
}
else {
p = p->next;
}
}
if (p == NULL) {
printf("查无此生!\n\n");
}
break;
}
}
printf("按任意键返回主界面!\n");
getchar();
}
//求课代表
void ClassRepresentative(StuList* L) {
StuList* p = L->next;
int m, n = 0;//n计数
int length = ListLength(L);
if (ListEmpty(L)) {
printf("该班级无学生!");
getchar();
return;
}
printf("请输入一个整数,报到该数该同学退出:");
scanf_s("%d", &m);
while (m > length || m <= 0) {
printf("请输入一个不大于%d且大于0的整数:", length);
scanf_s("%d", &m);
}
int i = 1;
//为学生编号
while (p != NULL) {
p->stu.num = i;
p = p->next;
i++;
}
p = L->next;
//遍历选课代表
while (length != 1)
{
if (p->stu.num == -1) {
//将p指向下一个同学
if (p->next == NULL) {
p = L->next;
}
else {
p = p->next;
}
}
else {
if (++n == m) {
//将学生编号置为-1
p->stu.num = -1;
length--;
n = 0;
}
//将p指向下一个同学
if (p->next == NULL) {
p = L->next;
}
else {
p = p->next;
}
}
}
while (true) {
if (p->stu.num != -1) {
printf("课代表的名字为%s,学号为%s。", p->stu.name, p->stu.stuID);
getchar();
return;
}
//将p指向下一个同学
if (p->next == NULL) {
p = L->next;
}
else {
p = p->next;
}
}
}
//记录考勤
void WriteAttend(StuList*& L) {
system("cls");
printf("请输入第%d节课学生考勤情况(1到勤,2缺勤,3请假):\n", lesson + 1);
int choice;
StuList* p = L->next;
while (p != NULL) {
printf("%s--%s:", p->stu.stuID, p->stu.name);
scanf_s("%d", &choice);
while (choice != 1 && choice != 2 && choice != 3) {
printf("输入有误,请重新输入:");
scanf_s("%d", &choice);
}
switch (choice)
{
case 1:
{
p->stu.attendance[lesson] = present;
break;
}
case 2:
{
p->stu.attendance[lesson] = absence;
break;
}
case 3:
{
p->stu.attendance[lesson] = leave;
break;
}
}
p = p->next;
}
++lesson;
printf("按任意键返回主界面!\n");
getchar();
}
//查询考勤
void ReadAttend(StuList* L) {
system("cls");
StuList* p = L->next;
printf("学生考勤情况(*未上课,√到勤,×缺勤,#请假):\n");
printf("学号-----------姓名\t1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20\n");
while (p != NULL) {
printf("%s--%s:\t", p->stu.stuID, p->stu.name);
for (int i = 0; i < MaxLesson; i++) {
switch (p->stu.attendance[i]) {
case none:
{
printf("* ");
break;
}
case present:
{
printf("√ ");
break;
}
case absence:
{
printf("× ");
break;
}
case leave:
{
printf("# ");
break;
}
}
}
printf("\n");
p = p->next;
}
getchar();
}
//导出csv文件
void ToCsv(StuList* L) {
DispList(L);
FILE* file;
file = fopen("D:\\stuinfo.csv", "w");
if (file == NULL) {
printf("导出文件失败");
return;
}
fwrite("学号", 1, strlen("学号"), file);
fwrite(",", 1, strlen(","), file);
fwrite("姓名", 1, strlen("姓名"), file);
fwrite(",", 1, strlen(","), file);
fwrite("电话", 1, strlen("电话"), file);
fwrite(",", 1, strlen(","), file);
fwrite("家长联系方式", 1, strlen("家长联系方式"), file);
fwrite("\n", 1, strlen("\n"), file);
StuList* p = L->next;
while (p != NULL) {
fwrite(p->stu.stuID, 1, strlen(p->stu.stuID), file);
fwrite(",", 1, strlen(","), file);
fwrite(p->stu.name, 1, strlen(p->stu.name), file);
fwrite(",", 1, strlen(","), file);
fwrite(p->stu.tel, 1, strlen(p->stu.tel), file);
fwrite(",", 1, strlen(","), file);
fwrite(p->stu.parentTel, 1, strlen(p->stu.parentTel), file);
fwrite("\n", 1, strlen("\n"), file);
p = p->next;
}
fclose(file);
printf("导出成功!\n\n");
printf("按任意键返回主界面!\n");
getchar();
}
int main() {
StuList* s;
int Menu;
InitList(s);
while (1) {
system("cls");
printf("**********欢迎使用学生成绩管理系统!**********\n\n");
printf("\t\t功能菜单:\n\n");
printf("\t\t1.查询学生信息\n\n");
printf("\t\t2.更改学生信息\n\n");
printf("\t\t3.增加学生\n\n");
printf("\t\t4.删除学生\n\n");
printf("\t\t5.选择课代表\n\n");
printf("\t\t6.记录考勤\n\n");
printf("\t\t7.查询考勤\n\n");
printf("\t\t8.导出学生信息文件\n\n");
printf("\t\t9.退出\n\n");
printf("请选择编号功能编号输入(1-9):");
scanf_s("%d", &Menu);
while (Menu < 1 || Menu>9)
{
printf("请输入正确的功能编号(1-9):");
scanf_s("%d", &Menu);
}
switch (Menu)
{
case 1:
QueryList(s);
getchar();
break;
case 2:
ChangeList(s);
getchar();
break;
case 3:
AddStu(s);
getchar();
break;
case 4:
DeleteStu(s);
getchar();
break;
case 5:
ClassRepresentative(s);
getchar();
break;
case 6:
WriteAttend(s);
getchar();
break;
case 7:
ReadAttend(s);
getchar();
break;
case 8:
ToCsv(s);
getchar();
break;
case 9:
exit(0);
break;
}
}
return 1;
}
stuinfo.txt
学号,姓名,电话,家长联系方式
2019302110001,金锦,18542717461,17838148891
2019302110002,陈太群,17691624517,18946205951
2019302110003,孔雪,15667566092,14641531375
2019302110004,严珍,18160326308,16262626774
2019302110005,赵敏茹,15469063628,16712613157
2019302110006,曹显,15737835967,13139581638
2019302110007,孔颖,13762542249,16346581190
2019302110008,施彤,14267632793,13889995958
2019302110009,朱静雯,18765970862,13122719423
2019302110010,金明露,17622007631,13465792106
2019302110011,卫玫,19840787888,18359712341
2019302110012,吕明红,13519548155,13388446277
2019302110013,许玉凤,17985782359,16854225486
2019302110014,施梦,19777136837,16325770156
2019302110015,许夜白,15242773707,13957765018
2019302110016,金景玉,15574025326,17295278449
2019302110017,孙缘双,18832687788,17518477389
2019302110018,孔娟,15287399731,14370024576
2019302110019,金妹妹,17692611091,18336739873
2019302110020,魏怜云,15137516635,19283590987
2019302110021,蒋乐,18665557789,17808175005
2019302110022,寿秋菊,17794866268,18762390519
2019302110023,李之念,16563643462,16473848555
2019302110024,曹艳,19338018287,17999277926
2019302110025,孔千萍,17463404732,18743586607
2019302110026,蒋丽,15858750499,15181070174
2019302110027,施胜英,15110206064,13669338748
2019302110028,曹清怡,17765257546,13198164757
2019302110029,王朱婷,18614215082,13448312459
2019302110030,戚易梦,19873777283,18421961327
2019302110031,何岚,17521529152,17853640899
2019302110032,褚莉,16788998044,14430838173
2019302110033,蒲大芬,15237563814,19363440103
2019302110034,陈显,18803714325,19492446164
2019302110035,孙花娥,16748811098,19985283934
2019302110036,韩梅梅,13598244826,18771314008
2019302110037,褚丽红,18106350508,13139854750
2019302110038,曹丹,19456601571,19968293045
2019302110039,钱翠柔,19866028963,18404961305
2019302110040,秦海莲,13180620607,15470997726
2019302110041,褚海燕,17865866999,17661103884
2019302110042,沈君,15914009387,18951945482
2019302110043,金桂花,14426349419,17694974374
2019302110044,吕翠,16605230301,13761128801
2019302110045,胥丽,18201848464,15663593212
2019302110046,花琰,15705230675,13950396004
2019302110047,钱灵竹,13966114540,17552973892
2019302110048,王霄,16541417059,19226255139
2019302110049,金春文,15312683284,16105744312
2019302110050,周虹,16242059470,17364518767
2019302110051,郑寒,14312235618,19115104542
2019302110052,周红燕,16994903375,13634677268
2019302110053,褚欣悦,19752804234,14144993388
2019302110054,许月珍,16376423172,19155402446
2019302110055,褚黛,15889067125,17415207415
2019302110056,魏黛,17237785368,14660077142
2019302110057,杨明珠,14242342995,14613289124
2019302110058,郑幻波,19874136511,18509819698
2019302110059,褚半雪,19774752628,19957779773