使用 C 语言创建矩形面积计算器
发布于2026-08-13 阅读(0)
介绍
在本实验中,你将学习如何使用 C 编程语言创建一个矩形面积计算器。实验涵盖以下步骤:
**声明计算矩形面积的函数**:学习如何声明一个函数,让它接收矩形的高度和宽度作为输入,计算面积并返回结果。
**提示用户输入矩形尺寸**:学习如何使用 C 中的标准输入函数,提示用户输入矩形的高度和宽度。
**调用矩形面积计算函数**:学习如何调用前面声明的函数,并传入用户提供的尺寸来完成面积计算。
**显示计算出的矩形面积**:学习如何向用户展示计算出的矩形面积。
**编译并运行 C 程序**:学习如何编译并运行 C 程序,以查看最终结果。
声明计算矩形面积的函数
在这一步中,你将学习如何在 C 语言中声明一个用于计算矩形面积的函数。函数本质上是一段执行特定任务的代码块,可以在程序中反复使用。
1. 打开 WebIDE 并导航到 `~/project` 目录。
2. 创建一个名为 `rectangle.c` 的新文件:
```bash
cd ~/project
touch rectangle.c
```
3. 在 WebIDE 编辑器中打开 `rectangle.c` 文件,并添加以下函数声明:
```c
#include nnint calculateRectangleArea(int height, int width)n{n int area = height * width;n return area;n}n```nn此函数 `calculateRectangleArea` 接收两个整数参数:nn- `height`:表示矩形的高度n- `width`:表示矩形的宽度nn函数通过将高度和宽度相乘来计算面积,并将结果作为整数返回。nn- `int` 返回类型表示函数将返回一个整数值n- 函数名 `calculateRectangleArea` 具有描述性,并遵循驼峰命名法(camelCase)n- 参数 `height` 和 `width` 表示矩形的尺寸n- `area` 通过高度和宽度相乘计算得出n- `return area` 将计算出的面积返回给调用函数n","need_verify":true,"has_solution":false},{"position":2,"title":"提示用户输入矩形尺寸","layout":"doc-workbench-split","text":"## 提示用户输入矩形尺寸nn在这一步骤中,你将学习如何使用 C 语言中的标准输入函数提示用户输入矩形的尺寸。nn1. 在 WebIDE 中打开 `rectangle.c` 文件,并添加 `main()` 函数以与用户交互:nn```cnvoid main()n{n int height, width;nn printf("Enter the height of the rectangle: ");n scanf("%d", &height);nn printf("Enter the width of the rectangle: ");n scanf("%d", &width);n}n```nn2. 让我们分解用户输入的过程:nn- `printf()` 向用户显示提示信息n- `scanf()` 读取用户输入的整数n- `&height` 和 `&width` 是存储输入值的内存地址引用nn3. 更新完整的 `rectangle.c` 文件,包含函数和主方法:nn```cn#include nnint calculateRectangleArea(int height, int width)n{n int area = height * width;n return area;n}nnvoid main()n{n int height, width;nn printf("Enter the height of the rectangle: ");n scanf("%d", &height);nn printf("Enter the width of the rectangle: ");n scanf("%d", &width);n}n```nn编译并运行程序,看看用户输入提示是否正常显示。nn```bashngcc rectangle.c -o rectanglen./rectanglen```nn示例输出:nn```nEnter the height of the rectangle: 5nEnter the width of the rectangle: 10n```n","need_verify":true,"has_solution":false},{"position":3,"title":"调用矩形面积计算函数","layout":"doc-workbench-split","text":"## 调用矩形面积计算函数nn在这一步里,重点就是学会调用前面定义好的 `calculateRectangleArea()` 函数,并把用户输入的尺寸传进去,算出矩形面积。nn1. 更新 `rectangle.c` 文件中的 `main()` 函数,调用面积计算函数:nn```cnvoid main()n{n int height, width, rectangleArea;nn printf("Enter the height of the rectangle: ");n scanf("%d", &height);nn printf("Enter the width of the rectangle: ");n scanf("%d", &width);nn rectangleArea = calculateRectangleArea(height, width);n}n```nn2. 让我们分解函数调用的过程:nn- `rectangleArea` 存储函数返回的结果n- `calculateRectangleArea(height, width)` 将用户输入的值作为参数传递n- 函数计算面积并返回结果nn3. 更新完整的 `rectangle.c` 文件以包含函数调用:nn```cn#include nnint calculateRectangleArea(int height, int width)n{n int area = height * width;n return area;n}nnvoid main()n{n int height, width, rectangleArea;nn printf("Enter the height of the rectangle: ");n scanf("%d", &height);nn printf("Enter the width of the rectangle: ");n scanf("%d", &width);nn rectangleArea = calculateRectangleArea(height, width);n}n```nn编译并运行程序以测试函数调用。nn```bashngcc rectangle.c -o rectanglen./rectanglen```nn示例输出:nn```nEnter the height of the rectangle: 5nEnter the width of the rectangle: 10n```n","need_verify":true,"has_solution":false},{"position":4,"title":"显示计算出的矩形面积","layout":"doc-workbench-split","text":"## 显示计算出的矩形面积nn在这一步骤中,你将学习如何使用 C 语言中的 `printf()` 函数显示计算出的矩形面积。nn1. 更新 `rectangle.c` 文件中的 `main()` 函数以打印计算出的面积:nn```cnvoid main()n{n int height, width, rectangleArea;nn printf("Enter the height of the rectangle: ");n scanf("%d", &height);nn printf("Enter the width of the rectangle: ");n scanf("%d", &width);nn rectangleArea = calculateRectangleArea(height, width);nn printf("The area of the rectangle is: %d square units\n", rectangleArea);n}n```nn2. 让我们分解输出语句:nn- `printf()` 用于显示文本和值n- `%d` 是整数值的格式说明符n- `rectangleArea` 是包含计算面积的变量n- `square units` 用于为结果提供上下文nn3. 更新完整的 `rectangle.c` 文件以包含输出语句:nn```cn#include nnint calculateRectangleArea(int height, int width)n{n int area = height * width;n return area;n}nnvoid main()n{n int height, width, rectangleArea;nn printf("Enter the height of the rectangle: ");n scanf("%d", &height);nn printf("Enter the width of the rectangle: ");n scanf("%d", &width);nn rectangleArea = calculateRectangleArea(height, width);nn printf("The area of the rectangle is: %d square units\n", rectangleArea);n}n```nn编译并运行程序以测试输出显示。nn```bashngcc rectangle.c -o rectanglen./rectanglen```nn示例输出:nn```nEnter the height of the rectangle: 5nEnter the width of the rectangle: 10nThe area of the rectangle is: 50 square unitsn```n","need_verify":true,"has_solution":false},{"position":5,"title":"总结","layout":"doc-fullscreen","text":"## 总结nn在本实验中,你学习了如何在 C 语言中声明一个用于计算矩形面积的函数。该函数 `calculateRectangleArea` 接收两个整数参数——`height` 和 `width`——并将它们相乘以计算面积,最终返回一个整数结果。你还学习了如何使用 `printf()` 和 `scanf()` 函数提示用户输入矩形的尺寸,这两个函数分别用于显示提示信息并读取用户输入的整数值。nn接下来,你将学习如何调用矩形面积计算函数、显示计算出的面积,以及编译并运行 C 程序。n","need_verify":false,"has_solution":false}
本文转载于:https://labex.io/zh/tutorials/c-create-a-rectangle-area-calculator-in-c-438247 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。