Skip to content

Commit

Permalink
Merge pull request #12 from skytomo221/develop
Browse files Browse the repository at this point in the history
Update to 0.8
  • Loading branch information
skytomo221 authored Dec 4, 2019
2 parents 45a4957 + 2646350 commit 2a12061
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 69 deletions.
3 changes: 3 additions & 0 deletions .idea/artifacts/Q0_Calculator_jar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 15 additions & 4 deletions src/skytomo221/q0/Q0Button.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package skytomo221.q0;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.image.ImageProducer;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Objects;

/**
* 電卓で使用するボタンの定義です。
Expand Down Expand Up @@ -42,10 +47,16 @@ public void setMeaning(String meaning) {
*/
public Q0Button(String text) {
super(text);
File file = new File("./images/" + text + ".png");
if (file.exists()) {
setText("");
setIcon(new ImageIcon(text));
try {
URL path = getClass().getClassLoader().getResource("images/" + text + ".png");
if (path != null) {
Icon icon = new ImageIcon(createImage((ImageProducer) path.getContent()));
setText("");
setIcon(icon);
}
}
catch (IOException e) {
e.printStackTrace();
}
setMeaning(text);
}
Expand Down
132 changes: 73 additions & 59 deletions src/skytomo221/q0/Q0Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,68 +49,82 @@ public void actionPerformed(ActionEvent actionEvent) {
b.getMeaning().matches("\\d|¬|(?!or)(?!xor)(?!and)(?!mod)[A-Za-z]+")) {
viewer.emptyInput();
}
if (b.getText().equals("C")) {
viewer.resetInput();
} else if (b.getText().equals("AC")) {
viewer.resetInput();
viewer.resetLog();
} else if (b.getText().equals("%")) {
viewer.insertToEndOfInput("% of ");
} else if (b.getMeaning().equals("reciprocal")) {
viewer.insertToStartOfInput("1/(");
viewer.insertToEndOfInput(")");
} else if (b.getMeaning().equals("radical-symbol")) {
viewer.insertToStartOfInput("pow(");
viewer.insertToEndOfInput(", 1/");
} else if (b.getText().equals("x!")) {
viewer.insertToEndOfInput("!");
} else if (b.getMeaning().equals("plus-and-minus")) {
viewer.invertSign();
} else if (b.getMeaning().equals("clear-symbol")) {
viewer.backspace();
} else if (b.getText().equals("=")) {
try {
List<Token> tokens = lexer.parse(viewer.getTextOfInput());
if (displayLexerResult) {
System.out.println("[Lexer Log]");
for (Token token : tokens) {
System.out.println(token.toString());
switch (b.getMeaning()) {
case "C":
viewer.resetInput();
break;
case "AC":
viewer.resetInput();
viewer.resetLog();
break;
case "%":
viewer.insertToEndOfInput("% of ");
break;
case "reciprocal":
viewer.insertToStartOfInput("1/(");
viewer.insertToEndOfInput(")");
break;
case "radical-symbol":
viewer.insertToStartOfInput("pow(");
viewer.insertToEndOfInput(", 1/");
break;
case "x!":
viewer.insertToEndOfInput("!");
break;
case "plus-and-minus":
viewer.invertSign();
break;
case "clear-symbol":
viewer.backspace();
break;
case "=":
try {
List<Token> tokens = lexer.parse(viewer.getTextOfInput());
if (displayLexerResult) {
System.out.println("[Lexer Log]");
for (Token token : tokens) {
System.out.println(token.toString());
}
System.out.println("");
}
System.out.println("");
}
List<Expression> expressions = parser.parse(tokens);
if (displayParserResult) {
System.out.println("[Parser Log]");
for (Expression expression : expressions) {
System.out.println(expression.toString());
List<Expression> expressions = parser.parse(tokens);
if (displayParserResult) {
System.out.println("[Parser Log]");
for (Expression expression : expressions) {
System.out.println(expression.toString());
}
System.out.println("");
}
System.out.println("");
}
calculator.calculate(expressions);
if (displayCalculatorResult) {
System.out.println("[Calculator Log]");
System.out.println(calculator.getLog());
calculator.calculate(expressions);
if (displayCalculatorResult) {
System.out.println("[Calculator Log]");
System.out.println(calculator.getLog());
}
viewer.insertResultToLog(calculator.getAnswerToString());
} catch (LexerException ex) {
viewer.insertErrorToLog("[Lexer Error]", ex.getLexerErrorMessage());
viewer.insertInstructionsHyperlinkToLog();
} catch (ParserException ex) {
viewer.insertErrorToLog("[Parser Error]", ex.getParserErrorMessage());
viewer.insertInstructionsHyperlinkToLog();
} catch (CalculatorException ex) {
viewer.insertErrorToLog("[Calculator Error]", ex.getCalculatorErrorMessage());
viewer.insertInstructionsHyperlinkToLog();
} catch (Exception ex) {
viewer.insertErrorToLog("[Java Error]", ex.getMessage() + "\n\n");
} finally {
viewer.setCaretOfLogToBottom();
}
viewer.insertResultToLog(calculator.getAnswerToString());
} catch (LexerException ex) {
viewer.insertErrorToLog("[Lexer Error]", ex.getLexerErrorMessage());
viewer.insertInstructionsHyperlinkToLog();
} catch (ParserException ex) {
viewer.insertErrorToLog("[Parser Error]", ex.getParserErrorMessage());
viewer.insertInstructionsHyperlinkToLog();
} catch (CalculatorException ex) {
viewer.insertErrorToLog("[Calculator Error]", ex.getCalculatorErrorMessage());
viewer.insertInstructionsHyperlinkToLog();
} catch (Exception ex) {
viewer.insertErrorToLog("[Java Error]", ex.getMessage() + "\n\n");
} finally {
viewer.setCaretOfLogToBottom();
}
} else if (b.getText().equals("+") || b.getText().equals("-") || b.getText().equals("×")
|| b.getText().equals("÷")) {
viewer.insertToEndOfInput(" " + b.getText() + " ");
} else {
viewer.insertToEndOfInput(b.getMeaning());
break;
case "+":
case "-":
case "×":
case "÷":
viewer.insertToEndOfInput(" " + b.getText() + " ");
break;
default:
viewer.insertToEndOfInput(b.getMeaning());
break;
}
}
viewer.setHighlighted(false);
Expand Down
6 changes: 4 additions & 2 deletions src/skytomo221/q0/Q0Viewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.awt.image.ImageProducer;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -162,11 +165,10 @@ public Q0Viewer() {
setBackground(colors.get("black"));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
try {
setIconImage(ImageIO.read(new File("images/icon.png")));
setIconImage(createImage((ImageProducer) getClass().getClassLoader().getResource("images/icon.png").getContent()));
} catch (IOException e) {
e.printStackTrace();
}

UIManager.put("Button.select", colors.get("selected"));
UIManager.put("TabbedPane.borderHightlightColor", colors.get("black"));
UIManager.put("TabbedPane.darkShadow", colors.get("black"));
Expand Down
6 changes: 2 additions & 4 deletions src/skytomo221/q0/expression/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* 式を表します。
*/
public class Expression {
public abstract class Expression {
/**
* 式の名前
*/
Expand All @@ -27,7 +27,5 @@ public void setName(String name) {
this.name = name;
}

public Expression copy() {
return copy();
}
public abstract Expression copy();
}

0 comments on commit 2a12061

Please sign in to comment.