Created
July 10, 2011 18:23
-
-
Save radeks/1074802 to your computer and use it in GitHub Desktop.
iPods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To compile: | |
* $ clang iPod.c -o iPod | |
*/ | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
void print_help(FILE *file) { | |
/* print help here */ | |
} | |
int main(int argc, char *argv[]) { | |
if (argc != 2) { | |
print_help(stderr); | |
return 1; | |
} | |
enum { | |
Brazil, | |
Argentina, | |
NumberOfCountries | |
} country; | |
unsigned stocks[NumberOfCountries] = { 100, 100 }; | |
char *countryNames[NumberOfCountries] = { | |
"Brazil", | |
"Argentina" | |
}; | |
unsigned costs[NumberOfCountries] = { 100, 50 }; | |
size_t lengthOfLargestCountryName = 0; | |
for (int i = 0; i < NumberOfCountries; i++) { | |
size_t countryNameLength = strlen(countryNames[i]); | |
if (countryNameLength > lengthOfLargestCountryName) { | |
lengthOfLargestCountryName = countryNameLength; | |
} | |
} | |
char *countryNameToken = strtok(argv[1], ":"); | |
char *countryName = malloc(strlen(countryNameToken)); | |
memcpy(countryName, countryNameToken, strlen(countryNameToken)); | |
char *orderCountString = strtok(NULL, ":"); | |
unsigned cost; | |
for (int j = 0; j < NumberOfCountries; j++) { | |
if (strcmp(countryNames[j], countryName) == 0) { | |
unsigned orderCount = atoi(orderCountString); | |
stocks[j] -= orderCount; | |
cost = orderCount * costs[j]; | |
break; | |
} | |
} | |
printf("%u:%u:%u", cost, stocks[0], stocks[1]); | |
free(countryName); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment