Skip to content

Instantly share code, notes, and snippets.

@michaeljdeeb
Last active December 14, 2015 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaeljdeeb/5131772 to your computer and use it in GitHub Desktop.
Save michaeljdeeb/5131772 to your computer and use it in GitHub Desktop.
r/dailyprogrammer Challenge #119 [Easy] Change Calculator
//
// main.m
// ChangeCalculator
//
// Created by Michael Deeb on 3/10/13.
// Copyright (c) 2013 Michael Deeb. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
// Values of coins.
NSDecimalNumber *QUARTER = [NSDecimalNumber decimalNumberWithDecimal:[[NSNumber numberWithFloat:0.25] decimalValue]];
NSDecimalNumber *DIME = [NSDecimalNumber decimalNumberWithDecimal:[[NSNumber numberWithFloat:0.10] decimalValue]];
NSDecimalNumber *NICKLE = [NSDecimalNumber decimalNumberWithDecimal:[[NSNumber numberWithFloat:0.05] decimalValue]];
NSDecimalNumber *PENNY = [NSDecimalNumber decimalNumberWithDecimal:[[NSNumber numberWithFloat:0.01] decimalValue]];
// Look for the input file.
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *filePath = [path stringByAppendingPathComponent:@"file.in"];
NSString *fileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
// Let's put that big string into an array.
NSArray *lines = [fileContents componentsSeparatedByString:@"\n"];
// Decimal formatting stuff
NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc] init];
[numFormatter setMaximumFractionDigits:2];
// Round up if thousandths place >= 5, else round down (typical POS behavior)
[numFormatter setRoundingMode: NSNumberFormatterRoundHalfUp];
// Create NSNumber Array
NSMutableArray *array = [NSMutableArray array];
for (NSString *numberString in lines) {
// This seems inefficient.
NSString *formattedNumber = [NSString stringWithString:[numFormatter stringFromNumber:[NSDecimalNumber decimalNumberWithString:numberString]]];
[array addObject:[NSDecimalNumber decimalNumberWithString:formattedNumber]];
}
// Figure out the coinage.
// Subtracting rather than moding because it seems difficult with NSDecimalNumbers
for (__strong NSDecimalNumber *coins in array) {
// Use these to keep track of the amount of each coin
int quarterCount = 0;
int dimeCount = 0;
int nickleCount = 0;
int pennyCount = 0;
NSLog(@"==========");
// Prints out formatted number in array array. Would prefer printout of same number unformatted in lines array
NSLog(@"Change calculation for $%@", coins);
// Check if coins is greater than/equal to quarters
while([coins compare:QUARTER] == NSOrderedDescending || [coins compare:QUARTER] == NSOrderedSame) {
coins = [coins decimalNumberBySubtracting:QUARTER];
quarterCount++;
}
// Check if coins is greater than/equal to dimes
while([coins compare:DIME] == NSOrderedDescending || [coins compare:DIME] == NSOrderedSame) {
coins = [coins decimalNumberBySubtracting:DIME];
dimeCount++;
}
// Check if coins is greater than/equal to nickles
while([coins compare:NICKLE] == NSOrderedDescending || [coins compare:NICKLE] == NSOrderedSame) {
coins = [coins decimalNumberBySubtracting:NICKLE];
nickleCount++;
}
// Check if coins is greater than/equal to pennys
while([coins compare:PENNY] == NSOrderedDescending || [coins compare:PENNY] == NSOrderedSame) {
coins = [coins decimalNumberBySubtracting:PENNY];
pennyCount++;
}
// Only print out amounts greater than 0
if(quarterCount > 0)
NSLog(@"Quarters: %u", quarterCount);
if(dimeCount > 0)
NSLog(@"Dimes: %u", dimeCount);
if(nickleCount > 0)
NSLog(@"Nickles: %u", nickleCount);
if(pennyCount > 0)
NSLog(@"Pennies: %u", pennyCount);
// In the case of numbers smaller than the hundredths place
if(quarterCount == 0 && dimeCount == 0 && nickleCount == 0 && pennyCount == 0)
NSLog(@"This amount of money is too small to make in change with USD.");
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment