Commit e93eb5bb by chazog

Upload New File

parent ca5d1e82
Showing with 384 additions and 0 deletions
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int valDec(char *string);
int valHex(char *string);
int valOct(char *string);
int valBin(char *string);
int decToHex(int n);
int decToOct(int n);
int decToBin(int n);
int main()
{
int r = 0;
do
{
int i, val, d = 0, dig = 0, p, base, len, dec = 0;
char string[100];
do
{
printf("Enter the base of the number that you would like to check.\n\
1 - Decimal\n\
2 - Hexadecimal\n\
3 - Octal\n\
4 - Binary\n\
5 - EXIT\n");
scanf("%d", &base);
switch (base)
{
case 1:
printf("Enter the number: ");
scanf("%s", string);
p = valDec(string);
if (p == 0)
{
printf("Given input is a decimal number\n");
d = 1;
len = strlen(string);
for (i = 0; i < len; i++)
{
dec = dec * 10 + (string[i] - '0');
}
}
else
{
printf("Given input is not a decimal number\n\n");
d = 0;
}
break;
case 2:
printf("Enter the number: ");
scanf("%s", string);
p = valHex(string);
if (p == 0)
{
printf("Given input is a Hexadecimal number\n");
d = 1;
len = strlen(string);
dig = len - 1;
for (i = 0; i < len; i++)
{
if (string[i] >= '0' && string[i] <= '9')
{
val = string[i] - 48;
}
else if (string[i] >= 'a' && string[i] <= 'f')
{
val = string[i] - 97 + 10;
}
else if (string[i] >= 'A' && string[i] <= 'F')
{
val = string[i] - 65 + 10;
}
dec = dec + val * pow(16, dig);
dig--;
}
}
else
{
printf("Given input is not Hexadecimal number\n\n");
d = 0;
}
break;
case 3:
printf("Enter the number: ");
scanf("%s", string);
p = valOct(string);
if (p == 0)
{
printf("Given input is an Octadecimal number\n");
d = 1;
len = strlen(string);
for (i = 0; i < len; i++)
{
dec = dec * 8 + (string[i] - '0');
}
}
else
{
printf("Given input is not an Octadecimal number\n\n");
d = 0;
}
break;
case 4:
printf("Enter the number: ");
scanf("%s", string);
p = valBin(string);
if (p == 0)
{
printf("Given input is a Binary number\n");
d = 1;
len = strlen(string);
for (i = 0; i < len; i++)
{
dec = dec * 2 + (string[i] - '0');
}
}
else
{
printf("Given input is not a Binary number\n\n");
d = 0;
}
break;
case 5:
r = 1;
return 0;
break;
default:
printf("Invalid option! Choose a valid option\n");
d = 0;
break;
}
} while (d == 0);
do
{
printf("\n\nSelect which base you would like to convert it into\n\
1 - Decimal\n\
2 - Hexadecimal\n\
3 - Octal\n\
4 - Binary\n");
scanf("%d", &base);
switch (base)
{
case 1:
printf("The decimal value is %d", dec);
d = 1;
break;
case 2:
printf("The Hexadecimal value for the input is: ");
decToHex(dec);
d = 1;
break;
case 3:
printf("The Octadecimal value for the input is: ");
decToOct(dec);
d = 1;
break;
case 4:
printf("The binary value for the input is: ");
decToBin(dec);
d = 1;
break;
default:
printf("Invalid option! Choose a valid option\n");
d = 0;
break;
}
} while (d == 0);
printf("\n\n\n");
} while (r == 0);
return 0;
}
int valDec(char string[100])
{
int length, i, val;
length = strlen(string);
for (i = 0; i < length; i++)
{
if (!isdigit(string[i]))
{
val = 1;
}
else
{
val = 0;
}
}
return val;
}
int valHex(char string[100])
{
int length, i, val;
length = strlen(string);
for (i = 0; i < length; i++)
{
if (!isxdigit(string[i]))
{
val = 1;
}
else
{
val = 0;
}
}
return val;
}
int valOct(char string[100])
{
int length, i, val;
length = strlen(string);
for (i = 0; i < length; i++)
{
if (string[i] == '0' || string[i] == '1' || string[i] == '2' || string[i] == '3' || string[i] == '4' || string[i] == '5' || string[i] == '6' || string[i] == '7')
{
val = 0;
}
else
{
val = 1;
}
}
return val;
}
int valBin(char string[100])
{
int length, i, val;
length = strlen(string);
for (i = 0; i < length; i++)
{
if (string[i] == '0' || string[i] == '1')
{
val = 0;
}
else
{
val = 1;
}
}
return val;
}
int decToHex(int n)
{
char hex[1000];
int j, temp, i = 0;
while (n != 0)
{
temp = 0;
temp = n % 16;
if (temp < 10)
{
hex[i] = temp + 48;
i++;
}
else
{
hex[i] = temp + 55;
i++;
}
n = n / 16;
}
for (j = i - 1; j >= 0; j--)
{
printf("%c", hex[j]);
}
return 0;
}
int decToOct(int n)
{
int oct[1000];
int j, i = 0;
while (n > 0)
{
oct[i] = n % 8;
n = n / 8;
i++;
}
for (j = i - 1; j >= 0; j = j - 1)
{
printf("%d", oct[j]);
}
return 0;
}
int decToBin(int n)
{
int bin[1000];
int j, i = 0;
while (n > 0)
{
bin[i] = n % 2;
n = n / 2;
i++;
}
for (j = i - 1; j >= 0; j = j - 1)
{
printf("%d", bin[j]);
}
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment