关于c:CS139-C-项目

3次阅读

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

CS139 C Programming Project
November 24, 2020
The objective of this project is to practice the implementation of structures and functions in C.
The submission of the project requires the source code and a readme.pdf describing it.
Pokémon Database
Part 1 – Structure (4 points)
Implement a structure named pokemon, which contains the following elements

  1. id (int)
  2. name (char*)
  3. height (float)
  4. type (char*)
    It is suggested to use an array of the designed structure as the database, for example:
    struct pokemon pkdb[100]; or
    struct pokemon *pkdb[100];
    However, other forms such as linked list are also acceptable.
    Part 2 – Basic Functions (4 points)
    The following functions should be realized.
  5. int insert(int id, char name, float height, char type);
    Add a new pokemon into the database. Return 0 if successful. Return 1 if the max size of the
    database is exceeded.
  6. int delete_id(int id);
    Delete the pokemon of given id from the database. Return 0 if successful. Return 1 if the
    pokemon of given id doesn’t exist in the database.
    Then print a sentence to describe the situation according to the return value.
    Part 3 – Extended Functions (8 points)
    The following functions should be realized.
  7. void find_height(int param);
    param ∈ {1, 2}. Find the pokemons with maximum height (param=1) or minimum height
    1
    (param=2). Print the name of the found pokemons.
    Make sure to print all the pokemons if they have the same maximum or minimum height.
  8. void sort_id(); Sort the database with id-ascending order. Print all pokemon names in order.
  9. void sort_id_plus(int param);
    param ∈ {1, 2}. Sort the database with id-ascending order (param=1) or id-descending order
    (param=2). Print all pokemon names in order.
    Use pointer to function (such as the functions below) instead of duplicating the sort process.
    int ascending (int a, int b) {return b<a;}
    int descending (int a, int b) {return b>a;}
  10. void group(char* type);
    Print the name of all the pokemons belonging to the given type.
    Part 4 – Free Design (4 points)
    Design and implement a feature for the database. Feel free to add any element in the structure and
    add any function. Explain this feature in detail in readme file.
    For example, it could be a great idea to implement the evolutionary chains of pokemons. For pokemon
    Eevee, the function could find its evolution Vaporeon, etc.
    Input and Output
    The main function firstly reads the information of several pokemons, then reads a set of instructions
    and prints corresponding output.
    The first line of input is an integer n ≤ 50, indicating the number of pokemons in the following lines.
    For each pokemon, there are 4 lines containing its id, name, height and type respectively. Remark
    that each pokemon may have at least 1 type and at most 2 types. Types are separated with a single
    comma.
    Then there’s an integer m ≤ 20, indicating the number of instructions. Each line contains 1 instruction,
    which is consisted of a number and a parameter, separated by a blank. The number is the
    enumeration of corresponding function: 2 delete_id, 3 find_height, 4 sort_id, 5 sort_id_plus, 6 group.
    The parameter is passed into the function as argument.
    There’s a sample of input and output in the appendix. Each instruction and its output is aligned. It
    could be used to validate the functions.
    Remark that the format of the output is not important. Do not bother with order of print (except
    for sort_id or sort_id_plus), or number of blanks.
    Charmander Charizard Pikachu Nidoran Lapras
    Lapras
    Pikachu Nidoran
    Deleted successfully !
    Pokemon of id 29 doesn’t exist in the database.
    Charmander Charizard Pikachu Lapras
    Lapras Pikachu Charizard Charmander
    Charizard Charmander
    Lapras
正文完
 0