334 things smh
This commit is contained in:
parent
abd25a0086
commit
60ae1f3048
3
334/Makefile
Normal file
3
334/Makefile
Normal file
@ -0,0 +1,3 @@
|
||||
# Single make file for everything becauz lazy
|
||||
1:
|
||||
gcc bindec.c -o bin
|
64
334/bindec.c
Normal file
64
334/bindec.c
Normal file
@ -0,0 +1,64 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// 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;
|
||||
}
|
47
334/responses/binhex.txt
Normal file
47
334/responses/binhex.txt
Normal file
@ -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
|
19
334/responses/os-intro.txt
Normal file
19
334/responses/os-intro.txt
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user