#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
#include<stdio.h>
#define N 21
using namespace std;
const char* menus[20] = { "0.结束\n","1.显示","2.编序号:\n","3.按学校总分排序输出\n",
"4.按学校男总分排序输出\n","5.按学校女总分排序输出\n","6.查询某个学校成绩\n",
"7.查询某个项目成绩\n","8.保存信息\n","9.读取信息","10.插入一个元素","11.显示",
"12.输出","13.删除"};//指针数组
const int t = 14;
int n; //学校
int m; //男子项目
int w; //女子项目
struct pro
{
string name; //学校名称
int schoolnum[4]; //前5名学校的编号
int schoolnumy[2]; //前3名学校的编号
};
typedef struct pro p;
struct School //表示学校的结构体
{
int num;
string name; //学校名称
int score; //学校总分
int male; //男子总分
int female; //女子总分
};
typedef struct School school;
struct noteBook
{
school dataSet[N];
int length;
};
typedef struct noteBook NoteBook;
//前五得分
int integral[5] = { 7,5,3,2,1 };
//前三得分
int integraly[3] = { 7,5,3 };
void showMenu();
void init(NoteBook* notebook);
void control();
void input();
void print(school *sch);
void bianhao(school* sch);
void zongfen(school* sch);
void malezf(school* sch);//按学校男总分排序
void femalezf(school* sch);//按学校女总分排序
void cxsch(school* sch);//查询学校信息
void cxxm(school* sch, pro* p);//查询项目
void saveData(const school sch[], int num);
void readData(school sch[], int& numSchools);
void Instail(NoteBook* notebook, school newSchool, int index);
void displayAllData(NoteBook* notebook);
int deleteElement(NoteBook* notebook, int i);
int main() //主函数
{
printf("〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓\n");
printf("\t 运动会分数统计系统\n");
printf("\t |三名制(1,2,3)\n");
printf("\t |分数--5,3,2\n");
printf("\t |五名制(1,2,3,4,5) \n");
printf("\t |分数--7,5,3,2,1\n");
printf("〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓\n\t");
input();
control();
return 0;
}
/*---------显示菜单showMenu---------*/
void showMenu()
{
int i;
for (i = 0; i < t; i++)
{
printf("%s\n", menus[i]);
}
puts("\n");
}
/*---------初始化---------*/
void init(NoteBook* book)
{
book->length = 0;
}
/*---------控制菜单showMenu---------*/
void control()
{
NoteBook noteBook;
init(¬eBook);
school newSchool;
school myS;
pro p;
int i = 1;
int sel = 1;
int loc;
while (sel != 0)
{
showMenu();
puts("请输入你的选择为:\n");
scanf_s("%d", &sel);
switch (sel)
{
case 0:
return;
case 1:
displayAllData(¬eBook);
break;
break;
case 2:
bianhao(&myS);
break;
case 3:
zongfen(&myS);
break;
case 4:
malezf(&myS);
break;
case 5:
femalezf(&myS);
break;
case 6:
cxsch(&myS);
break;
case 7:
cxxm(&myS,&p);
break;
case 8:
saveData(&myS,n);
if (1)
puts("\nsave successed!");
else
puts("\nsave failed!");
break;
case 9:
readData(&myS, n);
break;
case 10:
cout << "输入新学校的信息:" << endl;
cout << "编号: ";
cin >> newSchool.num;
cout << "名称: ";
cin >> newSchool.name;
cout << "总分 男子总分 女子总分: ";
cin >> newSchool.score >> newSchool.male >> newSchool.female;
cout << "输入你要插入的位置:" << endl;
cin >> loc;
Instail(¬eBook,newSchool,loc);
break;
case 11:
case 12:
print(&myS);
break;
case 13:
printf("输入你要删除的位置:");
scanf_s("%d", &loc);
deleteElement(¬eBook, loc);
break;
default:
cout<<"error select!";
break;
}
}
}
/*---------输入学校,项目---------*/
void input()
{
school sch[N];
pro p[N];
int i, j;
int y;
int x, q;
printf("输入学校数目:");
y = 0;
while (1)
{
scanf_s("%d", &n); //学校数目
if (n >= 1 && n <= 20)
y = 1;
if (y)
break;
else
printf("输入数据有误,请重新输入:");
}
for (i = 1; i <= n; i++)
{
printf("输入第%d个学校的名称:", i);
cin >> sch[i].name; //给学校结构体的成员初始化为
sch[i].score = 0;
sch[i].female = 0;
sch[i].male = 0;
sch[i].num = i;
}
printf("输入男子项目数和女子项目数:");
y = 0;
while (1)
{
scanf_s("%d%d", &m, &w);
if (m <= 20 && m >= 1 && w <= 20 && w >= 1)
y = 1;
if (y)
break;
else
printf("输入数据有误,请重新输入:");
}
for (i = 1; i <= m + w; i++)
{
printf("输入第%d个项目的名称:\n", i);
cin >> p[i].name;
printf("该项目取前三名选0,取前5名选1:");
scanf_s("%d", &q);
if (q == 1)
{
printf("输入第%d个项目的前5名的学校编号:\n", i);
for (j = 1; j <= 5; j++)
{
y = 0;
while (1)
{
scanf_s("%d", &x); //学校编号
if (x >= 1 && x <= 20)
y = 1;
if (y)
break;
else
printf("输入数据有误,请重新输入:");
}
p[i].schoolnum[j] = x; //给项目结构体的成员赋值
sch[x].score += integral[j - 1]; //给学校结构体成员赋值
if (i <= m)
sch[x].male += integral[j - 1]; //学校结构体赋值,男子总分
else
sch[x].female += integral[j - 1]; //学校结构体赋值,女子总分
}
}
if (q == 0)
{
printf("输入第%d个项目的前3名的学校编号:\n", i);
for (j = 1; j <= 3; j++)
{
y = 0;
while (1)
{
scanf_s("%d", &x); //学校编号
if (x >= 1 && x <= 20)
y = 1;
if (y)
break;
else
printf("输入数据错误,请重新输入:");
}
p[i].schoolnumy[j] = x; //给项目结构体的成员赋值
sch[x].score += integraly[j - 1]; //给学校结构体成员赋值
if (i <= m)
sch[x].male += integraly[j - 1]; //学校结构体赋值
else
sch[x].female += integraly[j - 1]; //学校结构体赋值
}
}
}
}
/*---------输出---------*/
void print(school *sch)
{
cout <<left<< setw(8)<<sch->num << setw(10) << sch->name
<< setw(9) << sch->score << setw(8) << sch->male
<< setw(10) << sch->female << endl;
}
/*---------1.按编号排序---------*/
void bianhao(school *sch)
{
int i, j;
school t;
for (i = 1; i < n; i++)
{
for (j = i; j <= n; j++)
if (sch[i].num > sch[j].num)
{
t = sch[i]; sch[i] = sch[j]; sch[j] = t;
}
}
printf("\n按编号排列:\n");
printf("编号 学校名称 总分 男子总分 女子总分\n");
for (i = 1; i <= n; i++)
print(&sch[i]);
}
/*--------2.按学校总分排序---------*/
void zongfen(school* sch)
{
int i, j;
school t;
for (i = 1; i < n; i++)
{
for (j = i; j <= n; j++)
if (sch[i].score < sch[j].score)
{
t = sch[i]; sch[i] = sch[j]; sch[j] = t;
}
}
printf("\n按学校总分排列:\n");
printf("编号 学校名称 总分 男子总分 女子总分\n");
for (i = 1; i <= n; i++)
print(&sch[i]);
}
/*-------3.按学校男总分排序---------*/
void malezf(school* sch)
{
int i, j;
school t;
for (i = 1; i < n; i++)
{
for (j = i; j <= n; j++)
if (sch[i].male < sch[j].male)
{
t = sch[i]; sch[i] = sch[j]; sch[j] = t;
}
}
printf("\n按学校男子总分排列:\n");
printf("编号 学校名称 总分 男子总分 女子总分\n");
for (i = 1; i <= n; i++)
print(&sch[i]);
}
/*--------4.按学校女总分排序---------*/
void femalezf(school* sch)
{
int i, j;
school t;
for (i = 1; i < n; i++)
{
for (j = i; j <= n; j++)
if (sch[i].female < sch[j].female)
{
t = sch[i]; sch[i] = sch[j]; sch[j] = t;
}
}
printf("\n按学校女子总分排列:\n");
printf("编号 学校名称 总分 男子总分 女子总分\n");
for (i = 1; i <=n; i++)
print(&sch[i]);
cout << endl;
}
/*--------5.查询学校信息 ---------*/
void cxsch(school* sch)
{
int i, y, s;
printf("输入需要查询的学校编号:");
y = 0;
while (1)
{
scanf_s("%d", &s);
if (s >= 1 && s <= n)
y = 1;
if (y)
break;
else
printf("输入数据有误,请重新输入:");
}
printf("该学校相关信息:\n");
printf("编号 学校名称 总分 男子总分 女子总分\n");
for (i = 1; i <= n; i++)
{
if (sch[i].num == s)
{
print(&sch[i]);
break;
}
}
cout << endl;
}
/*--------6.查询项目信息 ---------*/
void cxxm(school* sch, pro* p)
{
int i, y, s, v;
printf("输入需要查询的项目编号:");
y = 0;
while (1)
{
scanf_s("%d", &s);
if (s >= 1 && s <= n)
y = 1;
if (y)
break;
else
printf("输入数据有误,请重新输入:");
}
printf("该项目取的前三名选0,取的前5名选1: ");
scanf_s("%d", &v);
if (v == 1)
{
cout << p[s].name << "前5名学校编号及名称为:" << endl;
printf("名次 编号 学校名称\n");
for (i = 1; i <= 5; i++)
cout << " " << i << " " << p[s].schoolnum[i]
<< setw(12) << sch[p[s].schoolnum[i]].name << endl;
}
if (v == 0)
{
cout << p[s].name << "前3名学校编号及名称为:" << endl;
printf("名次 编号 学校名称\n");
for (i = 1; i <= 3; i++)
cout << " " << i << " " << p[s].schoolnumy[i]
<< setw(12) << sch[p[s].schoolnumy[i]].name << endl;
}
cout << endl;
}
/*---------7.保存中的元素---------*/
void saveData(const school sch[], int num)
{
// 保存信息到文件
ofstream fout("运动会分数统计.txt", ios::out);
if (fout.is_open())
{
fout << "编号 学校名称 总分 男子总分 女子总分" << endl;
for (int i = 1; i <= num; i++)
{
fout << sch[i].num << setw(13) << sch[i].name << setw(8)
<< sch[i].score << setw(9) << sch[i].male << setw(10)
<< sch[i].female << endl;
}
fout.close();
cout << "保存成功!" << endl;
}
else
{
cout << "保存失败,文件无法打开!" << endl;
}
}
/*---------8.读取中的元素---------*/
void readData(school sch[], int& numSchools)
{
ifstream fin("运动会分数统计.txt", ios::app);
if (fin.is_open())
{
fin >> numSchools; // 读取学校数量
cout << "从文件中读取数据:" << endl;
cout << "编号 学校名称 总分 男子总分 女子总分" << endl;
for (int i = 1; i <= numSchools; i++)
{
fin >> sch[i].num >> ws;
getline(fin, sch[i].name, '#');
fin >> sch[i].score >> sch[i].male >> sch[i].female;
print(&sch[i]);
}
cout << "读取成功!" << endl;
fin.close();
}
else
{
cout << "读取失败,文件无法打开!" << endl;
}
}
/*---------9.第i个位置插入元素---------*/
void Instail(NoteBook* notebook, school newSchool, int index)
{
if (index < 1 || index > notebook->length + 1)
{
cout << "插入位置无效!" << endl;
return;
}
// 向数组中插入学校
for (int i = notebook->length; i >= index; --i)
{
notebook->dataSet[i + 1] = notebook->dataSet[i];
}
notebook->dataSet[index] = newSchool;
++notebook->length;
cout << "插入后的数据:" << endl;
for (int i = 1; i <= notebook->length; ++i)
{
print(¬ebook->dataSet[i]);
}
cout << "插入成功!" << endl;
}
/*---------10.输出---------*/
void displayAllData(NoteBook* notebook)
{
cout << "所有学校的信息:" << endl;
cout << left << setw(8) << "编号" << setw(20) << "学校名称" << setw(8)
<< "总分" << setw(8) << "男子总分" << setw(20) << "女子总分" << endl;
int len= notebook->length;
for (int i = 1; i <=len; i++)
{
print(¬ebook->dataSet[i]); // print 函数用于显示单个学校的信息
}
}
/*---------10.删除某个元素---------*/
int deleteElement(NoteBook* notebook, int i)
{
int k;
int n;
n = notebook->length;
if (i<1 || i>notebook->length)
return 0;
for (k = i; k < n; k++)
{
notebook->dataSet[k - 1] = notebook->dataSet[k];
}
notebook->length = n - 1;
return 1;
cout << "删除后的数据:" << endl;
for (int i = 1; i <= notebook->length; ++i)
{
print(¬ebook->dataSet[i]);
}
cout << "删除成功!" << endl;
}