字符作为数字,比较它们

时间:2014-11-17 14:31:48

标签: c char

我最近开始编程,目前我正在开发游戏。

在这个游戏中,我需要将当前输入与数组的最后一个输入进行比较。 在十进制数字系统中,它真的很容易。

这是我编写的代码,用于从系统到十进制的数字。

#include <stdio.h>
#define Max_size 256
#define N 36

void Game(int NumberSystem,int MaxRepetitions,char *Player1,char *Player2);

int main (void)
{

    int temp,status,NumberSystem,MaxRepetitions  ;

    char Player1 [Max_size] ;
    char Player2 [Max_size] ;
    printf("Please enter the first player's name\n");
    scanf("%s",&Player1);

    printf("Please enter the second player's name\n");
    scanf("%s",&Player2);


    printf("Please enter a Number System between %d and %d\n",4,35);
    status =  scanf("%d",&NumberSystem );

    while( NumberSystem  > 35 ||  NumberSystem < 4 || status != 1  )
    {
        while((temp=getchar()) != EOF && temp != '\n');
        printf("Please enter a valid Number System \n");
        status = scanf("%d", &NumberSystem );
    }




    printf("Please enter a Number of maximum repetitions\n");
    status = scanf("%d",&MaxRepetitions);

    while(  (MaxRepetitions<= 0) ||  (status != 1)   )
    {
        while((temp=getchar()) != EOF && temp != '\n');
        printf("Please enter a valid number of maximum repetitions\n");
        status = scanf("%d",&MaxRepetitions);
    }


    Game(NumberSystem,MaxRepetitions,Player1,Player2);


}


