【问题标题】:How to do this program in C? Part 3.2-3.9 [closed]如何在 C 中执行此程序?第 3.2-3.9 部分 [关闭]
【发布时间】:2013-10-21 13:38:59
【问题描述】:

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

步骤 3:如果两个矩形有共同区域,则相交 两个矩形不重叠,如果它们只是接触(公共边,或公共角)

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

i) max(xmin1, xmin2)

ii) 最大值(ymin1, ymin2)

您的输出将被格式化。如下所示,其中一个矩形显示为其左下角坐标(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;
}

【问题讨论】:

  • 显示你的输出格式?
  • 你不是说 int minInt(int x1, int x2){return x1y2;}
  • @JerryJeremiah;这将返回一个布尔值。 OP 想要两个数字的最大值或最小值。
  • 我认为这个问题不应该结束。至少OP是他的努力提出的。由于他是初学者,他似乎无法实现他的逻辑。投票决定重新开放。

标签: c scanf intersection pass-by-reference call-by-value


【解决方案1】:

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

【讨论】:

  • 赞成:我的意思是把 \n 从我的答案中去掉。立即编辑!
【解决方案2】:

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) );

【讨论】:

  • 而 printf 打印交叉点?变量应该是什么?我真的很感谢你的帮助。我只是一个初学者程序员,我从小就没有像你们许多人一样开始编程。但是人们一直不给我投票,当然,我的问题与作业有关,但我没有使用互联网的人来完成我的作业,他们可以帮助我学习。
  • 实际上我对您的问题和您显示的输出感到困惑。在您的程序中,您正在输入矩形的边,但您的问题是关于顶点:As shown below where a rectangle is shown as its lower left coordinates (xmin, ymin) and top right corner coordinates (xmax, ymax).
  • 程序实际上是输入一个矩形的左下顶点和右上顶点。这就是坐标。造成误会真的很抱歉。
  • 我已经编辑了我的答案。现在它就像魅力一样工作!
  • 会的。看看你的minInt 函数应该是int minInt(int x, int y){ if(x &lt; y){ return x; }else{ return y; } }。但是您要为ifelse 都返回y
【解决方案3】:

第一件事:

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

应该是

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多