Merge pull request #91 from GeoffYart/patch-1

I have merged this version of your code. Can you unitize this program with a function that gets the base and the number for converting and the converted.
This commit is contained in:
Christian Bender 2017-12-23 14:35:07 +01:00 committed by GitHub
commit 116a34754e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

38
Conversions/toDecimal.c Normal file
View File

@ -0,0 +1,38 @@
/*
* convert from any base to decimal
*/
#include <stdio.h>
#include <ctype.h>
int main(void) {
int base, i, j;
char number[100];
unsigned long decimal = 0;
printf("Enter the base: ");
scanf("%d", &base);
printf("Enter the number: ");
scanf("%s", &number[0]);
for (i = 0; number[i] != '\0'; i++) {
if (isdigit(number[i]))
number[i] -= '0';
else if (isupper(number[i]))
number[i] -= 'A' - 10;
else if (islower(number[i]))
number[i] -= 'a' - 10;
else
number[i] = base + 1;
if (number[i] > base)
printf("invalid number\n");
}
for (j = 0; j < i; j++) {
decimal *= base;
decimal += number[j];
}
printf("%lu\n", decimal);
}