📄 struct.c
字号:
/* Demonstrates structures that contain other structures. */
/* Receives input for corner coordinates of a rectangle and
calculates the area. Assumes that the y coordinate of the
lower-right corner is greater than the y coordinate of the
upper-left corner, that the x coordinate of the lower-
right corner is greater than the x coordinate of the upper-
left corner, and that all coordinates are positive. */
#include <stdio.h>
int length, width;
long area;
struct coord{
int x;
int y;
};
struct rectangle{
struct coord topleft;
struct coord bottomrt;
} mybox;
int main( void )
{
/* Input the coordinates */
printf("\nEnter the top left x coordinate: ");
scanf("%d", &mybox.topleft.x);
printf("\nEnter the top left y coordinate: ");
scanf("%d", &mybox.topleft.y);
printf("\nEnter the bottom right x coordinate: ");
scanf("%d", &mybox.bottomrt.x);
printf("\nEnter the bottom right y coordinate: ");
scanf("%d", &mybox.bottomrt.y);
/* Calculate the length and width */
width = mybox.bottomrt.x - mybox.topleft.x;
length = mybox.bottomrt.y - mybox.topleft.y;
/* Calculate and display the area */
area = width * length;
printf("\nThe area is %ld units.\n", area);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -