Note:
* Use fputc instead of printf
print_integer function is as follows:
void print_integer(int n, int radix, char*prefix) { char *s = "0123456789abcdefghijklmnopqrstuvwxyz";
if(n
int i, j, r;
for(i=0; prefix[i]!='\0'; i++) { fputc(prefix[i], stdout); }
char reslt[100];
for(i=0; n!=0; i++) { r = n % radix; reslt[i] = s[r]; n = n / radix; }
for(j=i-1; j>=0; j--) { fputc(reslt[j], stdout); } }
You will create a function called mintf(...) that is similar to the standard printf(...) but in place of the usual format codes, your mintf(...) will support the following format codes: %d integer (int, short, or char), expressed in decimal notation, with no prefix %x integer (int, short, or char), expressed in hexadecimal notation with the prefix "Ox"; use lowercase letters for digits beyond 9 %b integer (int, short, or char), expressed in binary notation with the prefix "Ob" %$ double, formatted with exactly two digits to the right of the decimal point, and a dollar sign to the left of the first digit. Example: "$35.99" %s string (char* or string literal) %c character (int, short, or char, between 0 and 255) expressed as its corresponding ASCII character %% a single percent sign (no parameter) For each occurrence of any of the above codes, your program shall print one of the arguments (after the format) to mintf(...) in the specified format. Anything else in the format string should be expressed as is. For example, if the format string included "%z", then "%z" would be printed. Likewise, a lone % at the end of the string would also be printed as is (e.g., example #5 below) because anything that doesn't match one of the format specifiers above should be expressed as is. Your code will also handle in and other backslash escapes, but you will not need to do anything special. The C compiler converts those for you. If this is confusing, just ignore it, and then try putting a in the format string when calling your mintf(...). Differences between mintf(...) and printf(...) mintf(...) supports %$ but printf(...) does not. mintf(...) adds a prefix ("Ox") to %x but printf(...) does not. For example, mintf("%x", 768336) will print oxbb950 but printf("%x", 768336) will print bb950. mintf(...) supports %b but printf(...) does not. Some non-standard variants of C do support %b but we are concerned with standard ISO 699 in this class. printf(...) supports many format specifiers that mintf(...) does not (e.g., %0, %p, %f, %e, %u, etc.). mintf.c is where your mintf(...) and print_integer (...) implementations will go. Copy your print_integer (...) from HW02 into it. Copy the function signature for mintf(...) from mintf.h into mintf.c. Make sure all of the following headers are included at the top of your mintf.c. (Type these manually into your file.) #include
#include #include #include "mintf.h" test_mintf.c will contain a main(...) function that will test your mintf(...) and print_integer (...). It must exercise all of the functionality in those two functions. For this homework, that means that in the course of running your test_mintf.c file, every line of code in your mintf.c should be executed at some point. To do this, you simply have several mintf(...) that include all of the format codes above, including a variety of valid values, including positive integers, negative integers, positive float values (for currency), negative float values (for currency), 0, strings, and an empty string. It should also have a few print_integer(...) statements to test that on its own. This will give you a very simple way to know for sure if your code works. Use test-driven development to do this assignment incrementally. test_mintf.txt will contain the expected output from running your tests in test_mintf.c. It will be a simple text file. Requirements file 1. Your submission must contain each of the following files, as specified: contents mintf.c functions print_integer(int n, int radix, char* prefix) + return type: void Print the number n to the console (stdout) in the specified number base (radix), with the prefix immediately before the first digit. radix may be any integer between 2 and 36 (inclusive). For values of radix above 10, use lowercase letters to represent the digits following 9. For example, print_integer(165, 16, "") should print "a5. (The number one-hundred sixty-five would be written as a5") in hexadecimal, i.e., base 16.) the number "one-hundred sixty-five". print_integer (...) should not print a newline (' ' or 'Tr'). For example, print_integer(123, 10, ""); print_integer(-456, 10, "") should print "123-456". mintf(const char *format, ...) + return type: void Print format with any format codes replaced by the respective additional arguments, as specified above. %d should work with any int between INT_MIN and INT_MAX. %$ should work with any double in the range of INT_MIN-0.99 up to INT_MAX + 0.99. Truncate to a multiple of 0.01 (e.g., 768.336 768.33, -768.336 -768.33). declarations declaration of print_integer (...), mintf(...) functions main(int argc, char* argv[]) + return type: int Test your mintf(...). Your main(...) must return EXIT_SUCCESS mintf.h test_mintf.c (0) fa test_mintf.txt output Expected output from running your test_mintf.c. 2. Your test_mintf.c must exercise all functionality (e.g., all format codes, negative numbers, any radix between 2 and 36, largest possible n, smallest possible n, etc.). 3. For format codes related to numbers (%d, %x, %b, %$), your program should handle any valid int value on the system it is being run on, including 0, positive numbers, and negative numbers. 4. Your code may not make any assumptions about the size of an int. 5. Your test_mintf.c should work-and give the same output-with any correct mintf.c, and likewise for your mintf.c. 6. For negative numbers, print the "-" before the prefix (e.g., "-$3.00" not "$-3.00", "-0x12AD" not "Ox- 12AD"). 7. Only the following external header files, functions, and symbols are allowed in your mintf.c. That means you may use printf(...) in your test_mintf.c but not in your mintf.c. You may use fputc(...) and stdout in either one (or both). header functions/symbols allowed in... limits.h INT_MAX, INT_MIN test mintf.c stdarg.h va_list, va_start, va_arg, va_end, va_copy mintf.c, test_mintf.c stdbool.hbool, true, false mintf.c, test_mintf.c stdio.h fputc, stdout, fflush mintf.c, test_mintf.c stdio.h printf test_mintf.c assert.h assert mintf.c, test_mintf.c stdlib.h EXIT_SUCCESS, abs mintf.c, test_mintf.c All others are prohibited unless approved by the instructor. Feel free to ask if there is something you would like to use. 8. Repeat: Do not call printf(...) Examples These are to help you understand the spec. Your test cases must be your own. Do not copy these (even with minor modific Code: mintf("768336"); Output: 768336 2. Code: mintf("My favorite number is %d!", 768336); Output: My favorite number is 768336! 3. Code: mintf("%d written in hex is %x.", 768336, 768336); Output: 768336 written in hex is Oxbb950. 4. Code: mintf("%d written in binary is %b.", 768336, 768336); Output: 768336 written in binary is Ob10111011100101010000. 0.375%"); 5. Code: mintf("Chance of coalescent cloudburst: Output: Chance of coalescent cloudburst: 0.375% 0.375%%"); 6. Code: mintf("Chance of coalescent cloudburst: Output: Chance of coalescent cloudburst: 0.375% 7. Code: mintf("%s the cat has only %d lives left.", "Orlicasta", 8); Output: Orlicasta the cat has only 8 lives left. 8. Code: mintf("Orlicasta's shoes cost %$.", 768.336); Output: Orlicasta's shoes cost $768.33. 9. Code: mintf("Orlicasta's bank balance is %$.", -768.336); Output: Orlicasta's bank balance is -$768.33. 10. Code: mintf("Excess parameters are ignored: %d", 768336, 768337, 768338); Output: Excess parameters are ignored: 768336 This should take no extra effort to code. 11. Code: mintf("Insufficient parameters may lead to undefined behavior: %d %d %d", 768336); Output: Insufficient parameters may lead to undefined behavior: 768336 22222222222???? You do not need to test for this. 12. Code: mintf("Mismatched parameters may lead to undefined behavior: %d", "Orlicasta"); Output: Mismatched parameters may lead to undefined behavior: ??????????????? You will create a function called mintf(...) that is similar to the standard printf(...) but in place of the usual format codes, your mintf(...) will support the following format codes: %d integer (int, short, or char), expressed in decimal notation, with no prefix %x integer (int, short, or char), expressed in hexadecimal notation with the prefix "Ox"; use lowercase letters for digits beyond 9 %b integer (int, short, or char), expressed in binary notation with the prefix "Ob" %$ double, formatted with exactly two digits to the right of the decimal point, and a dollar sign to the left of the first digit. Example: "$35.99" %s string (char* or string literal) %c character (int, short, or char, between 0 and 255) expressed as its corresponding ASCII character %% a single percent sign (no parameter) For each occurrence of any of the above codes, your program shall print one of the arguments (after the format) to mintf(...) in the specified format. Anything else in the format string should be expressed as is. For example, if the format string included "%z", then "%z" would be printed. Likewise, a lone % at the end of the string would also be printed as is (e.g., example #5 below) because anything that doesn't match one of the format specifiers above should be expressed as is. Your code will also handle in and other backslash escapes, but you will not need to do anything special. The C compiler converts those for you. If this is confusing, just ignore it, and then try putting a in the format string when calling your mintf(...). Differences between mintf(...) and printf(...) mintf(...) supports %$ but printf(...) does not. mintf(...) adds a prefix ("Ox") to %x but printf(...) does not. For example, mintf("%x", 768336) will print oxbb950 but printf("%x", 768336) will print bb950. mintf(...) supports %b but printf(...) does not. Some non-standard variants of C do support %b but we are concerned with standard ISO 699 in this class. printf(...) supports many format specifiers that mintf(...) does not (e.g., %0, %p, %f, %e, %u, etc.). mintf.c is where your mintf(...) and print_integer (...) implementations will go. Copy your print_integer (...) from HW02 into it. Copy the function signature for mintf(...) from mintf.h into mintf.c. Make sure all of the following headers are included at the top of your mintf.c. (Type these manually into your file.) #include #include #include #include "mintf.h" test_mintf.c will contain a main(...) function that will test your mintf(...) and print_integer (...). It must exercise all of the functionality in those two functions. For this homework, that means that in the course of running your test_mintf.c file, every line of code in your mintf.c should be executed at some point. To do this, you simply have several mintf(...) that include all of the format codes above, including a variety of valid values, including positive integers, negative integers, positive float values (for currency), negative float values (for currency), 0, strings, and an empty string. It should also have a few print_integer(...) statements to test that on its own. This will give you a very simple way to know for sure if your code works. Use test-driven development to do this assignment incrementally. test_mintf.txt will contain the expected output from running your tests in test_mintf.c. It will be a simple text file. Requirements file 1. Your submission must contain each of the following files, as specified: contents mintf.c functions print_integer(int n, int radix, char* prefix) + return type: void Print the number n to the console (stdout) in the specified number base (radix), with the prefix immediately before the first digit. radix may be any integer between 2 and 36 (inclusive). For values of radix above 10, use lowercase letters to represent the digits following 9. For example, print_integer(165, 16, "") should print "a5. (The number one-hundred sixty-five would be written as a5") in hexadecimal, i.e., base 16.) the number "one-hundred sixty-five". print_integer (...) should not print a newline (' ' or 'Tr'). For example, print_integer(123, 10, ""); print_integer(-456, 10, "") should print "123-456". mintf(const char *format, ...) + return type: void Print format with any format codes replaced by the respective additional arguments, as specified above. %d should work with any int between INT_MIN and INT_MAX. %$ should work with any double in the range of INT_MIN-0.99 up to INT_MAX + 0.99. Truncate to a multiple of 0.01 (e.g., 768.336 768.33, -768.336 -768.33). declarations declaration of print_integer (...), mintf(...) functions main(int argc, char* argv[]) + return type: int Test your mintf(...). Your main(...) must return EXIT_SUCCESS mintf.h test_mintf.c (0) fa test_mintf.txt output Expected output from running your test_mintf.c. 2. Your test_mintf.c must exercise all functionality (e.g., all format codes, negative numbers, any radix between 2 and 36, largest possible n, smallest possible n, etc.). 3. For format codes related to numbers (%d, %x, %b, %$), your program should handle any valid int value on the system it is being run on, including 0, positive numbers, and negative numbers. 4. Your code may not make any assumptions about the size of an int. 5. Your test_mintf.c should work-and give the same output-with any correct mintf.c, and likewise for your mintf.c. 6. For negative numbers, print the "-" before the prefix (e.g., "-$3.00" not "$-3.00", "-0x12AD" not "Ox- 12AD"). 7. Only the following external header files, functions, and symbols are allowed in your mintf.c. That means you may use printf(...) in your test_mintf.c but not in your mintf.c. You may use fputc(...) and stdout in either one (or both). header functions/symbols allowed in... limits.h INT_MAX, INT_MIN test mintf.c stdarg.h va_list, va_start, va_arg, va_end, va_copy mintf.c, test_mintf.c stdbool.hbool, true, false mintf.c, test_mintf.c stdio.h fputc, stdout, fflush mintf.c, test_mintf.c stdio.h printf test_mintf.c assert.h assert mintf.c, test_mintf.c stdlib.h EXIT_SUCCESS, abs mintf.c, test_mintf.c All others are prohibited unless approved by the instructor. Feel free to ask if there is something you would like to use. 8. Repeat: Do not call printf(...) Examples These are to help you understand the spec. Your test cases must be your own. Do not copy these (even with minor modific Code: mintf("768336"); Output: 768336 2. Code: mintf("My favorite number is %d!", 768336); Output: My favorite number is 768336! 3. Code: mintf("%d written in hex is %x.", 768336, 768336); Output: 768336 written in hex is Oxbb950. 4. Code: mintf("%d written in binary is %b.", 768336, 768336); Output: 768336 written in binary is Ob10111011100101010000. 0.375%"); 5. Code: mintf("Chance of coalescent cloudburst: Output: Chance of coalescent cloudburst: 0.375% 0.375%%"); 6. Code: mintf("Chance of coalescent cloudburst: Output: Chance of coalescent cloudburst: 0.375% 7. Code: mintf("%s the cat has only %d lives left.", "Orlicasta", 8); Output: Orlicasta the cat has only 8 lives left. 8. Code: mintf("Orlicasta's shoes cost %$.", 768.336); Output: Orlicasta's shoes cost $768.33. 9. Code: mintf("Orlicasta's bank balance is %$.", -768.336); Output: Orlicasta's bank balance is -$768.33. 10. Code: mintf("Excess parameters are ignored: %d", 768336, 768337, 768338); Output: Excess parameters are ignored: 768336 This should take no extra effort to code. 11. Code: mintf("Insufficient parameters may lead to undefined behavior: %d %d %d", 768336); Output: Insufficient parameters may lead to undefined behavior: 768336 22222222222???? You do not need to test for this. 12. Code: mintf("Mismatched parameters may lead to undefined behavior: %d", "Orlicasta"); Output: Mismatched parameters may lead to undefined behavior