记录球队成员的数据 --- Cprime plus 14章第6题

结构的成员如下: 编号,姓名,上场数,击中数,走垒数,打点数,安打率。

假设,每个球员的每条数据都是由上述数据构成,但是可能存在多条数据,因此要考虑到球员信息需要更新。

利用球员编号作为数组的索引,对数据进行更新。

还请各位指正

//第6题
# include <stdio.h>
# include <string.h>
# include <stdlib.h>

# define LEN 15
# define MAX 19

struct player {
    int no;
    char fname[LEN];
    char lname[LEN];
    int apps;        //出场次数
    int hit;         //击中
    int walk;        //走垒
    int RBI;         //打点
    float s_hit;    //安打率:累计击中数除以上场累计次数
};
char* s_get(char*, int);
int exist(struct player data[],int, int);

int main()
{
    struct player data[MAX];
    FILE* fp;
    int count = 0;
    int filect = 0;
    int size = sizeof(struct player);
    
    if ((fp = fopen("playerinfo.dat", "a+b")) == NULL)
    {
        fprintf(stderr, "Can not open : player_info.dat\n");
        exit(1);       //异常退出
    }

    rewind(fp);
    while (count < MAX && fread(&data[count], size, 1, fp) == 1)
    {
        if (count == 0)
            puts("Current data of the player:");
        printf("Player_No:%d, name:%s %s ,apps:%d, hit:%d,walk: %d, RBI:%d, Safe hit:%.4f\n", data[count].no, data[count].fname, data[count].lname, 
            data[count].apps, data[count].hit,data[count].walk, data[count].RBI,data[count].s_hit);
        count++;
    }
    filect = count;

    if (count == MAX)
    {
        fprintf(stderr, "The file is full.\n");
        exit(2);    //异常退出    
    }

    struct player temp;
    puts("Please add player_No to the file:");
    puts("Press [enter] at the start of a line to quit.");
    while (count < MAX && scanf("%d", &temp.no) == 1)
    {
        while (getchar() != '\n')
            continue;

        int j = exist(data,temp.no, count); //判断,是否已存有该编号球员的数据
        if (j)                              //如果已输入过该球员的数据,则对其数据进行更新
        {
                printf("Player :%s %s\n", data[temp.no].fname, data[temp.no].lname);  //利用球员编号作为索引
                puts("Enter the apps:");
                scanf("%d", &temp.apps);
                data[temp.no].apps = data[temp.no].apps + temp.apps;
                puts("Enter the hit:");
                scanf("%d", &temp.hit);
                data[temp.no].hit = data[temp.no].hit + temp.hit;
                puts("Enter the walk:");
                scanf("%d", &temp.walk);
                data[temp.no].walk = data[temp.no].walk + temp.walk;
                puts("Enter the RBI:");
                scanf("%d", &temp.RBI);
                data[temp.no].RBI = data[temp.no].RBI + temp.walk;
                data[temp.no].s_hit = (float)data[temp.no].hit / data[temp.no].apps;
                puts("Enter the player_no:");
                continue;
        }
        data[count].no = temp.no;             //如果没有该球员数据,则对这一结构数组中的成员进行赋值操作
        puts("Enter the first name:");
        s_get(data[count].fname, LEN);
        puts("Enter the last name:");
        s_get(data[count].lname, LEN);
        puts("Enter the apps:");
        scanf("%d", &data[count].apps);
        puts("Enter the hit:");
        scanf("%d", &data[count].hit);
        puts("Enter the walk:");
        scanf("%d", &data[count].walk);
        puts("Enter the RBI:");
        scanf("%d", &data[count].RBI);
        data[count].s_hit =(float) data[count].hit / data[count].apps;
        count++;                           

        while (getchar() != '\n')
            continue;

        puts("Enter the player_no:");
        
    }

    if (count > 0)
    {
        puts("Here is the data:");
        for (int i = 0; i < count; i++)
            printf("Player_No:%d, name:%s %s ,apps:%d, hit:%d, walk: %d, RBI:%d , Safe_hit %.4f \n", data[i].no, data[i].fname,
                data[i].lname, data[i].apps, data[i].hit,data[i].walk, data[i].RBI, data[i].s_hit);
        fwrite(&data[filect], size, count - filect, fp);
    }
    else
        puts("No data.");

    puts("Bye.");
    fclose(fp);

    return 0;
}

int exist(struct player data[],int n , int m)
{
    for (int i = 0; i < m; i++)
    {
        if (n == data[i].no)
            return 1;
    }
    return 0;
}

char* s_get(char* st, int n)
{
    char* ret_val;
    char* find;

    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        find = strchr(st, '\n');
        if (find)
            *find = '\0';
        else
            while (getchar() != '\n')
                continue;
    }
    return ret_val;
}

 

上一篇:关于 koa2 使用 koa-jwt 出现的问题


下一篇:使用viper管理配置文件 并 实现使用环境变量覆盖配置文件