跳过导航.
主页

课程设计题,想请教一下高手

设计, 课程

问题描述
足球联赛采用主客场双循环赛制,胜一场得3分,平局各得1分,负一场得0分,联赛排名以积分多者在前,当两队(或多队)积分相同时,则净胜球(即进球数与失球数之差)多者在前,若净胜球相同,则进球数多者在前,若仍相同,则抽签或踢附加赛决定名次(这在联赛结束后进行,联赛未结束则两队名次并列,本程序不做这方面要求)。试编一程序统计最近一轮比赛后,各队积分及排名。
基本要求
设积分表结构如下:队名(不超过15个字符),已比赛的场数,赢的场数,平的场数,负的场数,进球数,失球数,积分。积分表放在正文文件中。最近一轮的结果从键盘输入,其形式为:主队名(可用代码),客队名(可用代码),主队得分(即进球数),客队得分(即进球数)。程序应根据此轮结果修改各队的积分和名次,所得的最新记分表仍在原积分文件中并同时在屏幕上显示。
测试数据
可选择我国当年的甲A或甲B联赛的数据输入,并检查与报章公布的数据是否一致。
实现提示
定义一个球队类,每个球队是均是此类的对象。由于联赛中参赛的队伍数是固定的,因此可用对象数组来实现(当然也可以用链表结构)。每输入两个队的比赛成绩,则相应的队的有关数据(比赛场数,赢的场数,平的场数,负的场数,进球数,失球数,积分等)即可进行修改,比赛成绩录入完成,调用联赛排序方法(对象数组作为参数)排出名次并输出。
我们这学期才学面向对象的c++,这题好像要用图还有数据结构,希望高手能指点一下,我该怎么下手,需要哪些知识,谢谢了啊先



评论查看选项

选择您喜欢的显示评论的模式,并点击"保存设置"来激活您所做的改变。


#include
#include

#include
#include
#include
#include
#include
using namespace std;

class Team
{
enum {winScores = 3, drawScores = 1, lostScores = 0};//赢3平1负0分
char m_szName[16]; //队名
char m_szID[10]; //队号(可自动生成或输入,现采用自动生成方式)
unsigned m_nFields; //场次
unsigned m_nWin; //赢场数
unsigned m_nDraw; //平场数
unsigned m_nLost; //输场数
unsigned m_nGoalBalls; //赢球数
unsigned m_nLostBalls; //输球数
unsigned m_nScores; //积分
static unsigned s_nTeams; //总队数

public:
Team(char *szName = "",
char *szID = "",
unsigned nFields = 0,
unsigned nWin = 0,
unsigned nDraw = 0,
unsigned nLost = 0,
unsigned nGoalBalls = 0,
unsigned nLostBalls = 0,
unsigned nScores = 0
):m_nFields(nFields), m_nWin(nWin), m_nDraw(nDraw), m_nLost(nLost), m_nGoalBalls(nGoalBalls),
m_nLostBalls(nLostBalls), m_nScores(nScores)
{
if (szName != NULL)
{
strncpy(m_szName, szName, sizeof(m_szName) - 1);
m_szName[sizeof(m_szName) - 1] = '\0';
}
else
m_szName[0] = '\0';

if (szID != NULL)
{
strncpy(m_szID, szID, sizeof(m_szID) - 1);
m_szID[sizeof(m_szID) - 1] = '\0';
}
else
m_szID[0] = '\0';
}

Team(const Team &rhs)
{
strcpy(m_szName, rhs.m_szName);
strcpy(m_szID, rhs.m_szID);
m_nFields = rhs.m_nFields;
m_nWin = rhs.m_nWin;
m_nDraw = rhs.m_nDraw;
m_nLost = rhs.m_nLost;
m_nGoalBalls = rhs.m_nGoalBalls;
m_nLostBalls = rhs.m_nLostBalls;
m_nScores = rhs.m_nScores;
}

~Team(){}

const Team& operator=(const Team &rhs)
{
if (this != &rhs)
{
strcpy(m_szName, rhs.m_szName);
strcpy(m_szID, rhs.m_szID);
m_nFields = rhs.m_nFields;
m_nWin = rhs.m_nWin;
m_nDraw = rhs.m_nDraw;
m_nLost = rhs.m_nLost;
m_nGoalBalls = rhs.m_nGoalBalls;
m_nLostBalls = rhs.m_nLostBalls;
m_nScores = rhs.m_nScores;
}

return *this;
}
const char * getName() const{return m_szName;}
const unsigned int getScores() const { return m_nScores;}
const int getWinBalls() const { return m_nGoalBalls - m_nLostBalls;}
void print() const { cout << setw(15)<< m_szName < << " GB:" << m_nGoalBalls << " LB:" << m_nLostBalls
<<" S:" << m_nScores << ends << " W:"<< m_nWin
<< " D:"<< m_nDraw << " L:" << m_nLost << endl;}
//计算并更新数据
void compute(int iGoalBalls, int iLostBalls);
void createID() {} //自动生成ID
void promoteGrade(){}//升级
void demoteGrade(){} //降级

//是否first球队积分大于 two球队
friend bool operator>(const Team &first, const Team &two)
{
if (first.m_nScores > two.m_nScores ||
(first.m_nScores == two.m_nScores && (first.getWinBalls() >two.getWinBalls())))
return true;
return false;
}
//是否first球队积分小于 two球队
friend bool operator<(const Team &first, const Team &two)
{
if (first.m_nScores < two.m_nScores ||
(first.m_nScores == two.m_nScores && (first.getWinBalls() < two.getWinBalls())))
return true;
return false;
}
//是否是同一个球队(不是积分相等 find()中使用)
friend bool operator==(const Team &first, const Team &two)
{
if (strcmp(first.m_szName, two.m_szName) == 0)
return true;
return false;
}
};

