C 言語で四角形の面積計算プログラムを作成する

CBeginner
オンラインで実践に進む

はじめに

この実験では、C プログラミング言語を使って四角形の面積を計算するプログラムを作成する方法を学びます。この実験では、以下の手順が含まれます。

四角形の面積を計算する関数を宣言する:四角形の高さと幅を入力として受け取り、面積を計算して結果を返す関数を宣言する方法を学びます。

ユーザーに四角形の寸法を入力してもらう:C の標準入力関数を使って、ユーザーに四角形の高さと幅を入力してもらう方法を学びます。

四角形の面積関数を呼び出す:先に宣言した関数を呼び出し、ユーザーが入力した寸法を渡して面積を計算する方法を学びます。

計算した四角形の面積を表示する:計算した四角形の面積をユーザーに表示する方法を学びます。

C プログラムをコンパイルして実行する:最終結果を見るために C プログラムをコンパイルして実行する方法を学びます。

四角形の面積を計算する関数を宣言する

この手順では、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 は、2 つの整数型のパラメータを持ちます。

  • height:四角形の高さを表す
  • width:四角形の幅を表す

この関数は、高さと幅を掛けて面積を計算し、結果を整数として返します。

  • int 戻り値の型は、関数が整数値を返すことを示します
  • 関数名 calculateRectangleArea は分かりやすく、キャメルケースの命名規則に従っています
  • パラメータ 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. 関数と main メソッドの両方を含むように、完全な 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 という関数は、2 つの整数型のパラメータである heightwidth を受け取り、それらを掛けて面積を計算し、結果を整数として返します。また、printf()scanf() 関数を使って、ユーザーに四角形の寸法を入力してもらう方法も学びました。これらの関数は、それぞれプロンプトメッセージを表示し、ユーザーからの整数入力を読み取ります。

次に、四角形の面積関数を呼び出し、計算した面積を表示し、C プログラムをコンパイルして実行する方法を学びます。