介绍
在本实验中,你将学习如何使用 C 编程语言创建一个矩形面积计算器。实验涵盖以下步骤:
声明计算矩形面积的函数:你将学习如何声明一个函数,该函数接收矩形的高度和宽度作为输入,计算面积并返回结果。
提示用户输入矩形尺寸:你将学习如何使用 C 中的标准输入函数,提示用户输入矩形的高度和宽度。
调用矩形面积计算函数:你将学习如何调用之前声明的函数,并传递用户提供的尺寸以计算面积。
显示计算出的矩形面积:你将学习如何向用户显示计算出的矩形面积。
编译并运行 C 程序:你将学习如何编译并运行 C 程序以查看最终结果。
声明计算矩形面积的函数
在这一步骤中,你将学习如何在 C 语言中声明一个用于计算矩形面积的函数。函数是一段执行特定任务的代码块,可以在程序中重复使用。
- 打开 WebIDE 并导航到
~/project目录。 - 创建一个名为
rectangle.c的新文件:
cd ~/project
touch rectangle.c
- 在 WebIDE 编辑器中打开
rectangle.c文件,并添加以下函数声明:
#include <stdio.h>
int calculateRectangleArea(int height, int width)
{
int area = height * width;
return area;
}
此函数 calculateRectangleArea 接收两个整数参数:
height:表示矩形的高度width:表示矩形的宽度
函数通过将高度和宽度相乘来计算面积,并将结果作为整数返回。
int返回类型表示函数将返回一个整数值- 函数名
calculateRectangleArea具有描述性,并遵循驼峰命名法(camelCase) - 参数
height和width表示矩形的尺寸 area通过高度和宽度相乘计算得出return area将计算出的面积返回给调用函数
提示用户输入矩形尺寸
在这一步骤中,你将学习如何使用 C 语言中的标准输入函数提示用户输入矩形的尺寸。
- 在 WebIDE 中打开
rectangle.c文件,并添加main()函数以与用户交互:
void main()
{
int height, width;
printf("Enter the height of the rectangle: ");
scanf("%d", &height);
printf("Enter the width of the rectangle: ");
scanf("%d", &width);
}
- 让我们分解用户输入的过程:
printf()向用户显示提示信息scanf()读取用户输入的整数&height和&width是存储输入值的内存地址引用
- 更新完整的
rectangle.c文件,包含函数和主方法:
#include <stdio.h>
int calculateRectangleArea(int height, int width)
{
int area = height * width;
return area;
}
void main()
{
int height, width;
printf("Enter the height of the rectangle: ");
scanf("%d", &height);
printf("Enter the width of the rectangle: ");
scanf("%d", &width);
}
编译并运行程序以测试用户输入提示。
gcc rectangle.c -o rectangle
./rectangle
示例输出:
Enter the height of the rectangle: 5
Enter the width of the rectangle: 10
调用矩形面积计算函数
在这一步骤中,你将学习如何调用之前定义的 calculateRectangleArea() 函数,并传递用户输入的尺寸以计算矩形的面积。
- 更新
rectangle.c文件中的main()函数以调用面积计算函数:
void main()
{
int height, width, rectangleArea;
printf("Enter the height of the rectangle: ");
scanf("%d", &height);
printf("Enter the width of the rectangle: ");
scanf("%d", &width);
rectangleArea = calculateRectangleArea(height, width);
}
- 让我们分解函数调用的过程:
rectangleArea存储函数返回的结果calculateRectangleArea(height, width)将用户输入的值作为参数传递- 函数计算面积并返回结果
- 更新完整的
rectangle.c文件以包含函数调用:
#include <stdio.h>
int calculateRectangleArea(int height, int width)
{
int area = height * width;
return area;
}
void main()
{
int height, width, rectangleArea;
printf("Enter the height of the rectangle: ");
scanf("%d", &height);
printf("Enter the width of the rectangle: ");
scanf("%d", &width);
rectangleArea = calculateRectangleArea(height, width);
}
编译并运行程序以测试函数调用。
gcc rectangle.c -o rectangle
./rectangle
示例输出:
Enter the height of the rectangle: 5
Enter the width of the rectangle: 10
显示计算出的矩形面积
在这一步骤中,你将学习如何使用 C 语言中的 printf() 函数显示计算出的矩形面积。
- 更新
rectangle.c文件中的main()函数以打印计算出的面积:
void main()
{
int height, width, rectangleArea;
printf("Enter the height of the rectangle: ");
scanf("%d", &height);
printf("Enter the width of the rectangle: ");
scanf("%d", &width);
rectangleArea = calculateRectangleArea(height, width);
printf("The area of the rectangle is: %d square units\n", rectangleArea);
}
- 让我们分解输出语句:
printf()用于显示文本和值%d是整数值的格式说明符rectangleArea是包含计算面积的变量square units用于为结果提供上下文
- 更新完整的
rectangle.c文件以包含输出语句:
#include <stdio.h>
int calculateRectangleArea(int height, int width)
{
int area = height * width;
return area;
}
void main()
{
int height, width, rectangleArea;
printf("Enter the height of the rectangle: ");
scanf("%d", &height);
printf("Enter the width of the rectangle: ");
scanf("%d", &width);
rectangleArea = calculateRectangleArea(height, width);
printf("The area of the rectangle is: %d square units\n", rectangleArea);
}
编译并运行程序以测试输出显示。
gcc rectangle.c -o rectangle
./rectangle
示例输出:
Enter the height of the rectangle: 5
Enter the width of the rectangle: 10
The area of the rectangle is: 50 square units
总结
在本实验中,你学习了如何在 C 语言中声明一个用于计算矩形面积的函数。该函数 calculateRectangleArea 接收两个整数参数——height 和 width——并将它们相乘以计算面积,最终返回一个整数结果。你还学习了如何使用 printf() 和 scanf() 函数提示用户输入矩形的尺寸,这两个函数分别用于显示提示信息并读取用户输入的整数值。
接下来,你将学习如何调用矩形面积计算函数、显示计算出的面积,以及编译并运行 C 程序。



