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는 두 개의 정수 매개변수를 사용합니다.

  • height: 사각형의 높이를 나타냅니다.
  • width: 사각형의 너비를 나타냅니다.

이 함수는 높이와 너비를 곱하여 면적을 계산하고 결과를 정수로 반환합니다.

  • int 반환 유형은 함수가 정수 값을 반환함을 나타냅니다.
  • 함수 이름 calculateRectangleArea는 설명적이며 camelCase 명명 규칙을 따릅니다.
  • 매개변수 height 및 width는 사각형의 치수를 나타냅니다.
  • 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는 정수 값에 대한 형식 지정자 (format specifier) 입니다.
  • 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 함수는 두 개의 정수 매개변수, 즉 height와 width를 받아 곱하여 면적을 계산하고, 결과를 정수로 반환합니다. 또한 printf() 및 scanf() 함수를 사용하여 사용자에게 사각형의 치수를 입력하도록 요청하는 방법도 배웠습니다. printf()는 프롬프트 메시지를 표시하고, scanf()는 사용자로부터 정수 입력을 읽습니다.

다음으로, 사각형 면적 함수를 호출하고, 계산된 면적을 표시하며, C 프로그램을 컴파일하고 실행하는 방법을 배우게 됩니다.