00001 00003 #include <iostream> 00004 #include <istream> 00005 #include <ostream> 00006 #include <sstream> 00007 #include <string> 00008 00009 int main() 00010 { 00011 double prev_odometer(0.0), odometer(0.0); 00012 double total_fuel(0.0); 00013 std::string line; 00014 int linenum(0); 00015 bool error(false); 00016 while (std::getline(std::cin, line)) 00017 { 00018 ++linenum; 00019 std::string::size_type comment(line.find('#')); 00020 if (comment != std::string::npos) 00021 line.erase(comment); 00022 std::istringstream input(line); 00023 double fuel; 00024 if (input >> odometer) 00025 { 00026 char check; 00027 if (not (input >> fuel)) 00028 { 00029 std::cerr << "Missing fuel consumption on line " << linenum << '\n'; 00030 error = true; 00031 } 00032 else if (input >> check) 00033 { 00034 std::cerr << "Extra text on line " << linenum << '\n'; 00035 error = true; 00036 } 00037 else if (fuel != 0) 00038 { 00039 double distance(odometer - prev_odometer); 00040 std::cout << distance / fuel << '\n'; 00041 total_fuel += fuel; 00042 prev_odometer = odometer; 00043 } 00044 } 00045 } 00046 if (total_fuel != 0) 00047 { 00048 std::cout << "Net MPG=" << odometer / total_fuel; 00049 if (error) 00050 std::cout << " (estimated, due to input error)"; 00051 std::cout << '\n'; 00052 } 00053 }