gpt4 book ai didi

c - 我在人脸识别系统中使用了“嵌套的if”语句。显示的代码中的“嵌套”是否正确?

转载 作者:行者123 更新时间:2023-11-30 19:25:16 27 4
gpt4 key购买 nike

因此,问题基本上是我是否正确使用了嵌套的if语句。该代码基本上首先要求用户输入眼睛和鼻子到下巴的距离,然后计算它们的比率。之后,计算每个比率及其可能性之间的差异。一旦计算,if语句将用于确定哪些图像具有最接近的差值,从而将其用于验证站在相机前的人。

#include <stdio.h>
#include <math.h>
int main(void)
{
/* Declare variables. */
double eyes_1, eyes_2, eyes_3, nose_chin_1, nose_chin_2,
nose_chin_3, ratio_1, ratio_2, ratio_3, diff_1_2,
diff_2_3, diff_1_3;
/* Get user input from the keyboard. */
printf("Enter values in cm. \n");
printf("Enter eye distance and nose-chin distance for image 1: \n");
scanf_s("%lf %lf", &eyes_1, &nose_chin_1);
printf("Enter eye distance and nose-chin distance for image 2: \n");
scanf_s("%lf %lf", &eyes_2, &nose_chin_2);
printf("Enter eye distance and nose-chin distance for image 3: \n");
scanf_s("%lf %lf", &eyes_3, &nose_chin_3);
/* Compute ratios. */
ratio_1 = eyes_1 / nose_chin_1;
ratio_2 = eyes_2 / nose_chin_2;
ratio_3 = eyes_3 / nose_chin_3;
/* Compute differences. */
diff_1_2 = fabs(ratio_1 - ratio_2);
diff_1_3 = fabs(ratio_1 - ratio_3);
diff_2_3 = fabs(ratio_2 - ratio_3);

/* Find minimum difference and print image numbers. */
if (diff_1_2 <= diff_1_3)
{
printf("Best match is between images 1 and 2 \n");
if (diff_1_2 <= diff_2_3)
printf("Best match is between images 1 and 2 \n");
}

if (diff_1_3 <= diff_1_2)
{
printf("Best match is between images 1 and 3 \n");
if (diff_1_3 <= diff_2_3)
printf("Best match is between images 1 and 3 \n");
}

if (diff_2_3 <= diff_1_3)
{
printf("Best match is between images 2 and 3 \n");
if (diff_2_3 <= diff_1_2)
printf("Best match is between images 2 and 3 \n");
}

/* Exit program. */
return 0;
}


在此算法中,如果语句如下,则嵌​​套的部分:

if (diff_1_2 <= diff_1_3)
{
printf("Best match is between images 1 and 2 \n");
if (diff_1_2 <= diff_2_3)
printf("Best match is between images 1 and 2 \n");
}

if (diff_1_3 <= diff_1_2)
{
printf("Best match is between images 1 and 3 \n");
if (diff_1_3 <= diff_2_3)
printf("Best match is between images 1 and 3 \n");
}

if (diff_2_3 <= diff_1_3)
{
printf("Best match is between images 2 and 3 \n");
if (diff_2_3 <= diff_1_2)
printf("Best match is between images 2 and 3 \n");
}

最佳答案

这取决于您希望此算法显示的内容。
在该程序中,编译器首先对外部if语句进行赋值,如果为true,则为(x!= y)打印“图像x和y之间的最佳匹配”。然后,它对内部块进行赋值,因为如果为true,则再次打印同一行;如果为false,则进入下一个外部if语句。
而如果外部if语句为false,则它会直接移至下一个外部if语句。
现在,让我们看一下输出的可能性。
如果所有差异(x,y,z)均相等,则输出将显示三行有争议的语句的六行,很多人可能会将其视为逻辑错误。
我建议您要做的是在每种情况下都使用逻辑运算符(并停止使用nested-if)。
例如:if (diff_1_2 <= diff_1_3 && diff_1_2 <= diff_2_3)。它的作用是仅当两个条件都为真时,才允许程序返回1(或真)。

关于c - 我在人脸识别系统中使用了“嵌套的if”语句。显示的代码中的“嵌套”是否正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59128504/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com