我想比较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
什么是WEXITSTATUS? –
您可以减少一半的代码......这里有很多冗余 –
@DanielS .: _“宏:int WEXITSTATUS(int status) 如果WIFEXITED为状态为真,则该宏返回低位8 “# –