【c语言】结构体 求平面直角坐标系中矩形的面积

 
#include<stdio.h>
struct point{
    int x;
    int y;    
};
struct rectangle{
    struct point upper_left;
    struct point lower_right;
};
int main(){
    int s,a,b;
    struct rectangle c, d;
    a=c.lower_right.x-c.upper_left.x;
    b=d.upper_left.y-d.lower_right.y;
    s=a*b;
    printf("r的面积是%d",s);
    return 0;
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以按照以下步骤编写程序: 1. 定义平面直角坐标系上的点的结构体。 ```c typedef struct { double x; double y; } Point; ``` 2. 读入 n 个点的坐标,存储到结构体数组中。 ```c int n; printf("请输入点的数量:"); scanf("%d", &n); Point points[n]; for (int i = 0; i < n; i++) { printf("请输入第%d个点的坐标(用空格隔开):", i + 1); scanf("%lf %lf", &points[i].x, &points[i].y); } ``` 3. 对这些点的 x 轴坐标排序。可以使用冒泡排序或快速排序等算法,这里演示冒泡排序的方法。 ```c for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (points[j].x > points[j+1].x) { // 交换两个点的坐标 Point temp = points[j]; points[j] = points[j+1]; points[j+1] = temp; } } } ``` 4. 输出排序后的点的 x 轴坐标。 ```c printf("按 x 轴坐标从小到大排序后的点的坐标如下:\n"); for (int i = 0; i < n; i++) { printf("(%lf, %lf)\n", points[i].x, points[i].y); } ``` 完整代码如下: ```c #include <stdio.h> typedef struct { double x; double y; } Point; int main() { int n; printf("请输入点的数量:"); scanf("%d", &n); Point points[n]; for (int i = 0; i < n; i++) { printf("请输入第%d个点的坐标(用空格隔开):", i + 1); scanf("%lf %lf", &points[i].x, &points[i].y); } for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (points[j].x > points[j+1].x) { // 交换两个点的坐标 Point temp = points[j]; points[j] = points[j+1]; points[j+1] = temp; } } } printf("按 x 轴坐标从小到大排序后的点的坐标如下:\n"); for (int i = 0; i < n; i++) { printf("(%lf, %lf)\n", points[i].x, points[i].y); } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值