Online-Skater
Erfahrenes Mitglied
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.
Danke

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.
C++:
#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;
}
Danke