Minimale Geld stückelung



  • Schreiben Sie ein Programm, dass für einen benutzerdefinierten Geldbetrag die minimale Stückelung in Banknoten und -münzen errechnet.

    Und wie findet ihrs?

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	unsigned euro = 0;
    	unsigned cent = 0;
    
    	cout << "Euro Anteil: ";
    	cin >> euro;
    
    	cout << "Cent Anteil: ";
    	cin >> cent;
    
    	euro += cent / 100;
    	cent %= 100;
    
        if (!euro && !cent)
        {
            cout << "Sie armer Schlucker!" << endl;
            return 0;
        }
    
    	if (euro >= 500)
    	{
    		cout << euro / 500 << "x500EURO";
    		euro %= 500;
    		if (euro || cent)
    		{
    			cout << ", ";
    		}
    	}
    	if (euro >= 200)
    	{
    		cout << euro / 200 << "x200EURO";
    		euro %= 200;
    		if (euro || cent)
    		{
    			cout << ", ";
    		}
    	}
    	if (euro >= 100)
    	{
    		cout << euro / 100 << "x100EURO";
    		euro %= 100;
    		if (euro || cent)
    		{
    			cout << ", ";
    		}
    	}
    	if (euro >= 50)
    	{
    		cout << euro / 50 << "x50EURO";
    		euro %= 50;
    		if (euro || cent)
    		{
    			cout << ", ";
    		}
    	}
    	if (euro >= 20)
    	{
    		cout << euro / 20 << "x20EURO";
    		euro %= 20;
    		if (euro || cent)
    		{
    			cout << ", ";
    		}
    	}
    	if (euro >= 10)
    	{
    		cout << euro / 10 << "x10EURO";
    		euro %= 10;
    		if (euro || cent)
    		{
    			cout << ", ";
    		}
    	}
    	if (euro >= 5)
    	{
    		cout << euro / 5 << "x5EURO";
    		euro %= 5;
    		if (euro || cent)
    		{
    			cout << ", ";
    		}
    	}
    	if (euro >= 2)
    	{
    		cout << euro / 2 << "x2EURO";
    		euro %= 2;
    		if (euro || cent)
    		{
    			cout << ", " << euro << "x1EURO";
    			if (cent)
    			{
    				cout <<  ", ";
    			}
    		}
    	}
    	if (cent >= 50)
    	{
    		cout << cent / 50 << "x50CENT";
    		cent %= 50;
    		if (cent)
    		{
    			cout << ", ";
    		}
    	}
    	if (cent >= 20)
    	{
    		cout << cent / 20 << "x20CENT";
    		cent %= 20;
    		if (cent)
    		{
    			cout << ", ";
    		}
    	}
    	if (cent >= 10)
    	{
    		cout << cent / 10 << "x10CENT";
    		cent %= 10;
    		if (cent)
    		{
    			cout << ", ";
    		}
    	}
    	if (cent >= 5)
    	{
    		cout << cent / 5 << "x5CENT";
    		cent %= 5;
    		if (cent)
    		{
    			cout << ", ";
    		}
    	}
    	if (cent >= 2)
    	{
    		cout << cent / 2 << "x2CENT";
    		cent %= 2;
    		if (cent)
    		{
    			cout << cent << "x1CENT";
    		}
    	}
    
    	cout << '.' << endl;
    
    	return 0;
    }
    


  • Ist ziemlich viel Wiederholung, schreit nach ner Funktion 😉



  • Schonmal ganz gut. Wie du bestimmt gemerkt hat, kommen in deinem Program oft dieselben paar Zeilen Code vor. Könntest du dir vorstellen, wie man diese Redundanz reduzieren könnte?



  • Irgendwie muss ich gerade an den switch-case Thread hier denken 🤡



  • #include <iostream>
    
    using namespace std;
    
    size_t euronen[]= {500,200,100,50,20,10,5,2,1};
    size_t cents[]	= {50,20,10,5,2,1};
    
    #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
    
    int main()
    {
        size_t i;
        unsigned euro = 0;
        unsigned cent = 0;
    
        cout << "Euro Anteil: ";
        cin >> euro;
    
        cout << "Cent Anteil: ";
        cin >> cent;
    
        euro += cent / 100;
        cent %= 100;
    
        if (!euro && !cent)
        {
            cout << "Sie armer Schlucker!" << endl;
            return 0;
        }
    
        for(i = 0;i < ARRAY_SIZE(euronen);i++)
        {
            if(euro >= euronen[i])
            {
                cout << euro / euronen[i] << " x " << euronen[i] << " EURO\n";
                euro %= euronen[i];
            }
            if(!euro)
                break;
        }
    
        for(i = 0;i < ARRAY_SIZE(cents);i++)
        {
            if(cent >= cents[i])
            {
                cout << cent / cents[i] << " x " << cents[i] << " CENT\n";
                cent %= cents[i];
            }
            if(!cent)
                break;
        }
    
        return 0;
    }
    

    Disclaimer: Schnell dahingeschrieben und kann Spuren von C-Paradigmen beinhalten.


  • Mod

    cpp_Jungspund schrieb:

    Irgendwie muss ich gerade an den switch-case Thread hier denken 🤡

    Und, wie schon dort gesagt, wäre switch hier entweder unnötig, oder ein bewusstes Zugeständnis an anfängerhaften Code. Siehe dachschadens Lösung, für einen Hinweis, wie eine bessere Richtung aussehen könnte.

    PS: Was würdest du hier den überhaupt switchen? Willst du das for-switch Idiom benutzen?



  • Witzig, ich hatte fast den gleichen Code (als Schleifen), nur dass die Arrays nicht global waren, und dass ich die (wenn 0 dann break) Optimierung vergessen hatte (Ist ja schon mal nur 1/3 so lang, inkl. konstanter Länge von Einlesen etc.

    // nicht getestet
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        const unsigned N_EURO_ARTEN = 9;
        const unsigned N_CENT_ARTEN = 6;
    
        unsigned euroArten[N_EURO_ARTEN] = {
            500, 200, 100, 50, 20, 10, 5, 2, 1
        };
    
        unsigned centArten[N_CENT_ARTEN] = {
            50, 20, 10, 5, 2, 1
        };
    
        unsigned euro = 0;
        unsigned cent = 0;
    
        cout << "Euro Anteil: ";
        cin >> euro;
    
        cout << "Cent Anteil: ";
        cin >> cent;
    
        euro += cent / 100;
        cent %= 100;
    
        for (unsigned i = 0; i != N_EURO_ARTEN; ++i)
        {
            if (euro >= euroArten[i])
            {
                cout << euro / euroArten[i] << 'x' << euroArten[i] << "EURO";
                euro %= euroArten[i];
                if ((euro && i != N_EURO_ARTEN - 1) || cent)
                {
                    cout << ", ";
                }
            }
        }
    
        for (unsigned i = 0; i != N_CENT_ARTEN; ++i)
        {
            if (cent >= centArten[i])
            {
                cout << cent / centArten[i] << 'x' << centArten[i] << "CENT";
                cent %= centArten[i];
                if (cent && i != N_CENT_ARTEN - 1)
                {
                    cout << ", ";
                }
            }
        }
    
        cout << '.' << endl;
    
        return 0;
    }
    

    Das ARRAY_SIZE Makro ist ja schon sexy, man muss nich immer die Länge mitgeben und 0 size array geht ja nicht (ist jetzt bezogen auf C).

    Aber dann hat der grundlegende Ansatz ja auf jeden Fall gepasst.

    LG

    P.S.: Würden euch auch die Haare zu Berge stehen, wenn ihr in der Vorlesung sitzt und hört die ganze Zeit "Deklaration, Deklaration, Deklaration"

    int main()
    {
        double foobar;  // <-- DEFINITION!!!
    }
    


  • HarteWare schrieb:

    ... man muss nich immer die Länge mitgeben

    Was hältst Du denn dann hiervon:

    ...
        unsigned euroArten[N_EURO_ARTEN] = {
            500, 200, 100, 50, 20, 10, 5, 2, 1
            , 0 //ungültiger Wert als Grenze
        };
    ...
    
    for(int i = 0; euroArten[i]; ++i)
    {
       ...
    }
    
    ...
    


  • Belli schrieb:

    Was hältst Du denn dann hiervon:

    Inband-Signaling ist hässlich.


  • Mod

    HarteWare schrieb:

    man muss nich immer die Länge mitgeben

    stimmt, dafür gibts die neue for-Syntax. Und zwei Schleifen sind ja eigentlich eine zuviel

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        unsigned long long euro = 0;
        unsigned long long cent = 0;
    
        cout << "Euro Anteil: ";
        cin >> euro;
    
        cout << "Cent Anteil: ";
        cin >> cent;
    
        cent += 100 * euro;
    
        auto separator = "";
        for ( auto i : { 50000, 20000, 10000, 5000, 2000, 1000, 500, 200, 100, 50, 20, 10, 5, 2, 1 } )
        {
            if ( cent >= i )
            {
                cout << separator << cent / i << " x " << ( i >= 100 ? i / 100 : i ) << ( i >= 100 ? " EURO" : " CENT" );
                cent %= i;
                separator = ", ";
            }
        }
    
        cout << ".\n";
    }
    


  • @Belli,

    ich glaube man kann sich im Allgemeinen nicht darauf verlassen, dass 0 in einem Array kein gültiger Wert ist.

    @camper

    find ich eigentlich ne coole Idee, gleich alles in Cent zu machen. Es wird auch das ursprüngliche Ausgabe-Format beachtet.



  • Man muss sich nicht immer im Allgemeinen auf Dinge verlassen können, oft reicht auch im Speziellen.



  • HarteWare schrieb:

    @Belli,

    ich glaube man kann sich im Allgemeinen nicht darauf verlassen, dass 0 in einem Array kein gültiger Wert ist.

    Selbstverständlich nicht! Das war ein Vorschlag für das konkrete Problem.


Anmelden zum Antworten