diff options
author | Gentoo <installgentoo@endianness.com> | 2021-03-27 10:00:26 +1100 |
---|---|---|
committer | Gentoo <installgentoo@endianness.com> | 2021-03-27 10:00:26 +1100 |
commit | 13f15caa504af9f58948f99d5838d1bdeaa66203 (patch) | |
tree | 2f3ea8d1714c4d4f8d7037b224bdffc9854dc919 /main.c | |
download | div-algo-binary-13f15caa504af9f58948f99d5838d1bdeaa66203.tar.gz div-algo-binary-13f15caa504af9f58948f99d5838d1bdeaa66203.tar.bz2 div-algo-binary-13f15caa504af9f58948f99d5838d1bdeaa66203.zip |
initial commit
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 170 |
1 files changed, 170 insertions, 0 deletions
@@ -0,0 +1,170 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#define DEFAULT_SIZE 40 //default size out output binary string + +//powq(): square roots +unsigned long long powq(int number, int power) +{ +unsigned long long result = 1;//result int, 1 by default +if (!power){return 1;}//if to the power of 0, return 1 +while(power)//loop until all powers have been calculated + { + result *= number;//in the end result=number*number*number*etc until power is reached + --power;//--power since a power has been calculated + } +return result; +} + +//itob(): Converts and integer to binary with the division algorithm and shows working. Requires length of result string +void itob(char s[], char *result, int length) +{ +if (s[0] == '\0'){result[0] = 'E'; result[1] = '\0'; return;}//catch case of blank input +if (s[0] == '0' && s[1] == '\0'){printf("0\t| 0\n"); result[0] = '0'; result[1] = '\0'; return;}//catch case of 0 input + +int power = 0; //int to store the power. +while(s[power] != '\0'){++power;}//increment power for however many non-null chars in the string. +--power;//The correct power is actually one less than above returns + +int i = 0;//counter int +unsigned long long integer = 0;//where the converted int is stored +while (s[i] != '\0')//convert the string of decimal numbers into a long long int + { + if (s[i] >= '0' && s[i] <= '9'){integer += ((s[i] - '0') * powq(10,power));}//integer = integer + ([char converted to number] * (10^[position of number in string])) + + ++i; + --power;//Deincrement the power. E.g. ([charvalue]*(10^3))+([nextcharvalue]*(10^2)) etc + } + +i = 0; +while (integer) + { + //if the quotient divided by 2 has remainder 1, output a 1, otherwise output a 0 + if ((integer % 2 == 1)) + { + result[i] = '1'; + } + else + { + result[i] = '0'; + } + printf("%llu\t| %c\n", integer, result[i]); + + integer /= 2; + + ++i; + if (i > length-1) + { + printf("For some reason, length += <any int> fails to actually work, so your result is too long to process sorry.\n"); + result[0] = 'E'; result[1] = '\0'; + return; +// length += DEFAULT_SIZE; +// printf("Length: %d\n", length); +// result = realloc(result, length*sizeof(char)); +// if (!result){fprintf(stderr, "Realloc failed!\n"); exit(1);} + } + } +result[i] = '\0';//terminate result with null char + +char c; +int j = i-1;//i-1 is the first character at the end of the string + +//the string needs to be reversed, for the result to be correct. + +for(i=0; i < j; ++i, --j) + { + c = result[j]; + result[j] = result[i]; + result[i] = c; + } + +} + +int main(int argc, char *argv[]) +{ +char input[DEFAULT_SIZE];//input string is DEFAULT_SIZE in length + +//result string +char *result = malloc(DEFAULT_SIZE*sizeof(char)); +if (!result){printf("Malloc failed!\n"); return 1;} +result[DEFAULT_SIZE-1]='\0';//add null terminating char + +if (!argv[1]){goto stdinMode;} + + if (argv[1][1] == '-') + { + printf("Usage (stdin mode): %s\n", argv[0]); + printf("Ctrl+D to exit\n"); + printf("Usage (cli args mode): %s [decnum]\n", argv[0]); + printf("Converts decimal number to binary and via the division algorithm.\n"); + printf("Licence: GPLv3\n"); + return 0; + } + + + //strip out invalid chars from the input string + int a,q;//counter variables + for (a=0; argv[1][a] != '\0'; ++a) + { + if ((argv[1][a] < '0')||(argv[1][a] > '9'))//if argv[1][i] is a character to remove.... + { + q=a; + while(argv[1][q] != '\0'){argv[1][q++] = argv[1][q+1];}//shift all characters above left, overwriting the character + --a;//deincrement a, since the string was just shrunk by one and the one just above could indeed be a matching char + } + } + + + + itob(argv[1], result, DEFAULT_SIZE-1); + printf("%s\n",result);//print string with all leading 0's + + free(result); + return 0; + +stdinMode: ; +int i; +nextnum: i=0; +char *character = fgets(input, DEFAULT_SIZE, stdin); +if (!character){free(result); exit(0);}//exit if EOF (fgets() returns -1 on EOF or error) +character = strstr(input,"\n");//try to find the newline in the input so it can be overwritten +if (!character){printf("Input too long\n"); while(getchar() != '\n'); goto nextnum;}//if more than 40 chars were read, input was too long +character = '\0'; //overwrite newline with null char + +//strip out invalid chars from the input string +int j;//another counter variable +for (i=0; input[i] != '\0'; ++i) + { + if ((input[i] < '0')||(input[i] > '9'))//if input[i] is an invalid character + { + j=i; + while(input[j] != '\0'){input[j++] = input[j+1];}//shift all characters above left, overwriting the character + --i;//deincrement i, since the string was just shrunk by one and the one just above could indeed be a matching char + } + } + +itob(input, result, DEFAULT_SIZE-1); +int resultlen = strlen(result); +if ((resultlen % 4) == 0) + { + printf("Result: "); + for (i=0; i < resultlen; i += 4) + { + printf("%c%c%c%c ",result[i],result[i+1],result[i+2],result[i+3]); + } + printf("\bb\n"); + } +else + { + printf("Result: %sb\n", result); + } + +goto nextnum; + +//this wont be reached, but included anyway in case the above goto is removed +free(result); + +return 0; +} + |