some totally unrelated tips...
variables are your friend. Magic numbers (eg, 0.2, 0.18) are difficult to follow later when you try to edit and fix a bug in the code 3 years after you (or worse, someone else) wrote it.
inverted logic makes the conditionals easier to write and follow. that is, >300 else if > 200 else
is easier than the way you wrote it. Its probably possible to avoid the conditions entirely, but lets not do that right now.
the case blocks are virtually identical apart from some magic numbers.
slightly easier to read and follow would be something like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
case 'D':
units = unitsD;
pct1 = 0.1; //better, these could be in a container, even a C array, but baby steps...
pct2 = 0.12 //explain what each on is with a comment, eg "this is the percent of furlongs per fortnight when traveling across antartica"
pct3 = 0.09
break;
case 'C'
units = unitsC;
pct1 = 0.2;
pct2 = 0.22
pct3 = 0.18
if( units > 300)
bill = ((pct1 * 200) + (pct2 * 100) + (pct3 * (units - 300)));
else
... etc
|
what this does is factor out the magic constants and only do the computations in one place.
this is useful because its the same computations for each type. If they were different, eg if C type were instead < 500 else < 1000 else < 1500, you would need to do it the way you have it now or make those values variables as well and manage that too.
all that combined greatly reduces the redundancy and size of your code, making it easier to read.