Skip to content

Instantly share code, notes, and snippets.

@kahless62003
Created April 3, 2016 00:01
Show Gist options
  • Save kahless62003/fd129d351c0dc51279859a592800909f to your computer and use it in GitHub Desktop.
Save kahless62003/fd129d351c0dc51279859a592800909f to your computer and use it in GitHub Desktop.
Change calculators
/*
* keep_the_change_ya_filthy_animal.c
* low tech variable based approach
*/
#include <stdio.h>
#include <string.h>
int main(void)
{
float input_in_pounds=0.0;
const int
pence_in_one_hundred_pounds=100*100,
pence_in_fifty_pounds=50*100,
pence_in_twenty_pounds=20*100,
pence_in_ten_pounds=10*100,
pence_in_five_pounds=5*100,
pence_in_two_pounds=2*100,
pence_in_one_pound=100,
pence_in_fifty_pence=50,
pence_in_twenty_pence=20,
pence_in_ten_pence=10,
pence_in_five_pence=5,
pence_in_two_pence=2,
pence_in_penny=1;
int input_in_pence=0,
number_one_hundred_pound_notes=0,
number_fifty_pound_notes=0,
number_twenty_pound_notes=0,
number_ten_pound_notes=0,
number_five_pound_notes=0,
number_two_pound_coins=0,
number_one_pound_coins=0,
number_fifty_pence_pieces=0,
number_twenty_pence_pieces=0,
number_ten_pence_pieces=0,
number_five_pence_pieces=0,
number_two_pence_pieces=0,
number_pennies=0;
short int caniexit=0;
char line[1024];
do
{
printf("Enter total amount: \u00A3");
fgets(line, sizeof(line)-1, stdin);
if(sscanf(line,"%f",&input_in_pounds)==1)
{
input_in_pence=(int)(input_in_pounds*100);
printf("Total in pence= %i\n", input_in_pence);
while (input_in_pence >= pence_in_one_hundred_pounds)
{
input_in_pence = input_in_pence - pence_in_one_hundred_pounds;
number_one_hundred_pound_notes++;
}
while (input_in_pence >= pence_in_fifty_pounds)
{
input_in_pence = input_in_pence - pence_in_fifty_pounds;
number_fifty_pound_notes++;
}
while (input_in_pence >= pence_in_twenty_pounds)
{
input_in_pence = input_in_pence - pence_in_twenty_pounds;
number_twenty_pound_notes++;
}
while (input_in_pence >= pence_in_ten_pounds)
{
input_in_pence = input_in_pence - pence_in_ten_pounds;
number_ten_pound_notes++;
}
while (input_in_pence >= pence_in_five_pounds)
{
input_in_pence = input_in_pence - pence_in_five_pounds;
number_five_pound_notes++;
}
while (input_in_pence >= pence_in_two_pounds)
{
input_in_pence = input_in_pence - pence_in_two_pounds;
number_two_pound_coins++;
}
while (input_in_pence >= pence_in_one_pound)
{
input_in_pence = input_in_pence - pence_in_one_pound;
number_one_pound_coins++;
}
while (input_in_pence >= pence_in_fifty_pence)
{
input_in_pence = input_in_pence - pence_in_fifty_pence;
number_fifty_pence_pieces++;
}
while (input_in_pence >= pence_in_twenty_pence)
{
input_in_pence = input_in_pence - pence_in_twenty_pence;
number_twenty_pence_pieces++;
}
while (input_in_pence >= pence_in_ten_pence)
{
input_in_pence = input_in_pence - pence_in_ten_pence;
number_ten_pence_pieces++;
}
while (input_in_pence >= pence_in_five_pence)
{
input_in_pence = input_in_pence - pence_in_five_pence;
number_five_pence_pieces++;
}
while (input_in_pence >= pence_in_two_pence)
{
input_in_pence = input_in_pence - pence_in_two_pence;
number_two_pence_pieces++;
}
while (input_in_pence >= pence_in_penny)
{
input_in_pence = input_in_pence - pence_in_penny;
number_pennies++;
}
if(number_one_hundred_pound_notes!=0)
printf("Number of One Hundred Pound Notes= %i\n", number_one_hundred_pound_notes);
if(number_fifty_pound_notes!=0)
printf("Number of Fifty Pound Notes= %i\n", number_fifty_pound_notes);
if(number_twenty_pound_notes!=0)
printf("Number of Twenty Pound Notes= %i\n", number_twenty_pound_notes);
if(number_ten_pound_notes!=0)
printf("Number of Ten Pound Notes= %i\n", number_ten_pound_notes);
if(number_five_pound_notes!=0)
printf("Number of Five Pound Notes= %i\n", number_five_pound_notes);
if(number_two_pound_coins!=0)
printf("Number of Two Pound Coins= %i\n", number_two_pound_coins);
if(number_one_pound_coins!=0)
printf("Number of One Pound Coins= %i\n", number_one_pound_coins);
if(number_fifty_pence_pieces!=0)
printf("Number of Fifty Pence Pieces= %i\n", number_fifty_pence_pieces);
if(number_twenty_pence_pieces!=0)
printf("Number of Twenty Pence Pieces= %i\n", number_twenty_pence_pieces);
if(number_ten_pence_pieces!=0)
printf("Number of Ten Pence Pieces= %i\n", number_ten_pence_pieces);
if(number_five_pence_pieces!=0)
printf("Number of Five Pence Pieces= %i\n", number_five_pence_pieces);
if(number_two_pence_pieces!=0)
printf("Number of Tuppence Pieces= %i\n", number_two_pence_pieces);
if(number_pennies!=0)
printf("Number of Pennies= %i\n", number_pennies);
printf("\n");
number_one_hundred_pound_notes=0,
number_fifty_pound_notes=0,
number_twenty_pound_notes=0,
number_ten_pound_notes=0,
number_five_pound_notes=0,
number_two_pound_coins=0,
number_one_pound_coins=0,
number_fifty_pence_pieces=0,
number_twenty_pence_pieces=0,
number_ten_pence_pieces=0,
number_five_pence_pieces=0,
number_two_pence_pieces=0,
number_pennies=0;
}
else
{
caniexit=1;
}
}
while (caniexit==0);
puts("\nKeep the change ya filthy animal!\n");
return 0;
}
/*
* keep_the_change_ya_filthy_animal2.c
* higher tech array based approach
*/
#include <stdio.h>
#include <string.h>
int main(void)
{
float input_in_pounds=0.0;
int input_in_pence=0;
short int caniexit=0;
char line[1024];
const int max_denom=13;
const char * denom_text[] =
{
"One Hundred Pound Notes",
"Fifty Pound Notes",
"Twenty Pound Notes",
"Ten Pound Notes",
"Five Pound Notes",
"Two Pound Coins",
"One Pound Coins",
"Fifty Pence Pieces",
"Twenty Pence Pieces",
"Ten Pence Pieces",
"Five Pence Pieces",
"Tuppence Pieces",
"Pennies"
};
int denoms[2][max_denom];
denoms[0][0] = 100*100;
denoms[0][1] = 50*100;
denoms[0][2] = 20*100;
denoms[0][3] = 10*100;
denoms[0][4] = 5*100;
denoms[0][5] = 2*100;
denoms[0][6] = 100;
denoms[0][7] = 50;
denoms[0][8] = 20;
denoms[0][9] = 10;
denoms[0][10] = 5;
denoms[0][11] = 2;
denoms[0][12] = 1;
denoms[1][0] = 0;
denoms[1][1] = 0;
denoms[1][2] = 0;
denoms[1][3] = 0;
denoms[1][4] = 0;
denoms[1][5] = 0;
denoms[1][6] = 0;
denoms[1][7] = 0;
denoms[1][8] = 0;
denoms[1][9] = 0;
denoms[1][10] = 0;
denoms[1][11] = 0;
denoms[1][12] = 0;
int lc0;
do
{
printf("Enter total amount: \u00A3");
fgets(line, sizeof(line)-1, stdin);
if(sscanf(line,"%f",&input_in_pounds)==1)
{
input_in_pence=(int)(input_in_pounds*100);
printf("Total in pence= %i\n", input_in_pence);
for (lc0=0; lc0 < max_denom; lc0++)
{
while (input_in_pence >= denoms[0][lc0])
{
input_in_pence = input_in_pence - denoms[0][lc0];
denoms[1][lc0]++;
}
}
for (lc0=0; lc0 < max_denom; lc0++)
{
if(denoms[1][lc0]!=0)
printf("Number of %s= %i\n", denom_text[lc0], denoms[1][lc0]);
}
printf("\n");
denoms[1][0] = 0;
denoms[1][1] = 0;
denoms[1][2] = 0;
denoms[1][3] = 0;
denoms[1][4] = 0;
denoms[1][5] = 0;
denoms[1][6] = 0;
denoms[1][7] = 0;
denoms[1][8] = 0;
denoms[1][9] = 0;
denoms[1][10] = 0;
denoms[1][11] = 0;
denoms[1][12] = 0;
}
else
{
caniexit=1;
}
}
while (caniexit==0);
puts("\nKeep the change ya filthy animal!\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment