몽상실현개발주의

[따배씨] 7.4 다중선택 else if ~ 7.5 else 와 if 짝짓기 본문

Language/C

[따배씨] 7.4 다중선택 else if ~ 7.5 else 와 if 짝짓기

migrationArc 2021. 5. 24. 06:59

[따배씨] 7.4 다중선택 else if ~ 7.5 else 와 if 짝짓기

따배씨 - 따라하며 배우는 C언어

7강 분기

7.4 다중선택 else if

#include <stdio.h>

// assessment stansard tax base
#define BASE1 12000000.0
#define BASE2 46000000.0
#define BASE3 88000000.0
#define BASE4 150000000.0
#define BASE5 300000000.0
#define BASE6 500000000.0

// percent to rate
#define RATE1 (6.0 / 100.0)
#define RATE2 (15.0 / 100.0)
#define RATE3 (24.0 / 100.0)
#define RATE4 (35.0 / 100.0)
#define RATE5 (38.0 / 100.0)
#define RATE6 (40.0 / 100.0)
#define RATE7 (42.0 / 100.0)

#define BASIC_DECUCTION 1500000.0

int main()
{
    double income = 0.0;    // annual income
    double tax = 0.0;
    
    scanf("%lf", &income);
    
    income -= BASIC_DECUCTION;
    
    if (income <= BASE1)
    {
        tax = income * RATE1;
    }
    else if (income <= BASE2)
    {
        tax = BASE1 * RATE1 + (income - BASE1) * RATE2;
    }
    else if (income <= BASE3)
    {
        tax = BASE1 * RATE1 + (BASE2 - BASE1) * RATE2 + (income - BASE2) * RATE3;
    }
    else if (income <= BASE4)
    {
        tax = BASE1 * RATE1 + (BASE2 - BASE1) * RATE2 + (BASE3 - BASE2) * RATE3 + (income - BASE3) * RATE4;
    }
    else if (income <= BASE5)
    {
        tax = BASE1 * RATE1 + (BASE2 - BASE1) * RATE2 + (BASE3 - BASE2) * RATE3 + (BASE4 - BASE3) * RATE4 + (income - BASE4) * RATE5;
    }
    else if (income <= BASE6)
    {
        tax = BASE1 * RATE1 + (BASE2 - BASE1) * RATE2 + (BASE3 - BASE2) * RATE3 + (BASE4 - BASE3) * RATE4 + (BASE5 - BASE4) * RATE5 + (income - BASE5) * RATE6;
    }
    else
    {
        tax = BASE1 * RATE1 + (BASE2 - BASE1) * RATE2 + (BASE3 - BASE2) * RATE3 + (BASE4 - BASE3) * RATE4 + (BASE5 - BASE4) * RATE5 + (BASE6 - BASE5) * RATE6 + (income - BASE6) * RATE7;
    }
    printf("Tax is = %.1f\n", tax);
    printf("Your inocme after tax deduction = %.1f\n", income - tax);
    return 0;
}

 

 

 

7.5 else 와 if 짝짓기

#include <stdio.h>

int main(){
    int number;
    scanf("%d", &number);
    
    if (number == 1)
        printf("One\n");
    else
        if (number == 2)
            printf("Two\n");
        else
            if (number == 3)
                printf("Three\n");
    
    return 0;
}
  • 최소 127 단계의 Nestings 를 지원해 주어야 하는것이 표준

 

#include <stdio.h>

int main(){
    int number;
    scanf("%d", &number);
    
    if (number > 5)
      if (number < 10)
        printf("Larger than 5 smaller than 10\n");
  	else
      printf("Less than or equal to 5");
  
    return 0;
}
  • 3 입력시 아무것도 출력 되지 않음
    • indenting 상으로는 "Less than or equal to 5" 이 출력되어야 하지만, Compiler가 indenting을 무시
    • 중괄호를 사용하여 범위를 지정하는 것이 좋다

 

 


이 글의 모든 사진과 내용의 출처는 홍정모 교수님께 있음을 알려드립니다.

http://blog.naver.com/atelierjpro

 

실리콘 밸리의 프로그래머 : 네이버 블로그

안녕하세요! 홍정모 블로그에 오신 것을 환영합니다. 주로 프로그래밍 관련 메모 용도로 사용합니다. 강의 수강하시는 분들은 홍정모 연구소 카페로 오세요.

blog.naver.com

http://www.inflearn.com/course/following-c

 

홍정모의 따라하며 배우는 C언어 - 인프런 | 강의

'따배씨++'의 성원에 힘입어 새롭게 개발된 C 언어로 시작하는 프로그래밍 입문 강의입니다. '따배씨'와 함께 프로그래밍 인생을 업그레이드 해보세요., 따라하며 배우는 C언어 '따배씨++'의 성원

www.inflearn.com

Comments