【发布时间】:2015-02-04 00:19:15
【问题描述】:
我正在开发一个测试函数,它应该自动测试值以查看是否存在意外错误。
有六个“持有者”的值范围应该在 -999 到 1000 之间
我是这样尝试的:
#include <stdlib.h>
#include <stdio.h>
int inuti(double x, double y, double x1, double y1, double x2, double y2) {
int x_inuti;
int y_inuti;
if (x1 < x2)
x_inuti = x > x1 && x < x2;
else
x_inuti = x > x2 && x < x1;
if (y1 < y2)
y_inuti = y > y1 && y < y2;
else
y_inuti = y > y2 && y < y1;
return x_inuti && y_inuti;
}
int main(void) {
double x, y, x1, y1, x2, y2;
int resultat;
x = -999;
y = 1000;
x1 = -999;
y1 = 1000;
x2 = -999;
y2 = 1000;
while (x<1000 && y>-999 && x1<1000 && y2 >-999 && x2<1000 && y2>-999) {
x++;
y--;
y1++;
x1--;
x2++;
y2--;
printf("point x-value: %.1f \n", x);
printf("point y-value: %.1f \n", y);
printf("\n");
printf("corner side x-value: %.1f \n", x1);
printf("corner side y-value: %.1f \n", y1);
printf("\n");
printf("other corner side x-value%.1f \n", x2);
printf("other corner side y-value%.1f \n", y2);
printf("\n");
resultat = inuti(x, y, x1, y1, x2, y2);
if (resultat == 1)
printf("point was inside rectangle.\n");
else
printf("point was outside rectangle.\n");
printf("\n");
printf("\n");
}
getchar();
return 0;
}
但它不包括所有组合,甚至超过了最大和最小数量。
有人知道循环所有组合的更好方法吗?
感谢所有帮助
谢谢
【问题讨论】:
-
简单的答案是六个嵌套的
for循环。但请记住,每个变量大约有 2000 个不同的值,因此组合的总数为 2000^6,这是一个 非常 大的数字。您不可能在合理的时间内完成所有这些组合。 -
怎么会超过最大和最小数?
-
因为例如代码检查
x1<1000,但循环有x1--;。此外,while会检查y2两次,但从不检查y1。 -
@user3386109 谢谢!
-
使用
int而不是double;浮点数有时不能准确表示整数,而且它可能跑得更快。
标签: c visual-studio-2013 combinations