一、前言
2022年11月26日,有个同学咨询我我会写C语言吗,C语言还是我大学学的,很多都忘了,然后他让我用链表写一个<校运动会管理系统>,然后想着周末也没什么事,就写了一个,然后这个项目的题目是:
1. 要实现一个使用密码登录功能 2. 假设参加运动会的有n个学院,学校编号为1.....n,比赛分成m个男子项目,和w个女子项目。项目编号为男子1.....m,女子m+1....mw。 不同的运动项目取前八名积分,且前八名的积分分别为: 9、7、6、5、4、3、2、1 (m<=20,n<=20) 3. 可以输入各个项目的前八名的成绩, 4. 能统计各学院的总分并排序; 5. 可以按学院编号、学院总分、男女团体总分排序输出: 6. 可以按学院编号查询学院某个项目的情况,可以按项目编号查询取得前八名的学院。 7. 可以查找汇总某名选手参加的项目和获取的名次和积分。 注意: 输出形式:有中文或英文提示,各学院分数为整型。 界面要求:有合理的提示,每个功能可以设立菜单,根据提示,可以完成相关的功能要求 存储结构:学生自己根据系统功能要求自己设计,要求运动会的相关数据要存储在数据文件中。 (数据文件的数据读写方法等相关内容可以参考c语言程序设计的教材)请在最后的上交资料中指明使用的存储结构。
二、源码及项目展示
1、登录页面
int contrast(char password[]) //密码校对函数 { static char key[10]="123456789"; if(strcmp(password,key)==0) //比较输入字符和系统设定的密码是否相同 return 1; else return 0; } void Login()//自定义登录函数 { char a; printf("\n"); printf("===================欢迎进入校运会管理系统==================\n"); printf("* 作者:王恒杰 班级: xxxxxxxxxxxxxxx 学号:xxxxxxxxxxx \n"); printf("***************************************************************\n"); printf("********************输入密码登陆请按回车键*********************\n"); printf("***************************************************************\n"); scanf("%c",&a); if(a=='\n') { char password[10]; int count=0,s; printf("您有三次输入机会!\n\n"); printf("请输入密码:\n"); scanf("%s",password); s=contrast(password);//调用密码校对函数 if(s==1) { system("CLS"); Sleep(500); printf("\n 密码正确!!\n\n"); system("color 3f"); printf("********************************************************\n"); printf("******************欢迎进入操作界面**********************\n"); printf("********************************************************\n"); printf("*********************请录入信息*************************\n\n\n"); } if(s==0) { count++; while(count!=3) { printf("密码错误,请再次输入密码:\n"); scanf("%s",password); s=contrast(password); if(s==1) { system("CLS"); Sleep(500); printf("\n 密码正确!\n\n"); system("color 3f"); printf("********************************************************\n"); printf("**********************欢迎进入**************************\n"); printf("********************************************************\n"); printf("*********************请录入信息*************************\n\n\n"); return ;//?????????????????? } if(s==0) count++; } if(count==3) printf("你已经错误输入3次,系统退出!\n"); exit(0); } } else exit(1); }
我们启动项目 ,会要按回车键,进入登录,然后有三次输入密码的机会
如果连续三次输入错误,那就自动退出系统
系统默认的密码是123456789
密码正确,正式进入系统
2、校运动会
1、源码
Sports.cpp
#include "sports.h" //初始化头指针fd void init(LinkList &mylist) { mylist = (LinkNode *)malloc(sizeof(LinkNode)); //申请头结点 mylist->rank = 0; mylist->score = 0; mylist->sex = -1; strcpy(mylist->name, ""); strcpy(mylist->college, ""); strcpy(mylist->item, ""); mylist->next = NULL; } //根据排名得到相应的分数 int get_score(int rank) { if (rank == 1) return 9; else if (rank == 2) return 7; else if (rank == 3) return 6; else if (rank == 4) return 5; else if (rank == 5) return 4; else if (rank == 6) return 3; else if (rank == 7) return 2; else if (rank == 8) return 1; else return 0; } //初始化程序,输入学院名,项目名和选手成绩 void input(LinkList &mylist, int &college_num, int &item_num, char colleges[][20], char items[][20]) { int i, r, flag = 0; int score = 0, rank = 0, sex = 0; // 存储输入变量; char name[20] = {'\0'}, college[20] = {'\0'}, item[20] = {'\0'}; // 存储输入变量存储; int count = 1; // 计数器 printf("请输入学院个数 n<10 \n"); while (r = scanf("%d", &college_num) != 1 || college_num > 10 || college_num < 0) { printf(" 您的输入有误,请重新输入! \n"); fflush(stdin); /*清空输入缓冲区,也可以使用rewind(stdin);*/ } for (i = 0; i < college_num; i++) { printf("请输入第%d学院名\n", i + 1); scanf("%s", &colleges[i]); fflush(stdin); } printf("请输入项目个数 n<10 \n"); while (r = scanf("%d", &item_num) != 1 || item_num > 10 || item_num < 0) { printf(" 您的输入有误,请重新输入! \n"); fflush(stdin); } for (i = 0; i < item_num; i++) { printf("请输入第%d项目名\n", i + 1); scanf("%s", &items[i]); } while (true) { fflush(stdin); printf("请输入第%d个选手数据\n", count); printf("请输入姓名\n"); r = scanf("%s", &name); if (r == 0) { printf(" 您的输入有误,请重新输入! \n"); break; } printf("请输入性别 0代表女性 1代表男性\n"); r = scanf("%d", &sex); if (r == 0) { printf(" 您的输入有误,请重新输入! \n"); break; } printf("请输入学院\n"); r = scanf("%s", &college); if (r == 0) { printf(" 您的输入有误,请重新输入! \n"); break; } printf("请输入项目\n"); r = scanf("%s", &item); if (r == 0) { printf(" 您的输入有误,请重新输入! \n"); break; } printf("请输入名次 1-8\n"); r = scanf("%d", &rank); if (r == 0 && rank < 0 && rank > 8) { printf(" 您的输入有误,请重新输入! \n"); break; } score = get_score(rank); set_data(mylist, score, rank, sex, name, college, item); count++; printf("0 结束输入数据 , 1 输入数据\n"); scanf("%d", &flag); if (flag == 0) break; } } //创建链表 void set_data(LinkList &mylist, int score, int rank, int sex, char name[], char college[], char item[]) { LinkNode *p = mylist->next, *s; //如果链表为空,则做初始化链表 if (p == NULL) { s = (LinkNode *)malloc(sizeof(LinkNode)); assert(s != NULL); s->score = score; s->rank = rank; s->sex = sex; strcpy(s->name, name); strcpy(s->college, college); strcpy(s->item, item); s->next = NULL; mylist->next = s; } else { while (p->next != NULL) { p = p->next; } s = (LinkNode *)malloc(sizeof(LinkNode)); assert(s != NULL); s->score = score; s->rank = rank; s->sex = sex; strcpy(s->name, name); strcpy(s->college, college); strcpy(s->item, item); s->next = NULL; p->next = s; } } //打印所有的数据 void show(LinkList mylist) { int count = 1; Node *p = mylist->next; while (p != NULL) { printf("打印第%d个成绩\n", count); print(p); p = p->next; count++; } } //保存数据为.txt文件 void save(LinkList mylist, char colleges[][20], int colleges_num, char items[][20], int item_num) { int i; //保存选手数据 FILE *file; file = fopen("sports_data.txt", "w"); if (file == NULL) { printf("文件创建失败请检查!\n"); exit(0); } Node *p = mylist->next; while (p != NULL) { fprintf(file, "%s\t", p->name); fprintf(file, "%d\t", p->sex); fprintf(file, "%s\t", p->college); fprintf(file, "%s\t", p->item); fprintf(file, "%d\t", p->rank); fprintf(file, "%d\n", p->score); p = p->next; } fclose(file); file = NULL; //将指针置为NULL ,打开另一个文件 file = fopen("colleges_items.txt", "w"); if (file == NULL) { printf("文件创建失败请检查!\n"); exit(0); } //保存项目数和学院数 fprintf(file, "%d\t", colleges_num); fprintf(file, "%d\t", item_num); //保存项目名字和学院名字 for (i = 0; i < colleges_num; i++) { fprintf(file, "%s\t", colleges[i]); } for (i = 0; i < item_num; i++) { fprintf(file, "%s\t", items[i]); } fclose(file); } //读取txt文件中的数据 void read(LinkList mylist, char colleges[][20], int &colleges_num, char items[][20], int &item_num) { if (mylist->next != NULL) { printf("已读取数据!\n"); return; } FILE *file; int i = 0; file = fopen("sports_data.txt", "r"); if (file == NULL) { printf("文件读取失败请初始化!\n"); exit(0); } int score, rank, sex; // 作为输入变量存储; char name[20], college[20], item[20]; // 作为输入变量存储; //当指针不为结束符,一直++ //读入所有的选手成绩 while (!feof(file)) { fscanf(file, "%s", &name); fscanf(file, "%d\t", &sex); fscanf(file, "%s\t", &college); fscanf(file, "%s\t", &item); fscanf(file, "%d\t", &rank); fscanf(file, "%d\n", &score); set_data(mylist, score, rank, sex, name, college, item); } fclose(file); //读取学院名字和项目名字 file = NULL; file = fopen("colleges_items.txt", "r"); if (file == NULL) { printf("文件读取失败请初始化!\n"); exit(0); } while (!feof(file)) { fscanf(file, "%d\t", &colleges_num); fscanf(file, "%d\t", &item_num); for (i = 0; i < colleges_num; i++) { fscanf(file, "%s\t", &colleges[i]); } for (i = 0; i < colleges_num; i++) { fscanf(file, "%s\t", &items[i]); } } fclose(file); } //学院分数 void college_score(LinkList mylist, char colleges[][20], int colleges_num) { int score[N] = {0}; // int i; LinkList list; Node *p = NULL; //计算每个学院的分数 init(list); for (i = 0; i < colleges_num; i++) { p = mylist->next; while (p != NULL) { if (strcmp(colleges[i], p->college) == 0) { score[i] += p->score; } p = p->next; } } //按分数大小建立一个链表 for (i = 0; i < colleges_num; i++) { Insert(list, score[i], 0, 0, "", colleges[i], ""); } //打印学院成绩 p = NULL; p = list->next; while (p != NULL) { printf("%s学院得分为%8d\n", p->college, p->score); p = p->next; } free_link(list); } //释放链表 void free_link(LinkList &mylist) { Node *p = NULL, *q = NULL; p = mylist->next; //链表为空直接返回 if (p == NULL) { free(mylist); return; } while (p != NULL) { q = p; p = p->next; free(q); } free(mylist); } //按男女统计得分 void sex_score(LinkList mylist, char colleges[][20], int colleges_num) { LinkList man_score, woman_score; init(man_score); init(woman_score); int i; for (i = 0; i < colleges_num; i++) { int a[N] = {0}; Node *p = mylist->next; LinkList man, womon; init(man); init(womon); while (p != NULL) { //统计某学院男生,女生成绩 if (strcmp(colleges[i], p->college) == 0 && p->sex == 0) { Insert(womon, p->score, p->rank, p->sex, p->name, p->college, p->item); } if (strcmp(colleges[i], p->college) == 0 && p->sex == 1) { Insert(man, p->score, p->rank, p->sex, p->name, p->college, p->item); } p = p->next; } p = man->next; while (p != NULL) { a[i] += p->score; p = p->next; } Insert(man_score, a[i], 0, 0, "", colleges[i], ""); p = NULL; a[i] = 0; p = womon->next; while (p != NULL) { a[i] += p->score; p = p->next; } Insert(woman_score, a[i], 0, 0, "", colleges[i], ""); free_link(man); free_link(womon); } Node *q; q = man_score->next; printf("学院男生团体的前八名为\n"); while (q != NULL) { printf("%s男生团体得分为: %d\n", q->college, q->score); q = q->next; } q = NULL; q = woman_score->next; printf("学院女生团体的前八名为\n"); while (q != NULL) { printf("%s女生团体得分为: %d\n", q->college, q->score); q = q->next; } free_link(man_score); free_link(woman_score); } //查询某学院某个项目获奖情况 void college_item(LinkList mylist) { char item[20], college[20]; printf("请输入您要查询的学院名:\n"); scanf("%s", &college); printf("请输入您要查询的项目名:\n"); scanf("%s", &item); Node *p = mylist->next; while (p != NULL) { if (strcmp(college, p->college) == 0 && strcmp(item, p->item) == 0) { print(p); } p = p->next; } } //查询选手成绩 void query_name(LinkList mylist) { char name[20]; printf("请输入您要查询的选手姓名:\n"); scanf("%s", &name); Node *p = mylist->next; while (p != NULL) { if (strcmp(name, p->name) == 0) { print(p); } p = p->next; } } // 按大小顺序插入建立一个链表 void Insert(LinkList &head, int score, int rank, int sex, char name[], char college[], char item[]) { LinkNode *p = head->next, *s = NULL; LinkNode *q = head; //如果链表为空,则做初始化链表 if (p == NULL) { s = (LinkNode *)malloc(sizeof(LinkNode)); assert(s != NULL); s->score = score; s->rank = rank; s->sex = sex; strcpy(s->name, name); strcpy(s->college, college); strcpy(s->item, item); s->next = NULL; head->next = s; } else { while (p != NULL && score <= p->score) { q = p; p = p->next; } s = (LinkNode *)malloc(sizeof(LinkNode)); assert(s != NULL); s->score = score; s->rank = rank; s->sex = sex; strcpy(s->name, name); strcpy(s->college, college); strcpy(s->item, item); s->next = p; q->next = s; } } //查询项目得分情况 void query_item(LinkList mylist) { char item[20]; LinkList head; init(head); printf("请输入您要查询的项目名:\n"); scanf("%s", &item); Node *p = mylist->next; while (p != NULL) { if (strcmp(item, p->item) == 0) { Insert(head, p->score, p->rank, p->sex, p->name, p->college, p->item); } p = p->next; } p = head->next; printf("%s项目前八名为\n", item); while (p != NULL) { print(p); p = p->next; } } //打印指针内的数据 void print(Node *p) { printf("\n\n\n\n"); printf("*************************\n"); printf("姓名为 "); printf("%s\n", p->name); printf("性别为 "); printf("%d\n", p->sex); printf("学院为 "); printf("%s\n", p->college); printf("项目为 "); printf("%s\n", p->item); printf("名次为 "); printf("%d\n", p->rank); printf("分数为 "); printf("%d\n", p->score); }
main.cpp
#include "sports.h" int contrast(char password[]) //密码校对函数 { static char key[10]="123456789"; if(strcmp(password,key)==0) //比较输入字符和系统设定的密码是否相同 return 1; else return 0; } void Login()//自定义登录函数 { char a; printf("\n"); printf("===================欢迎进入校运会管理系统==================\n"); printf("* 作者:王恒杰 班级: xxxxxxxxxxxxxxx 学号:xxxxxxxxxxx \n"); printf("***************************************************************\n"); printf("********************输入密码登陆请按回车键*********************\n"); printf("***************************************************************\n"); scanf("%c",&a); if(a=='\n') { char password[10]; int count=0,s; printf("您有三次输入机会!\n\n"); printf("请输入密码:\n"); scanf("%s",password); s=contrast(password);//调用密码校对函数 if(s==1) { system("CLS"); Sleep(500); printf("\n 密码正确!!\n\n"); system("color 3f"); printf("********************************************************\n"); printf("******************欢迎进入操作界面**********************\n"); printf("********************************************************\n"); printf("*********************请录入信息*************************\n\n\n"); } if(s==0) { count++; while(count!=3) { printf("密码错误,请再次输入密码:\n"); scanf("%s",password); s=contrast(password); if(s==1) { system("CLS"); Sleep(500); printf("\n 密码正确!\n\n"); system("color 3f"); printf("********************************************************\n"); printf("**********************欢迎进入**************************\n"); printf("********************************************************\n"); printf("*********************请录入信息*************************\n\n\n"); return ;//?????????????????? } if(s==0) count++; } if(count==3) printf("你已经错误输入3次,系统退出!\n"); exit(0); } } else exit(1); } //主页面 int main() { system("color 0a");/*两个16进制参数控制颜色,可以调节,想知道参数对应的颜色的 话加一个数字后点击运行。注意两个数字一样时体验可能不好*/ Login();//调用登录函数 LinkList mylist; //选手数据链表 init(mylist); int college_num=0; //学院数量 int item_num=0; //项目数量 char colleges[N][20]={'\0'}; //学院 char items[N][20]={'\0'}; //运动会项目 int select=0; while (true) { printf("********************************************************\n"); printf("===================校运会管理系统==================\n"); printf("[1]初始化程序 [2]读取数据 \n"); printf("[3]打印数据 [4]保存数据 \n"); printf("[5]统计学院的总分 [6]男女团体总分 \n"); printf("[7]查询学院项目 [8]查找选手 \n"); printf("[9]查询项目获奖 \n"); printf("[0]退出系统 \n"); printf("*********************************************************\n"); printf("请选择: >\n"); scanf("%d", &select); switch (select) { case 1: { input(mylist,college_num,item_num,colleges,items); break; } case 2: read(mylist,colleges,college_num,items,item_num); show(mylist); break; case 3: show(mylist); break; case 4: save(mylist,colleges,college_num,items,item_num); break; case 5: college_score(mylist,colleges,college_num); break; case 6: sex_score(mylist,colleges,college_num); break; case 7: college_item(mylist); break; case 8: query_name(mylist); break; case 9: query_item(mylist); break; case 0: free_link(mylist); return 0; default: printf("输入的数据有误,请重新输入\n"); break; } } }
sports.h
#include<stdio.h>//标准输入输出头文件 #include<stdlib.h>//标准库头文件,含system、exit函数 #include<string.h>//字符串处理头文件 #include <time.h>//日期和时间头文件 #include<windows.h>//延迟函数Sleep的头文件 #include <assert.h> #include <malloc.h> #define N 10 typedef int ElemType; typedef struct Node { ElemType score; //这是分 数 ElemType rank; //排名 ElemType sex; //性别 char name[20]; //姓名 char college[20]; //学院 char item[20]; //项目 struct Node *next; } LinkNode, *LinkList; void init(LinkList &mylist); int get_score(int rank); void input(LinkList &mylist, int &college_num, int &item_num, char colleges[][20], char items[][20]); void set_data(LinkList &mylist, int score, int rank, int sex, char name[], char college[], char item[]); void show(LinkList mylist); void save(LinkList mylist, char colleges[][20], int colleges_num, char items[][20], int item_num); void read(LinkList mylist, char colleges[][20], int &colleges_num, char items[][20], int &item_num); void college_score(LinkList mylist, char colleges[][20], int colleges_num); void selectsort(int R[], int n); void sex_score(LinkList mylist, char colleges[][20], int colleges_num); void college_item(LinkList mylist); void query_name(LinkList mylist); void query_item(LinkList mylist); void Insert(LinkList &head, int score, int rank, int sex, char name[], char college[], char item[]); void print(Node *p); void free_link(LinkList &mylist);
按1初始化页面
先定义学院,再定义项目
然后定义选手姓名
需要有16个人,这是可以查学院里面学生的信息