所以问题基本上是我是否正确使用了嵌套的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语句赋值