Skip to content

Commit

Permalink
Better formula to compute total consumption
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
miska committed May 26, 2015
1 parent d6fd550 commit d8740c9
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions car.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit d8740c9

Please sign in to comment.