我正在尝试比较3个随机数,但是我得到的是第一个数字始终是较低的,并得到真正的较低数字的valor,我不知道我在哪里失败,这是代码

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);
}
}


我总是得到第一个“ if”结果,或者如果两个数字相等,我得到第四个“ if”

我在叫白衣

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


我要添加的部分是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,team 3 = 11 s

之后,我得到的,TEAM1 = 9号,TEAM2 = 11S,team3 = 14S

最佳答案

您最近对帖子的添加使其更加清晰。
您需要等待3个子进程,如下所示:

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


因此,team1..team3将具有这些进程的退出代码。但是函数wait不等待任何特定的进程,第一次等待将返回第一个子进程退出的退出代码。因此,您将永远不知道team1将是team1还是team2或team3的得分!

由于孩子的睡眠过程取决于得分,因此第一个停止的过程得分最低,因为得分较高的过程睡眠时间更长。因此,在您的情况下,team1永远是得分最低的团队。

关于c - 比较数字在C中无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20732359/

10-10 23:36