diff --git a/assignment2.txt b/assignment2.txt new file mode 100644 index 0000000..830d042 --- /dev/null +++ b/assignment2.txt @@ -0,0 +1,15 @@ +Harry Brickner + +1) Sure! For example, "int six = 1 + 2 + 3;" + +2) As far as the computer is concerned, the only errors that exist are syntax errors. A program will run with logical errors, but not work the way it's intended to, whereas a program will not run if it has syntax errors. + +3) True + +4) It is not valid. "0 < x" is a boolean, so the "< 15" tries to compare itself to a boolean value. + +5) 1) invalid: an if() is followed by a {, not a "then" + 2) invalid: the boolean needs to be surrounded by parentheses + 3) valid + 4) valid + diff --git a/calculator.c b/calculator.c new file mode 100644 index 0000000..6aac85d --- /dev/null +++ b/calculator.c @@ -0,0 +1,131 @@ +#include +#include + +char ascii[21][31] = { + "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "b b", + "b b", + "b b", + "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "b b b b b b", + "b 1 b 2 b 3 b + b = b", + "b b b b b b", + "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "b b b b b b", + "b 4 b 5 b 6 b - b del b", + "b b b b b b", + "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "b b b b b b", + "b 7 b 8 b 9 b * b clr b", + "b b b b b b", + "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "b b b b b b", + "b 0 b . b % b / b exitb", + "b b b b b b", + "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" +}; +char value[30] = " "; +char buttons[4][5] = { + {'1', '2', '3', '+', '='}, + {'4', '5', '6', '-', 'd'}, + {'7', '8', '9', '*', 'c'}, + {'0', '.', '%', '/', 'x'} +}; +int selectedX = 0; +int selectedY = 0; + +void replaceBackground(char from, char to){ + int selectedXLoc = selectedX * 6 + 1; + int selectedYLoc = selectedY * 4 + 5; + for(int x = 0; x < 5; x++){ + for(int y = 0; y < 4; y++){ + if(ascii[selectedYLoc + y][selectedXLoc + x] == from) + ascii[selectedYLoc + y][selectedXLoc + x] = to; + } + } +} +void scrollUp(int lines){ + for(int i = 0; i < lines; i++) printf("\033[F"); +} +void update(){ + scrollUp(22); + ascii[2][0] = 'b'; + ascii[2][30] = 'b'; + for(int i = 0; i < 29; i++) ascii[2][i + 1] = value[i]; + for(int i = 0; i < 21; i++){ + for(int j = 0; j < 31; j++){ + switch(ascii[i][j]){ + case 's': printf("%s", "░"); break; + case 'b': printf("%s", "█"); break; + default: printf("%c", ascii[i][j]); + } + } + printf("\n"); + } +} + +void changeSelected(int deltaX, int deltaY){ + replaceBackground('s', ' '); + /* have to do this because modulo operators give negative results in certain cases */ + selectedX = (((selectedX + deltaX) % 5) + 5) % 5; + selectedY = (((selectedY + deltaY) % 4) + 4) % 4; + replaceBackground(' ', 's'); +} +double mod(double a, double b){ + while(a >= 0){ + a -=b; + } + a += b; + return b; +} +int printIndex = 0; +void executeButton(){ + + + double num1, num2; + char operation; + double output; + switch(buttons[selectedY][selectedX]){ + case 'd': if(printIndex == 0) break; value[--printIndex] = ' '; break; + case 's': for(int i = 0; i < 30; i++)value[i] = ' '; printIndex = 0; break; + case '=': + + if(sscanf(value, "%lf%c%lf", &num1, &operation, &num2) < 3){ + printf("Error\n"); + exit(0); + } + + switch(operation){ + case '+': output = num1 + num2; break; + case '-': output = num1 - num2; break; + case '*': output = num1 * num2; break; + case '/': output = num1 / num2; break; + case '%': output = mod(num1, num2); break; + } + printf("%f\n", output); + case 'x': exit(0); + default: value[printIndex++] = buttons[selectedY][selectedX]; + } + update(); + printf("%d", printIndex); +} + + +int main(){ + for(int i = 0; i < 22; i++)printf("\n"); + replaceBackground(' ', 's'); + update(); + while(1){ + char input = getchar(); + switch(input){ + case 'q': return 0; + case 'w': changeSelected(0, -1); update(); break; + case 'a': changeSelected(-1, 0); update(); break; + case 's': changeSelected(0, 1); update(); break; + case 'd': changeSelected(1, 0); update(); break; + case 'e': executeButton(); break; + + } + } + return 0; +} diff --git a/calculatorOld.c b/calculatorOld.c new file mode 100644 index 0000000..4e94ade --- /dev/null +++ b/calculatorOld.c @@ -0,0 +1,31 @@ +#include + +double mod(double a, double b){ + while(a >= 0){ + a -=b; + } + a += b; + return b; +} +int main(){ + double num1; + double num2; + printf("Hello, what's your first number?\n"); + scanf("%lf", &num1); + printf("And what's your second number?\n"); + scanf("%lf", &num2); + printf("Which of the following operations should I do? + - * / %%\n"); + char operation; + scanf("\n%c", &operation); + + double output; + switch(operation){ + case '+': output = num1 + num2; break; + case '-': output = num1 - num2; break; + case '*': output = num1 * num2; break; + case '/': output = num1 / num2; break; + case '%': output = mod(num1, num2); break; + } + printf("Answer is %lf\n", output); + return 0; +} diff --git a/randnum.c b/randnum.c new file mode 100644 index 0000000..d11901f --- /dev/null +++ b/randnum.c @@ -0,0 +1,21 @@ +#include +#include +#include + +int main(){ + srand(time(0)); + int r = (rand() % 10) + 1; + printf("guess a number!\n"); + while(1){ + int guess; + scanf("%d",&guess); + if(guess > r){ + printf("Too high!\n"); + }else if(guess < r){ + printf("Too low!\n"); + }else{ + printf("Correct!\n"); + return 0; + } + } +} diff --git a/sign.c b/sign.c new file mode 100644 index 0000000..0ce2f9b --- /dev/null +++ b/sign.c @@ -0,0 +1,13 @@ +#include +/* Harry Brickner + Tells someone if a number is positive or negative. */ +int main(){ + float input; + printf("Hello! Gimme a number please!\n"); + scanf("%f", &input); + /* I cant figure out string concatenation so... here's this instead */ + printf("It's "); + printf(input > 0? "Positive" : input < 0? "Negative" : "Zero"); + printf("\n"); + return 0; +}