C++尺寸大小計算

苦瓜黃瓜金銀花發表於2020-12-12

英文題目(老師給的原版題目):

Hat size=weight in pounds divided by height in inches and all that multiplied by 2.9。
Jacket size(chest in inches)=height times weight divided by 288 and then adjusted by adding 1/8 of an inch for each 10 years over age 30.(Note that the adjustment only takes place after a full 10 years.So,there is no adjustment for ages 30 through 39,but 1/8 of an inch is added for age 40.)
Waist in inches=weight divided by 5.7 and then adjusted by adding 1/10 of an inch for each 2 years over age 28.(Note that the adjustment only takes place after a full 2 years.So,there is no adjustment for age 29,but 1/10 of an inch is added for age 30.)
Use functions for each calculation.Your program should allow the user to repeat this calculation as often as the user wishes.

中文題目(簡單翻譯了一下):

帽子大小=體重(磅)除以身高(英寸)所有這些乘以2.9。
夾克尺寸(胸圍英寸)=身高乘以體重除以288,然後對30歲以上的每10歲增加1/8英寸進行調整。(請注意,調整隻在整整10年之後進行。因此,30到39歲的年齡沒有調整,但40歲的年齡會增加1/8英寸。)
腰圍(英寸)=體重除以5.7,然後對28歲以上的每2歲增加1/10英寸進行調整。(請注意,調整隻在整整兩年之後進行。因此,29歲的年齡沒有調整,但30歲的年齡增加了1/10英寸。)
對每個計算使用函式。您的程式應該允許使用者按照自己的意願重複這個計算。

程式碼:

#include <iostream>
#include <stdlib.h>
using namespace std;

float HatSize(float Weight,float Height){
    float size,temp;
    temp=Weight/Height;
    size=temp*2.9;
    return size;
}

float JacketSize(float Weight,float Height,int Age){
    float temp,size;
    temp=Height*Weight;
    size=temp/288;
    if(Age<=30){
        return size;
    }
    else{
        int temp1;
        float temp2,temp3;
        temp1=Age-30;
        temp2=temp1/10;
        temp3=temp2/8;
        size+=temp3;
        return size;
    }
}

float WaistSize(float Weight,int Age){
    float size;
    size=Weight/5.7;
    if(Age<=28){
        return size;
    }
    else{
        int temp1;
        float temp2,temp3;
        temp1=Age-28;
        temp2=temp1/2;
        temp3=temp2/10;
        size+=temp3;
        return size;
    }
}

int main() {
    float height,weight,hatsize,jacketsize,waistsize;
    int age;
    char ch;
    do{
        cout<<"請輸入身高(英寸):"<<endl;
        cin>>height;
        cout<<"請輸入體重(磅):"<<endl;
        cin>>weight;
        cout<<"請輸入年齡:"<<endl;
        cin>>age;
        hatsize=HatSize(weight, height);
        jacketsize=JacketSize(weight, height, age);
        waistsize=WaistSize(weight, age);
        cout<<"你的帽子尺寸是:"<<hatsize<<"英寸"<<endl;
        cout<<"你的夾克尺寸是:"<<jacketsize<<"英寸"<<endl;
        cout<<"你的腰圍是:"<<waistsize<<"英寸"<<endl;
        cout<<"是否繼續查詢?(y 或 n)"<<endl;
        cin>>ch;
        if(ch!='y' && ch!='n'){
            cout<<"輸入錯誤,程式退出!"<<endl;
            exit(0);
        }
    }while(ch!='n');
    cout<<"退出成功!"<<endl;
    return 0;
}

比較基礎的C++題目,適合初學者。

相關文章