ConvertDec_to_Hex-Oct-Bin.c ConvertDec_to_Hex-Oct-Bin.c is a simple conversion program that prompts the user for their n
Posted: Fri Jul 08, 2022 6:16 am
ConvertDec_to_Hex-Oct-Bin.c ConvertDec_to_Hex-Oct-Bin.c is a simple conversion program that prompts the user for their name, followed by a prompt for an integer in the range [1- 1,000,000] inclusive (or "x" to exit the program). The integer entered will be displayed in decimal, hexadecimal, octal, and binary representation. Hexadecimal, octal, and binary representations must be computed algorithmically (i.e. NOT accomplished via format specifier). The above mentioned display shall occur from within main(). The user is then asked if the output should be saved to a text file. If user replies in the affirmative, a name for the text file is requested. The newly created text file will contain the user's name, current date, and output results. The program loops until "x" is entered to exit. The current date shall be provided by char "getDateAndTime() as discussed in the lecture notes. Example user interface: Enter your name: Hal Greenwald Enter an Integer [1- 1,000,000] or type x to exit: 2000000 Error: 2000000 is out of range Enter an Integer [1- 1,000,000] or type x to exit: 240 Decimal: 240 Hexadecimal: FO Octal: 360 Binary: 11110000 Save to a file? (y/n): x Error: illegal value Save to a file? (y/n): x Enter file name: cs222 hw3.txt File saved. Enter an Integer [1- 1,000,000] or type x to exit: X Goodbye!
Terminal output: Scat es222 bw3.txt Hal Greenwald Today's date: Mon Jun 27 3:49:30 2022 Decimal: 240 Hexadecimal: FO Octal: 360 Binary: 11110000 User defined functions: As a minimum, include the following 6 user-defined functions, all of which should be prototyped, and called from within main(). char "getDateAndTime(); Returns a character string with the current date and time int getInteger() Read user input into a character string using fgets(). "x"returns-1 indicating program exit. Otherwise, convert the string into an integer (which will be returned to main()) using int atoi(const char "str); or int sscanf(const char "s, const char "format,...); void decimalToBinary(int decValue, char binString[]): Accepts a decimal value decValue and a character string buffer binString[] Convert decvalue into its corresponding hinary value and store the result as a character string within binString[] binString[] may then be displayed from within main() void decimalToHex(int decValue, char hexString[]): Accepts a decimal value decValue and a character string buffer hexString[] Convert decvalue into its corresponding hexadecimal value and store the result as a character string within hexString[] hexString[] may then be displayed from within main() void decimalToOct(int decValue, char octString[]): Accepts a decimal value decValue and a character string buffer octString[] Convert decvalue into its corresponding octal value and store the result as a character string within octString[] octString may then be displayed from within main() int saveFile(char name[], char date[], int decValue, char octString[] char hexString[], char binString[]): Prompt the user whether the output should be saved to a text file. If user replies in the affirmative, a name for the text file is requested. The newly created text file will contain the user's name, current date, and output results. If the output file cannot be opened, print an appropriate error message and return 0. Otherwise return 1;
Rubric 10 points: • Is the source code well formatted using clearly readable indentation and white space (while viewed within vi) and well commented? 1 point • Range checking for numeric input [1-1,000,000) 1 point • Save to a file? (y/n): If any character other than yor'' is entered An error message is displayed and the user is re-prompted. I point Are each of the above mentioned UDFs properly implemented 1 point each (6 points total) • Is the output (text file) correct and accessible? 1 points • Function prototypes are required for each UDF other than main 1 point will be deducted if prototypes are omitted or incorrect. • No global variables are permitted for this exercise. 1 point will be deducted for every global variable used. Submit via Blackboard by July 7, 11:59 PM. 2 points will be deducted for each day past the due date. int main() char date Time[32]; char name[32]. char binString[32]: char octString[32]: char hexString[32]: int decVal = 0; int saveSuccess = 0; int main() pseudocode: strcpy(dateTime.getDateAndTime()). prompt user for name decVal = getInteger(); //exit clause if (decVal == -1) return 0; // run conversions decimalToBinary (decVal, binString]: decimalToHex(decVal, hexString); decimalToOct(decVal, octString); // print conversions to the console. printf("Decimal: %d\n", decal): printf("Binary: %s\n", binString): printf("Octal: %s\n", octString); printf("Hexidecimal: %s\n", hexString); // save the file saveSuccess saveFile(name, date, decValue, octString, hexString, binString): if (IsaveSuccess) // equivalent to: if (saveSuccess == 0) return 1; return 0;