#include
#include
void octal (char *choiceC, int *answerC );
void hexidecimal (char *choiceB, int *answerB);
void decimal (char *choiceA, int *answerA);
void bin2dec(void);
void dec2bin (int *dec);
void main( void )
{
char go;
char binY;
char choice1;
char choice2;
int numtest;
int bintest;
int flag;
flag = 0;
printf ("\n\n");
printf ("This is an integer conversion program.\n");
go = 'y';
do
{
printf ("\n\n");printf("********************************************************\n");
printf("* HEXADECIMAL DECIMAL OCTAL CONVERSION TO BINARY *\n");
printf("* NOTE: You should apply binary into a decimal integer*\n");
printf("* in converting into hexadecimal or octal *\n");
printf("********************************************************\n");
printf("D=DECIMAL\n");
printf("HEXADECIMAL\n");
printf("O=OCTAL\n");
printf("B=BINARY\n");
scanf("%c", &choice1);
getchar();
printf ("\n\n");
printf ("Number : ");
if ((choice1 == 'd') || (choice1 == 'D'))
{
scanf ("%d", &numtest);
getchar();
}
else if ((choice1 == 'h') || (choice1 == 'H'))
{
scanf ("%x", &numtest);
getchar();
}
else if ((choice1 == 'b') || (choice1 == 'B'))
{
scanf ("%d", &numtest);
getchar();
}
else if ((choice1 == 'o') || (choice1 == 'O'))
{
scanf ("%o", &numtest);
getchar();
}
else
{
flag = 1;
system ("cls");
printf ("You enterd the wrong values for Choice of Number.");
printf ("\nPlease proceed with other options of the program...");
goto close; /* Okay, so I cheated. BFD.
I didn't want to use a GOTO,
But I was getting tired and sleepy
*/
}
/*
*
* ABOVE IS
*BINARY READ AREA
*
*
*
*/
bintest = numtest;
printf ("\n");
/*
* Call to function to convert to Binary
*/
dec2bin(&bintest);
printf ("\n");
printf ("\n");
printf (" CONVERT TO:\n");
printf (" D=DECIMAL\n");
printf (" H=HEXADECIMAL\n");
printf (" O=OCTAL\n");
scanf ("%c", &choice2);
getchar();
if ((choice2 == 'd') || (choice2 == 'D'))
{
decimal (&choice1, &numtest);
}
else if ((choice2 == 'h') || (choice2 == 'H'))
{
hexidecimal (&choice1, &numtest);
}
else if ((choice2 == 'o') || (choice2 == 'O'))
{
octal (&choice1, &numtest);
}
else
{
flag = 1;
system ("cls");
printf ("You enterd the wrong values for Choice of Number.");
printf ("\nPlease proceed with other options of the program...");
goto close; /* Okay, so I cheated. BFD.
I didn't want to use a GOTO,
But I was getting tired and sleepy
*/
}
printf ("\n");
printf ("Do you wish to type in a binary number and convert it to decimal?");
printf ("\n Y for Yes, and N for no : ");
scanf ("%c", &binY);
getchar();
if ((binY == 'Y') || (binY == 'y'))
{
bin2dec();
}
else if ((binY != 'y') || (binY != 'Y'))
{
flag = 1;
system ("cls");
printf ("\nPlease proceed with other options of the program...");
goto close; /* Okay, so I cheated. BFD.
I didn't want to use a GOTO,
But I was getting tired and sleepy
*/
}
close:
printf ("\n\n");
printf ("The system is preparing to exit. Either you mistyped an entry,\n");
printf ("or you have reached the end of the program. \n\n");
printf ("Go again? (Y for Yes) : ");
scanf ("%c", &go);
getchar();
system("cls");
numtest = '\0';
choice1 = '\0';
choice2 = '\0';
} while ((go == 'y') || (go == 'Y'));
printf ("Thank you for using the program.\n");
}
void decimal(char *choiceA, int *answerA)
{ int ans = *answerA;
char ch = *choiceA;
if ((ch == 'd') || (ch == 'D'))
{
printf ("\n\nThe integer ( %d ) in decimal is equivalent to ( %d ) in decimal.\n", ans, ans);
}
else if ((ch == 'h') || (ch == 'H'))
{
printf ("\n\nThe integer ( %x ) in hex is equivalent to ( %d ) in decimal.\n", ans, ans);
}
else if ((ch == 'o') || (ch == 'O'))
{
printf ("\n\nThe integer ( %o ) in octal is equivalent to ( %d ) in decimal.\n", ans, ans);
}
}
void hexidecimal (char *choiceB, int *answerB)
{ int ans = *answerB;
char ch = *choiceB;
if ((ch == 'd') || (ch == 'D'))
{
printf ("\n\nThe integer ( %d ) in decimal is equivalent to ( %x ) in HEXADECIMAL.\n", ans, ans);
}
else if ((ch == 'h') || (ch == 'H'))
{
printf ("\n\nThe integer ( %x ) in hex is equivalent to ( %x ) in HEXADECIMAL.\n", ans, ans);
}
else if ((ch == 'o') || (ch == 'O'))
{
printf ("\n\nThe integer ( %o ) in octal is equivalent to ( %x ) in HEXADECIMAL.\n", ans, ans);
}
}
void octal (char *choiceC, int *answerC)
{ int ans = *answerC;
char ch = *choiceC;
if ((ch == 'd') || (ch == 'D'))
{
printf ("\n\nThe integer ( %d ) in decimal is equivalent to ( %o ) in octal.\n", ans, ans);
}
else if ((ch == 'h') || (ch == 'H'))
{
printf ("\n\nThe integer ( %x ) in hex is equivalent to ( %o ) in octal. \n", ans, ans);
}
else if ((ch == 'o') || (ch == 'O'))
{
printf ("\n\nThe integer ( %o ) in octal is equivalent to ( %o ) in octal.\n", ans, ans);
}
}
void bin2dec(void)
{
char binaryhold[512];
char *binary;
int i=0;
int dec = 0;
int z;
printf ("Please enter the binary digits.\n");
printf ("Binary digits are either 0 or 1, and nothing else! \n");
printf ("Binary Entry : ");
binary = gets(binaryhold);
i=strlen(binary);
for (z=0; z
{
dec=dec*2+(binary[z]=='1'? 1:0); /* if Binary[z] is equal to 1, then 1 else 0 */
}
printf ("\n");
printf ("Decimal value of %s is %d", binary, dec);
printf ("\n");
}
void dec2bin (int *dec)
{
int input = *dec;
int i;
int count = 0;
int binary [32]; /* 32 Bit MAX only
32 elements total */
/* DISABLED REQUEST FOR INPUT
printf ("Enter num to conver to binary :");
scanf ("%d", &input);
getchar();
*/
do
{
i = input%2; /* MOD 2 to get 1 or a 0*/
binary[count] = i;/* Load Elements into the Binary Array */
input = input/2; /* Divid input by 2 to decrement via binary */
count++; /* Count how many elements is needed*/
}while (input > 0);
/* Reverse and output binary digits */
printf ("Binary representation is: ");
do
{
printf ("%d", binary[count - 1]);
count--;
} while (count > 0);
printf ("\n");
}
No comments:
Post a Comment