⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addplayer.c

📁 C语言
💻 C
字号:
#include "myfun.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
#include <stdlib.h>

#define W 11         /*号码的最大长度*/
#define K 15         /*名字的最大长度*/
#define R 10         /*国籍的最大长度*/

/*函数功能:添加选手
  函数参数:struct equipage型指针变量p,表示结构体数组地址
            整型变量n,表示选手人数
  函数返回值:无
*/
void AddPlayer(struct equipage *p,int n)
{
    int i = 1,j = 0;
    int flag ;

    while(i <= n)
    {
        system("cls");
        printf("!!! Add No.%d's information: \n\n",i );

        /*控制输入长度不大于W的号码,并且如果输入的号码已经存在则让其重新输入*/
        printf("Enter the rider's license number [ 1--%d digits ] :\n",W);
        do
        {
            int judge = 0;
            flag = 1;
            judge = GetNum(p->license);
            while ( judge > W )
            {
                printf("\nThe length is more than %d !\nInput again:\n",W);
                judge = GetNum(p->license);
            }
            if (i > 1)
            {
                for (j = 1;j < i && flag == 1;j++)
                {
                    if( strcmp(p->license,(p-j)->license) == 0 )
                    {
                        printf("\nThis number has already existed !");
                        printf("\n\aPlease input again :\n");
                        flag = 0;
                    }
                }
            }
        } while (flag != 1);

        /*控制输入长度不大于K的名字*/
        printf("\nEnter the rider's name [ 1--%d letters] :\n",K);
        while ( GetName(p->name_r) > K )
            printf("\nThe number is more than %d !\nInput again:\n",K);

        /*控制输入长度不大于R的国籍*/
        printf("\nEnter the rider's club or country [ 1--%d letters] :\n",R);
        while ( GetName(p->nation) > R )
            printf("\nThe number is more than %d !\nInput again:\n",R);

        /*控制输入长度不大于W的号码,并且如果输入的号码已经存在则让其重新输入*/
        printf("\nEnter the horse's identification number [ 1--%d digits ] :\n",W);
        do
        {
            int judge = 0;
            flag = 1;
            judge = GetNum(p->ident);
            while ( judge > W )
            {
                printf("\nThe length is more than %d !\nInput again:\n",W);
                judge = GetNum(p->ident);
            }
            if (i > 1)
            {
                for (j = 1;j < i && flag == 1;j++)
                {
                    if( strcmp(p->ident,(p-j)->ident) == 0 )
                    {
                        printf("\nThis number has already existed !");
                        printf("\n\aPlease input again :\n");
                        flag = 0;
                    }
                }
            }
        } while (flag != 1);

        /*控制输入长度不大于K的名字*/
        printf("\nEnter the horse's name [ 1--%d letters] :\n",K);
        while ( GetName(p->name_h) > K )
            printf("\nThe number is more than %d !\nInput again:\n",K);

        /*控制输入长度不大于3的年龄*/
        printf("\nEnter the horse's age [ 1--2 digits ] :\n");
        while ( (p->age = GetInt()) > 99 )
            printf("\nThe number is more than 99 !\nInput again:\n");

        p++;
        i++;
    }

}

/*函数功能:控制输入正确的名字
  函数参数:字符型指针变量a,表示结构体中数组元素首地址
  函数返回值:字符串长度
*/
int GetName(char *a)
{
    int i = 0;
    int flag = 1;
    char t = '\0';

    do
    {
        t = getche();
        if (i == 0 && t == '\r')       /*用户什么都没有输入*/
        {
            flag = 0;
            Alarm();
            continue;
        }
        if (!isalpha(t) && !isspace(t))
        {
            i = 0;
            Alarm();
            continue;
        }
        if (isalpha(t))
        {
            *(a + i) = t;
            i++;
            flag = 1;
        }
    } while (t != '\r' || flag == 0);
    *(a + i) = '\0';
    return i;
}

/*函数功能:控制输入正确的号码
  函数参数:字符型指针变量a,表示结构体中数组元素首地址
  函数返回值:字符串长度
*/
int GetNum(char *a)
{
    int i = 0;
    int flag = 1;
    char t = '\0';

    do
    {
        t = getche();
        if (i == 0 && t == '\r')
        {
            flag = 0;
            Alarm();
            continue;
        }
        if (!isdigit(t) && !isspace(t))
        {
            i = 0;
            Alarm();
            continue;
        }
        if (isdigit(t))
        {
            *(a + i) = t;
            i++;
            flag = 1;
        }
    } while (t != '\r' || flag == 0);
    *(a + i) = '\0';                        /*'\0'至关重要!!*/
    return i;
}

/*函数功能:控制输入正确的int类型数据
  函数参数:无
  函数返回值:字符串代表的整型值
*/
int GetInt(void)
{
    char a[MAX_LEN] = {'\0'};
    char t = '\0';
    int i = 0,j;
    int flag = 1;

    do
    {
        t = getche();
        if (i == 0 && t == '\r')
        {
            flag = 0;
            Alarm();
            continue;
        }
        if (!isdigit(t) && !isspace(t))
        {
            for (j = 0;j < MAX_LEN;j++)  a[j] = '\0';
            i = 0;
            Alarm();
            continue;
        }
        if (isdigit(t))
        {
            a[i] = t;
            i++;
            flag = 1;
        }
    } while (t != '\r' || flag == 0);

    return atoi(a);                  /*把字符串转化为数值类型*/
}

/*函数功能:用户输入错误时发出警报
  函数参数:无
  函数返回值:无
*/
void Alarm(void)
{
    printf("\n\n\aError !\n");
    printf("Please input again : \n");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -