Computer >> 컴퓨터 >  >> 프로그래밍 >> C 프로그래밍

C 언어 switch-case 문으로 삼각형·정사각형·원·직사각형·평행사변형 넓이 계산하기

문제 정의

switch-case 문을 활용하여 삼각형, 정사각형, 원, 직사각형, 평행사변형의 넓이를 계산하는 C 프로그램을 작성해 보겠습니다.

해결 방법

사용자가 선택한 메뉴 번호(case 번호)에 따라 각 도형의 넓이가 계산됩니다. 각 도형별 넓이 계산 공식은 다음과 같습니다.

  • 삼각형의 넓이 — 헤론의 공식(Heron's Formula)을 사용합니다. 세 변 a, b, c를 입력받아 다음과 같이 계산합니다.
s=(float)(a+b+c)/2;
area=(float)(sqrt(s*(s-a)*(s-b)*(s-c)));

먼저 세 변의 합을 2로 나누어 반둘레 s를 구한 뒤, sqrt 함수를 이용해 넓이를 산출합니다. 이때 math.h 헤더 파일이 필요합니다.

  • 정사각형의 넓이 — 실행 시점에 한 변의 길이를 입력받아 계산합니다.
area=(float)side*side;
  • 원의 넓이 — 실행 시점에 반지름을 입력받아 계산합니다.
area=(float)3.14159*radius*radius;

원주율 π는 3.14159 값을 그대로 사용했습니다.

  • 직사각형의 넓이 — 실행 시점에 가로와 세로 길이를 입력받아 계산합니다.
area=(float)len*breadth;
  • 평행사변형의 넓이 — 밑변과 높이를 입력받아 계산합니다.
area=(float)base*height;

예제 코드

다음은 switch-case 문으로 삼각형, 정사각형, 원, 직사각형, 평행사변형의 넓이를 계산하는 전체 C 프로그램입니다.

#include<stdio.h>
#include<math.h>
main(){
   int choice;
   printf("Enter\n1 to find area of Triangle\n2 for finding area of Square\n3 for finding area of Circle\n4 for finding area of Rectangle\n5 for Parallelogram\n");
   scanf("%d",&choice);
   switch(choice) {
      case 1: {
         int a,b,c;
         float s,area;
         printf("Enter sides of triangle\n");
         scanf("%d%d %d",&a,&b,&c);
         s=(float)(a+b+c)/2;
         area=(float)(sqrt(s*(s-a)*(s-b)*(s-c)));
         printf("Area of Triangle is %f\n",area);
         break;
      }
      case 2: {
         float side,area;
         printf("Enter Sides of Square\n");
         scanf("%f",&side);
         area=(float)side*side;
         printf("Area of Square is %f\n",area);
         break;
      }
      case 3: {
         float radius,area;
         printf("Enter Radius of Circle\n");
         scanf("%f",&radius);
         area=(float)3.14159*radius*radius;
         printf("Area of Circle %f\n",area);
         break;
      }
      case 4: {
         float len,breadth,area;
         printf("Enter Length and Breadth of Rectangle\n");
         scanf("%f %f",&len,&breadth);
         area=(float)len*breadth;
         printf("Area of Rectangle is %f\n",area);
         break;
      }
      case 5: {
         float base,height,area;
         printf("Enter base and height of Parallelogram\n");
         scanf("%f %f",&base,&height);
         area=(float)base*height;
         printf("Enter area of Parallelogram is %f\n",area);
         break;
      }
      default: {
         printf("Invalid Choice\n");
         break;
      }
   }
}

코드 핵심 포인트

  • 각 case 블록마다 필요한 변수를 지역적으로 선언하여 메모리를 효율적으로 관리합니다.
  • 모든 case 마지막에는 break; 문을 두어 switch 문을 빠져나가도록 합니다.
  • 1~5 이외의 값이 입력되면 default 블록에서 "Invalid Choice(잘못된 선택)" 메시지를 출력합니다.
  • 삼각형 넓이 계산 시 제곱근 연산을 위해 math.h 헤더 파일을 포함해야 합니다.

실행 결과

위 프로그램을 컴파일하고 실행하면 다음과 같은 결과를 확인할 수 있습니다.

Run 1:
1 to find area of Triangle
2 for finding area of Square
3 for finding area of Circle
4 for finding area of Rectangle
5 for Parallelogram
5
Enter base and height of Parallelogram
2 4 6 8
Enter area of Parallelogram is 8.000000

Run 2:
1 to find area of Triangle
2 for finding area of Square
3 for finding area of Circle
4 for finding area of Rectangle
5 for Parallelogram
3
Enter Radius of Circle
4.5
Area of Circle is 63.617199

첫 번째 실행에서는 밑변 2, 높이 4인 평행사변형의 넓이 8.0이 출력되었고, 두 번째 실행에서는 반지름 4.5인 원의 넓이 약 63.617199가 출력된 것을 확인할 수 있습니다.