Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ConvertDec_to_Hex-Oct-Bin.c ConvertDec_to_Hex-Oct-Bin.c is a simple conversion p

ID: 3914436 • Letter: C

Question

ConvertDec_to_Hex-Oct-Bin.c

ConvertDec_to_Hex-Oct-Bin.c is a simple conversion program that prompts the user for their name and the current date, followed by a prompt for an integer from 1 to 1,000,000. The integer entered will be displayed in hexadecimal, octal, and binary representation. Conversions must be computed algorithmically (i.e. NOT accomplished via format specifiers ). 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 (see example below).

User Interface:

--------------------------------------------------------------------

Enter an Integer ( 1 – 1000000) or type x to exit: 240

Decimal: 240

Hexadecimal: F0

Octal: 360

Binary: 11110000

Save to a file? (y/n): y

Enter file name: cs.txt

File saved.

Enter an Integer ( 1 – 1000000) or type x to exit: x

Good bye!

---------------------------------------------------------------------

Terminal output:

---------------------------------------------------------------------

$cat cs.txt

Timmy Scott

Today's date and time: Weds July 11 15:35:25 2018

Decimal: 240

Hexadecimal: F0

Octal: 360

Binary: 11110000

----------------------------------------------------------------------

User defined functions:

As a minimum, include the following 2 user-defined functions: (prototypes may vary as long as the function works properly)

char *getDateAndTime();

Returns a character string with the current date and time

int getInteger()

Read user input into a character string.

“x” returns -1 to exit program.

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, ...);

Explanation / Answer

#include<stdio.h>

#include<time.h>

#include<string.h>

#include<stdlib.h>

#define MAX 100 //stack array MAX SIZE

int stack[MAX], top = -1;

int getInteger(char *decimal)

{

//Function to read user input into character string, convert the string

//into an integer and return the integer

printf("Enter an integer (1 - 1000000) or type x to exit: ");

scanf("%s", decimal);

if(strcmp(decimal,"x") == 0)

return -1;

return atoi(decimal);

}

char *getDateAndTime()

{

//Function to return current date and time

time_t rawtime;

struct tm *timeinfo;

time(&rawtime);

timeinfo = localtime(&rawtime);

return asctime(timeinfo);

}

int isEmpty()

{

//Function to check if stack is empty or not

if(top == -1)

return 1;//Stack is empty

else

return 0;//Stack is not empty

}

void PUSH(int val)

{

//Function to push into stack

if(top == MAX - 1)

printf("Cannot PUSH. Stack Overflow!");

else

stack[++top] = val;

}

int POP()

{

//Function to pop top element from stack

if(top == -1)

printf("Cannot POP. Stack Underflow!");

else

return stack[top--];

}

//This function converts decimal number (base 10) in base of 16, 8 and 2

void convert(int deciOrig, char *hexadecimal, char *octal, char *binary)

{

int deciSave, quotient, divisor, remainder;

printf("Decimal: %d", deciOrig);

  

int arr[3] = {16, 8, 2};

for(int i = 0; i < 3; i++)

{

deciSave = deciOrig;

divisor = arr[i];

do

{

quotient = deciSave/divisor;

remainder = deciSave % divisor;

PUSH(remainder);

deciSave = quotient;

}while(quotient != 0);

if(i == 0)

printf(" Hexadecimal: ");

if(i == 1)

printf(" Octal: ");

if(i == 2)

printf(" Binary: ");

int j = 0, k = 0, l = 0;

while(!isEmpty())

{

if(i == 0)

{

//For hexadecimal

int n = POP();

switch(n)

{

case 10: printf("A");hexadecimal[j++] = 'A';break;

case 11: printf("B");hexadecimal[j++] = 'B';break;

case 12: printf("C");hexadecimal[j++] = 'C';break;

case 13: printf("D");hexadecimal[j++] = 'D';break;

case 14: printf("E");hexadecimal[j++] = 'E';break;

case 15: printf("F");hexadecimal[j++] = 'F';break;

default: printf("%d", n);hexadecimal[j++] = n + '0';break;

}

}

else

if(i == 1)

{

//For octal

char n = POP() + '0';

printf("%c", n);

octal[k++] = n;

}

else

if(i == 2)

{

//For binary

char n = POP() + '0';

printf("%c", n);

binary[l++] = n;

}

}

}

}

int main()

{

char userName[20] = "";   

char decimal[30] = "", hexadecimal[30] = "", octal[30] = "", binary[30] = "";

printf("Enter your name: ");

fgets(userName, 20, stdin);

while(1)

{

int number = getInteger(decimal);

if(number == -1)

{

printf("Good bye!");

exit(0);

}

else

{

char choice;

convert(number, hexadecimal, octal, binary);

printf(" Save to a file? (y/n): ");

fflush(stdin);

scanf("%c", &choice);

if(choice == 'y' || choice == 'Y')

{

char fileName[20];

printf("Enter file name: ");

scanf("%s", fileName);

FILE *f = fopen(fileName, "a");//Open file in append mode

if(f != NULL)

{

fprintf(f,"%s %s %s %s %s %s %s %s %s %s %s ",userName,"Today's date and time: ",getDateAndTime(),"Decimal: ",decimal,"Hexadecimal: ",hexadecimal,"Octal: ",octal,"Binary: ",binary);

fclose(f);

printf(" File saved");

}

else

{

printf("Cannot open file!");

}

}

}

}

return 0;

}