main.c (1029B)
1 /* 2 2018 David DiPaola 3 licensed under CC0 (public domain, see https://creativecommons.org/publicdomain/zero/1.0/) 4 */ 5 6 #include <stdio.h> 7 8 #include <stdlib.h> 9 10 #include "data.h" 11 12 int 13 main(int argc, char * argv[]) { 14 if (argc < 4) { 15 fprintf(stderr, "syntax: usinflation <start year> <start amount> <end year>" "\n"); 16 return 1; 17 } 18 int start_year = atoi(argv[1]); 19 float start_amount = (float)atof(argv[2]); 20 int end_year = atoi(argv[3]); 21 22 if ((start_year < DATA_STARTYEAR) || (start_year > DATA_ENDYEAR)) { 23 fprintf(stderr, "ERROR: start year: %i is invalid. valid years are %i-%i" "\n", start_year, DATA_STARTYEAR, DATA_ENDYEAR); 24 return 2; 25 } 26 if ((end_year < DATA_STARTYEAR) || (end_year > DATA_ENDYEAR)) { 27 fprintf(stderr, "ERROR: end year: %i is invalid. valid years are %i-%i" "\n", end_year, DATA_STARTYEAR, DATA_ENDYEAR); 28 return 3; 29 } 30 31 float ratio = DATA[end_year-DATA_STARTYEAR] / DATA[start_year-DATA_STARTYEAR]; 32 float end_amount = ratio * start_amount; 33 printf("%G" "\n", end_amount); 34 return 0; 35 } 36