ホテルルーム料金プログラム(C言語)

2255 ワード

CodeBlocks 17.12でテスト済み


hotel.h
#ifndef HOTEL_H_INCLUDED
#define HOTEL_H_INCLUDED

#define QUIT  5
#define HOTEL1 80.00
#define HOTEL2 125.00
#define HOTEL3 155.00
#define HOTEL4 200.00
#define DISCOUNT 0.95
#define STARS "*******************************"

// 
int menu(void);

// 
int getnights(void);

// 
void showprice(double, int);

#endif // HOTEL_H_INCLUDED

hotel.c
#include 
#include "hotel.h"

int menu(void)
{
    int code, status;

    printf("
%s%s
", STARS, STARS); printf(" :
"); printf("1) 2)
"); printf("3) 4)
"); printf("5)
"); printf("%s%s
", STARS, STARS); while((status = scanf("%d", &code)) != 1 || (code < 1 || code > 5)) { if(status != 1) scanf("%*s"); printf(" 1 5
"); } return code; } int getnights(void) { int nights; printf(" ?
"); while(scanf("%d", &nights) != 1) { scanf("%*s"); printf(" , :2.
"); } return nights; } void showprice(double rate, int nights) { int n; double total = 0.0; double factor = 1.0; for(n = 1; n <= nights; n++, factor *= DISCOUNT) total += rate * factor; printf(" %0.2f 。
", total); }

usehotel.c
#include 
#include "hotel.h"

int main(void)
{
    int nights;
    double hotel_rate;
    int code;

    while((code = menu())!= QUIT)
    {
        switch(code)
        {
            case 1: hotel_rate = HOTEL1;
                break;
            case 2: hotel_rate = HOTEL2;
                break;
            case 3: hotel_rate = HOTEL3;
                break;
            case 4: hotel_rate = HOTEL4;
                break;
            default:hotel_rate = 0.0;
                printf("Oops!
"); break; } nights = getnights(); showprice(hotel_rate, nights); } printf(" , 。
"); system("pause"); return 0; }