header fileは省略します。
このファイルでBMIが最も高い人の名前とその値を出力し、
BMI順に並べてほかのファイルに出力するという課題があります。
Compareなるものをつくってやろうとしましたが、結局誰が一番高かったのかというデータを保存しないといけません。
どういう風に名前を取り出したらいいでしょうか、また
BMI順に並べる方法も教えていただきたいです。
コード /* Read patient data file (patient.dat) into an array of class objects. Calculate BMI (body mass index) of each patient and print on the screen. Check out try yourself part at the end of this code !!! */ #include <iostream> #include <cstdlib> #include <string> #include <sstream> #include <fstream> #include"Patient.h" #include<math.h> using namespace std; int main(){ int num_patients = 4; Patient patients[num_patients]; int bmi[10]; int max=bmi[0]; int patient_counter = 0; // Read file to objet string row, buffer; ifstream my_file ("patient.dat"); if (my_file.is_open()) { while ( getline (my_file,row) ) { stringstream ss(row); // insert row into a stream ss >> buffer; patients[patient_counter].set_name(buffer); ss >> buffer; patients[patient_counter].set_height(buffer); ss >> buffer; patients[patient_counter].set_weight(buffer); patient_counter++; } my_file.close(); } else cout << "Unable to open file"; // print BMIs of people for(int i=0;i<num_patients;i++){ cout << patients[i].get_name() << " " << patients[i].bmi() << endl; } // // cout<<"the highest bmi "<<max<<endl; // cout<<The heaviest person is << // Try yourself: // 0- Separate class interface, implementation, and driver program into files // 1- Find the person with max. BMI // 2- Sort people according to their BMIs and write to another file // 3- Use try, throw, catch for file I/O exceptions }
コード/* * Patient.cpp * * Created on: 2015/11/10 * Author: 礼央 */ #include <iostream> #include <cstdlib> #include <string> #include <sstream> #include <fstream> #include"Patient.h" using namespace std; void Patient::set_name(string name){ this->name = name; } string Patient::get_name() const{ return name; } void Patient::set_height(string height){ this->height = atof(height.c_str()); } void Patient::set_weight(string weight){ this->weight = atof(weight.c_str()); } double Patient::bmi(){ return weight / (height*height); } int Patient::Compare(int a,int b,int c,int d){ int max=a; if(b>a) max=b; if(c>b) max=c; if(d>c) max=d; }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。