ERLEDIGT
NEIN
NEIN
ANTWORTEN
1
1
ZUGRIFFE
966
966
EMPFEHLEN
-
15.02.07 21:09 #1
Hallo Tutorianer
Ich habe mal wieder etwas Langweile geschoben und dachte mir, um dem Abhilfe zu schaffen, programmierst einen Wechselgeldrechner.
Das ist mir auch gelungen, nur bei manchen Eingaben kommt er nicht klar, z.B.
Kosten: 3.89
Bezahlt: 4
Rückgeld 0.11
Ausgabe: 1 * 0.1
Aber den einen Cent möchte er mir nicht geben, ich habe im Debugger nachgeschaut, der Compiler oder wer auch immer meint 0.11 - (1 * 0.10) = 0.009999..
Auch bei manchen Sachen: floor(0.1/0.1) = 0
Ich denke mal das liegt an der precision aber ich weis nicht wie ich es beheben kann.
Ich habe da so eine Ahnung das ich es unnötig kompliziert gemacht habe, wenn wer weiß wie es viel einfacher geht dann bin ich dem nicht abgeneigt.Code cpp:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include <vcl.h> #include <iostream.h> #include <math.h> #pragma hdrstop //--------------------------------------------------------------------------- #pragma argsused double* Changer(double* arr,double &foo) { unsigned int high = floor(foo); double low = foo - high; short int up[8] = {500,200,100,50,20,5,2,1}; short int count=0,tmp=0; for (short int i = 0; i < 8; i++) // höherwertiger Teil { tmp = floor(high/up[i]); // Hilfsvar if (tmp > 0) { arr[count] = tmp; arr[count+1] = up[i]; high = high - (tmp * up[i]); count = count + 2; if (high == 0) break; } } double down[6] = {0.5,0.2,0.1,0.05,0.02,0.01}; for (short int i = 0; i < 6; i++) // niederwertiger Teil { tmp = floor(low/down[i]); if (tmp > 0) { arr[count] = tmp; arr[count+1] = down[i]; low = low - (tmp * down[i]); if (low == 0) break; count = count + 2; } } return arr; } int main(int argc, char* argv[]) { double price,paid,diff; cout << "Price to be paid ? (max. 10000 Euro) "; cin >> price; cout << "What you paid ? "; cin >> paid; if (paid > price && paid <= 10000) { diff = paid - price; cout << "The remainder is: " << diff << endl; double rest[20]={0}; double* remaind = Changer(&rest[0],diff); short int i=0; while (remaind[i] != 0) { cout << remaind[i] << " x " << remaind[i+1] << endl; i=i+2; } } else cout << "You are a cheater :D"; cin.get(); cin.get(); return 0; }
DankeVisit shadowmasta
-
18.02.07 13:00 #2
Ok habe es gelöst. Es lag an der Ungenauigkeit des Datentyps double (siehe Bitstruktur usw)
Korrektur:
Zeile 30: tmp = floor(low/down[i] + 0.005)
sprich Rundungen auf 1/1000.
mfgVisit shadowmasta





Zitieren
Login






