Skip to content

Instantly share code, notes, and snippets.

@torazuka
Created August 24, 2011 19:55
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/1169019 to your computer and use it in GitHub Desktop.
Save torazuka/1169019 to your computer and use it in GitHub Desktop.
Chapter04_8_drill_PPPC++
#include "../../std_lib_facilities.h"
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;
int count = 0;
double n = 0;
string unit = "x";
double max = 0;
double min = 0;
cout << "値を単位("<< unit_cm << ", " << unit_m << ", " << unit_in
<< ", " << unit_ft << ")つきで入力する。「Ctrl-z」を入力したら終了する。\n";
while(cin >> n >> unit){
if(cin.fail()){
cout << "input error. \n";
break;
}
double tmp = 0;
if(unit == unit_cm){
tmp = n;
}else if(unit == unit_m){
tmp = n * cm_per_m;
cout << n << unit_m << " == ";
}else if(unit == unit_in){
tmp = n * cm_per_in;
cout << n << unit_in << " == ";
}else if(unit == unit_ft){
tmp = n * in_per_ft * cm_per_in;
cout << n << unit_ft << " == ";
}else{
cout << "[ERROR] 値を単位("<< unit_cm << ", " << unit_m << ", " << unit_in
<< ", " << unit_ft << ")つきで入力してください。\n";
return 1;
}
cout << tmp << unit_cm;
// これまでの入力値のうち最大/最小であれば表示
if(tmp < min){
cout << " the smallest so far";
min = tmp;
}
if(max < tmp){
cout << " the largest so far";
max = tmp;
}
cout << '\n';
// 初回の入力を最大/最小の規定値とする。ダサい。どうすればいいのん?
count++;
if(count == 1){
max = tmp;
min = tmp;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment