C 語言 講義 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 A B C
程式設計大法: 第二層 選擇型計算型程式
構成要件: 1. 選擇型(單選、二選一、多選一)
2. 輸入資料 -> 選擇(+計算) -> 輸出結果
題1: 週年慶, 全館打9折, 滿5000改打8折 (輸入購物總金額, 顯示折扣後的金額)
題2: 計算絕對值 (輸入實數數字, 顯示絕對值)
題3: 計算BMI值, 計算公式: BMI = 體重(公斤) / 身高^2(公尺^2), 並判斷:
1)過輕 BMI<18.5,
2)標準 18.5 <= BMI <24,
3)過重 BMI>=24
程式: 週年慶, 全館打9折, 滿5000改打8折 (輸入購物總金額, 顯示折扣後的金額)
#include<stdio.h>
int main()
{
int money, total; // money代表購物金額, f代表折扣後的金額
printf("Input money? ");
scanf("%d",&money);
if(money > 5000) total = (int) ( (float)money * 0.8 );
else total = (int) ( (float)money * 0.9 );
printf("total : %d\n",total);
}
執行方法: 直接執行
#include<stdio.h>
int main()
{
float x, y;
printf("Input a real number? ");
scanf("%f",&x);
y=x;
if(x < 0) y = -y;
printf("%f 的絕對值為 %f\n",x,y);
}
執行方法: 直接執行
#include<stdio.h>
int main()
{
float h, w, bmi;
printf("Input weight (unit : kg)? ");
scanf("%f",&w);
printf("Input height (unit : meter)? ");
scanf("%f",&h);
bmi = w / ( h*h );
printf("BMI 為 %f, ",bmi);
if(bmi < 18.5) printf("過輕\n");
else if(18.5 <= bmi && bmi < 24) printf("標準\n");
else printf("過重\n");
}
執行方法: 直接執行