我如何比较两支球队的胜率?在 C

How can i compare 2 teams with their winning percentage? in C

我在这段代码中遇到了这些错误:in void teamEnter(){

[错误] 字符串常量前需要标识符或“(”

[错误]'ans1'未声明(首次在此函数中使用)

[错误]'ans2'未声明(首次在此函数中使用)

其实我想减去2支球队的胜率。我尝试了很多方法,但我找不到如何将团队名称与 teamWinPercentage 相等,然后将第一支球队减去第二支球队。我现在尝试过一些不同的方法吗?我在这段代码中遇到错误,我认为这段代码不会达到我的主要目标。谢谢..


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int mainMenuChoose, leagueMenuChoose, leagueSubtractMenuChoose, returnMainMenu;

void mainMenu(){
printf("\n\tChoose");
printf("\n_____________________________________");
printf("\n1- see league");
printf("\n2- Compare Teams");

printf("\n\nChoose what you want : ");
scanf("%d",&mainMenuChoose);

switch(mainMenuChoose){
    case 1:
        leagueMenu();
        break;
    case 2:
        leagueSubtractMenu();
        break;
    default:
        printf("\nPlease enter correctly!");
 }
}

void leagueMenu(){
printf("\n\n\tLIG Menu");
printf("\n_____________________________________");
printf("\n1- See Team");
printf("\n2- Back to Main Menu");
printf("\n\n Choose: ");
scanf("%d",&leagueMenuChoose);

switch(leagueMenuChoose){
    case 1:
        seeTeams();
        break;
   case 2:
        mainMenu();
        break;
    default:
        printf("\nPlease enter correctly!!");
  }
 }

void leagueSubtractMenu(){
printf("\n\n   League subtract menu");
printf("\n_____________________________________");
printf("\n1- Enter Teams");
printf("\n2- Genel Menuye Donme Islemi");
printf("\n\nChoose: ");
scanf("%d",&leagueSubtractMenuChoose);

switch(leagueSubtractMenuChoose){
    case 1:
        enterTeam();
        break;
    
    case 2:
        mainMenu();
        break;
    default:
        printf("\nPlease enter correctly!");
  }
 }
 void enterTeam(){
    /*
printf("\n 1 - Arsenal");
printf("\n 2 - Aston Villa");
printf("\n 3 - Brentford");
printf("\n 4 - Brighton & Hove Albion");
printf("\n 5 - Burnley");
printf("\n 6 - Chelsea");
printf("\n 7 - Crystal Palace");
printf("\n 8 - Everton");
printf("\n 9 - Leeds United");
printf("\n 10 - Leicester City");
printf("\n 11 - Liverpool");
printf("\n 12 - Manchester City");
printf("\n 13 - Manchester United");
printf("\n 14 - New Castle United");
printf("\n 15 - Norwich City");
printf("\n 16 - Southampton");
printf("\n 17 - Tottenham");
printf("\n 18 - Watford");
printf("\n 19 - West Ham United");
printf("\n 20 - Wolverhampton\n\n");
*/

FILE *pFile;
if (pFile !=NULL){

char *teams[]={"Arsenal","Aston Villa","Brentford","Brighton & Hove Albion","Burnley","Chealsea","Crystal Palace",
    "Everton","Leeds United","Leicester City","Liverpool","Manchester City","Manchester United",
    "New Castle United","Norwich City","Southampton","Tottenham","Watford","West Ham United","Wolverhampton"};
    char ans1[25];char ans2[25];


float teamWinPerc[20]={0.642,0.441,0.430,0.500,0.385,0.685,0.485,0.367,0.371,0.455,0.828,0.842,
    0.567,0.444,0.228,0.430,0.614,0.228,0.541,0.500};

printf("\nCarefull for case type!\n");  
printf("Enter first team: ");
scanf("%s",&ans1);
    if(strcmp(teams,ans1)==0){
        printf("Enter second team: ");
        scanf("%s",&ans2);
        if(strcmp(teams,ans2)==0){
            printf("%s - %s",ans1,ans2);
        }
        else{
        printf("Wrong data!");
            
        printf("\n Back to main menu with 0 or exit with different value: ");
            scanf("%d",&returnMainMenu);

            while(returnMainMenu==0){
            mainMenu();
            }
        }
    }
    else{
        printf("Wrong Data!");
        
        printf("\n Back to main menu with 0 or exit with different value: ");
            scanf("%d",&returnMainMenu);

            while(returnMainMenu==0){
            mainMenu();
            }
    }
    /*
    int i;
    for(i=1;i<21;i++){
        printf("[%d] = %s\n",i,takimAd[i]);
        printf("%d. Takimi giriniz:",i+1);
        
    }*/
        //scanf("%s",takimAd[20]);

    //fscanf(pFile, "%d %s %3.f",&sira,&name,&ky);
    

    //for(sira=1;sira<21;sira++){}
    
    /*while(fgets(name, sizeof(name),pFile)){
        fscanf(pFile, "%d %s %3.f",&sira,&name,&ky);
        printf("1.Takimi Girin: ");
        scanf("%s",&takim1);
            if(strcmp(name,takim1)==0){
            printf("%s",takim1); 
            fscanf(pFile, "%d %s %3.f",&sira,&name,&ky); 
            }
            
        printf("2.Takimi Girin: ");
        scanf("%s",&takim2);
            if(strcmp(name,takim2)==0){
            printf("%s",takim2);
            fscanf(pFile, "%d %s %3.f",&sira,&name,&ky); 
            }
        printf("%3.f - %3.f = %3.f",ky-ky);
    }*/
    
}

else{
    printf("File cant open.\n");
}

//char Arsenal=(float) 0.642;
//char Astonvilla=(float)0.441;
/*chelsea,manchesteru,tottenham,
manchesterc,westham,wolverhampton,brighton,
crystalpalace;*/

/*printf("Karsilastirmak istediginiz takimlari secin:\n");
printf("1. Takimi secin:\n ");
scanf("%c",&Arsenal);
printf("2. Takimi secin:\n ");
scanf("%c",&Astonvilla);
printf("%.3f - %.3f",Arsenal,Astonvilla);
*/
return 0;
}

void seeTeams(){
FILE * fPointer;
fPointer =fopen("takimlar.txt","r+");
char singleLine[200];

while(!feof(fPointer)){
fgets(singleLine,200,fPointer);
puts(singleLine);
}

fclose(fPointer);

printf("\n Back to main menu with 0 or exit with different value:");
scanf("%d",&returnMainMenu);

while(returnMainMenu==0){
    mainMenu();
 }
}

我认为 two-dimension 数组和结构体将是更好的解决方案,但在仅使用数组的解决方案下方,atof 函数将数组转换为浮点数。

#include <stdio.h>
#include <stdlib.h>

#define NAME_LENGTH 80

char teams[][2][NAME_LENGTH] =
  {
    {"Arsenal", "0.642"},
    {"Aston Villa", "0.441"},
    {"Brentford", "0.430"}
};

int main()
{
    float team_a;
    float team_b;

    team_a = atof(teams[0][1]);
    team_b = atof(teams[0][1]);


    if(team_a > team_b)
    {
            printf("The best team is %s with a %f score\n", teams[0][0], team_a);
    }
    else
    {
            printf("The best team is %s with a %f score\n", teams[1][0], team_b);
    }

    return 0;
}
scanf("%s",ans1); // remove & ans1 is address
scanf("%s",ans2); // remove & ans2 is address

// what are you trying with this? I think you want to know
// if ans1 is inside team, you should performs one strcmp() 
// on each teams member element [0]... [19]
if(strcmp(teams,ans1)==0) 

// void function but return integer, you cant do that
void enterTeam()
{
--snip--
return 0; // if it is void don't return integer
}

FILE *pFile; // need to be initialized before to use 
if (pFile !=NULL) // uninitialized

下面是 strcmp() 的示例,在列表中查找名称

#include <stdio.h>
#include <string.h>

#define NAME_LENGTH 80

char *teams[NAME_LENGTH] =
    {"Arsenal","Aston Villa","Brentford","Brighton & Hove Albion",
    "Burnley","Chealsea","Crystal Palace","Everton","Leeds United",
    "Leicester City","Liverpool","Manchester City","Manchester United",
    "New Castle United","Norwich City","Southampton","Tottenham","Watford",
    "West Ham United","Wolverhampton"};


int is_listed(char *);

int main()
{
    int r = 0;
    char team_name[80];

    printf("Give me a team name, I will verify:  ");
    scanf("%s", team_name);

    r = is_listed(team_name);

    if(r == -1)
    {
        printf("\n Name is not in list!\n");
    }
    else
    {
        printf("\n Name is position %d in the list\n", r);
    }

    return 0;
}

// Return position in the list, -1 if it is not in the list
int is_listed(char *my_name)
{
    int n;
    for(n =0; n<20; n++)
    {
        printf("\n%s ?? %s", teams[n], my_name);
        if(strcmp(teams[n], my_name) == 0)
        {
            return n;
        }
    }
    return -1;
}