如何做到这一点的程序用C?部分3.2-3.9 [英] How to do this program in C? Part 3.2-3.9

查看:173
本文介绍了如何做到这一点的程序用C?部分3.2-3.9的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

多个条件,如多个,如果要正确打印所需的交集矩形else语句?

步骤3:两个矩形相交,如果他们具有共同的两个长方形的区域,如果他们只需触摸(公共边缘或公共角)不重叠

两个矩形相交(如上规定)当且仅当,

我)最大(xmin1,xmin2)LT;分钟(xmax1,xmax2)以及

二)最大(YMIN1,YMIN2)LT;分钟(ymax1,ymax2)

您的输出进行格式化。如下图所示,其中一个矩形显示其左下坐标(XMIN,YMIN)和右上角的坐标为(XMAX,YMAX)。其中,坐标是在一笛卡尔平面坐标。

示例输出:

 输入两个矩形:1 1 4 42 2 5 5矩形1:(1,1)(4,4)矩形2:(2,2)(5,5)路口矩形:(2,2)(4,4)

 输入两个矩形:1 1 4 45 5 10 10矩形1:(1,1)(4,4)矩形2:(5,5)(10,10)这两个矩形不相交

code:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;INT readRect为(int * W,INT * X,诠释* Y,诠释* Z){
返回scanf函数(%D%D,W,X,Y,Z);
}内政部INT(INT X1,X2 INT){
返回X1,X2;
}INT MAXINT(INT Y1,Y2 INT){
    返回Y1,Y2;
}诠释主要(无效){诠释A,B,C,D,E,F,G,H;
的printf(>>输入两个矩形:\\ n);readRect(安培; A,和B,和C,和D);
readRect(急症室,和放大器; F,放克,和放大器; H);
的printf(矩形1:(%D,%D)(%D,%D)\\ n,A,B,C,D);
的printf(矩形2:(%D,%D)(%D,%D)\\ n,E,F,G,H);如果(MAXINT(A,E)LT;内政部(C,G)及和放大器; MAXINT(B,F)和LT;内政部(D,G)){
        的printf(交集矩形:(%D,%D)(%D,%D)\\ n????,,,,);
}
其他{
         的printf(这些矩形不相交\\ n);
}返回EXIT_SUCCESS;
}


解决方案

您的 max函数是错了。结果
1.你是不是比较这些功能最大/最小值的两个内线传递的参数。结果
2.你不能从一个函数返回两个值。

您应该做这样的;

  INT内政部(INT X1,X2 INT){
    如果(X1< X2)
        返回X1;
    其他
        返回X2;
}INT MAXINT(INT X1,X2 INT){
    如果(X1> X2)
        返回X1;
    其他
        返回X2;
}

和改变你的的printf 打印路口矩形

 的printf(交集矩形:(%D,%D)(%D,%D)\\ n,MAXINT(A,E),MAXINT(B,F)内政部(C,G),内政部(D,H));

Are multiple conditions, as in multiple if else statements needed for the intersection rectangles to be printed correctly?

Step 3: Two rectangles intersect if they have an area in common Two rectangles do not overlap if they just touch (common edge, or common corner)

Two rectangles intersect(as specified above)if, and only if,

i) max(xmin1, xmin2) < min(xmax1, xmax2) and

ii) max(ymin1, ymin2) < min(ymax1, ymax2)

Your output is to be formatted. As shown below where a rectangle is shown as its lower left coordinates (xmin, ymin) and top right corner coordinates (xmax, ymax). Where the coordinates are coordinates in a Cartesian plane.

Sample output:

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)  

and

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 

Code:

#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;
}

解决方案

Your function for max and min is wrong.
1. you are not comparing the parameter passed inside these functions for maximum/minimum of two.
2. You can't return two values from a function.

You should do like this;

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

And change your printf printing the intersection rectangle to

printf("intersection rectangle: (%d,%d)(%d,%d)\n", maxInt(a,e), maxInt(b,f), minInt(c,g), minInt(d,h) );

这篇关于如何做到这一点的程序用C?部分3.2-3.9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