一个篮球球队比赛小程序的问题。
//下面是我的c++作业。大致是这样的://有两个文件 Team_and_Player.txt 和 Game.txt 分别储存了 球队和球员的记录以及赛程记录。
//接下来是我编的程序:但是跑出来的时候一直说 "access violation"什么的。要崩溃了。
Game.txt的格式是:
0 1
1 2
0 2
0 1
Team_and_Player.txt是
3
0 Boston_Celtics
1 New_Jersey_Nets
2 Chicago_Bulls
15
0 20 Ray_Allen PG 13
2 3 Omer_Asik PF 18
1 26 Stephen_Graham SF 20
0 11 Glen_Davis SF 10
2 6 Keith_Bogans SG 15
0 5 Kevin_Garnett PF 13
0 4 Nenad_Krstic C 5
2 21 Jimmy_Butler SF 6
0 45 Carlos_Arroyo SG 10
1 11 Brook_Lopez C 10
1 2 Jordan_Farmar SG 5
1 0 Sundiata_Gaines PG 4
2 13 Joakim_Noah C 8
1 10 Damion_James PF 10
2 12 Jannero_Pargo PG 16
//之前的player.h 和team.h分别是:
#ifndef player_h
#define player_h
#include <string>
using namespace std;
class Player{
public:
Player();
void showInfo();
void setName(string n);
string getName();
void setNumber(int n);
int getNumber();
void setSkillPoint(double sp);
double getSkillPoint();
void increaseSkillPoint(double sp);
private:
string name;
int number;
double skillPoint;
};
#endif
//】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】
#ifndef team_h
#define team_h
#include <string>
#include "Player.h"
using namespace std;
class Team{
public:
Team();
double getTotalSkillPoint();
void win();
void lose();
void draw();
void showInfo();
void setName(string n);
string getName();
Player Center;
Player SmallForward;
Player PowerForward;
Player PointGuard;
Player ShootingGuard;
private:
string name;
int winCount, loseCount, drawCount;
};
#endif
//】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】】
//主程序:
#include <iostream>
#include <fstream>
#include <string>
#include "Player.h"
#include "Team.h"
using namespace std;
int l;
Player :: Player(){
}
void Player :: showInfo(){
cout << number << " " << name << " " << skillPoint <<endl;
}
void Player :: setName(string n){
name = n;
}
string Player :: getName(){
return name;
}
void Player :: setNumber(int n){
number = n;
}
int Player :: getNumber(){
return number;
}
void Player :: setSkillPoint(double sp){
skillPoint = sp;
}
double Player :: getSkillPoint(){
return skillPoint;
}
void Player :: increaseSkillPoint(double sp){
skillPoint = skillPoint + sp;
}
Team :: Team(){
/***********************/
/* Missing Codes here */
/***********************/
/* Hints: initialize winCount, loseCount, drawCount */
}
// Call this member function if the team wins a game
void Team :: win(){
winCount++;
Center.increaseSkillPoint(0.5);
SmallForward.increaseSkillPoint(0.5);
PowerForward.increaseSkillPoint(0.5);
ShootingGuard.increaseSkillPoint(0.5);
PointGuard.increaseSkillPoint(0.5);
/***********************/
/* Missing Codes here */
/***********************/
/* Hints: Think about what we will do for a winning team in terms of skillPoints of the 5 team players */
}
// Call this member function if the team loses a game
void Team :: lose(){
loseCount++;
}
// Call this member function if it is a draw game for a game that this team plays
void Team :: draw(){
drawCount++;
}
// Call this member function to return the total skllPoints of all 5 players
double Team :: getTotalSkillPoint(){
double total = 0;
double c,pf,pg,sf,sg;
c=Center.getSkillPoint();
pf=PowerForward.getSkillPoint();
sf=SmallForward.getSkillPoint();
pg=PointGuard.getSkillPoint();
sg=ShootingGuard.getSkillPoint();
total=c+pf+sf+pg+sg;
/***********************/
/* Missing Codes here */
/***********************/
/* Hints: Think of what member function we should call for each team player to get individual skillPoint */
return total;
}
void Team :: showInfo(){
string T_name=getName();
double totalskillpoint;
totalskillpoint=getTotalSkillPoint();
cout<<"Team"<<" "<<l<<" : "<<T_name<<endl;//l
cout<<"Win : "<<winCount<<endl;
cout<<"Lose : "<<loseCount<<endl;
cout<<"Draw : "<<drawCount<<endl;
cout<<"Total Games : "<<winCount+loseCount+drawCount<<endl;
cout<<"Total Skill Points : "<< totalskillpoint<<endl;
Center.showInfo();
PowerForward.showInfo();
SmallForward.showInfo();
ShootingGuard.showInfo();
PointGuard.showInfo();
/***********************/
/* Missing Codes here */
/***********************/
/* Hints: Pay attention to the formatting, please refer to section 3 of the question paper */
/* Hints: To display the information about the players, think about the member functions of the Player class */
}
void Team :: setName(string n){
name = n;
}
string Team :: getName(){
return name;
}
void play(Team &A , Team &B);
int main()
{
cout << "Tian Yu Nong 2011955030\n";
int numOfTeams;
string numOfTeams_string;
Team *teams;
ifstream Teamnumber;
Teamnumber.open("Team_and_Player.txt",ios::in);//fail situation
Teamnumber>>numOfTeams_string;
numOfTeams=atoi(numOfTeams_string.c_str());
teams=new Team[numOfTeams];
for(int k=0;k<numOfTeams;k++)
{
string temp,crap;//d
Teamnumber>>crap;
Teamnumber>>temp;
teams[k].setName(temp);
}
int numOfPlayers;//d
string numOfPlayers_string;//d
Teamnumber>>numOfPlayers_string;//伪代码,读取人数
numOfPlayers=atoi(numOfPlayers_string.c_str());
for(int j=0;j<numOfPlayers;j++)
{
string tteam;//d
string tnumber;//d
string tname;//d
string tposition;//d
string tpoint;//d
Teamnumber>>tteam>>tnumber>>tname>>tposition>>tpoint;
int tnumber_converted=atoi(tnumber.c_str());
double tpoint_converted=atoi(tpoint.c_str());
int tteam_converted=atoi(tteam.c_str());
if(tposition=="PG"){ teams[tnumber_converted].PointGuard.setName(tname);
teams[tnumber_converted].PointGuard.setNumber(tnumber_converted);
teams[tnumber_converted].PointGuard.setSkillPoint(tpoint_converted);}
if(tposition=="SG"){ teams[tnumber_converted].ShootingGuard.setName(tname);
teams[tnumber_converted].ShootingGuard.setNumber(tnumber_converted);
teams[tnumber_converted].ShootingGuard.setSkillPoint(tpoint_converted);}
if(tposition=="SF"){ teams[tnumber_converted].SmallForward.setName(tname);
teams[tnumber_converted].SmallForward.setNumber(tnumber_converted);
teams[tnumber_converted].SmallForward.setSkillPoint(tpoint_converted);}
if(tposition=="PF"){ teams[tnumber_converted].PowerForward.setName(tname);
teams[tnumber_converted].PowerForward.setNumber(tnumber_converted);
teams[tnumber_converted].PowerForward.setSkillPoint(tpoint_converted);}
if(tposition=="C"){ teams[tnumber_converted].Center.setName(tname);
teams[tnumber_converted].Center.setNumber(tnumber_converted);
teams[tnumber_converted].Center.setSkillPoint(tpoint_converted);}
}
Teamnumber.close();
ifstream fgame;
fgame.open("game.txt",ios::in);
while(!fgame.eof())
{
string steam1;
string steam2;
int team1;
int team2;
fgame>>steam1>>steam2;
team1=atoi(steam1.c_str());
team2=atoi(steam2.c_str());
play(teams[team1],teams[team2]);
}
fgame.close();
for(l=0;l<numOfTeams;l++)
{
teams[l].showInfo();
}
system("pause");
return 0;
}
//--------------------------------------------------------------------------------------///
/*******************************************************************************/
/* Sub-task 3. Read the Team and Player information from Team_and_Player.txt. */
/*******************************************************************************/
/* Hints 1: Initialize the dynamic array of Team (see the Guideline Sub-task 3 for syntax */
/* Hints 2: Open the Team_and_Player.txt file, where you can know the size of the "teams" array
and read the Team information into the array teams */
/* Hints 3: Read the list of Players, for each player information, store their information in the corresponding team
e.g., if a record is "0 20 Ray_Allen PG 13", then team[0].PointGuard.setName("Ray_Allen"); ... */
/*************************************************************/
/* Subtask 5. Open the Game.txt and simulate the game season.*/
/**************************************************************/
/*********************************************************************************************/
/* Final task. Display information for each team, in ascending order of teamID.*/
/**********************************************************************************************/
//system("PAUSE");
void play(Team &A , Team &B){
if(A.Center.getSkillPoint()<B.Center.getSkillPoint()) {A.Center.increaseSkillPoint(1);}
else if(A.Center.getSkillPoint()==B.Center.getSkillPoint()) {A.Center.increaseSkillPoint(0.5);B.Center.increaseSkillPoint(0.5);}
else {B.Center.increaseSkillPoint(1);}
if(A.PowerForward.getSkillPoint()<B.PowerForward.getSkillPoint()) {A.PowerForward.increaseSkillPoint(1);}
else if(A.PowerForward.getSkillPoint()==B.PowerForward.getSkillPoint()) {A.PowerForward.increaseSkillPoint(0.5);B.PowerForward.increaseSkillPoint(0.5);}
else {B.PowerForward.increaseSkillPoint(1);}
if(A.SmallForward.getSkillPoint()<B.SmallForward.getSkillPoint()) {A.SmallForward.increaseSkillPoint(1);}
else if(A.SmallForward.getSkillPoint()==B.SmallForward.getSkillPoint()) {A.SmallForward.increaseSkillPoint(0.5);B.SmallForward.increaseSkillPoint(0.5);}
else {B.SmallForward.increaseSkillPoint(1);}
if(A.ShootingGuard.getSkillPoint()<B.ShootingGuard.getSkillPoint()) {A.ShootingGuard.increaseSkillPoint(1);}
else if(A.ShootingGuard.getSkillPoint()==B.ShootingGuard.getSkillPoint()) {A.ShootingGuard.increaseSkillPoint(0.5);B.ShootingGuard.increaseSkillPoint(0.5);}
else {B.ShootingGuard.increaseSkillPoint(1);}
if(A.PointGuard.getSkillPoint()<B.PointGuard.getSkillPoint()) {A.PointGuard.increaseSkillPoint(1);}
else if(A.PointGuard.getSkillPoint()==B.PointGuard.getSkillPoint()) {A.PointGuard.increaseSkillPoint(0.5);B.PointGuard.increaseSkillPoint(0.5);}
else {B.PointGuard.increaseSkillPoint(1);}
if(A.getTotalSkillPoint()>B.getTotalSkillPoint()) {A.win();B.lose();}
if(A.getTotalSkillPoint()<B.getTotalSkillPoint()) {B.win();A.lose();}
if(A.getTotalSkillPoint()==B.getTotalSkillPoint()) {A.draw();B.draw();}
/*******************************************************************************************/
/* Subtask 4. Build the function void play (Team &A , Team &B) to simulate one game event.*/
/********************************************************************************************/
// RULE 1: Handle the battle between SmallForward, PowerForward,PointGuard,ShootingGuard and Center of two teams.
// Hints: To get the skillPoint of the SmallForward of team A, we can use A.SmallForward.getSkillPoint()
// RULE 2: The team with the higher total skillPoint among their players
// (players in the five positions: C, PF, SF, SG, PG) wins the game.
// Hints : Think about the member function getTotalSkillPoint() of the Team class
// RULE 3: Each player in the winning team will gain 0.5 skillPoints.
// Hints: Think about the member functions win(), lose(), draw() of the Team class
}