C programming struggling on the part with y/n and saving the file... please incl
ID: 3604209 • Letter: C
Question
C programming
struggling on the part with y/n and saving the file... please include comments thank you
ConvertDec to Hex-Oct-Bin.c ConvertDec_to_Hex-0ct-Bin.c is a simple conversion program that prompts the user for an integer from 1 to 1,000,000, and displays this integer value in hexadecimal, octal, and binary representation. 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. If the requested text file cannot be created, user is re-prompted for a different file name. The text file will contain 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: FO Octal: 360 Binary: 11110000 Save to a file? (y/n): y Enter file name: cs222 hw3.txt File saved Enter an Integer (1- 1000000) or type x to exit: x Good byeExplanation / Answer
#include <iostream>
#include<stdio.h>
#include<string.h>
char* reversestring(char *a)
{
int i,l;
char *r;
l=strlen(a);
r=(char*)malloc(l*sizeof(char));
for(i=0;i<l;i++)
r[i]=*(a+l-i-1);
r[i]='';
return r;
}
char*DecToHex(int dec)
{
char hex[10];
int i=0,tnum,rem;
tnum=dec;
while(tnum!=0)
{
rem=tnum%16;
hex[i++]=rem<=9?'0'+rem:'A'+rem-10;
tnum/=16;
}
hex[i]='';
return(reversestring(hex));
}
int DecToOct(int dec)
{
int oct=0;
int i=1,tnum,rem;
tnum=dec;
while(tnum!=0)
{
rem=tnum%8;
oct=i*rem+oct;
tnum/=8;
i=i*10;
}
return(oct);
}
int DecToBin(int dec)
{
int bin=0;
int i=1,tnum,rem;
tnum=dec;
while(tnum!=0)
{
rem=tnum%2;
bin=i*rem+bin;
tnum/=2;
i*=10;
}
return(bin);
}
int BinToDec(int bin)
{
int rem,dec=0,base=1;
int num;
num=bin;
while(num>0)
{
rem = num % 10;
dec = dec + rem * base;
num = num / 10 ;
base = base * 2;
}
return dec;
}
int HexToDec(char* hex)
{
int dec=0,base=1,i,d;
char c;
i=strlen(hex)-1;
while(i>=0)
{
c=*(hex+i);
if(c>='A'&&c<='F')
d=10+c-'A';
else if(c>='a'&&c<='f')
d=10+c-'a';
else
d=c-'0';
dec+=d*base;
base*=16;
i--;
}
return dec;
}
int OctToDec(int oct)
{
int dec=0,base=1;
int t;
t=oct;
while(t>0)
{
dec+=(t%10)*base;
t/=10;
base*=8;
}
return dec;
}
void int main()
{
int inputnum,from,to,fromto,out;
char inputs[10];
char *outs;
printf(" Choose the number system to convert from. ");
printf("From: 1.Decimal 2.Binary 3.Octal 4.Hexadecimal ");
scanf("%d", &from);
printf(" enter the number. ");
if(from<4)
scanf("%d",&inputnum);
else
scanf("%s",inputs);
printf("Choose the number system to convert to. ");
printf("To: 1.Decimal 2.Binary 3.Octal 4.Hexadecimal ");
scanf("%d", &to);
fromto=from*10+to;
switch(fromto)
{
case 12:out=DecToBin(inputnum);break;
case 13:out=DecToOct(inputnum);break;
case 14:outs=DecToHex(inputnum);break;
case 21:out=BinToDec(inputnum);break;
case 23:out=DecToOct(BinToDec(inputnum));break;
case 24:outs=DecToHex(BinToDec(inputnum));break;
case 31:out=OctToDec(inputnum);break;
case 32:out=DecToBin(OctToDec(inputnum));break;
case 34:outs=DecToHex(OctToDec(inputnum));break;
case 41:out=HexToDec(inputs);break;
case 42:out=DecToBin(HexToDec(inputs));break;
case 43:out=DecToOct(HexToDec(inputs));break;
}
if(to==4)
printf(" %s",outs);
else
printf(" %d",out);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.