使用 C 语言创建矩形面积计算器

CCBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在本实验中,你将学习如何使用 C 编程语言创建一个矩形面积计算器。实验涵盖以下步骤:

声明计算矩形面积的函数:你将学习如何声明一个函数,该函数接收矩形的高度和宽度作为输入,计算面积并返回结果。

提示用户输入矩形尺寸:你将学习如何使用 C 中的标准输入函数,提示用户输入矩形的高度和宽度。

调用矩形面积计算函数:你将学习如何调用之前声明的函数,并传递用户提供的尺寸以计算面积。

显示计算出的矩形面积:你将学习如何向用户显示计算出的矩形面积。

编译并运行 C 程序:你将学习如何编译并运行 C 程序以查看最终结果。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c/BasicsGroup -.-> c/variables("`Variables`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") c/FunctionsGroup -.-> c/function_parameters("`Function Parameters`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/UserInteractionGroup -.-> c/output("`Output`") subgraph Lab Skills c/variables -.-> lab-438247{{"`使用 C 语言创建矩形面积计算器`"}} c/function_declaration -.-> lab-438247{{"`使用 C 语言创建矩形面积计算器`"}} c/function_parameters -.-> lab-438247{{"`使用 C 语言创建矩形面积计算器`"}} c/user_input -.-> lab-438247{{"`使用 C 语言创建矩形面积计算器`"}} c/output -.-> lab-438247{{"`使用 C 语言创建矩形面积计算器`"}} end

声明计算矩形面积的函数

在这一步骤中,你将学习如何在 C 语言中声明一个用于计算矩形面积的函数。函数是一段执行特定任务的代码块,可以在程序中重复使用。

  1. 打开 WebIDE 并导航到 ~/project 目录。
  2. 创建一个名为 rectangle.c 的新文件:
cd ~/project
touch rectangle.c
  1. 在 WebIDE 编辑器中打开 rectangle.c 文件,并添加以下函数声明:
#include <stdio.h>

int calculateRectangleArea(int height, int width)
{
    int area = height * width;
    return area;
}

此函数 calculateRectangleArea 接收两个整数参数:

  • height:表示矩形的高度
  • width:表示矩形的宽度

函数通过将高度和宽度相乘来计算面积,并将结果作为整数返回。

  • int 返回类型表示函数将返回一个整数值
  • 函数名 calculateRectangleArea 具有描述性,并遵循驼峰命名法(camelCase)
  • 参数 heightwidth 表示矩形的尺寸
  • area 通过高度和宽度相乘计算得出
  • return area 将计算出的面积返回给调用函数

提示用户输入矩形尺寸

在这一步骤中,你将学习如何使用 C 语言中的标准输入函数提示用户输入矩形的尺寸。

  1. 在 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);
}
  1. 让我们分解用户输入的过程:
  • printf() 向用户显示提示信息
  • scanf() 读取用户输入的整数
  • &height&width 是存储输入值的内存地址引用
  1. 更新完整的 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() 函数,并传递用户输入的尺寸以计算矩形的面积。

  1. 更新 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);
}
  1. 让我们分解函数调用的过程:
  • rectangleArea 存储函数返回的结果
  • calculateRectangleArea(height, width) 将用户输入的值作为参数传递
  • 函数计算面积并返回结果
  1. 更新完整的 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() 函数显示计算出的矩形面积。

  1. 更新 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);
}
  1. 让我们分解输出语句:
  • printf() 用于显示文本和值
  • %d 是整数值的格式说明符
  • rectangleArea 是包含计算面积的变量
  • square units 用于为结果提供上下文
  1. 更新完整的 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 接收两个整数参数——heightwidth——并将它们相乘以计算面积,最终返回一个整数结果。你还学习了如何使用 printf()scanf() 函数提示用户输入矩形的尺寸,这两个函数分别用于显示提示信息并读取用户输入的整数值。

接下来,你将学习如何调用矩形面积计算函数、显示计算出的面积,以及编译并运行 C 程序。

您可能感兴趣的其他 C 教程