var calculator = [];
var minusReplacement = '_';
//var command = '';
function setup() {
createCanvas(400, 400);
calculator[0] = '';
}
function draw() {
background(220);
noStroke();
fill(200, 50, 200);
textSize(32);
for (var i = 0; i < calculator.length; i++) {
text(' ' + calculator[i], 25, (400 + (i * 40)) - (calculator.length
* 40));
}
}
function button(b) {
if (b == '=') {
if (calculator[calculator.length - 1] == '' &&
calculator.length > 2) {
calculator[calculator.length - 1] = calculator[calculator.length -
3];
}
var peices = splitTokens(calculator[calculator.length - 1], ['+',
minusReplacement, '*', '/']);
var mafs = splitTokens(calculator[calculator.length - 1], ['a',
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0']);
//console.log(peices);
//console.log(mafs);
var total = peices[0];
if (total == 'a') {
if (calculator.length > 2) {
total = calculator[calculator.length - 2];
} else {
total = 0;
}
}
for (var i = 0; i < mafs.length; i++) {
if (mafs[i] == '+') {
total = int(total) + int(peices[i + 1]);
}
if (mafs[i] == minusReplacement) {
total = int(total) - int(peices[i + 1]);
}
if (mafs[i] == '*') {
total = int(total) * int(peices[i + 1]);
}
if (mafs[i] == '/') {
total = int(total) / int(peices[i + 1]);
}
}
//console.log(total);
calculator[calculator.length] = total;
calculator[calculator.length] = '';
} else if (b == 'cc') {
calculator = [];
calculator[0] = '';
} else if (b == 'c') {
calculator[calculator.length - 1] = '';
}
/*else if(b == 'a' && commands.length > 1) {
commands[commands.length-1] = str(commands[commands.length-1]) +
str(commands[commands.length-2]);
}else if(b == 'a') {
commands[commands.length-1] = str(commands[commands.length-1]) +
0;
}*/
else if (b == '-') {
calculator[calculator.length - 1] = calculator[calculator.length -
1] + minusReplacement;
} else {
calculator[calculator.length - 1] = calculator[calculator.length -
1] + b;
}
}
Get Answers For Free
Most questions answered within 1 hours.