unsigned Team::s_nTeams = 1;

void Team::compute(int iGoalBalls, int iLostBalls)
{
if (iGoalBalls < 0 || iLostBalls < 0)
{
cout << "input wrong (must >= 0) " << endl;
return ;
}

m_nFields++;
if (iGoalBalls > iLostBalls)
m_nWin++;
if (iGoalBalls == iLostBalls)
m_nDraw++;
if (iGoalBalls < iLostBalls)
m_nLost++;

m_nGoalBalls += iGoalBalls;
m_nLostBalls += iLostBalls;
m_nScores = m_nWin * winScores + m_nDraw * drawScores + m_nLost * lostScores;
}

void main()
{
vector vTeam;
ifstream in("save.dat", ios::binary);//| ios::nocreate);
//读取以前的数据
if (in)
{
int i = 0;
do{
Team t;
in.seekg(sizeof(Team) * i++);
in.read((char*)&t, sizeof(Team));
if (*t.getName() == 0) break;
vTeam.push_back(t);
t.print();
}while(1);
}

in.close();

char szHost[16], szGuest[16];
int iHostBalls, iGuestBalls;
//输入本场比分
cout << "Input host team , guest team and scores of each other(example: one two 3 2)\n";
do{
cin >> szHost >> szGuest >> iHostBalls >> iGuestBalls;
}while(strcmp(szHost, szGuest) == 0);

Team tHost(szHost), tGuest(szGuest);
vector::iterator itTemp;
//本球队有记录则更新否则添加
if ((itTemp = find(vTeam.begin(), vTeam.end(), tHost)) == vTeam.end())
{
tHost.compute(iHostBalls, iGuestBalls);
tHost.createID();
vTeam.push_back(tHost);
}
else
(*itTemp).compute(iHostBalls, iGuestBalls);

if ((itTemp = find(vTeam.begin(), vTeam.end(), tGuest)) == vTeam.end())
{
tGuest.compute(iGuestBalls, iHostBalls);
tGuest.createID();
vTeam.push_back(tGuest);
}
else
(*itTemp).compute(iGuestBalls, iHostBalls);
//排序并显示
sort(vTeam.begin(), vTeam.end());

vector vT2(vTeam.size());
reverse_copy(vTeam.begin(), vTeam.end(), vT2.begin());

for (vector::iterator it = vT2.begin(); it != vT2.end(); it++)
(*it).print();
//保存
ofstream out("save.dat");
if (out)
for (it = vT2.begin(); it != vT2.end(); ++it)
out.write((char *)it, sizeof(Team));

out.close();

}

