2013-12-22 20 views
-4

我想比较3个随机数字,但我得到的第一个数字总是较低,并获得真正的较低数字的勇气,我不知道在哪里我失败了,这里是代码比较号码不能正常工作在C

void winner(int team1, int team2, int team3){ 

if (team1 < team2 && team1 < team3){ 
    printf("Team 1 winner %d seconds\n", team1); 
} 
if (team2 < team1 && team2 < team3){ 
    printf("Team 2 winner %d seconds\n", team2); 
} 
if (team3 < team1 && team3 < team2){ 
    printf("Team 3 winner %d seconds\n", team3); 
} 
if (team1 == team2 && team1 < team3){ 
    printf("Draw Team 1 and Team 2, %d seconds\n", team1); 
} 
if (team1 == team3 && team1 < team2){ 
    printf("Draw Team 1 and Team 3, %d seconds\n", team1); 
} 
if (team2 == team3 && team2 < team1){ 
    printf("Draw Team 2 and Team 3, %d seconds\n", team2); 
} 
if (team1 == team2 && team2 == team3){ //Fixed this compare, tanks Guilles 
    printf("Draw Team 1, Team 2 and Team 3, %d seconds\n", team1); 
} 
} 

我一直都想与第一个“如果”的结果,或者如果2号是平等的,我得到的第4个“如果”

我调用函数惠特尔

winner(WEXITSTATUS(team1), WEXITSTATUS(team2), WEXITSTATUS(team3)); 

我添加了t他的部分在哪里得到的,TEAM1,TEAM2和team3

int corredores(int equipo){ 
struct timespec tw = { .tv_sec = 0, .tv_nsec = 10000000 }; 
pid_t Cp, Rn1, Rn2; 
int min = 2, max = 6; 
int T1, T2, TC; 

Cp = fork();   //Creacion del capitan 

if (Cp == -1){ 
    printf("Un Capitan no esta listo, no habra carrera\n"); 
    exit(-1); 
} 

else if (Cp == 0){  //Codigo del Capitan 
    nanosleep(&tw, 0); 
    Rn2 = fork();  //Creacion del Segundo Corredor 

    if (Rn2 == -1){ 
    printf("El Segundo Corredor de un Equipo no esta listo, no habra carrera\n"); 
    exit(-1); 
    } 

    else if (Rn2 == 0){  //Codigo del Segundo corredor 
     nanosleep(&tw, 0); 
     Rn1 = fork();  //Creacion del Primer corredor 
     if (Rn1== -1){ 
      printf("El Primer Corredor de un Equipo no esta listo, no habra carrera\n"); 
      exit(-1); 
     } 

     else if (Rn1 == 0){  //Codigo del Primer corredor 
      srand(getpid()); 
      nanosleep(&tw, 0); 
      T1 = aleatorio(min, max); 
      printf("El tiempo del Primer Corredor del Equipo %d es de %d segundos\n",equipo, T1); 
      sleep(T1); 
      exit(T1); 
     } 
     wait(&T1); 
     srand(getpid()); 
     T2 = aleatorio(min, max); 
     printf("El tiempo del Segundo Corredor del Equipo %d es de %d segundos\n",equipo, T2); 
     nanosleep(&tw, 0); 
     sleep(T2); 
     T2 = T2 + WEXITSTATUS(T1); 
     exit(T2); 
    } 
    wait(&T2); 
    srand(getpid()); 
    TC = aleatorio(min, max); 
    printf("El tiempo Capitan del Equipo %d es de %d segundos\n",equipo, TC); 
    nanosleep(&tw, 0); 
    sleep(TC); 
    TC = TC + WEXITSTATUS(T2); 
    exit(TC); 
} 

return WEXITSTATUS(TC); 
} 

,并呼吁通过

team1 = corredores(equipo); 

补充说, “aleatorio” 功能,我认为它的正常工作

int aleatorio(int min, int max){ 
return rand() % (max-min+1) + min; 

}

我觉得问题在这里某处

wait(&team1); 
wait(&team2); 
wait(&team3); 
printf("======================================\n"); 
ganador(WEXITSTATUS(team1), WEXITSTATUS(team2), WEXITSTATUS(team3)); 

第一功能结束获取TEAM1,第二,TEAM2,例如第三team3 ,TEAM1 = 14个S,TEAM2 = 9个S,团队3 = 11号第

后,我得到,TEAM1 = 9 s,team2 = 11s,team3 = 14s

+0

什么是WEXITSTATUS? –

+0

您可以减少一半的代码......这里有很多冗余 –

+1

@DanielS .: _“宏:int WEXITSTATUS(int status) 如果WIFEXITED为状态为真,则该宏返回低位8 “# –

回答

1

您最后一次加入帖子使它更清晰一些。 你等3个个子过程是这样的:

wait(&team1); 
wait(&team2); 
wait(&team3); 

所以team1..team3将这些进程的退出代码。但函数wait不等待任何特定进程,第一次等待将返回第一个子进程退出的退出代码。所以你永远不会知道team1是否会成为team1或team2或team3的分数!

由于孩子根据分数处理睡眠,所以停止的第一个过程将得分最低,因为得分较高的过程睡眠时间较长。所以在你的情况下,team1将永远是得分最低的球队。

+0

不,第二名是3名选手,即时将第一名选手(T1)的时间加到第二名选手(T2)的时间,以获得球队总时间 – jadelabe

+0

确定,清楚。这意味着问题也可以在函数'aleatorio'中。看起来像你必须调试。只需调用'优胜者(3,2,1)'来验证比较函数是否正确。但请评论吉勒斯的评论,你最后的比较是错误的。 – wimh

+1

我添加到帖子aleatorio功能,我认为问题是,赢家(3,2,1)正常工作 – jadelabe