C语言习题~day39

1.

思路:

1. 首先定义了一个基类 Shape,但它实际上主要是作为派生类的基础部分,这里只是包含坐标信息,实际未作具体使用。

2. 定义了 Rectangle 矩形类,包含长和宽等属性,以及计算矩形面积的函数 getRectangleArea。

3. 定义了 Circle 圆类,包含半径属性和计算圆面积的函数 getCircleArea。

4. 定义了 Square 正方形类,它是从 Rectangle 类派生的,继承了矩形类的属性和方法,然后专门定义了计算正方形面积的函数 getSquareArea。

5. 在 main 函数中,分别输入矩形的长和宽、圆的半径、正方形的边长,然后调用相应的面积计算函数来计算并输出它们的面积。

#include <stdio.h>

// 基类 shape
typedef struct {
    int x;
    int y;
} Shape;

// 矩形类
typedef struct {
    Shape base;
    int length;
    int width;
} Rectangle;

// 计算矩形面积
int getRectangleArea(Rectangle rect) {
    return rect.length * rect.width;
}

// 圆类
typedef struct {
    Shape base;
    double radius;
} Circle;

// 计算圆面积
double getCircleArea(Circle circle) {
    return 3.14 * circle.radius * circle.radius;
}

// 正方形类,从矩形类派生
typedef struct {
    Rectangle base;
} Square;

// 计算正方形面积
int getSquareArea(Square square) {
    return square.base.length * square.base.length;
}

int main() {
    Rectangle rect;
    Circle circle;
    Square square;

    scanf("%d %d", &rect.length, &rect.width);  // 修改这里,先输入长和宽
    scanf("%lf", &circle.radius);
    scanf("%d", &square.base.length);

    printf("%d\n", getRectangleArea(rect));
    printf("%g\n", getCircleArea(circle));
    printf("%d\n", getSquareArea(square));

    return 0;
}

相关推荐

  1. C语言习题~day32

    2024-06-14 12:24:03       34 阅读
  2. C语言习题~day35

    2024-06-14 12:24:03       27 阅读
  3. C语言习题~day32

    2024-06-14 12:24:03       20 阅读
  4. C语言-----习题

    2024-06-14 12:24:03       47 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-14 12:24:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-14 12:24:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-14 12:24:03       87 阅读
  4. Python语言-面向对象

    2024-06-14 12:24:03       96 阅读

热门阅读

  1. linux段异常信号量

    2024-06-14 12:24:03       31 阅读
  2. 黑苹果/Mac如何升级 Mac 新系统 Sequoia Beta 版

    2024-06-14 12:24:03       34 阅读
  3. 文本相似度的三种算法

    2024-06-14 12:24:03       28 阅读
  4. WPS中XLS表格使用的技巧记录

    2024-06-14 12:24:03       29 阅读
  5. 2024年湘潭大学软件体系结构考试总结

    2024-06-14 12:24:03       30 阅读
  6. 探索未来:前沿科技的突破与挑战

    2024-06-14 12:24:03       26 阅读
  7. 轻兔推荐 —— Syncthing

    2024-06-14 12:24:03       27 阅读
  8. spark mllib 特征学习笔记 (一)

    2024-06-14 12:24:03       25 阅读