CAN YOU MODIFY THIS CODE SO IT DOES ALL THE CONVERSION INCLUDING DECIMAL TO HEX
ID: 3700360 • Letter: C
Question
CAN YOU MODIFY THIS CODE SO IT DOES ALL THE CONVERSION INCLUDING DECIMAL TO HEX AND DECIMAL TO OCTAL. MAKE METHODS FOR BOTH CONVERSIONS PLEASE AND SHOW OUTPUT. #include #include #include #include #include /*Removed this method as it is not required char getche();*/ char* getDateAndTime(); /*changed the parameter from long to int*/ long DecimaltoBinary(int n); /*Added this method as it required*/ int getInteger(char* str_array); int main() { /*change the type of long to int as getInteger return the integer*/ int Decimal; do { char buffer[100]; printf(" Enter an Integer (1–1000000) or type x to exit: "); fgets(buffer, 100, stdin); /*getting the decimal value from calling the method*/ Decimal=getInteger(buffer); /*if user inputs x then getInteger return -1*/ if(Decimal == -1){ printf("Goodbye! "); /*terminating the program*/ break; } printf("Decimal: %d ", Decimal); printf("Hexadecimal: %x ", Decimal); printf("Octal: %o ", Decimal); long x = DecimaltoBinary(Decimal); printf("Binary: %ld ",x); printf("Save to file? (y/n): "); char ch[5]; fgets(ch, 100, stdin); FILE *fptr; char file[100]; /*getting the user value and comparing with y*/ if(ch[0]=='y') { /*taken in while loop if file not created then againg ask for file name*/ while(1){ printf(" Enter the file name:"); fgets(file, 100, stdin); FILE *fptr; fptr = fopen(file, "w"); if(fptr == NULL) { printf("Error! on opening file exiting program"); } else{ char name[100]; printf("Enter your Name : ",&name); fgets(name,100,stdin); char* time=getDateAndTime(); /*modified the file output content*/ fprintf(fptr,"Name : %s ",name); fprintf(fptr, "%s ",time); fprintf(fptr, "Decimal : %d ",Decimal); fprintf(fptr,"Hexadecimal: %x ", Decimal); fprintf(fptr, "Octal : %o ",Decimal); fprintf(fptr,"Binary : %ld",x); printf(" File saved"); fclose(fptr); break; } } } } while (1); return 0; } long DecimaltoBinary(int n) { int remainder; long binary = 0, i = 1; while (n != 0) { remainder = n % 2; n = n / 2; binary = binary + (remainder*i); i = i*10; } return binary; } char* getDateAndTime() { time_t t = time(NULL); struct tm *tm = localtime(&t); return asctime(tm); }/*added this method this method takes string and checking if it contains x if yes then return -1 else convert the string to int by usin atoi*/ int getInteger(char *str_array) { if(str_array[0]=='x') { return -1; } return atoi(str_array); }
Explanation / Answer
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/*Removed this method as it is not required char getche();*/
char* getDateAndTime();
/*changed the parameter from long to int*/
long DecimaltoBinary(int);
char* DecimalToHexa(int);
char* DecimalToOcta(int);
/*Added this method as it required*/
int getInteger(char* str_array);
int main() {
/*change the type of long to int as getInteger return the integer*/
int Decimal;
do {
char buffer[100];
printf(" Enter an Integer (1–1000000) or type x to exit: ");
fgets(buffer, 100, stdin);
/*getting the decimal value from calling the method*/
Decimal=getInteger(buffer);
/*if user inputs x then getInteger return -1*/
if(Decimal == -1)
{
printf("Goodbye! ");
/*terminating the program*/
break;
}
printf("Decimal: %d ", Decimal);
printf("Hexadecimal: %x ", Decimal);
printf("Octal: %o ", Decimal);
long x = DecimaltoBinary(Decimal);
printf("Binary: %ld ",x);
printf("Converted Hexa: %s ", DecimalToHexa(Decimal));
printf("Converted Octa: %s ", DecimalToOcta(Decimal));
fflush(stdin);
printf("Save to file? (y/n): ");
char ch[5];
fgets(ch, 100, stdin);
FILE *fptr; char file[100];
/*getting the user value and comparing with y*/
if(ch[0]=='y')
{
/*taken in while loop if file not created then againg ask for file name*/
while(1)
{
printf(" Enter the file name:");
fgets(file, 100, stdin);
FILE *fptr;
fptr = fopen(file, "w");
if(fptr == NULL)
{
printf("Error! on opening file exiting program");
}
else
{
char name[100];
printf("Enter your Name : ",&name);
fgets(name,100,stdin);
char* time=getDateAndTime();
/*modified the file output content*/
fprintf(fptr,"Name : %s ",name);
fprintf(fptr, "%s ",time);
fprintf(fptr, "Decimal : %d ",Decimal);
fprintf(fptr,"Hexadecimal: %x ", DecimalToHexa(Decimal));
fprintf(fptr, "Octal : %o ",DecimalToOcta(Decimal));
fprintf(fptr,"Binary : %ld",x);
printf(" File saved");
fclose(fptr);
break;
}
}
}
}while (1);
return 0;
}
long DecimaltoBinary(int n)
{
int remainder;
long binary = 0, i = 1;
while (n != 0)
{
remainder = n % 2;
n = n / 2;
binary = binary + (remainder*i);
i = i*10;
}
return binary;
}
char* DecimalToHexa(int decimal)
{
//char STR[100];
char *hexa = (char *) malloc(sizeof(char) * 100);
char HEXC[5];
int num,I,J,LEN,HEXD[5];
num=decimal;
LEN=0;
while(decimal>0)
{
HEXD[LEN]=decimal%16;
decimal=decimal/16;
LEN++;
}
for(I=LEN-1;I>-1;I--)
{
if(HEXD[I]<10)
HEXC[I]=HEXD[I]+48;
else
HEXC[I]=HEXD[I]+55;
}
/* reversing the string */
for(I=LEN-1,J=0;I>-1;I--,J++)
{
hexa[J] = HEXC[I];
}
hexa[J] = '';
//printf("Hexa: %s ", hexa);
return hexa;
}
char* DecimalToOcta(int decimal)
{
char *octa = (char *) malloc(sizeof(char) * 100);
int num,I,J,len,OCT[5];
num=decimal;
len=0;
while(decimal>0)
{
OCT[len]=decimal%8;
decimal=decimal/8;
len++;
}
printf("Octa OCT: %s ", OCT);
/* reversing the string */
for(I=len-1,J=0;I>-1;I--,J++)
{
sprintf(&octa[J],"%d",OCT[I]);
}
octa[J] = '';
return octa;
}
char* getDateAndTime()
{
time_t t = time(NULL);
struct tm *tm = localtime(&t);
return asctime(tm);
}
/*added this method this method takes string and checking if it contains x if yes then return -1 else convert the string to int by usin atoi*/
int getInteger(char *str_array)
{
if(str_array[0]=='x')
{
return -1;
}
return atoi(str_array);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.