Skip to content

Instantly share code, notes, and snippets.

@radeks
Created July 10, 2011 18:23
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 radeks/1074802 to your computer and use it in GitHub Desktop.
Save radeks/1074802 to your computer and use it in GitHub Desktop.
iPods
/*
* 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