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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
/*
Name:
Date: 14/10/10 15:04
Description: Making Change
*/
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
ifstream infile;
ofstream outfile;
void make_change(float cost, float payment, float& change,
int& twentiesInTill, int& tensInTill, int& fivesInTill,
int& dollarsInTill, int& quartersInTill, int& dimesInTill,
int& nickelsInTill, int& penniesInTill, int& twentiesInChange,
int& tensInChange, int& fivesInChange, int& dollarsInChange,
int& quartersInChange, int& dimesInChange,
int& nickelsInChange, int& penniesInChange)
{
change = (payment - cost);
int changeInPennies = static_cast<int>(change * 100.0 + 0.001);
if(changeInPennies > 2000){
for(twentiesInChange = 0; twentiesInTill >= twentiesInChange; twentiesInChange++){
twentiesInTill = (twentiesInTill - 1);
changeInPennies = (changeInPennies - 2000);}
}
else if(changeInPennies > 1000){
for(tensInChange = 0; tensInTill >= tensInChange; tensInChange++){
tensInTill = (tensInTill - 1);
changeInPennies = (changeInPennies - 1000);}
}
else if(changeInPennies > 500){
for(fivesInChange = 0; fivesInTill >= fivesInChange; fivesInChange++){
fivesInTill = (fivesInTill - 1);
changeInPennies = (changeInPennies - 500);}
}
else if(changeInPennies > 100){
for(dollarsInChange = 0; dollarsInTill >= dollarsInChange; dollarsInChange++){
dollarsInTill = (dollarsInTill - 1);
changeInPennies = (changeInPennies - 100);}
}
else if(changeInPennies > 25){
for(quartersInChange = 0; quartersInTill >= quartersInChange; quartersInChange++){
quartersInTill = (quartersInTill - 1);
changeInPennies = (changeInPennies - 25);}
}
else if(changeInPennies > 10){
for(dimesInChange = 0; dimesInTill >= dimesInChange; dimesInChange++){
dimesInTill = (dimesInTill - 1);
changeInPennies = (changeInPennies - 10);}
}
else if(changeInPennies > 5){
for(nickelsInChange = 0; nickelsInTill >= nickelsInChange; nickelsInChange++){
nickelsInTill = (nickelsInTill - 1);
changeInPennies = (changeInPennies - 5);}
}
else if(changeInPennies > 1){
for(penniesInChange = 0; penniesInTill >= penniesInChange; penniesInChange++){
penniesInTill = (penniesInTill - 1);
changeInPennies = (changeInPennies - 1);}
}
outfile << "Cost = "<< cost << "\nPayment = "<< payment << "\n";
outfile << "Change = "<< change << ", consisting of:\n" << twentiesInChange << " twenties ";
outfile << tensInChange << " tens " << fivesInChange << " fives ";
outfile << dollarsInChange << " dollars " << quartersInChange << " quarters ";
outfile << dimesInChange << " dimes " << nickelsInChange << " nickels ";
outfile << penniesInChange << " pennies\nTill Contents:\n";
outfile << twentiesInTill << " twenties " << tensInTill << " tens ";
outfile << fivesInTill << " fives " << dollarsInTill << " dollars " << quartersInTill << " quarters ";
outfile << dimesInTill << " dimes " << nickelsInTill << " nickels " << penniesInTill << " pennies\n\n";
}
int main(){
infile.open("transactions.dat");
if (infile.fail()) {
cerr << "Can't open transactions.dat for input.\n";
return 1;}
outfile.open("change.out");
if (outfile.fail()){
cerr << "Can't open change.out for output.\n";
return 2;}
float cost, payment, change;
int twentiesInTill, tensInTill, fivesInTill, dollarsInTill, quartersInTill, dimesInTill, nickelsInTill, penniesInTill;
int twentiesInChange, tensInChange, fivesInChange, dollarsInChange, quartersInChange, dimesInChange, nickelsInChange, penniesInChange;
infile >> twentiesInTill >> tensInTill >> fivesInTill >> dollarsInTill >> quartersInTill >> dimesInTill >> nickelsInTill >> penniesInTill;
outfile << "Initial Till Contents:\n" << twentiesInTill << " twenties " << tensInTill << " tens ";
outfile << fivesInTill << " fives " << dollarsInTill << " dollars " << quartersInTill << " quarters ";
outfile << dimesInTill << " dimes " << nickelsInTill << " nickels " << penniesInTill << " pennies\n\n";
while(infile >> cost >> payment){
make_change(cost, payment, change, twentiesInTill, tensInTill, fivesInTill,
dollarsInTill, quartersInTill, dimesInTill,nickelsInTill,
penniesInTill, twentiesInChange, tensInChange, fivesInChange,
dollarsInChange, quartersInChange, dimesInChange,
nickelsInChange, penniesInChange);}
infile.close();
outfile.close();
return 0;
}
|