lpk.cpp

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

Generated on Sun Nov 30 09:53:22 2008 for Exploring C++ - Final Forms of Key Examples by  doxygen 1.5.3