Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions assignment2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Harry Brickner

1) Sure! For example, "int six = 1 + 2 + 3;"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, so long as each operator has the required amount of operands


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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right!


3) True
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remember that there are no boolean values in C, so it's technically 1


4) It is not valid. "0 < x" is a boolean, so the "< 15" tries to compare itself to a boolean value.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct.


5) 1) invalid: an if() is followed by a {, not a "then"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct.

2) invalid: the boolean needs to be surrounded by parentheses
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right!

3) valid
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct!

4) valid
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

take another look and find the mistake!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. ...
    4) invalid: there's no semicolon after the "c = 0"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup! 👍


131 changes: 131 additions & 0 deletions calculator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#include <stdio.h>
#include <stdlib.h>

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;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is impressive! Very nicely done. I will point out that your mod function is a bit flawed (53 % 9 returned 9). As is, since only integers are valid for the mod operation, casting to int for the operation would have been acceptable. Your code is nicely organized and your variables/functions are well-named. My only other suggestion is to include on-screen instructions for the user, and as always, comments would be appreciated (name, as well as comments for areas that aren't immediately obvious, such as your switch statement in update()). Ideally you should always document what each function does in a comment above it as a courtesy to anyone who may be using or looking at your code.

I also am glad you included your original calculator, written to spec. By all means, for future assignments, continue to add to your code as you did here (submit your original as well, for our reference).

Very nicely done, overall.

31 changes: 31 additions & 0 deletions calculatorOld.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <stdio.h>

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;
}
21 changes: 21 additions & 0 deletions randnum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdio.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect job! My only suggestion is to add comments!

#include <stdlib.h>
#include <time.h>

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;
}
}
}
13 changes: 13 additions & 0 deletions sign.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <stdio.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect job! Nice touch with the ternary conditionals and comments!

/* 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;
}