0

是否需要多个条件,例如多个 if else 语句才能正确打印相交矩形?

Step 3: 两个矩形如果有共同的面积就相交 两个矩形不重叠如果它们只是接触(公共边,或公共角)</p>

当且仅当,两个矩形相交(如上所述)

i) max(xmin1, xmin2) < min(xmax1, xmax2) 并且

ii) 最大值(ymin1, ymin2) < 最小值(ymax1, ymax2)

您的输出将被格式化。如下图所示,其中一个矩形显示为其左下角坐标(xmin, ymin)和右上角坐标(xmax, ymax)。其中坐标是笛卡尔平面中的坐标。

样本输出:

enter two rectangles: 

1 1 4 4

2 2 5 5

rectangle 1: (1,1)(4,4) 

rectangle 2: (2,2)(5,5) 

intersection rectangle: (2,2)(4,4)  

enter two rectangles: 

1 1 4 4

5 5 10 10

rectangle 1: (1,1)(4,4) 

rectangle 2: (5,5)(10,10) 

these two rectangles do not intersect 

代码:

#include <stdio.h>
#include <stdlib.h>

int readRect (int *w, int *x, int *y, int *z){
return scanf("%d%d%d%d",w,x,y,z);
}

int minInt(int x1, int x2){
return x1, x2;
}

int maxInt(int y1, int y2){
    return y1, y2;
}

int main (void){

int a,b,c,d,e,f,g,h;
printf(">>enter two rectangles:\n");

readRect(&a,&b,&c,&d);
readRect(&e,&f,&g,&h);
printf("rectangle 1:(%d,%d)(%d,%d)\n",a,b,c,d);
printf("rectangle 2:(%d,%d)(%d,%d)\n",e,f,g,h);

if(maxInt(a,e) < minInt(c,g) && maxInt(b,f) < minInt(d,g)){
        printf("intersection rectangle: (%d,%d)(%d,%d)\n",?,?,?,?);
}
else {
         printf("these rectangles do not intersect\n");
}

return EXIT_SUCCESS;
}
4

3 回答 3

1

第 1 步 - 罪魁祸首是 scanf 中的“\n”。如果您删除它,它将起作用如果您在第 2 步或第 3 步中需要任何特定帮助,请告诉我。

于 2013-10-14T02:39:03.910 回答
0

第一件事:

return scanf("%d%d%d%d\n",w,x,y,z);

应该

return scanf("%d %d %d %d",w,x,y,z);

然后你可以输入你的数字列表作为一个空格分隔的列表,它会正确扫描它们。

你问题的其他部分,你必须尝试自己解决,使你的问题更具体,并提出新的问题。

于 2013-10-14T02:28:56.560 回答
0

你的功能maxmin是错误的。
1.您没有比较在这些函数中传递的参数的最大值/最小值两个。
2.你不能从一个函数中返回两个值。

你应该这样做;

int minInt(int x1, int x2){
    if(x1 < x2)     
        return x1;
    else 
        return x2;
}

int maxInt(int x1, int x2){
    if(x1 > x2)     
        return x1;
    else 
        return x2;
} 

并将您的printf打印相交矩形更改为

printf("intersection rectangle: (%d,%d)(%d,%d)\n", maxInt(a,e), maxInt(b,f), minInt(c,g), minInt(d,h) );
于 2013-10-14T03:55:16.517 回答