In: Computer Science
Create design document for a program that will allow the user:
PLEASE ADD PSEUDOCODE AND USE C PROGRAMMING
USE FUNCTIONS IF POSSIBLE
/*
program is written in c programming language for all the above operations using function
*/
#include<math.h>
#include <stdio.h>
void numtoBinary(int n);
void Binarytodec(int n);
void hextobin(char* hd);
void bintohex(int n);
//conversion from number to
ieee single precision
int binary(int n, int i)
{
int k;
for (i--; i >= 0; i--)
{
k = n >> i;
if (k & 1)
printf("1");
else
printf("0");
}
}
typedef union
{
float f;
struct
{
unsigned int mantissa : 23;
unsigned int exponent : 8;
unsigned int sign : 1;
} field;
} myfloat;
//conversion from ieee single precision to number
typedef union {
float f;
struct
{
unsigned int mantissa : 23;
unsigned int exponent : 8;
unsigned int sign : 1;
} raw;
} myfloat1;
unsigned int convertToInt(int* arr, int low, int high)
{
unsigned f = 0, i;
for (i = high; i >= low; i--) {
f = f + arr[i] * pow(2, high - i);
}
return f;
}
//main method
int main()
{
int ch,a,i;
char str[100] ;
myfloat var;
myfloat1 var1;
unsigned int ieee[32];
printf("1.conversion from number to binary\n 2.conversion from
binary to number\n3.conversion from hexadecimal to binary");
printf("\n 4.conversion from binary to hexadecimal\n 5.conversion
from number to IEEE single pricision\n 6.conversion from IEEE
single pricision to number");
printf("\n \n enetr a choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter a decimal number");
scanf("%d",&a);
numtoBinary(a);
break;
case 2:
printf("enter a
binary number");
scanf("%d",&a);
Binarytodec(a);
break;
case 3:
printf("enter a hexadecimal number");
scanf("%s",str);
hextobin(str);
break;
case 4:
printf("enter a binary
number");
scanf("%d",&a);
bintohex(a);
break;
case 5:
printf("Enter any float number: ");
scanf("%f",&var.f);
printf("%d ",var.field.sign);
binary(var.field.exponent, 8);
printf(" ");
binary(var.field.mantissa, 23);
printf("\n");
break;
case 6:
printf("enter 32 bit unsigned IEEE number");
for(i=0;i<32;i++)
{
scanf("%d",&ieee[i]);
}
unsigned f = convertToInt(ieee, 9, 31);
var1.raw.mantissa = f;
f = convertToInt(ieee, 1, 8);
var1.raw.exponent = f;
var1.raw.sign = ieee[0];
printf("The float value of the given IEEE-754 representation is :
\n");
printf("%f", var1.f);
break;
default:
printf("invalid
choice");
break;
}
return 0;
}
//conversion from decimal
to binary
void numtoBinary(int n)
{
int bN[32];
printf("decimal number : %d",n);
int i = 0;
while (n > 0) {
bN[i] = n % 2;
n = n / 2;
i++;
}
printf("\n binary number :");
for (int j = i - 1; j >= 0; j--)
printf("%d",bN[j]);
}
//conversion from binary to
decimal
void Binarytodec(int n)
{
int dec = 0, i = 0, rem;
printf("binary number : %d",n);
while (n != 0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}
printf("\n decimal number :%d",dec);
}
//conversion from
hexadecimal to binary
void hextobin(char* hd)
{
long int i = 0;
while (hd[i]) {
switch (hd[i]) {
case '0':
printf("0000");
break;
case '1':
printf("0001");
break;
case '2':
printf("0010");
break;
case '3':
printf("0011");
break;
case '4':
printf("0100");
break;
case '5':
printf("0101");
break;
case '6':
printf("0110");
break;
case '7':
printf("0111");
break;
case '8':
printf("1000");
break;
case '9':
printf("1001");
break;
case 'A':
case 'a':
printf("1010");
break;
case 'B':
case 'b':
printf("1011");
break;
case 'C':
case 'c':
printf("1100");
break;
case 'D':
case 'd':
printf("1101");
break;
case 'E':
case 'e':
printf("1110");
break;
case 'F':
case 'f':
printf("1111");
break;
default:
printf("\nInvalid hexadecimal digit %c",
hd[i]);
}
i++;
}
}
//conversion from binary to
hexadecimal
void bintohex(int bval)
{
long int hexval = 0, i = 1, rem;
while (bval != 0)
{
rem = bval % 10;
hexval = hexval + rem * i;
i = i * 2;
bval = bval / 10;
}
printf("hexadecimal number: %lX", hexval);
}