关于后端:CS-215-链表

38次阅读

共计 9747 个字符,预计需要花费 25 分钟才能阅读完成。

CS 215 – Winter 2019
Project 3/4
Learning Objectives:

  • Implementation of a program using Object Oriented Design.
  • Implementation of lists using Dynamically-Allocated Linked Lists.
    General Description:
    You are to write a program that implements score keeping for a league of sports teams. The league
    consists of a list of teams that are members of the league, and a list of games/matches.
    Team data and initial game data are read from text files. Using menu options, the user may view various
    parts of the data, and may add new games to the data. The game data is then written to a text file on
    exit from the program.
    The program presents the user with the menu as
    shown here, starting with a logo that contains the
    program name and the name of the programmer.
    The menu option entered by the user is validated
    until a valid menu option is entered. Only the first
    character of the user’s input is recognized, and that
    character is treated as not case sensitive. After
    performance of a valid option, the menu repeats
    until the exit option is selected.
    List all teams:
    A list of all teams read from the input file is printed
    using the format shown in the example. The header
    line includes the total number of teams in
    parenthesis. Field widths include:
  • Team Id 4
  • Team Name 25
  • Coach 20
    with two spaces between columns. No system pause
    is needed after the list is printed.
    List all games:
    A list of all games read from the input file and entered by the
    user is printed using the format shown in the example. The
    header line includes the total number of teams in
    parenthesis. The field widths include:
  • Date 10 (left justified)
  • Team Ids 4 (left justified)
  • Scores 3 (right justified)
    with two spaces between columns. Note that the winning
    team is listed first on each line. After the last game is printed,
    a blank line is followed by a system pause.
    2
    Query Team:
    First, the list of teams is printed, exactly as in print all teams above.
    Next, the user is asked to enter the Team Id of the selected team. The user’s input is validated to be a
    valid Team Id, repeating until a valid
    value is entered.
    A report of the selected team is printed
    as shown in the example. It includes
    the number of games won and lost by
    the team, based on counts taken from
    the game data.
    Finally, the game list is printed,
    formatted exactly as described above
    for print all teams. Note that both
    games lost (team data in the right
    columns) and games won (team data in
    the center columns) are printed.
    Add Game:
    First, the list of all teams is printed exactly as described above for print all teams.
    The user is then asked to enter the Team Id for the first team. This is validated/repeated exactly as
    described for Query Team. The Team Id
    for the second team is entered/validated.
    Next, the points for the two teams and
    the date of the game are entered. There is
    no validation for these three values, and it
    is assumed the user will enter integers for
    scores, and a string with no spaces in the
    correct date format for the date.
    Once all data is entered, and the game data added to the internal list, the program prints“Game
    added”. Note the program may have to“swap”team 1 with team 2 when adding the game, depending
    on the higher score (since the winning team should be stored as team 1).
    Exit:
    The program should write the current game data to an
    output file called“games2.txt”, then do a system
    pause before ending the program.
    3
    Detailed Design:
    The program should be designed and implemented using Object Oriented Programming. All lists should
    be implemented as Dynamically-Allocated Linked Lists.
    main program:
    Write a free function called doAbort() that is given an error message (string). It prints the error message
    on the screen, followed by a newline; does a system pause; and ends the program with exit(1); For any
    .cpp in the project that needs to invoke this free function, place its prototype at the top of the .cpp file.
    The main program should declare a league class and invoke the go() method. This should be followed by
    the standard system pause and return 0;
    The classes consist of the following:
    team
    A team consists of the following data:
  • Team Id (assumed to be length 4 or less with no spaces)
  • Name (assumed to be length 25 or less and may have spaces)
  • Coach (assumed to be length 20 or less and may have spaces)
  • next (pointer to the next team object in a linked list of teams)
    Standard set and get methods should be coded for the Team Id, Name and Coach (probably not needed
    for the next pointer, but those may be added if needed).
    Write a standard constructor (pointers are usually set to NULL in constructors).
    You may declare the team list class to be a friend, but no other classes or free functions should be
    declared as friends.
    team list
    The team list is a dynamic linked list of team objects. The only data needed is a head pointer for the
    linked list (optional: a tail pointer). The constructor should set the head (and tail) to NULL.
    The methods that should be implemented include:
    addTeam: given a new team object
    Dynamically allocates a new team object and copies the data from the given object into the
    dynamic object. It should then add the new object to the front (head) of the linked list.
    4
    readData:
    Reads data from a file called“teams.txt”and adds them to the linked list.
    The format of the input file is, for each team:
  • first line: the Team Id, a string with no spaces
  • second line: the Team Name, a string with spaces
  • third line: the Coach, a string with spaces

    There is no“number of teams”nor a Sentinel value at the end of the data. Instead use the .eof()
    method of an ifstream to detect end of file. Ex: while(!f.eof()) {…}
    A sample input file is provided on the course website. Here is one with two teams:
    UK
    Kentucky Wildcats
    Calipari
    LOU
    Louisville Cardinals
    Job Open
    printTeams:
    Prints the team list report as described above, based on data currently in the linked list of
    teams.
    getTeamRef: given: a Team Id
    returns: a pointer to a team, or NULL
    Searches the current linked list for a team object that has a matching Team Id member. When
    found, return the pointer to the team object found; otherwise, return NULL for“not found”.
    getNumTeams: returns: the number of teams in the list.
    Count and return the number of nodes in the list, which may be 0.
    game: game objects will be part of a linked list (gameList) and will also point to objects in the Team List
    A game consists of the following data for a game played between two teams:
  • date a string assumed to be in YYYY/MM/DD format (length 10)
  • points scored(2) points scored by team 1 and team 2
  • team pointer (2) a pointer to a team in the teamList list for team 1 and team 2
  • next a pointer to a game, the next game in the list or NULL
    The class may make class gameList a friend, but no other classes or functions.
    Constructor: standard constructor initializing all members of the object.
    set: one set method given a date, two team pointers, and two scores.
    alternative: one set method for each member (except next)
    gets: one get for each member, except next.
    5
    gameList: this is a Linked List of game objects, including a head and tail pointer.
    data and constructor
    head – a pointer to a game object, the first in the list
    tail – a pointer to a game object, the last in the list
    constructor – simply sets the two pointers to NULL.
    addGame – given a game object.
  • dynamically allocates a new game object
  • copies the data members from the given object into the newly allocated object
  • inserts the newly allocated object into the game list at the tail of the list.
    readGames: – given a Team List Object.
    This method reads game data from a file called“games.txt”(validate the file opens, and
    close at the end) and creates the initial game list.
    The format of the game input file is: each line contains data on one game in the following order:
    DATE TEAM ID 1 POINTS FOR TEAM 1 TEAM ID2 POINTS FOR TEAM 2
    There is no“number of games”at the top of file, and there is no sentinel value/record at the
    end of the file. Use eof() to detect the end of file. A sample data file is provided on the course
    website.
    The method should read data into a local game object. It will have to“look up”a pointer to each
    team using the Team List Object passed in. It should invoke addGame on the populated game
    object to add a new game to the list.
    This function invokes the global free function (declared in main() and prototyped in this .cpp file)
    abort() when:
  • the file fails to open
    “gameList::readGames: unable to open games.txt”
  • the Team Id read is not found in the given Team List for either team:
    “gameList::read: invalid team id 1/2 = ” + teamIdReadFromFile
    getNumGames – counts and returns the number of games in the entire list.
    getNumWins – given a pointer to a team object, searches the list and counts/returns the number of
    games won by the team pointed to by the given pointer.
    getNumLosses – given a pointer to a team object, searches the list and counts/returns the number of
    games lost by the team pointed to by the given pointer.
    6
    printGames – given a pointer to a team object
  • when the given pointer is NULL, prints all games in the list.
    (See List all games example above).
  • when the given pointer is NOT NULL, prints all games in the list where the given pointer points
    to the same team object as“team pointer 1”or“team pointer 2”in the list.
    (See Query Team example above).
  • Print a“header”as shown in the examples above, including the number of teams in a dashed
    line for the first line (invoke getNumGames()). Put two blanks between each column. Assume
    the max lengths on the data values are:
    o date: 10
    o points: 3
    o team Id 4
    writeGames –
    Writes the current game data to a text file called“games2.txt”, in the same format as the input
    file described in readGames above. The method should use the two team pointers for each
    game to retrieve the Team Id for each.
    league: this represents our whole application. Main simply declares a league object and invokes its go().
    Most of the methods have been written for you (posted on the course website). Similar to your lab 8,
    complete the methods where there are // TO DO: comment tags in the code.
    All methods are private except go()!
    Data and constructors
  • a team list object
  • a game list object
  • constructor: since both members have their own constructors, a constructor is not needed.
    askTeamId – given: a (string) prompt that is assumed to be like“Enter team id …”
    returns: a pointer to the team found.
    This method prints the prompt and allows the user to enter a Team Id (assume no spaces). It
    searches the Team List for the team Id entered. When found, it returns the team Id pointer. When
    not found, it repeats the question (prompt) until the user enters a valid team Id. See Query Team in
    the specifications above.
    addGame –
    This method prints the team list, invokes askTeamId() twice to get the team pointers for two teams,
    asks for the points for the two teams and the date of the game. It populates a local game object
    then invokes addGame in the game list to add a new game to the list.
    7
    queryTeam –
    This method first prints the team list. It invokes askTeamId to get a pointer to the team to look up. It
    uses the get() methods from the team class to print the data from the found team object. Finally, it
    prints all games for the team by invoking printGames() giving it the found team pointer. See Query
    Team above for the format of the output.
    getMenuOption – returns the user’s selected menu option. This is written for you.
    go()
    This is the“main”of the application. It starts by invoking the reads in both the team list and game
    list objects. This is followed by the main menu control loop for the application.
    Submission:
    zip the following files into a .zip file, and submit the .zip in Canvas:
  • game.h, game.cpp
  • gameList.h, gameList.cpp
  • team.h, team.cpp
  • teamList.h, teamList.cpp
  • league.h, league.cpp
  • proj3/main.cpp
    There is no need to include data files (.txt)
正文完
 0