質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

2回答

1145閲覧

患者ファイル

reotantan

総合スコア295

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2015/11/17 23:42

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; }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

こんにちは。

どういう風に名前を取り出したらいいでしょうか、

名前の取出し方はPatientクラスの定義をよく見て下さい。既に関数が用意されているようですよ。

BMI順に並べる方法も教えていただきたいです。

C++なら、この場合は標準ライブラリのstd::sort()を使うことができます。

ただ、使い方が結構難しいです。
ここをよく読んでみて下さい。
(私も良くお世話になるサイトです。丁寧なので分かりやすいです。)

投稿2015/11/18 01:03

Chironian

総合スコア23272

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

わたしはDB屋なんで、テーブルからデータを読み込む時にSELECT文にORDER BY句を付けてsortしちゃいますが、
C++ sort
でググるといっぱい見つかりますので参考にされては?

投稿2015/11/18 00:18

Orlofsky

総合スコア16415

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問