I got to step 1 on the first step. I’m having trouble constructing the loop (use a counter? that’s the only way I know how to create loops so far), and the if statements. I understand what I need to do, check if it is a number or a “.” but how do I do that? When I try to used storedOperator.hasValue() it won’t let me unless I write out a check for every single number (unless theres a way to combine the multiple checks into one line that I haven’t learned yet?).
I’m struggling at grasping the if statements. “If its an operator, write the variable into the last position,” what does this accomplish? I’m going to just throw my current code into the bottom of this post because at this point I’m lost and showing what I have is the most helpful I think.
I’m sorry for so much trouble, but the teacher assigned this with no information about lists, only gave an intro on arrays and strings and told us to do this project. Seeing as it’s due tomorrow, I’m not even sure if I’ll be able to complete anyways.
/*--------------------------------------------------------------------------------------------------------------------------------------
Calculator Project
--------------------------------------------------------------------------
----- changelog ----------------------------------------------------------
Date Time ---------------Notes------------------------------------------
---- -------------------------------------------------------------------
10/1/20 Initial Coding - setup button locations
10/2/20 Cleaned code and combined arrays into one
10/3/20 Added in Numbers on the buttons, added for loops
10/4/20 Added in stringlist, changed variables, cleaned code
------------------------------------------------------------------------*/
float buttonsx, buttonsy,
wd, ht,
c2, num1, num2;
int whilecounter;
StringList storedOperator;
StringList test;
String value;
String operator;
String number;
String temp;
//declares arrays for drawing boxes locations
float[] x = {200, 150, 200, 250, 150, 200, 250, 150, 200, 250, 300, 300, 300, 300, 300, 300};
float[] y = {250, 200, 200, 200, 150, 150, 150, 100, 100, 100, 0, 50, 100, 150, 200, 250};
void setup()
{
//sets up number and operator stringlist
storedOperator = new StringList(); //creates float list for storing the numbers and operators
size(500, 500);
whilecounter = 0;
buttonsx = 300;
buttonsy = 50;
wd = 45;
ht = 45;
}
void draw()
{
for (int boxes = 0; boxes < 14; boxes = boxes+1) //draws the boxes and puts numbers on them
{
fill(250, 250, 230);
rect(x[boxes], y[boxes], wd, ht);
fill(50);
text(boxes, (x[boxes] + 20), (y[boxes] + 25));
}
for (int operators = 10; operators < 16; operators = operators + 1) //draws operators and labels
{
fill(250, 250, 230);
rect(x[operators], y[operators], wd, ht);
fill(50);
}
//adds in the overlay for operations buttons
text("+", (x[10] + 20), (y[10] + 25));
text("-", (x[11] + 20), (y[11] + 25));
text("*", (x[12] + 20), (y[12] + 25));
text("/", (x[13] + 20), (y[13] + 25));
text(".", (x[14] + 20), (y[14] + 25));
text("=", (x[15] + 20), (y[15] + 25));
}
void mouseReleased() //sense the clicks on the boxes
{
for (int i = 0; i < 10; i++) { //stores the value of the first click in storedOperator
if ((x[i] < mouseX) && (mouseX < (x[i] + wd)) && (y[i] < mouseY) && (mouseY < (y[i]+ht))) {
String value = str(i);
storedOperator.append(value);
println(storedOperator);
}
}
if ((x[10] < mouseX) && (mouseX < (x[10] + wd)) && (y[10] < mouseY) && (mouseY < (y[10]+ht))) {
operator = "+"; //storing the plus operator if clicked
storedOperator.append(operator);
}
if ((x[11] < mouseX) && (mouseX < (x[11] + wd)) && (y[11] < mouseY) && (mouseY < (y[11]+ht))) {
operator = "-"; //storing the minus operator if clicked
storedOperator.append(operator);
}
if ((x[12] < mouseX) && (mouseX < (x[12] + wd)) && (y[12] < mouseY) && (mouseY < (y[12]+ht))) {
operator = "*"; //storing the multiplication operator if clicked
storedOperator.append(operator);
}
if ((x[13] < mouseX) && (mouseX < (x[13] + wd)) && (y[13] < mouseY) && (mouseY < (y[13]+ht))) {
operator = "/"; //storing the division operator if clicked
storedOperator.append(operator);
}
if ((x[14] < mouseX) && (mouseX < (x[14] + wd)) && (y[14] < mouseY) && (mouseY < (y[14]+ht))) {
operator = "."; //storing the division operator if clicked
storedOperator.append(operator);
}
if ((x[15] < mouseX) && (mouseX < (x[15] + wd)) && (y[15] < mouseY) && (mouseY < (y[15]+ht))) {
while (whilecounter < storedOperator.size()) {
whilecounter++;
if (storedOperator.hasValue() {
temp = temp += storedOperator.get(0);
println(temp);
}
}