您的位置:首页 > 理论基础 > 数据结构算法

<数据结构>运动会分数统计

2016-12-29 17:51 344 查看
#include<iostream>
#include<iomanip>
#include<string.h>
#include<fstream>
#include<stdlib.h>
#include<ctype.h>
#include<stdio.h>
#include<conio.h>
using namespace std;

class School    //学校
{
private:
char name[20];
int number;
int boy;
int girl;
public:
School *next;
void School_add();
void School_sort();
// int School_getlong(School *first);
void School_output(School *p);
int School_isexist(int a);
void School_show(int a);
void School_search(int a);
void School_addmark(int a, int b, int c);
void School_order(School *temp, int type);
};
class Sport    //运动项目
{
private:
char name[20];
int isboy;            //0为女项目,1为男项目
int is3;            //0为取前五名,1为取前五名
int number;            //项目编号
int first;            //
int second;            //
int third;            //
int fourth;            //
int fifth;            //
public:
Sport *next;
int Sport_isexist(int a);
void Sport_add();
// int Sport_getlong(Sport *first);
void Sport_output(Sport *p);
void Sport_search(int a);
};

School* head1;
Sport* head2;

void School::School_add()
{
School* p;
int mark = 0;
p = new School;
cout << "请输入学校的名称:";
cin >> p->name;
int c;
while (mark != 1)
{

cout << "请输入学校编号:";
cin >> c;
p->number = c;
mark = 1;
p->boy = 0;
p->girl = 0;
p->next = head1->next;
head1->next = p;
cout << "成功添加了一个学校" << endl;
cout << "是否还要添加学校?(y/n)" << endl;

char input;
cin >> input;
switch (input)
{

case 'y':
mark = 0;
School_add();
case 'n':
mark = 1;
return;
}
}

delete p;

}
void School::School_output(School* p)//输出学校
{
cout << "当前学校(名称) 编号 男团总分 女团总分 总分\t\n";
//  School *p;
p = head1;
p = p->next;
while (p)
{
cout << p->name << "\t\t" << p->number << "\t"
<< p->boy << "\t" << p->girl << "  \t  " << (p->girl + p->boy) << endl;
p = p->next;
}
}

int School::School_isexist(int a)
{
int b = 0;
School* p;
p = head1;
p = p->next;
while (p)
{
if (p->number == a)
{
return 1;
}
p = p->next;
}
return 0;
}

void School::School_show(int a)//输出所有学校
{
School *p;
p = head1;
p = p->next;
while (p)
{
if (p->number == a)
{
cout << p->name << " " << endl;
return;
}
p = p->next;
}
cout << "无";
}

void School::School_search(int a)
{
School* p;
p = head1;
p = p->next;
while (p)
{
if (p->number == a)
{
cout << "学校名称:" << p->name << "  " << "男子团体总分:" << p->boy << "  "
<< "女子团体总分:" << p->girl << "  " << "总分:" << (p->boy + p->girl) << "  ";
return;
}
p = p->next;
}
cout << "无此编号:" << endl;
}

void School::School_addmark(int a, int b, int c) //a为分数,b为学校编号,c=1表示男,c=0表示女
{
School* p;
p = head1;
p = p->next;
while (p)
{
if (p->number == b)
{
if (c == 1)
{
p->boy = p->boy + a;
}
else
{
p->girl = p->girl + a;
}
}
p = p->next;
}
}

void School::School_sort()             //单独编写此排序函数。不难,有心即可。
{
School* q, *small;
School* temp1;
temp1 = new School;
temp1->next = NULL;
q = head1;
q = q->next;
small = head1;
small = small->next;
temp1->girl = q->girl;
q->girl = small->girl;
small->girl = temp1->girl;
temp1->boy = q->boy;
q->boy = small->boy;
small->boy = temp1->boy;
strcpy_s(temp1->name, q->name);
strcpy_s(q->name, small->name);
strcpy_s(small->name, temp1->name);
temp1->number = q->number;
q->number = small->number;
small->number = temp1->number;    //
}

