Skip to content

Commit

Permalink
Added MODULO and bugfixes!
Browse files Browse the repository at this point in the history
  • Loading branch information
Jazzeus committed Mar 17, 2022
1 parent cfa4cbb commit 8b99caf
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions src/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public void keyTyped(KeyEvent e) {
JButton button9 = new JButton("9");
button9.setBounds(220, 310, 50, 35);

// Modulo
JButton buttonMod = new JButton("M");
buttonMod.setBackground(Color.orange);
buttonMod.setBounds(340, 355, 50, 35);
JButton buttonPercent = new JButton("%");
buttonPercent.setBackground(Color.orange);
buttonPercent.setBounds(340, 310, 50, 35);
Expand Down Expand Up @@ -116,6 +120,7 @@ public void keyTyped(KeyEvent e) {
panel.add(button7);
panel.add(button8);
panel.add(button9);
panel.add(buttonMod);
panel.add(buttonPercent);
panel.add(buttonDivide);
panel.add(buttonMulti);
Expand Down Expand Up @@ -323,6 +328,14 @@ public void actionPerformed(ActionEvent e) {
System.out.println(count);
}
});
buttonMod.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
input = input + "mod";
text.setText(input);
count++;
}
});
buttonPercent.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Expand Down Expand Up @@ -404,27 +417,6 @@ public void actionPerformed(ActionEvent e) {
} else {
inputNew = input;
}
// execute the operations
// if (inputNew.contains("+") && inputNew.contains("*")) {
// System.out.println("test");
// split = inputNew.split("\\+");
// split = inputNew.split("\\*");
// if (split.length >= 2) {
// result = Double.parseDouble(split[0]);
// for (int i = 1; i < split.length; i++) { //arrWerte.length, da wir ja unterschiedliche Längen bei den Arrays haben; z.B. bei 3 Zahlen = 3 Stellen im Array
// result = result + Double.parseDouble(split[i]) * Double.parseDouble(split[split.length - 1]);
// }
// System.out.println(result);
// interimResult = String.valueOf(result);
// // replacing the "." with "," again
// finalResult = "";
// if (interimResult.contains(".")) {
// finalResult = interimResult.replace(".", ",");
// }
// text.setText(String.valueOf(finalResult));
// System.out.println(finalResult);
// }
// }
if (inputNew.contains("+")) {
split = inputNew.split("\\+");
if (split.length >= 2) {
Expand Down Expand Up @@ -455,6 +447,24 @@ public void actionPerformed(ActionEvent e) {
}
text.setText(String.valueOf(finalResult));
}
} else if (inputNew.contains("mod")) {
split = inputNew.split("mod");
if (split.length >= 1) {
if (split.length >= 2) {
result = Double.parseDouble(split[0]);
for (int i = 1; i < split.length; i++) { //arrWerte.length, da wir ja unterschiedliche Längen bei den Arrays haben; z.B. bei 3 Zahlen = 3 Stellen im Array
result = result % Double.parseDouble(split[i]);
}
System.out.println(result);
interimResult = String.valueOf(result);
finalResult = "";
if (interimResult.contains(".")) {
finalResult = interimResult.replace(".", ",");
}
text.setText(String.valueOf(finalResult));
}
text.setText(String.valueOf(finalResult));
}
} else if (inputNew.contains("-")) {
split = inputNew.split("\\-");
if (split.length >= 2) {
Expand Down

0 comments on commit 8b99caf

Please sign in to comment.