// SS07, Aufg. 23
// Compilieren+Linken
// g++ -Wall -pedantic bsp_23.cpp
// Dokumentation
// kdoc -p -d bsp_23 bsp_23.cpp
// kdoc -p -d tex_bsp_23 -f latex bsp_23.cpp
#include
#include // setw
#include // assert
using namespace std;
// ####################################################################
// Deklaration der Funktionen (gehoert in das Headerfile)
// ####################################################################
/**
Calculates the number of coins for each value such that
the price is represented.
@param price given price to be represented
@param coins resulting number of different coins
@param values decending currency values of the different coins
@param n given number of different coins
*/
void num_coins(const int& price, int coins[], const int values[], const int n);
/**
Prints the number of coins for each value such that
the price is represented.
@param coins number of different coins
@param values decending currency values of the different coins
@param n number of different coins
*/
void print_coins(const int coins[], const int values[], const int n);
/**
Prints the number of coins for each value such that
the price is represented.
@param coins number of different coins
@param weights weights of the different coins
@param n number of different coins
@return accumulated weight of the coins in the purse
*/
float weight_coins(const int coins[], const float weights[], const int n);
// ####################################################################
// Hauptprogramm
// ####################################################################
int main(const int argc, const char* argv[])
{
float kp, gm; // Kaufpreis und Geldmenge des Kunden
cout << "Kaufpreis = "; cin >> kp;
cout << "Geldmenge = "; cin >> gm;
// Umrechnung des Geldbetrages in Cent
const int preis = static_cast(round(kp*100)), // Preis
geld = static_cast(round(gm*100)); // Geld
// Muenzwerte in Cent
const int wert[] = {200, 100, 50, 20, 10, 5, 2, 1};
// Anzahl der verschiedenen Muenzwerte
const int nwerte = sizeof(wert)/sizeof(wert[0]);
// Muenzgewichte in Gramm
const float gewicht[nwerte] = {20.3, 10.3, 5.2, 2.1, 1.1, 0.52, 0.25, 0.18};
int muenzen[nwerte]={}; // mit 0 initialisieren
num_coins( geld-preis, muenzen, wert, nwerte );
print_coins( muenzen, wert, nwerte );
double masse = weight_coins(muenzen, gewicht, nwerte);
cout << endl;
cout.setf(ios::fixed, ios::floatfield);
cout.precision(2);
cout << "Preis : " << setw(6) << preis/100.0 << " Euro" << endl;
cout << "Cash : " << setw(6) << geld /100.0 << " Euro" << endl;
cout << "Das Wechselgeld von " << (geld-preis)/100.0 << " Euro wiegt "
<< setprecision(3) << masse << " Gramm." << endl << endl;
return 0;
}
// ####################################################################
// Definition der Funktionen
// ####################################################################
void num_coins(const int& price, int coins[], const int values[], const int n)
{
int pp = price;
for (int i=0; ivalues[i])
{
scale = 1;
currency = "Cent";
}
cout << "coins a " << setw(2) << values[i]/scale << " " << currency << ": "
<< setw(2) << coins[i] << endl;
}
cout << endl;
return;
}
//
float weight_coins(const int coins[], const float weights[], const int n)
{
float mass = 0.0;
for (int i=0; i
Generated by: ghaase on mephisto on Thu May 24 08:42:58 2007, using kdoc 2.0a54. |