//此次设计,主要针对的是这一排序函数。
//将4处同段的代码另外独立编写成以上的一个函数School_sort()。如此,程序代码得到进一步精炼。
void School::School_order(School *temp, int type)
{
//type=0按总分,type=1按男总分,type=2按女总分,type=3按学校编号
School* q=NULL, *small=NULL;
School* temp1;
School sh;
temp1 = new School;
temp1->next = NULL;
while (q&&small)
{
switch (type)
{
case 0:   //按总分排序
for (q = head1; q = q->next; q != NULL)
for (small = head1; small = small->next; small != NULL)
{
if ((q->boy + q->girl)<(small->boy + small->girl))
{
sh.School_sort();          //调用排序函数School_sort(),不会是同样的大段代码到处都是。
}
}
break;
case 1:    //按男团体排序
for (q = head1; q = q->next; q != NULL)
for (small = head1; small = small->next; small != NULL)
{
if (q->boy<small->boy)
{
sh.School_sort();         //调用排序函数School_sort(),
}
}
break;
case 2:    //按女团体排序
for (q = head1; q = q->next; q != NULL)
for (small = head1; small = small->next; small != NULL)
{
if (q->girl<small->girl)
{
sh.School_sort();         //调用排序函数School_sort(),
}
}
break;
case 3:
for (q = head1; q = q->next; q != NULL)
for (small = head1; small = small->next; small != NULL)
{
if (q->number<small->number)
{
sh.School_sort();         //调用排序函数School_sort(),
}
}
break;
default:
cout << "error" << endl;
break;
}
}
}

int Sport::Sport_isexist(int a)
{
int b = 0;
Sport* p;
p = head2;
while (p)
{
if (p->number == a)
{
return 1;
}
p = p->next;
}
return 0;
}

void Sport::Sport_add()
{
Sport* p;
// School sh;
int mark = 0;
p = new Sport;
cout << "请输入项目名称:";
cin >> p->name;
int c;
while (mark != 1)
{
cout << "请输入项目编号:";
cin >> c;
if (Sport_isexist(c))
{
cout << "该编号已存在!";
}
else
{
mark = 1;
p->number = c;
}
}
mark = 0;
while (mark != 1)
{
cout << "请输入项目类型(0为女子项目,1为男子项目):";
cin >> c;
// p->isboy=(int)(c-'0');//字符转换成数字
mark = 1;
p->isboy = c;
}
mark = 0;
while (mark != 1)
{
cout << "请输入项目名称情况(0为取前3名,1为取前5名):";
cin >> c;
// p->is3=(int)(c-'0');
mark = 1;
p->is3 = c;
}
mark = 0;
School sh;
while (mark != 1)
{
cout << "请输入第一名学校的编号:";
cin >> c;
if (!sh.School_isexist(c))
{
cout << "该学校不存在,请先添加!";
}
else
{
mark = 1;
p->first = c;
if (p->is3 == '0')
sh.School_addmark(5, c, p->isboy);
else
sh.School_addmark(7, c, p->isboy);
}
}
mark = 0;
while (mark != 1)
{
cout << "请输入第二名学校的编号:";
cin >> c;
if (!sh.School_isexist(c))
{
cout << "该学校不存在,请先添加!";
}
else
{
mark = 1;
p->second = c;
if (p->is3 == '0')
sh.School_addmark(3, c, p->isboy);
else
sh.School_addmark(5, c, p->isboy);
}
}
mark = 0;
while (mark != 1)
{
cout << "请输入第三名学校的编号:";
cin >> c;
if (!sh.School_isexist(c))
{
cout << "该学校不存在,请先添加";
}
else
{
mark = 1;
p->third = c;
if (p->is3 == '0')
sh.School_addmark(2, c, p->isboy);
else
sh.School_addmark(3, c, p->isboy);
}
}
mark = 0;
if (p->is3 == '1')
{
while (mark != 1)
{
cout << "请输入第四名学校的编号:";
cin >> c;
if (!sh.School_isexist(c))
{
cout << "该学校不存在,请先添加";
}
else
{
mark = 1;
p->fourth = c;
sh.School_addmark(2, c, p->isboy);
}
}
mark = 0;
while (mark != 1)
{
cout << "请输入第五名学校的编号:";
if (!sh.School_isexist(c))
{
cout << "该学校不存在,请先添加!";
}
else
{
mark = 1;
p->fifth = c;
sh.School_addmark(1, c, p->isboy);
}
}
}
else
{
p->fourth = '0';
p->fifth = '0';
}
p->next = head2->next;
head2->next = p;
cout << "成功添加了一个运动项目" << endl;
}

