본문 바로가기
C and C++/C and C++ Examples

[C Examples] 시간대별 통화요금 계산기 예제코드: if, else, char

by Henry Cho 2023. 2. 20.
728x90

시간대별 통화요금 계산기 예제코드: if, else, char

포스트 난이도: HOO_Junior


# Example Codes

 

#include <stdio.h>
#include <stdlib.h>

int main()
{

    int min, dayOrNight;
    float rCharge, pCharge;
    char serviceCode;
    
    printf("The rates are computed as follows: Regular service: $10.00 plus first 50 minutes are free.\n");
    printf("Charges for over 50 minutes are $0.20 per minute, Premium service: $25.00 plus. \n");
    printf("Enter your service code:");
    scanf("%c",&serviceCode);
    printf("Enter your number of minutes for using service:");
    scanf("%d", &min);
    
    if(serviceCode == 'r' || serviceCode == 'R'){
        if(min>50){
            rCharge = (min-50)*0.20 +10.00;
            printf("Charge: $%.2f \n", rCharge);
        }
        else{
            rCharge=10.00;
            printf("Charge: $%.2f\n", rCharge);
        }
    }
    else if(serviceCode=='p'|| serviceCode=='P'){
        printf("Are you using the service day(1) or night(2):");
        scanf("%d",&dayOrNight);
        
        if(dayOrNight==1)
        {
            if(min>75){
            pCharge = (min-75)*0.1 + 25;
            printf("Charge: $%.2f\n", pCharge);
            }
            else{
            pCharge = 25;
            printf("Charge: $%.2f\n", pCharge); 
            }
        }
        else if (dayOrNight==2){
            if(min>100){
            pCharge = (min-100)*0.05 + 25;
            printf("Charge: $%.2f\n", pCharge);
            }
            else{
            pCharge = 25;
            printf("Charge: $%.2f\n", pCharge); 
            }
        }
        else{
            printf("You typed the wrong day or night, please try it again.\n");
        }
    }
    else
    printf("You typed the wrong code, please try it again.\n");
    printf("Coded by HOO.\n");
    return 0;
}

The rates are computed as follows: Regular service: $10.00 plus first 50 minutes are free.
Charges for over 50 minutes are $0.20 per minute, Premium service: $25.00 plus. 
Enter your service code:r
Enter your number of minutes for using service:300
Charge: $60.00 
Coded by HOO.

The rates are computed as follows: Regular service: $10.00 plus first 50 minutes are free.
Charges for over 50 minutes are $0.20 per minute, Premium service: $25.00 plus. 
Enter your service code:p
Enter your number of minutes for using service:95
Are you using the service day(1) or night(2):1
Charge: $27.00
Coded by HOO.

 

728x90

댓글