Skip to content

Instantly share code, notes, and snippets.

@torazuka
Created August 25, 2011 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torazuka/1172140 to your computer and use it in GitHub Desktop.
Save torazuka/1172140 to your computer and use it in GitHub Desktop.
Chapter04_10_drill_PPPC++
#include "../../std_lib_facilities.h"
// 入力された値をm似変換して保持する。最後に一覧を出す。
int main(){
const string unit_cm = "cm";
const string unit_m = "m";
const string unit_in = "in";
const string unit_ft = "ft";
const double cm_per_m = 100;
const double cm_per_in = 2.54;
const double in_per_ft = 12;
double input = 0;
string unit = "x";
double max = 0;
double min = 0;
double sum = 0;
int count = 0;
vector<double> input_datas;
cout << "値を単位("<< unit_cm << ", " << unit_m << ", " << unit_in
<< ", " << unit_ft << ")つきで入力する。「Ctrl-z」を入力したら終了する。\n";
while(cin >> input){
if(cin.fail()){
cout << "input error. \n";
break;
}
cin >> unit;
if(cin.fail()){
cout << "input error. \n";
break;
}
double tmp = 0;
if(unit == unit_cm){
tmp = input;
}else if(unit == unit_m){
tmp = input * cm_per_m;
}else if(unit == unit_in){
tmp = input * cm_per_in;
}else if(unit == unit_ft){
tmp = input * in_per_ft * cm_per_in;
}else{
cout << "[ERROR] 値を単位("<< unit_cm << ", " << unit_m << ", " << unit_in
<< ", " << unit_ft << ")つきで入力してください。\n";
return 1;
}
// これまでの入力値のうち最大/最小であれば表示
if(tmp < min){
cout << " the smallest so far";
min = tmp;
}
if(max < tmp){
cout << " the largest so far";
max = tmp;
}
cout << '\n';
// 合計の計上
sum += tmp / cm_per_m;
input_datas.push_back(tmp / cm_per_m);
// 初回の入力を最大/最小の規定値とする。
count++;
if(count == 1){
max = tmp;
min = tmp;
}
}
cout << "最大値: " << max / cm_per_m << "m" << endl;
cout << "最小値: " << min / cm_per_m << "m" << endl;
cout << "合計: " << sum << "m" << endl;
cout << "入力された値の数: " << count << endl;
cout << "入力値の一覧: ";
for(int i = 0; i < input_datas.size(); i++){
cout << ' ' << input_datas[i] << 'm';
}
cout << '\n';
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment