added modulo to calculator

yeah
This commit is contained in:
LegendaryDoge30 2026-07-19 16:24:35 +02:00
parent adaab85b3a
commit 40b9ca1a47
2 changed files with 5 additions and 4 deletions

View File

@ -48,7 +48,7 @@ public class GUICalculator extends GuiScreen {
if (!inputField.textboxKeyTyped(p_73869_1_, p_73869_2_))
super.keyTyped(p_73869_1_, p_73869_2_);
String input = inputField.getText().replaceAll("[^\\d+\\-*/^!.()\\sA-Za-z]+", "");
String input = inputField.getText().replaceAll("[^\\d+\\-*/%^!.()\\sA-Za-z]+", "");
if (p_73869_1_ == 13 || p_73869_1_ == 10) { // when pressing enter (CR or LF)
if (selectedHist != -1) {

View File

@ -21,7 +21,7 @@ public class Calculator {
for (int i = 0; i < tokens.length; i++) {
if (tokens[i] == ' ') continue;
if (tokens[i] >= '0' && tokens[i] <= '9' || tokens[i] == '.' || (tokens[i] == '-' && (i == 0 || "+-*/^(".contains(String.valueOf(tokens[i - 1]))))) {
if (tokens[i] >= '0' && tokens[i] <= '9' || tokens[i] == '.' || (tokens[i] == '-' && (i == 0 || "+-*/%^(".contains(String.valueOf(tokens[i - 1]))))) {
StringBuilder buffer = new StringBuilder();
if (tokens[i] == '-') {
buffer.append('-'); // for negative numbers
@ -37,7 +37,7 @@ public class Calculator {
operators.pop();
if (!operators.isEmpty() && operators.peek().length() > 1)
values.push(evaluateFunction(operators.pop(), values.pop()));
} else if (tokens[i] == '+' || tokens[i] == '-' || tokens[i] == '*' || tokens[i] == '/' || tokens[i] == '^') {
} else if (tokens[i] == '+' || tokens[i] == '-' || tokens[i] == '*' || tokens[i] == '/' || tokens[i] == '%' || tokens[i] == '^') {
while (!operators.isEmpty() && hasPrecedence(String.valueOf(tokens[i]), operators.peek()))
values.push(evaluateOperator(operators.pop().charAt(0), values.pop(), values.pop()));
operators.push(Character.toString(tokens[i]));
@ -67,6 +67,7 @@ public class Calculator {
case '-': return y - x;
case '*': return y * x;
case '/': return y / x;
case '%': return y % x;
case '^': return Math.pow(y, x); // should not happen here, but oh well
}
@ -102,7 +103,7 @@ public class Calculator {
char secondChar = second.charAt(0);
if (secondChar == '(' || secondChar == ')') return false;
else return (firstChar != '*' && firstChar != '/' && firstChar != '^') || (secondChar != '+' && secondChar != '-');
else return (firstChar != '*' && firstChar != '/' && firstChar != '%' && firstChar != '^') || (secondChar != '+' && secondChar != '-');
}
/** Returns the input with all powers evaluated */