Hello,
I need help finishing my calculator. Unfortunatey the teacher has not been much help, and I have struggled with this for two and a half weeks now.
This is my code. I will attach my flowchart as well. Please help.
int numButtons, wd, ht, choice;
Float acc = 0.0;
int op = 0;
float result = 0;
String num = "";
boolean decPoint = false;
//declares arrays for drawing boxes locations
//0---------------0----1----2----3----4----5----6----7----8----9 + _ * / = . C
int[] x = {200, 150, 200, 250, 150, 200, 250, 150, 200, 250, 300, 300, 300, 300, 300, 300, 300};
int[] y = {250, 200, 200, 200, 150, 150, 150, 100, 100, 100, 0, 50, 100, 150, 200, 250, 300};
String keys[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", "*", "/", "=", ".", "C"};
void setup() {
size(500, 500);
numButtons = 16;
wd = 45;
ht = 45;
for (int boxes = 0; boxes < 16; boxes = boxes+1) //draws all the boxes and puts labels on them
{
fill(250, 250, 230);
rect(x[boxes], y[boxes], wd, ht);
fill(50);
text(keys[boxes], (x[boxes] + 20), (y[boxes] + 25));
}
}
void draw() {
}
void mouseReleased() //sense the clicks on the boxes
{
for (int i = 0; i < numButtons; i++) { //loop to sense and print number clicks
if ((x[i] < mouseX) && (mouseX < (x[i] + wd)) && (y[i] < mouseY) && (mouseY < (y[i]+ht))) {
choice = i; //if mouse is clicked, set i to choice
}
}
if (choice >= 0 && choice <= 9) { //digit?
num = num + choice; //add to num
} else if (choice == 15) { //point?
if (decPoint == false) { //if no dp, add decimal to number and set to true
num = num + '.';
decPoint = true;
} else if (decPoint == true) //if dp, print an error message
println("Only one decimal allowed!");
} else if (choice >= 10 && choice <= 13) { //operator?
if (op == 0) { //if no current op, set op, and num is assigned to acc
op = choice;
acc = float(num);
}
if (op == 10){
acc = (acc + float(num));
op = 0;
println(acc);
}
num = ""; //resets num
decPoint = false; //resets decpoint
}
}