《C Primer Plus》第六版 第十二章第二題 程式設計練習
好的,既然上次已經發了部落格,那就不算萌新了.......
編譯環境:VS 2017 (Community)
執行環境: Win 10 × 64
上題目:
/* 12-2 ---在美國,通常以英里/加侖來計算油耗;在歐洲,以升/100 公里來計算。 下面是程式的一部分,提示使用者選擇計算模式(美製或英制),然後接收資料並計算油耗。 */ //pe12-2b.c //與 pe12-2a.c 一起編譯 #include <stdio.h> #include "pe12-2a.h" int main(void) { int mode; printf("Enter 0 for metric mode, 1 for US mode: "); scanf_s("%d", &mode); while (mode >= 0) { set_mode(mode); get_info(); show_info(); printf("Enter 0 for metric mode, 1 for US mode"); printf(" (-1 to quit): "); scanf_s("%d", &mode); } printf("Done.\n"); return 0; }
下面是一些輸出示例:
Enter 0 for metric mode, 1 for US mode: 0
Enter distance traveled in kilometers: 600
Enter fuel consumed in liters: 78.8
Fuel consumption is 13.13 lirers per 100 km.
Enter 0 for metric mode, 1 for US mode (-1 to quit): 1
Enter distance traveled in miles: 434
Enter fuel consumed in gallons: 12.7
Fuel consumption is 34.2 lirers per gallon.
Enter 0 for metric mode, 1 for US mode (-1 to quit): 3
Invalid mode specified. Mode 1(US) used.
Enter distance traveled in miles: 388
Enter fuel consumed in gallons: 15.3
Fuel consumption is 25.4 lirers per gallon.
Enter 0 for metric mode, 1 for US mode (-1 to quit): -1
Done.
如果使用者輸入了不正確的模式,程式向用戶給出提示訊息並使用上一次輸入正確的模式。
請提供pe12-2a.h標頭檔案和pe12-2a.c原始檔。原始碼檔案應定義3個具有檔案作用域,內部連結的
變數。一個表示模式,一個表示距離,一個表示消耗的燃料。get_info()函式根據使用者輸入的模
式提示使用者輸入相應資料,並將其儲存到檔案作用於變數中。show_info()函式根據設定的模式
計算並顯示油耗。可以假設使用者輸入的都是數值資料。
以上,題目結束!
根據題目做出的3個變數:
- distance 汽車行駛的距離
- double consumption 消耗的燃料
- int mode 使用者設定的模式
以及3個函式:
- void set_mode(int number); //獲取使用者輸入的模式
- void get_info(void); //根據使用者輸入的模式提示使用者輸入資料
- void show_info(void); //根據設定的模式計算並顯示油耗
以及1個附加的(我附加的):
4. double WrongJudge(double number);//處理非法輸入,後期強制int型別轉換
下面是分檔案給出程式碼:
pe12-2a.h:
#pragma once
//函式原型
void set_mode(int number); //獲取使用者輸入的模式
void get_info(void); //根據使用者輸入的模式提示使用者輸入資料
void show_info(void); //根據設定的模式計算並顯示油耗
double WrongJudge(double number); //處理非法輸入,後期強制int型別轉換
pe12-2a.c:
//pe12-2a
//與 pe12-2b 一起編譯
#include <stdio.h>
#include "pe12-2a.h"
int distance; //汽車行駛的距離
double consumption; //消耗的燃料
int mode; //使用者設定的模式
//獲取使用者輸入的模式
void set_mode(int number)
{
if (number >= 0 && number <= 1)
{
mode = number;
}
else if (mode == 0) //按照先前的選項進行運算
{
printf("Invalid mode specified. Mode 0(USA) used.\n");
}
else if (mode == 1)
{
printf("Invalid mode specified. Mode 1(US) used.\n");
}
}
//根據使用者輸入的模式提示使用者輸入資料
void get_info(void)
{
if (mode == 0)
{
//提示美製的輸入 獲取行程(千米)
printf("Enter distance traveled in kilometers: ");
distance = (int)WrongJudge((double) distance);
//提示消耗的燃料輸入
printf("Enter fuel consumed in liters: ");
consumption = WrongJudge(consumption);
}
else if (mode == 1)
{
//提示英制的輸入 獲取行程(英里)
printf("Enter distance traveled in miles: ");
distance = (int)WrongJudge((double)distance);
//提示消耗的燃料輸入
printf("Enter fuel consumed in gallons: ");
consumption = WrongJudge(consumption);
}
}
//根據設定的模式計算並顯示油耗
void show_info(void)
{
if (mode == 0)
{
printf("Fuel consumption is %.2f lirers per 100 km.\n",
100 * (consumption / distance));
}
else if (mode == 1)
{
printf("Fuel consumption is %.1f lirers per gallon.\n",
(distance / consumption));
}
}
//處理非法輸入,後期強制int型別轉換
double WrongJudge(double number)
{
while (scanf_s("%lf", &number) != 1) //處理非法輸入
{
printf("That's not a value number, try again: ");
continue;
}
return number;
}
pe12-2b.c:
//pe12-2b.c
//與 pe12-2a.c 一起編譯
#include <stdio.h>
#include "pe12-2a.h"
int main(void)
{
int mode;
printf("Enter 0 for metric mode, 1 for US mode: ");
scanf_s("%d", &mode);
while (mode >= 0)
{
set_mode(mode);
get_info();
show_info();
printf("Enter 0 for metric mode, 1 for US mode");
printf(" (-1 to quit): ");
scanf_s("%d", &mode);
}
printf("Done.\n");
return 0;
}
好了,讓我們來看看實際的效果:
看吧,和題目上的一模一樣,非常好!
原始碼我都已經發出來了,以上
Alex Mercer (boy) 鳴謝!