Create a gist now

Instantly share code, notes, and snippets.

@michaeljdeeb /main.m
Last active Dec 14, 2015

What would you like to do?
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