void Sport::Sport_output(Sport *p)                        //输出项目的情况
{
p = head2;
p = p->next;
cout << "当前项目名称" << "\t" << "编号" << " " << "B/G" << " " << "3/5" << " " << "第一名" << " "
<< "第二名" << "  " << "第三名" << "  " << "第四名" << "  " << "第五名" << " " << endl;
School sh;
while (p)
{
cout << p->name << "\t" << " " << p->number << "   " << p->isboy << " " << p->is3 << "      "
<< "  ";
sh.School_show(p->first);
sh.School_show(p->second);
sh.School_show(p->third);
sh.School_show(p->fourth);
sh.School_show(p->fifth);
p = p->next;
cout << "\n";

}
cout << endl;
}
void Sport::Sport_search(int a)                        //搜索项目
{
Sport *p;
School sh;
p = head2;
p = p->next;
while (p)
{
if (p->number == a)
{
cout << "项目名:" << p->name << endl << "项目类型:";
if (p->isboy == 1)
{
cout << "男子项目";
}
else
{
cout << "女子项目";
}
cout << endl << "第一名:";
sh.School_show(p->first);
cout << endl << "第二名:";
sh.School_show(p->second);
cout << endl << "第三名:";
sh.School_show(p->third);
cout << endl << "第四名:";
sh.School_show(p->fourth);
cout << endl << "第五名:";
sh.School_show(p->fifth);
return;
}
p = p->next;
}
cout << "无此编号";
}

void main()
{
system("color C");
head1 = new School;
head1->next = NULL;
head2 = new Sport;
head2->next = NULL;
//school_add();
School sh;
Sport sp;
School* p1;
Sport* p2;
p1 = head1;
p1 = p1->next;
p2 = head2;
p2 = p2->next;
int choose;
char temp;
int a = 1;
while (a != 0)
{
cout << "                                                           " << endl;
cout << "                欢迎使用运动会分数统计系统           " << endl;
cout << " ----------------------------------------------------------" << endl;
cout << "                 -----July制作/05/18----                   " << endl;
cout << "                                                           " << endl;
cout << "        1.输入学校;                 2.输入运动项目         " << endl;
cout << "        3.按学校编号输出总分;       4.按学校总分排序;      " << endl;
cout << "        5.按男团体总分排序;         6.按女团体总分排序;    " << endl;
cout << "        7.按学校编号查询;           8.按项目编号查询;      " << endl;
cout << "                        0.退出                             " << endl;
cout << "            注:输入运动项目之前请输入学校               " << endl;
cout << " ----------------------------------------------------------" << endl;
cout << "               -------请选择(0-8):------                   " << endl;
cin >> choose;
switch (choose)
{

case 1:
sh.School_add();
break;
case 2:
sp.Sport_output(p2);
sh.School_output(p1);
sp.Sport_add();
break;
case 3:
sh.School_order(p1, 3);
sh.School_output(p1);
break;
case 4:
sh.School_order(p1, 0);
sh.School_output(p1);
break;
case 5:
sh.School_order(p1, 1);
sh.School_output(p1);
break;
case 6:
sh.School_order(p1, 2);
sh.School_output(p1);
break;
case 7:
cout << "请输入学校编号:";
cin >> temp;
sh.School_search(temp);
break;
case 8:
cout << "请输入项目编号:";
cin >> temp;
sp.Sport_search(temp);
break;
case 0:
a = 0;
break;
default:
cout << "操作非法\n";
}
}
system("exit");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: