有人要的
// 2007/5/23 0:12:20-1:29:5
//g++ -pipe -o p7 p7.cpp && p7
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int debug=0;
int main(){
const float standard[]={18.5,24,27,30,35};
const char* name[]={"過輕","正常","過重","輕度肥胖","中度肥胖","重度肥胖"};
const int sc=sizeof standard/sizeof *standard+1;//6
int countP[2]={0},countBMI[2][6/*sc*/]={0}; // [sex][standard]
float sumBMI[2]={0};
ifstream ifs("bmi.dat",ifstream::in);
while(ifs.good()){
int sex=0;
float weight,height;
ifs>>sex>>height>>weight;
if(!sex)continue;
if(sex<1||sex>2){
cout<<"資料錯誤: "<<sex<<" "<<height<<" "<<weight<<endl;
continue;
}
//height/=100;
float BMI=weight/height/height;
--sex;
++countP[sex];
sumBMI[sex]+=BMI;
int i=0;
for(;i<sc-1;i++)
if(BMI<standard[i])break;
if(debug)cout<<(sex+1)<<" "<<height<<" "<<weight<<" "<<BMI<<" "<<i<<endl;
countBMI[sex][i]++;
}
ifs.close();
cout<<endl<<"女性 男性"<<endl
// http://www.cplusplus.com/reference/iostream/ostream/
<<setfill('=')<<setw(50)<<""<<endl;
cout<<"調查人數 "<<countP[0]<<" "<<countP[1]<<endl;
for(int i=0;i<sc;i++)
cout<<name[i]<<" "<<countBMI[0][i]<<" "<<countBMI[1][i]<<endl;
cout<<"平均BMI "<<sumBMI[0]/countP[0]<<" "<<sumBMI[1]/countP[1]<<endl;
return 0;
}
g++ -pipe -o p7 p7.cpp && p7
資料錯誤: 3 1.9 78.6
資料錯誤: 3 1.9 78.6
女性 男性
==================================================
調查人數 8 10
過輕 1 0
正常 5 5
過重 1 2
輕度肥胖 1 1
中度肥胖 0 1
重度肥胖 0 1
平均BMI 22.0327 25.1954