Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较数字在C中不能正常工作_C_Compare - Fatal编程技术网

比较数字在C中不能正常工作

比较数字在C中不能正常工作,c,compare,C,Compare,我试着比较3个随机数,但我得到的第一个数总是较低的,并且得到真正较低的数的值,我不知道我在哪里失败了,这是代码 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 < te

我试着比较3个随机数,但我得到的第一个数总是较低的,并且得到真正较低的数的值,我不知道我在哪里失败了,这是代码

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);
}
}
我正在添加我得到的部分,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);
补充,Aleorio函数,我认为它工作正常

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 例如,团队1=14秒,团队2=9秒,团队3=11秒


在那之后,我得到了,team1=9秒,team2=11秒,team3=14秒

你最后一次添加到帖子中使它更加清晰。 您等待3个子进程,如下所示:

wait(&team1);
wait(&team2);
wait(&team3);
因此,team1..team3将拥有这些流程的退出代码。但是函数wait不等待任何特定进程,第一次等待将返回退出的第一个子进程的退出代码。所以你永远不会知道团队1是团队1、团队2还是团队3的得分


由于子进程的睡眠取决于分数,因此停止的第一个进程的分数最低,因为分数较高的进程睡眠时间较长。因此,在您的情况下,team1将始终是得分最低的团队。

您可以将此代码减少一半。。。公司有很多裁员here@DanielS.:Macro:int WEXITSTATUS int status如果状态的WIFEXITED为true,则此宏将从子进程返回退出状态值的低位8位。我很确定如果你像下面这样调用你的方法,你不会得到第一个if:winner2,1,2@丹尼尔:用谷歌搜索宏,它不是OP写的。例如。。。然而,我必须说:看看usage+printf格式,我认为OP并不真正理解它们返回的内容,除了team2==team3==team1,应该写为team1==team2&&team2==team3或者其他一些变体之外,您的函数做了您想要的。问题出在您没有显示的代码部分,即计算team1、team2和team3的代码部分。发布一个完整的程序来重现问题。不,第2组是3名跑步者,我将第一名跑步者的时间T1加上第二名跑步者的时间T2,以获得团队的总时间OK,明白了。这意味着问题也可能出现在函数aleatorio中。看起来你需要调试一些东西。只需调用winner3,2,1来验证比较函数是否正常。但是请注意Gilles的评论,你上次的比较是错误的。我在帖子中添加了aleatorio函数,我认为问题在于winner3,2,1可以很好地工作,因为添加了很多。我完全重写了我的答案。问或搜索一个单独的后续问题。这样每个人都会看到新问题。如果你在这里的评论中问一些问题,我是唯一收到通知的人。您最初的问题是Compare number not work normal in C,现在您想问一些问题,比如如何获取特定子进程的退出代码。这显然是另一回事。其他人可能比我更能回答这个问题。
wait(&team1);
wait(&team2);
wait(&team3);