C語言 - 頭文件使用案例


源代碼分門別類管理,通過頭文件。

放置一些函數聲明,變量聲明,常量定義,宏定義。

hotel.h

#ifndef HOTEL_H_INCLUDED
#define HOTEL_H_INCLUDED

#define HOTEL1 872.0 // 各家酒店的默認房費
#define HOTEL2 1838.0 // 各家酒店的默認房費
#define HOTEL3 789.0 // 各家酒店的默認房費
#define HOTEL4 1658.0 // 各家酒店的默認房費
#define DISCOUNT 0.95 // 折扣率


// 菜單函數:顯示菜單選項,接收並返回用戶的輸入
int Menu(void);

// 返回用戶預訂的天數
int GetNights(void);

// 根據入住的天數顯示最終需要支付的金額
double ShowPrice(int choice,int nights);
#endif // HOTEL_H_INCLUDED

hotel.c

#include <stdio.h>
// 自定義的頭文件用雙引號
#include "hotel.h"

char hotelNames[4][50] = {
    "貝羅酒店","香榭麗舍酒店","阿斯圖里亞斯酒店","斯克里布酒店"
};

int Menu(void) {
    int choice; // 用戶的選擇
    int i;
    printf("請選擇入住的酒店:\n");
    for (i = 0; i< 4;i++) {
        printf("%d、%s\n",i+1,hotelNames[i]); // 寫完就去main中測試一下
    }

    printf("5、退出程序\n");

    printf("請輸入您的選擇:");

    int result = scanf("%d",&choice);

    // 判斷是否合法
    while ( result !=1 || choice < 1 || choice >5 ) {
        if (result != 1) {
            scanf("%*s"); // 消除錯誤的輸入
            // fflush(stdin);
        }

        printf("必須輸入1-5之間的整數:");
        result = scanf("%d",&choice);
    }



    return choice;
}

int GetNights(void) {
    int nights;

    printf("先生、女士,請輸入要入住的天數:");

    int result = scanf("%d",&nights);

    // 判斷是否合法
    while ( result !=1 || nights < 1) {
        if (result != 1) {
            scanf("%*s"); // 消除錯誤的輸入
            // fflush(stdin);
        }

        printf("必須輸入大於1的整數!\n");
        printf("先生、女士,請輸入要入住的天數:");
        result = scanf("%d",&nights);
    }

    return nights;
}

double ShowPrice(int choice,int nights) {
    double hotelPrice;
    double totalPrice = 0;
    if (choice == 1) {
        hotelPrice = HOTEL1;
    }

    if (choice == 2) {
        hotelPrice = HOTEL2;
    }

    if (choice == 3) {
        hotelPrice = HOTEL3;
    }

    if (choice == 4) {
        hotelPrice = HOTEL4;
    }

    int i;
    for (i = 0 ;i<nights ;i ++ ) {
        if (i == 0) {
            totalPrice = hotelPrice * DISCOUNT;
        } else {
            totalPrice += hotelPrice * DISCOUNT;
        }

        hotelPrice = hotelPrice * DISCOUNT;
    }

    return totalPrice;

}

main.c

#include <stdio.h>
#include <stdlib.h>
#include "hotel.h"  // 最好引入一下頭文件
extern char hotelNames[4][50]; // 聲明為外部變量

int main()
{
    int choice;
    int nights;
    double totalPrice;
    // 用戶輸入入住的酒店和天數,程序計算出對應的金額
    // 1.顯示菜單 - 封裝成函數
    choice = Menu();

    if (choice > 0 && choice <5) {
        printf("當前用戶選擇的是:%s\n",hotelNames[choice-1]); // 多遇到一些錯誤,在錯誤中成長。將順序思維,改為模塊思維。
    }

    if (choice == 5) {
        printf("歡迎使用本系統,再見。\n");
        exit(0);
    }


    nights = GetNights();
    if (nights > 0) {
        printf("當前用戶選擇入住%d天\n",nights); // 多遇到一些錯誤,在錯誤中成長。將順序思維,改為模塊思維。
    }

    // 2.計算過程
    totalPrice = ShowPrice(choice,nights);
    printf("您入住的酒店是:%s \t 入住天數: %d \t 總費用: %0.2f \n",hotelNames[choice-1],nights,totalPrice);

    printf("歡迎使用本系統,再見。\n");
    return 0;
}

頭文件有約束作用。可以重復使用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM