redid structure for 334 keeping things clean now

This commit is contained in:
shockrah
2019-09-08 18:26:58 -07:00
parent 6e9ef2f3d2
commit bc5affc51b
8 changed files with 107 additions and 2 deletions

64
334/homework/1/bindec.c Normal file
View 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/homework/1/binhex.txt Normal file
View 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

View 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

44
334/homework/2/msh.c Normal file
View File

@@ -0,0 +1,44 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PROMPT "msh> "
#define EXIT_CMD "exit"
// Accounting for the \n
#define MAX_BUF 121
void echo(const char*);
void remove_newline(char*);
void exit_branch(const char*);
int main(void) {
char buffer[MAX_BUF];
while(1) {
printf("%s", PROMPT);
fgets(buffer, MAX_BUF, stdin);
// process the input
remove_newline(buffer);
exit_branch(buffer);
echo(buffer);
}
return 0;
}
void echo(const char* buf) {
printf("%s\n", buf);
}
void remove_newline(char* buf) {
char* c = buf;
while(*c != '\n') { c++; }
*c='\0';
}
void exit_branch(const char* buf) {
if(strcmp(buf, EXIT_CMD)) {
return;
}
exit(0);
return; // just for gcc but exit shouldn't fail
}

View File

@@ -0,0 +1,22 @@
# Instructions: edit this file to provide your homework answers.
#
# Problem numbers are shown on lines that begin with #@.
# Please put your solution to a problem on the line *after* the
# line with the problem number.
#
# Do not modify this file *except* to add lines as needed after each problem.
# Blank lines are ignored. You don't need to put your name in the file.
#
# No points will be awarded to incorrectly-formatted files.
#
#@ 1 -- enter 'a', 'b', or 'c' (don't use quotes in any answer)
b
#@ 2 -- enter 'a', 'b', or 'c'
c
#@ 3 -- enter 'a' or 'b'
b
#@ 4 -- enter 'a' or 'b'
b
#@ 5 -- enter 'a', 'b', or 'c'
c

32
334/homework/2/puzzle.txt Normal file
View File

@@ -0,0 +1,32 @@
# Instructions: edit this file to provide your homework answers.
#
# Problem numbers are shown on lines that begin with #@.
# Please put your solution to a problem on the line *after* the
# line with the problem number.
#
# Do not modify this file *except* to add lines as needed after each problem.
# Blank lines are ignored. You don't need to put your name in the file.
#
# No points will be awarded to incorrectly-formatted files.
#
#@ 1 -- enter a single number
1
#@ 2 -- enter 'Y' or 'N'
2
#@ 3 -- enter 'Y' or 'N'
Y
#@ 4 -- enter 'Y' or 'N'
N
#@ 5 -- enter text on a single line
some stuff
#@ 6 -- enter 'Y' or 'N'
Y
#@ 7 -- enter text on a single line
stuff
#@ 8 -- enter 'Y' or 'N'
N
#@ 9 -- enter text on a single line
some stuff
#@ 10 -- enter 'Y' or 'N'
Y