ggg.C:5:9: #include expects "FILENAME" or
ggg.C:6:9: #include expects "FILENAME" or
ggg.C:7:9: #include expects "FILENAME" or
ggg.C: In function `int main()':
ggg.C:152: `vector' undeclared (first use this function)
ggg.C:152: (Each undeclared identifier is reported only once for each function
it appears in.)
ggg.C:152: syntax error before `;' token
ggg.C:153: variable `std::ifstream in' has initializer but incomplete type
ggg.C:163: `vTeam' undeclared (first use this function)
ggg.C:179: syntax error before `::' token
ggg.C:181: `itTemp' undeclared (first use this function)
ggg.C:201: syntax error before `(' token
ggg.C:202: `vT2' undeclared (first use this function)
ggg.C:204: syntax error before `::' token
ggg.C:204: `it' undeclared (first use this function)
ggg.C:207: variable `std::ofstream out' has initializer but incomplete type

ggggg.C:1:9: #include expects "FILENAME" or
ggggg.C:2:9: #include expects "FILENAME" or
ggggg.C:4:9: #include expects "FILENAME" or
ggggg.C:5:9: #include expects "FILENAME" or
ggggg.C:6:9: #include expects "FILENAME" or
ggggg.C:7:9: #include expects "FILENAME" or
ggggg.C:8:9: #include expects "FILENAME" or
ggggg.C: In constructor `Team::Team(char*, char*, unsigned int, unsigned int,
unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)':
ggggg.C:38: `NULL' undeclared (first use this function)
ggggg.C:38: (Each undeclared identifier is reported only once for each function
it appears in.)
ggggg.C:40: `strncpy' undeclared (first use this function)
ggggg.C: In copy constructor `Team::Team(const Team&)':
ggggg.C:57: `strcpy' undeclared (first use this function)
ggggg.C: In member function `void Team::print() const':
ggggg.C:90: `cout' undeclared (first use this function)
ggggg.C:90: `setw' undeclared (first use this function)
ggggg.C:90: syntax error before `;' token
ggggg.C: In function `bool operator==(const Team&, const Team&)':
ggggg.C:119: `strcmp' undeclared (first use this function)
ggggg.C: In member function `void Team::compute(int, int)':
ggggg.C:131: `endl' undeclared (first use this function)
ggggg.C: At global scope:
ggggg.C:149: `main' must return `int'
ggggg.C: In function `int main(...)':
ggggg.C:150: `vector' undeclared (first use this function)
ggggg.C:150: syntax error before `;' token
ggggg.C:151: `ifstream' undeclared (first use this function)
ggggg.C:153: `in' undeclared (first use this function)
ggggg.C:161: `vTeam' undeclared (first use this function)
ggggg.C:173: `cin' undeclared (first use this function)
ggggg.C:177: syntax error before `::' token
ggggg.C:179: `itTemp' undeclared (first use this function)
ggggg.C:179: `find' undeclared (first use this function)
ggggg.C:197: `sort' undeclared (first use this function)
ggggg.C:199: syntax error before `(' token
ggggg.C:200: `vT2' undeclared (first use this function)
ggggg.C:200: `reverse_copy' undeclared (first use this function)
ggggg.C:202: syntax error before `::' token
ggggg.C:202: `it' undeclared (first use this function)
ggggg.C:205: `ofstream' undeclared (first use this function)
ggggg.C:205: syntax error before `(' token
ggggg.C:206: `out' undeclared (first use this function)