Created
May 29, 2020 20:05
-
-
Save mustooch/fb22f2e5b6d0b0c8357515d15f88ce5c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
#define ERR -1 | |
int hua2dec(char *str) { | |
long int res = 0; | |
int len = strlen(str); | |
if (str[0] != 'h' || str[len-1] != 'a') return ERR; | |
int power = len-3; | |
for (int i=1; i<=strlen(str)-2; i++) { | |
if (str[i] == 'U') { | |
res += ceil((int)pow(2, power)); | |
power--; | |
} else if (str[i] == 'u') { | |
power--; | |
} else { | |
return ERR; | |
} | |
} | |
return res; | |
} | |
int main () { | |
printf("hUUuuuUua : %u\n", hua2dec("hUUuuuUua")); | |
printf("huuUUuua : %u\n", hua2dec("huuUUuua")); | |
printf("huuuuUa : %u\n", hua2dec("huuuuUa")); | |
printf("hUUUUUa : %u\n", hua2dec("hUUUUUa")); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
numbers take the form hUuUuUua where 'h' and 'a' get removed, and the U's and u's get processed as 1's and 0's (respectively) in binary - returns in decimal form.