函數double computeArea(Rectangle *r)
使用給定的兩點座標,topLeft
和botRight
計算矩形的面積。我有點困惑,爲什麼我的r
沒有進入main
函數的功能。用於計算給定兩點矩形區域的嵌套結構(C語言)
這裏就是我所做的:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <math.h>
typedef struct {
double x;
double y;
} Point;
typedef struct {
Point topLeft; /* top left point of rectangle */
Point botRight; /* bottom right point of rectangle */
} Rectangle;
double computeArea(Rectangle *r);
int main()
{
Point p;
Rectangle r;
printf("\nEnter top left point: ");
scanf("%lf", &r.topLeft.x);
scanf("%lf", &r.topLeft.y);
printf("Enter bottom right point: ");
scanf("%lf", &r.botRight.x);
scanf("%lf", &r.botRight.y);
printf("Top left x = %lf y = %lf\n", r.topLeft.x, r.topLeft.y);
printf("Bottom right x = %lf y = %lf\n", r.botRight.x, r.botRight.y);
printf("Area = %d", computeArea(&r));
return 0;
}
double computeArea(Rectangle *r)
{
double height, width, area;
height = ((r->topLeft.y) - (r->botRight.y));
width = ((r->topLeft.x) - (r->botRight.x));
area = height*width;
return (area);
}
預期輸出:
Enter top left point: 1 1
Enter bottom right point: 2 -1
Top left x = 1.000000 y = 1.000000
Bottom right x = 2.000000 y = -1.000000
Area = 2.000000
輸出我得到相反:
Enter top left point: 1 1
Enter bottom right point: 2 -1
Top left x = 1.000000 y = 1.000000
Bottom right x = 2.000000 y = -1.000000
Area = 0
你得到的實際輸出是什麼? – Inox