diff --git a/334/Makefile b/334/Makefile new file mode 100644 index 0000000..5a2856f --- /dev/null +++ b/334/Makefile @@ -0,0 +1,3 @@ +# Single make file for everything becauz lazy +1: + gcc bindec.c -o bin diff --git a/334/bin b/334/bin new file mode 100755 index 0000000..c1564b3 Binary files /dev/null and b/334/bin differ diff --git a/334/bindec.c b/334/bindec.c new file mode 100644 index 0000000..10579f8 --- /dev/null +++ b/334/bindec.c @@ -0,0 +1,64 @@ +#include +#include +#include + +// max length input string +#define MAXSTR 25 + +// convert input binary string to a number +int bindec(char*, size_t); + +int main() { + + // user input string + char s[MAXSTR+3]; + + // prompt for input + printf("> "); + + // read input string; at most MAXSTR+1 chars accepted + // Note: this is tricky. If we accept only MAXSTR chars, + // we can't see if user entered more chars and they are + // being dropped by fgets. + fgets(s, MAXSTR+3, stdin); + + // check input length; n does not include final carriage return + int n = strlen(s)-1; + if (n > MAXSTR) { + printf("input cannot be more than %d characters\n", MAXSTR); + exit(1); + } + + for(size_t i = 0; i < n; i++) { + if(s[i] == '0' || s[i] == '1') { + continue; + } + else { + printf("input must contain only zeros and ones\n"); + exit(1); + } + } + if(!n) { + printf("No input given only hit enter\n"); + exit(1); + } + + int output = bindec(s, MAXSTR); + printf("%d\n", output); + + return 0; +} + +int bindec(char* buf, size_t len) { + int ret = 0; + for(size_t i = 0; i < len; i++) { + if(buf[i] == '1') { + ret <<= 1; + ret++; + } + else { + ret <<= 0; + } + } + return ret; +} diff --git a/334/responses/binhex.txt b/334/responses/binhex.txt new file mode 100644 index 0000000..573c944 --- /dev/null +++ b/334/responses/binhex.txt @@ -0,0 +1,47 @@ + +# Problem lines begin with #@. +# For each problem, put your answer on +# the line following the problem line. +# Do not modify this file in any way +# except to insert your answers. + +#@ 1 -- enter a number +22 +#@ 2 -- enter a number +7 +#@ 3 -- enter a number +3 +#@ 4 -- enter a number +25 +#@ 5 -- enter a number +255 +#@ 6 -- enter a binary number +1101 +#@ 7 -- enter a binary number +10101 +#@ 8 -- enter a binary number +10000 +#@ 9 -- enter a binary number +110111 +#@ 10 -- enter a binary number +1010000 +#@ 11 -- enter a number +44 +#@ 12 -- enter a number +18 +#@ 13 -- enter a number +471 +#@ 14 -- enter a number +255 +#@ 15 -- enter a number +123 +#@ 16 -- enter a hex number (beginning with 0x) +0x12 +#@ 17 -- enter a hex number +0x05 +#@ 18 -- enter a hex number +0x58 +#@ 19 -- enter a hex number +0x7b +#@ 20 -- enter a hex number +0x64 diff --git a/334/responses/os-intro.txt b/334/responses/os-intro.txt new file mode 100644 index 0000000..eb32a76 --- /dev/null +++ b/334/responses/os-intro.txt @@ -0,0 +1,19 @@ + +# Homework 1 answer sheet. +# +# Problem lines begin with #@. +# For each problem, put your answer on +# the line following the problem line. +# Do not modify this file in any way +# except to insert your answers. + +#@ 1 -- enter 'a', 'b', or 'c' (don't use quotes in any answer) +a +#@ 2 -- enter 'a', 'b', or 'c' +b +#@ 3 -- enter one or more letters, separated by commas +b +#@ 4 -- enter 'a' or 'b' +a +#@ 5 -- enter 'a', 'b', or 'c' +b