From d8740c9808d595a1db24b61ef6aecfef154a7029 Mon Sep 17 00:00:00 2001 From: Michal Hrusecky Date: Tue, 26 May 2015 18:42:45 +0200 Subject: [PATCH] Better formula to compute total consumption People might not start with full tank and last one might not be full either. So counting consumption from the first full tank till the last one. --- car.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/car.cpp b/car.cpp index 55d7310..c8023d3 100644 --- a/car.cpp +++ b/car.cpp @@ -359,24 +359,24 @@ double Car::consumption() const unsigned long int maxDistance = 0; unsigned long int minDistance = 999999999; double totalConsumption = 0; + double partConsumption = 0; foreach(Tank *tank, _tanklist) { - if(tank->distance() > maxDistance) - maxDistance = tank->distance(); - if(tank->distance() < minDistance) - minDistance = tank->distance(); - totalConsumption += tank->quantity(); - } - if(maxDistance == 0) return 0; - foreach(Tank *tank, _tanklist) - { - if(tank->distance() == minDistance) + if(tank->full()) { - totalConsumption -= tank->quantity(); - break; + totalConsumption += partConsumption; + partConsumption = 0; + if(tank->distance() > maxDistance) + maxDistance = tank->distance(); + if(tank->distance() < minDistance) + minDistance = tank->distance(); } + if(maxDistance > 0) + partConsumption += tank->quantity(); + } + if((maxDistance == 0) || (maxDistance == minDistance)) return 0; return totalConsumption / ((maxDistance - minDistance)/ 100.0); }