最近天天混死了,综述这两天要快点写完
现在把这学期期末的数据结构课设提交一下
得分91
我只提交了代码,文档部分我不会提交
#include
#include
#include
#include
#include
#include
//宏定义
#define MAX 50
//函数声明
void input_file_school();
void input_file_xiangmu();
void create_file_xiangmu();
void Menu();
void input_score_menu();
void out();
void print_school();
void print_xiangmu_male();
void print_xiangmu_female();
void input_male_xiangmu_score(int id);
void input_male_xiangmu_score_menu();
void input_female_xiangmu_score(int id);
void input_female_xiangmu_score_menu();
void printf_sorted_total_score();
void printf_sorted_male_score();
void printf_sorted_name();
void printf_sorted_female_score();
void search_school_xiangmu_menu();
void initialize();
void sorted_menu();
void search_school_xiangmu_menu_male();
void search_school_xiangmu_menu_female();
void print_school_2();
void search_menu();
void search_gender_xiangmu_menu();
//--------------------------------------
/*
本区域为数据结构定义区以及操作区域
本程序使用的是链表的数据结构,所采用的操作有
1.创建链表
2.插入链表
3.打印链表
*/
//数据结构创建:
typedef int ElemType;//数据元素类型定义,此处选择常用的int,后续可根据数据类型自行更改
//线性表的单链表存储结构(结构指针):
typedef struct LNode{
ElemType data;//数据域
struct LNode*next;//指针域
}LNode,*LinkList;//LNode是struct LNode的别名,LinkList是LNode的指针(LinkList=struct LNode*)
//创建具有头结点的链表函数
//空的链表
LinkList Create(){
//n为传入元素个数
LinkList L;
L=(LinkList)malloc(sizeof(LNode));
L->next=NULL;
return L;
}
void ListInsert(LinkList L,ElemType e){//注意传入LinkList &L,是引用的意思,函数体内可以更改L的元素的值,也可以更改L的值
//在末尾插入数字
while(L->next){
L=L->next;
}
LinkList p=(LinkList)malloc(sizeof(LNode));
L->next=p;
p->next=NULL;
p->data=e;
}
void PrintLinkList(LinkList L){
//传入L为头指针(head)
if(L->next==NULL)
return ;
L=L->next;//现在L指向数据元素结构体的第一个
if(L->data){//得有值
while(L->next){
printf("%d ",L->data);L=L->next;
}
printf("%d\n",L->data);//最后那个结构体的指针域是NULL,但是数据域有值,别忘了输出
}
return ;
}
//-----------------------------------------------------------------------------------------
//---------------------------------------
//全局变量定义
int male_xiangmu_num=1;
int female_xiangmu_num=1;
int school_num=1;
//---------------------------------------
//--------------------------------------
//定义各种结构体
struct name_pointer{
char xiangmu_name[MAX];
int id;
LinkList List_head=NULL;//默认为NULL
};
struct school{
int id;
char school_name[MAX];
int total=0;
int male_total=0;
int female_total=0;
name_pointer name_pointer_male[MAX];//这是一个结构体,里面存储各个项目的名字以及对应的地址
name_pointer name_pointer_female[MAX];
};
struct xiangmu_rating{
int i;
int num;
char name_rating[MAX][MAX];
};
struct xiangmu{
int id;
char xiangmu_name[MAX];
};
struct xiangmu_file{
int id;
char file_name[MAX];
};
//------------------------------------------------------------
//结构体数组定义
school schools[MAX];
xiangmu xiangmus_male[MAX];//男生项目数组
xiangmu xiangmus_female[MAX];//女生项目数组
xiangmu_file xiangmu_files_male[MAX];//男生文件数组
xiangmu_file xiangmu_files_female[MAX];//女生文件数组
xiangmu_rating xiangmu_rating_male[MAX];//男生项目前几名排名
xiangmu_rating xiangmu_rating_female[MAX];//女生项目前几名排名
// 指向各个项目的指针数组
LinkList male_pointer[MAX];
LinkList female_pointer[MAX];
int rating_five_score[6]={0,7,5,3,2,1};
int rating_three_score[4]={0,5,3,2};
//功能函数
//读取学校信息
void input_file_school(){
FILE *fp;
if((fp=fopen("school.txt","r"))==NULL) //打开文件
{
printf("Can not open file!\n");
exit(1);
}
while(!feof(fp)) //读取到文件尾
{
fscanf(fp,"%d %s",&schools[school_num],schools[school_num].school_name);
school_num++;
}
}
//从文件中读取项目
void input_file_xiangmu(){
FILE *fp;
if((fp=fopen("xiangmu_male.txt","r"))==NULL) //打开文件
{
printf("Can not open file!\n");
exit(1);
}
while(!feof(fp)) //读取到文件尾
{
fscanf(fp,"%d %s",&xiangmus_male[male_xiangmu_num].id,xiangmus_male[male_xiangmu_num].xiangmu_name);
for(int i=1;i<school_num;i++){
strcpy((schools[i].name_pointer_male[male_xiangmu_num]).xiangmu_name,xiangmus_male[male_xiangmu_num].xiangmu_name);
schools[i].name_pointer_male[male_xiangmu_num].id=male_xiangmu_num;
schools[i].name_pointer_male[male_xiangmu_num].List_head=Create();
}
male_xiangmu_num++;
}
//下面读取女生项目
FILE *ff;
if((ff=fopen("xiangmu_female.txt","r"))==NULL) //打开文件
{
printf("Can not open file!\n");
exit(1);
}
while(!feof(ff)) //读取到文件尾
{
fscanf(ff,"%d %s",&xiangmus_female[female_xiangmu_num].id,xiangmus_female[female_xiangmu_num].xiangmu_name);
for(int i=1;i<school_num;i++){
strcpy(schools[i].name_pointer_female[female_xiangmu_num].xiangmu_name,xiangmus_female[female_xiangmu_num].xiangmu_name);
schools[i].name_pointer_female[female_xiangmu_num].id=female_xiangmu_num;
schools[i].name_pointer_female[female_xiangmu_num].List_head=Create();
/*
每个学校都建立这样一个数组,数组元素是结构体,里面有项目名字,对应的id和所存储的链表地址
*/
}
female_xiangmu_num++;
}
}
//创建各个项目的文件
void create_file_xiangmu(){
//男生
{
char s[20];
char tmp[20];
s[0]='m';
s[1]='_';
s[2]='\0';
for(int i=1;i<male_xiangmu_num;i++){
FILE *f;
strcpy(tmp,s);
strcat(s,xiangmus_male[i].xiangmu_name);
if((f=fopen(s,"w"))==NULL)
{
printf("cannot open file!\n");
exit(0);
}
else{
fputs("\n",f);
strcpy(xiangmu_files_male[i].file_name,s);
}
fclose(f);
strcpy(s,tmp);
}
}
//女生
{
char q[20];
char tm[20];
q[0]='f';
q[1]='_';
q[2]='\0';
for(int i=1;i<female_xiangmu_num;i++){
FILE *fp;
strcpy(tm,q);
strcat(q,xiangmus_female[i].xiangmu_name);
if((fp=fopen(q,"w"))==NULL)
{
printf("cannot open file!\n");
exit(0);
}
else{
fputs("\n",fp);
strcpy(xiangmu_files_female[i].file_name,q);
}
fclose(fp);
strcpy(q,tm);
}
}
}
//界面显示函数---------------------------------------------------------------------------------
void Menu()
{
system("cls");
printf("******************************************************************************************************************\n\n");
printf("\t\t\t 欢迎进入! \n");
printf(" 请输入对应功能的数字完成操作 \n");
printf(" 1.录入成绩 \n");
printf(" 2.成绩排序 \n");
printf(" 3.查询成绩 \n");
printf(" 0.退出 \n");
printf("\n");
int choice;
scanf("%d",&choice);
switch(choice)
{
case 1: input_score_menu(); break;
case 2: sorted_menu(); break;
case 3: search_menu(); break;
case 0: out();exit(0);
default:
printf("输入错误,按任意键返回!\n");
getch();
Menu();break;
}
}
void sorted_menu(){
system("cls");
printf("******************************************************************************************************************\n\n");
printf("\t\t\t 请选择排序方式\t\t\t\n");
printf(" 1.按序号排序 \n");
printf(" 2.按学校名排序 \n");
printf(" 3.按团体成绩排序 \n");
printf(" 4.按男生团体成绩排序 \n");
printf(" 5.按女生团体成绩排序 \n");
printf(" 0.返回 \n");
printf("\n");
int choice;
scanf("%d",&choice);
switch(choice){
case 1: print_school_2();sorted_menu();break;
case 2: printf_sorted_name();sorted_menu();break;
case 3: printf_sorted_total_score();sorted_menu();break;
case 4: printf_sorted_male_score();sorted_menu();break;
case 5: printf_sorted_female_score();sorted_menu();break;
case 0: Menu();break;
default:
printf("输入错误,按任意键返回!\n");
getch();
sorted_menu();break;
}
}
void input_score_menu(){
system("cls");
printf("******************************************************************************************************************\n\n");
printf("\t\t\t请选择录入男生或女生项目的成绩\t\t\t\n");
printf("\t\t\t1.男生\t2.女生\t0.返回\n");
int choice;
scanf("%d",&choice);
switch(choice)
{
case 1: input_male_xiangmu_score_menu(); input_score_menu(); break;
case 2: input_female_xiangmu_score_menu(); input_score_menu();break;
case 0: Menu();break;
default:
printf("输入错误,按任意键重新输入!\n");
getch();
input_score_menu();break;
}
}
//查询界面
void search_menu(){
system("cls");
printf("\t\t\t 请选择查询方式\t\t\t\n");
printf(" 1.按学校查询 \n");
printf(" 2.按项目查询 \n");
printf(" 0.返回 \n");
int choice;
scanf("%d",&choice);
switch(choice)
{
case 1: search_school_xiangmu_menu(); search_menu();break;
case 2: search_gender_xiangmu_menu();search_menu(); break;
case 0: Menu();break;
default:
printf("输入错误,按任意键重新输入!\n");
search_menu();break;
}
}
//查询某个项目的情况,这个需要区分男女
void search_gender_xiangmu_menu(){
system("cls");
printf("\t\t\t请选择查询项目类别\t\t\t\n");
printf(" 1.男生项目 \n");
printf(" 2.女生项目 \n");
printf(" 0.返回 \n");
int choice;
scanf("%d",&choice);
switch(choice)
{
case 1: search_school_xiangmu_menu_male(); break;
case 2: search_school_xiangmu_menu_female(); break;
case 0: search_menu();break;
default:
printf("输入错误,按任意键重新输入!\n");
getch();
search_gender_xiangmu_menu();break;
}
}
void out(){
system("cls");
printf(" ***************************************************\n");
printf(" * *\n");
printf(" * 感谢您的使用! *\n");
printf(" * *\n");
printf(" * *\n");
printf(" ***************************************************\n");
}
//打印所有学校
void print_school(){
printf("所有参赛学校如下:\n");
for(int i=1;i<school_num;i++){
printf("%d ",schools[i].id);
printf("%s\n",schools[i].school_name);
}
}
//打印具体项目
void print_xiangmu_male(){
printf("男子所有项目如下:\n");
for(int i=1;i<male_xiangmu_num;i++){
printf("%d ",xiangmus_male[i].id);
int j=0;
while(xiangmus_male[i].xiangmu_name[j]){
printf("%c",xiangmus_male[i].xiangmu_name[j]);
j++;
}
printf("\n");
}
}
void print_xiangmu_female(){
printf("女子所有项目如下:\n");
for(int i=1;i<female_xiangmu_num;i++){
printf("%d ",xiangmus_female[i].id);
int j=0;
while(xiangmus_female[i].xiangmu_name[j]){
printf("%c",xiangmus_female[i].xiangmu_name[j]);
j++;
}
printf("\n");
}
}
//输入男生各个项目成绩
void input_male_xiangmu_score(int id){
int x=id;
printf("请输入需要添加的成绩个数(输入前三名或者前五名):\n");
int num;
scanf("%d",&num);
if(num!=3&&num!=5){
printf("您的输入有误,请按任意键重新输入!\n");
getch();
}
else{
printf("请输入学校名和成绩,中间以空格区分:\n");
printf("(注意要按照成绩排名由高到低输入,系统将会自动赋予积分)\n");
char str[MAX][MAX];
for(int i=1;i<=num;i++){
fflush(stdin);
gets(str[i]);
FILE *f=fopen(xiangmu_files_male[id].file_name,"a");
if((f=fopen(xiangmu_files_male[id].file_name,"a"))==NULL)
{
printf("cannot open file!\n");
exit(0);
}
else{
fputs(str[i],f);
fputs("\n",f);
}
fclose(f);
char *name;
char *score_str;
int score_int;
/* 获取第一个子字符串 */
name = strtok(str[i], " ");
score_str= strtok(NULL, " ");
score_int=atoi(score_str);
//先存入项目排名中
strcpy(xiangmu_rating_male[id].name_rating[i],name);
xiangmu_rating_male[id].num=num;
//查找并添加成绩
if(num==3){//前三名加分法
for(int k=1;k<school_num;k++){
if(strcmp(name,schools[k].school_name)==0){
schools[k].total+=rating_three_score[i];
schools[k].male_total+=rating_three_score[i];
ListInsert(schools[k].name_pointer_male[id].List_head,score_int);
}
}
}
else if(num==5){//前五名加法
for(int k=1;k<school_num;k++){
if(strcmp(name,schools[k].school_name)==0){
schools[k].total+=rating_five_score[i];
schools[k].male_total+=rating_five_score[i];
ListInsert(schools[k].name_pointer_male[id].List_head,score_int);
}
}
}
}
printf("输入成功!\n");getch();
}
}
//提示输入男生成绩的页面
void input_male_xiangmu_score_menu(){
system("cls");
printf("******************************************************************************************************************\n\n");
print_xiangmu_male();
printf("\n");
printf("请输入添加成绩的项目的序号\n");
int id;
scanf("%d",&id);
if(id>=1&&id<male_xiangmu_num)
input_male_xiangmu_score(id);
else{
printf("输入错误,按任意键重新输入!\n");
getch();
input_male_xiangmu_score_menu();
}
}
//输入女生各个项目成绩
void input_female_xiangmu_score(int id){
int x=id;
printf("请输入需要添加的成绩个数(输入前三名或者前五名):\n");
int num;
scanf("%d",&num);
if(num!=3&&num!=5){
printf("您的输入有误,请按任意键重新输入!\n");
getch();
}
else{
printf("请输入学校名和成绩,中间以空格区分:\n");
printf("(注意要按照成绩排名由高到低输入,系统将会自动赋予积分)\n");
char str[MAX][MAX];
for(int i=1;i<=num;i++){
fflush(stdin);
gets(str[i]);
FILE *f=fopen(xiangmu_files_female[id].file_name,"a");
if((f=fopen(xiangmu_files_female[id].file_name,"a"))==NULL)
{
printf("cannot open file!\n");
exit(0);
}
else{
fputs(str[i],f);
fputs("\n",f);
}
fclose(f);
char *name;
char *score_str;
int score_int;
/* 获取第一个子字符串 */
name = strtok(str[i], " ");
score_str= strtok(NULL, " ");
score_int=atoi(score_str);
//先存入项目排名中
strcpy(xiangmu_rating_female[id].name_rating[i],name);
xiangmu_rating_female[id].num=num;
//查找并添加成绩
if(num==3){//前三名加分法
for(int k=1;k<school_num;k++){
if(strcmp(name,schools[k].school_name)==0){
schools[k].total+=rating_three_score[i];
schools[k].female_total+=rating_three_score[i];
ListInsert(schools[k].name_pointer_female[id].List_head,score_int);
}
}
}
else if(num==5){//前五名加法
for(int k=1;k<school_num;k++){
if(strcmp(name,schools[k].school_name)==0){
schools[k].total+=rating_five_score[i];
schools[k].female_total+=rating_five_score[i];
ListInsert(schools[k].name_pointer_female[id].List_head,score_int);
}
}
}
}
printf("输入成功!\n");getch();
}
}
//提示输入女生成绩的页面
void input_female_xiangmu_score_menu(){
system("cls");
printf("******************************************************************************************************************\n\n");
print_xiangmu_female();
printf("\n");
printf("请输入添加成绩的项目的序号\n");
int id;
scanf("%d",&id);
if(id>=1&&id<female_xiangmu_num)
input_female_xiangmu_score(id);
else{
printf("输入错误,按任意键重新输入!\n");
getch();
input_female_xiangmu_score_menu();
}
}
void printf_sorted_total_score(){
for(int i = 1; i <school_num-1; i++)
for(int j = 1; j < school_num - i; j++)
if(schools[j].total<schools[j+1].total){
school tmp=schools[j];
schools[j]=schools[j+1];
schools[j+1]=tmp;
}
printf("按照降序排列的学校团体成绩:\n");
for(int i=1;i<school_num;i++){
printf("%d: %s %d\n",i,schools[i].school_name,schools[i].total);
}
printf("按任意键继续!\n");
getch();
}
void printf_sorted_male_score(){
for(int i = 1; i <school_num-1; i++)
for(int j = 1; j < school_num - i; j++)
if(schools[j].male_total<schools[j+1].male_total){
school tmp=schools[j];
schools[j]=schools[j+1];
schools[j+1]=tmp;
}
printf("按照降序排列的学校男生团体成绩:\n");
for(int i=1;i<school_num;i++){
printf("%d: %s %d\n",i,schools[i].school_name,schools[i].male_total);
}
printf("按任意键继续!\n");
getch();
}
void printf_sorted_name(){
for(int i = 1; i <school_num-1; i++)
for(int j = 1; j < school_num - i; j++)
if(strcmp(schools[j].school_name,schools[j+1].school_name)>0){
school tmp=schools[j];
schools[j]=schools[j+1];
schools[j+1]=tmp;
}
printf("按照学校名升序排列的学校名:\n");
for(int i=1;i<school_num;i++){
printf("%d: %s\n",i,schools[i].school_name);
}
printf("按任意键继续!\n");
getch();
}
void printf_sorted_female_score(){
for(int i = 1; i <school_num-1; i++)
for(int j = 1; j < school_num - i; j++)
if(schools[j].female_total<schools[j+1].female_total){
school tmp=schools[j];
schools[j]=schools[j+1];
schools[j+1]=tmp;
}
printf("按照降序排列的学校女生团体成绩:\n");
for(int i=1;i<school_num;i++){
printf("%d: %s %d\n",i,schools[i].school_name,schools[i].female_total);
}
printf("按任意键继续!\n");
getch();
}
//查询学校项目
void search_school_xiangmu_menu(){
system("cls");
printf("******************************************************************************************************************\n\n");
print_school();
printf("请输入查询的学校:\n");
int n;
scanf("%d",&n);
if(n<1||n>school_num){
printf("输入错误,按任意键重新输入!\n");
getch();
search_school_xiangmu_menu();
}
else{
printf("男生项目情况:\n");
for(int i=1;i<male_xiangmu_num;i++){
printf("%s:\n",schools[n].name_pointer_male[i].xiangmu_name);
PrintLinkList(schools[n].name_pointer_male[i].List_head);
}
printf("\n\n");
printf("女生项目情况:\n");
for(int i=1;i<female_xiangmu_num;i++){
printf("%s:\n",schools[n].name_pointer_female[i].xiangmu_name);
PrintLinkList(schools[n].name_pointer_female[i].List_head);
}
printf("按任意键继续!\n");
getch();
}
}
void search_school_xiangmu_menu_male(){
system("cls");
printf("******************************************************************************************************************\n\n");
print_xiangmu_male();
printf("请输入查询的项目:\n");
int n;
scanf("%d",&n);
if(n<1||n>=male_xiangmu_num){
printf("输入错误!\n");
printf("按任意键重新输入!\n");
getch();
}
for(int i=1;i<=xiangmu_rating_male[n].num;i++){
printf("%s\n",xiangmu_rating_male[n].name_rating[i]);
}
printf("按任意键继续!\n");
getch();
}
void search_school_xiangmu_menu_female(){
system("cls");
printf("******************************************************************************************************************\n\n");
print_xiangmu_female();
printf("请输入查询的项目:\n");
int n;
scanf("%d",&n);
if(n<1||n>=female_xiangmu_num){
printf("输入错误!\n");
printf("按任意键重新输入!\n");
getch();
}
else{
for(int i=1;i<=xiangmu_rating_female[n].num;i++){
printf("%s\n",xiangmu_rating_female[n].name_rating[i]);
}
printf("按任意键继续!");
getch();
}
}
//打印所有学校2
void print_school_2(){
printf("所有参赛学校如下:\n");
for(int i=1;i<school_num;i++){
printf("%d ",schools[i].id);
printf("%s\n",schools[i].school_name);
}
printf("按任意键继续!");
getch();
}
//读取文件等一系列初始化操作
void initialize(){
input_file_school();
input_file_xiangmu();
create_file_xiangmu();
}
int main(){
initialize();
Menu();
return 0;
}