// roman.c: exercise to learn C with structs and strings // compile with c11 or later (gcc -std==c11 roman.c) // On Windows, can just use the command gcc roman.c // You can use CLion as well if you wish. #include #include // the functions appear before main; usually they would be the other way around, // but this organization is simpler #define MAX_NUMBERS 20 // declare a structure for a number with two fields, one containing the text of // the number (up to 50 characters; use #define to create a C-style constant) // and the value (an int). You can add other fields if you like. // read a number into a parameter, target; you must declare target // as a pointer to the roman number struct // The scanf reads a single word (which will be on its own line) into // tmp; you'll copy this into the roman number and set the integer // value in the structure // You can assume the input is less than 50 characters. void read_number( ) { char tmp[MAX_LENGTH]; scanf("%s", tmp); if ( !feof(stdin) ) { // check for end of file - very nearly the same as !cin.eof() // assign target's text to be a copy of tmp's data // todo: add code to set the value field of the struct; this will require an if statement } } // substitute VIIII with IX, IIII with IV; can assume just one of either in string // (so look backwards). You must use strcmp and strcat in your answer. void simplify_number( ) { int len = strlen(""); // todo: get string field of your struct // hint: if length < 4, do nothing // then: use strcmp to see if last 5 characters are "VIIII"; if so, // truncate the string at length - 5, use strcat to add "IX" at the end // If the last 5 characters are "IIII", truncate the string at length - 4 // (by setting character length - 4 to \0), add "IV" at the end } // yes, this should use more functions! But using less makes this easier. int main() { // todo: declare an array of MAX_NUMBERS structs called xs int count = 0; printf("Enter roman numbers:\n"); read_number(&xs[0]); while ( count < MAX_NUMBERS && !feof(stdin) ) { count++; // todo: pass xs[count] to read_number read_number( ); } for(int i = 0; i < count; ++i) // todo: pass xs[i] to simplify_number simplify_number( ); int sum = 0; for(int i = 0; i < count; ++i) { // add the i'th value to sum sum += 0; } printf("Rewritten numbers:\n"); for(int i = 0; i < count; ++i) // print the i'th roman number printf("%s\n", "todo"); printf("Sum: %d\n", sum); return 0; }