void Game(int NumberSystem,int MaxRepetitions,char *Player1,char *Player2)
{




    int status,counter,k,i,temp;
    char fnumber,number;

    counter = k = 1;

    int *array1 = malloc(MaxRepetitions*NumberSystem*sizeof(int));
    int *array2 = malloc(NumberSystem*sizeof(int));
    char CharArray[N] = {'0','1','2','3', '4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

    for(i=0; i<NumberSystem*sizeof(int),i++;)
        array2[i] = 0;





    printf("Let's start the game! %s begins.\n",Player1);
    status = scanf("%d",&fnumber);

    while(status != 1  || fnumber <= 0 || fnumber>NumberSystem-1)
    {
        while((temp=getchar()) != EOF && temp != '\n');
        printf("First number must be a positive decimal number between %d and %d\n",1,NumberSystem-1);
        status = scanf("%d", &fnumber);
    }

    array1[0] = fnumber;
    array2[fnumber]++;


    while( array2[number] < MaxRepetitions)
    {
        status = scanf("%d",&number);

        while(status != 1  || number < 0 || number>NumberSystem-1 || ( (array1[k-1]!= NumberSystem-1) && (array1[k-1] >= number) )              )
        {
            while((temp=getchar()) != EOF && temp != '\n');
            printf("You must enter a number greater than %d and smaller or equal to %d\n",array1[k-1],NumberSystem-1);
            status = scanf("%d", &number);
        }

        array2[number]++;
        array1[k] = number;
        k++;
        counter++;

    }



    if(counter%2 == 0)
    {
        printf("%s entered %d for the %dth time and exceeded the possible limit. %s won!",Player2,array1[k-1],MaxRepetitions,Player1);
    }

    else
    {
        printf("%s entered %d for the %dth time and exceeded the possible limit. %s won!",Player1,array1[k-1],MaxRepetitions,Player2);
    }




    free (array1);
    free (array2);

}

问题是我希望可以使用更大的数字系统,例如十六进制。我坚持将当前输入的字母的值与之前输入的字母进行比较。

我正在寻求任何建议或意见。 另外,如果我在第一段代码中做了一些非常愚蠢的事情(我可能做过),我很高兴听到你们的建设性批评。

谢谢!

这是“第二”版本的代码,我尝试使用更大的数字系统。这是马车,我不知道我做错了什么。

#include <stdio.h>
#define Max_size 256
#define N 36

void Game(int NumberSystem,int MaxRepetitions,char *Player1,char *Player2);

int main (void)
{

    int temp,status,NumberSystem,MaxRepetitions  ;

    char Player1 [Max_size] ;
    char Player2 [Max_size] ;
    printf("Please enter the first player's name\n");
    scanf("%s",&Player1);

    printf("Please enter the second player's name\n");
    scanf("%s",&Player2);


    printf("Please enter a Number System between %d and %d\n",4,35);
    status =  scanf("%d",&NumberSystem );

    while( NumberSystem  > 35 ||  NumberSystem < 4 || status != 1  )
    {
        while((temp=getchar()) != EOF && temp != '\n');
        printf("Please enter a valid Number System \n");
        status = scanf("%d", &NumberSystem );
    }




    printf("Please enter a Number of maximum repetitions\n");
    status = scanf("%d",&MaxRepetitions);

    while(  (MaxRepetitions<= 0) ||  (status != 1)   )
    {
        while((temp=getchar()) != EOF && temp != '\n');
        printf("Please enter a valid number of maximum repetitions\n");
        status = scanf("%d",&MaxRepetitions);
    }


    Game(NumberSystem,MaxRepetitions,Player1,Player2);


}


void Game(int NumberSystem,int MaxRepetitions,char *Player1,char *Player2)
{

    int status,counter,k,i,temp;
    char fnumber,number;

    counter = k = 1;

    char *array1 = malloc(MaxRepetitions*NumberSystem*sizeof(char));
    int *array2 = malloc(NumberSystem*sizeof(int));
    char CharArray[N] = {'0','1','2','3', '4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

    for(i=0; i<NumberSystem*sizeof(int),i++;)
        array2[i] = 0;

    printf("Let's start the game! %s begins.\n",Player1);


    //scanf("%c",&fnumber); // This part is ignored completely i don't know why

    status = 0;


   // for(i=0; i<NumberSystem-1; i++)
    //{if(fnumber == CharArray[i]) status = 1;}

    printf("Current status : %d\n",status);

    while(status == 0)
    {
        while((temp=getchar()) != EOF && temp != '\n');
        scanf("%c", &fnumber);



        for(i=0; i<NumberSystem-1; i++)
            if(fnumber == CharArray[i]) status = 1;break;

printf("First number must be a positive decimal number between %d and %d (in %d number system)\n",1,NumberSystem-1,NumberSystem);
    }

    array1[0] = fnumber;
    array2[fnumber]++;

    printf("Przed druga petla array1[0] : %c,array2[fnumber] : %d\n",array1[0],array2[fnumber]);


    while( array2[k] < MaxRepetitions)
    {
        scanf("%c",&number);
        status = 0;

        for(i=0; i<NumberSystem-1; i++)
            if(number == CharArray[i]) status = 1;

        while(status == 0   || ( (array1[k-1] != CharArray[NumberSystem-1]) && (array1[k-1] >= number) )              )
        {
            while((temp=getchar()) != EOF && temp != '\n');

            printf("You must enter a number greater than %c and smaller or equal to %d\n",array1[k-1],NumberSystem-1);
            scanf("%c", &number);

            status = 0;
            for(i=0; i<NumberSystem-1; i++)
                if(number == CharArray[i]) status = 1;break;

        }

        array2[number]++;
        array1[k] = number;
        k++;
        counter++;

    }

    if(counter%2 == 0)
    {
        printf("%s entered %d for the %dth time and exceeded the possible limit. %s won!",Player2,array1[k-1],MaxRepetitions,Player1);
    }

    else
    {
        printf("%s entered %d for the %dth time and exceeded the possible limit. %s won!",Player1,array1[k-1],MaxRepetitions,Player2);
    }

    free (array1);
    free (array2);

}

1 个答案:

答案 0 :(得分:0)

据我了解你想做什么,你离解决方案并不远。但我建议您使用%1s代替%c来阅读您的角色。 %c接受任何字符,包括空格和换行符。相反,%1s将跳过所有最终空格和换行符。

简单地说,当%1s给出一个以空字符结尾的字符串时,您必须使用数组(char anumber[2]; ), read one character into the array (the second will be \ 0`)并将其影响到您使用的字符。例如,您只需要替换

scanf("%c", &fnumber);

scanf("%1s", anumber);
fnumber = anumber[0];