├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── ic_launcher-web.png
│ │ ├── res
│ │ │ ├── drawable
│ │ │ │ ├── ic_launcher.png
│ │ │ │ ├── drawable-hdpi
│ │ │ │ │ └── ic_calculator.png
│ │ │ │ ├── drawable-ldpi
│ │ │ │ │ └── ic_calculator.png
│ │ │ │ ├── drawable-mdpi
│ │ │ │ │ └── ic_calculator.png
│ │ │ │ ├── drawable-xhdpi
│ │ │ │ │ └── ic_calculator.png
│ │ │ │ ├── drawable-xxhdpi
│ │ │ │ │ └── ic_calculator.png
│ │ │ │ ├── drawable-xxxhdpi
│ │ │ │ │ └── ic_calculator.png
│ │ │ │ └── button_color.xml
│ │ │ ├── drawable-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── drawable-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── drawable-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── values
│ │ │ │ ├── dimens.xml
│ │ │ │ ├── colors.xml
│ │ │ │ ├── styles.xml
│ │ │ │ └── strings.xml
│ │ │ ├── values-w820dp
│ │ │ │ └── dimens.xml
│ │ │ ├── menu
│ │ │ │ └── menu_main.xml
│ │ │ └── layout
│ │ │ │ ├── app_bar.xml
│ │ │ │ └── activity_main.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── calculator
│ │ │ └── xzya
│ │ │ └── ro
│ │ │ └── calculator
│ │ │ ├── OnSwipeTouchListener.java
│ │ │ ├── Parser.java
│ │ │ ├── Checker.java
│ │ │ ├── Postfix.java
│ │ │ └── MainActivity.java
│ └── androidTest
│ │ └── java
│ │ └── calculator
│ │ └── xzya
│ │ └── ro
│ │ └── calculator
│ │ └── ApplicationTest.java
├── build.gradle
└── proguard-rules.pro
├── settings.gradle
├── README.md
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Calculator
2 | Android Calculator App
3 | 
4 |
--------------------------------------------------------------------------------
/app/src/main/ic_launcher-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Xzya/Calculator/master/app/src/main/ic_launcher-web.png
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Xzya/Calculator/master/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Xzya/Calculator/master/app/src/main/res/drawable/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Xzya/Calculator/master/app/src/main/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Xzya/Calculator/master/app/src/main/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Xzya/Calculator/master/app/src/main/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/drawable-hdpi/ic_calculator.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Xzya/Calculator/master/app/src/main/res/drawable/drawable-hdpi/ic_calculator.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/drawable-ldpi/ic_calculator.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Xzya/Calculator/master/app/src/main/res/drawable/drawable-ldpi/ic_calculator.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/drawable-mdpi/ic_calculator.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Xzya/Calculator/master/app/src/main/res/drawable/drawable-mdpi/ic_calculator.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/drawable-xhdpi/ic_calculator.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Xzya/Calculator/master/app/src/main/res/drawable/drawable-xhdpi/ic_calculator.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/drawable-xxhdpi/ic_calculator.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Xzya/Calculator/master/app/src/main/res/drawable/drawable-xxhdpi/ic_calculator.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/drawable-xxxhdpi/ic_calculator.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Xzya/Calculator/master/app/src/main/res/drawable/drawable-xxxhdpi/ic_calculator.png
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Apr 10 15:27:10 PDT 2013
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | #607D8B
5 | #455A64
6 | #90A4AE
7 | #536DFE
8 | #ECEFF1
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/button_color.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | -
4 |
5 |
6 | -
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
9 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/calculator/xzya/ro/calculator/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package calculator.xzya.ro.calculator;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/app_bar.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | #built application files
2 | *.apk
3 | *.ap_
4 |
5 | # files for the dex VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # generated files
12 | bin/
13 | gen/
14 |
15 | # Local configuration file (sdk path, etc)
16 | local.properties
17 |
18 | # Windows thumbnail db
19 | Thumbs.db
20 |
21 | # OSX files
22 | .DS_Store
23 |
24 | # Eclipse project files
25 | .classpath
26 | .project
27 |
28 | # Android Studio
29 | .idea
30 | #.idea/workspace.xml - remove #
31 | .gradle
32 | build/
33 | /*/build/
34 | *.iml
35 | *.iws
36 | *.ipr
37 | *~
38 | *.swp
39 |
40 | # Eclipse project files
41 | .classpath
42 | .project
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 21
5 | buildToolsVersion "21.1.2"
6 |
7 | defaultConfig {
8 | applicationId "calculator.xzya.ro.calculator"
9 | minSdkVersion 15
10 | targetSdkVersion 21
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | compile 'com.android.support:appcompat-v7:21.0.+'
25 | }
26 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\Program Files\Android\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
10 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Calculator
5 | Calculator
6 | Hello world!
7 | Settings
8 | Enter new expression
9 | 7
10 | 8
11 | 9
12 | x
13 | C
14 | 4
15 | 5
16 | 6
17 | /
18 |
19 | 1
20 | 2
21 | 3
22 | +
23 | =
24 | 0
25 | .
26 | -
27 | (
28 | (
29 | )
30 | x\u207F
31 | √x
32 | +/-
33 | x!
34 | Ans
35 | Shift
36 | sin
37 | sin\u207B\u00B9
38 | cos
39 | cos\u207B\u00B9
40 | tan
41 | tan\u207B\u00B9
42 | log
43 | log\u2082
44 |
45 |
46 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/main/java/calculator/xzya/ro/calculator/OnSwipeTouchListener.java:
--------------------------------------------------------------------------------
1 | package calculator.xzya.ro.calculator;
2 |
3 | import android.content.Context;
4 | import android.view.GestureDetector;
5 | import android.view.GestureDetector.SimpleOnGestureListener;
6 | import android.view.MotionEvent;
7 | import android.view.View;
8 | import android.widget.Toast;
9 |
10 | /**
11 | * Created by Xzya on 24/2/2015.
12 | */
13 | public class OnSwipeTouchListener implements View.OnTouchListener {
14 |
15 | final GestureDetector gestureDetector;
16 |
17 | public OnSwipeTouchListener (Context ctx){
18 | gestureDetector = new GestureDetector(ctx, new GestureListener());
19 | }
20 |
21 | @Override
22 | public boolean onTouch(View view, MotionEvent motionEvent) {
23 | return false;
24 | }
25 |
26 | private final class GestureListener extends SimpleOnGestureListener {
27 |
28 | private static final int SWIPE_THRESHOLD = 50;
29 | private static final int SWIPE_VELOCITY_THRESHOLD = 50;
30 |
31 | @Override
32 | public boolean onDown(MotionEvent e) {
33 | return true;
34 | }
35 |
36 | @Override
37 | public boolean onSingleTapConfirmed(MotionEvent e) {
38 | return super.onSingleTapConfirmed(e);
39 | }
40 |
41 | @Override
42 | public boolean onSingleTapUp(MotionEvent e) {
43 | onClick();
44 | return super.onSingleTapUp(e);
45 | }
46 |
47 | @Override
48 | public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
49 | boolean result = false;
50 | try {
51 | float diffY = e2.getY() - e1.getY();
52 | float diffX = e2.getX() - e1.getX();
53 | if (Math.abs(diffX) > Math.abs(diffY)) {
54 | if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
55 | if (diffX > 0) {
56 | onSwipeRight();
57 | } else {
58 | onSwipeLeft();
59 | }
60 | }
61 | result = true;
62 | }
63 | else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
64 | if (diffY > 0) {
65 | onSwipeBottom();
66 | } else {
67 | onSwipeTop();
68 | }
69 | }
70 | result = true;
71 |
72 | } catch (Exception exception) {
73 | exception.printStackTrace();
74 | }
75 | return result;
76 | }
77 | }
78 |
79 | public void onSwipeRight() {
80 | }
81 |
82 | public void onSwipeLeft() {
83 | }
84 |
85 | public void onSwipeTop() {
86 | }
87 |
88 | public void onSwipeBottom() {
89 | }
90 |
91 | public void onClick() {
92 | }
93 | }
--------------------------------------------------------------------------------
/app/src/main/java/calculator/xzya/ro/calculator/Parser.java:
--------------------------------------------------------------------------------
1 | package calculator.xzya.ro.calculator;
2 |
3 | import java.util.LinkedList;
4 | import java.util.Queue;
5 | import java.util.Stack;
6 |
7 | /**
8 | * The parser is based on the Shunting-yard algorithm
9 | *
10 | * @author Dumitru Mihail Cristian
11 | * @see //http://en.wikipedia.org/wiki/Shunting-yard_algorithm
12 | *
13 | */
14 |
15 | public class Parser {
16 |
17 | public LinkedList stringToQueue(String inputString) {
18 | LinkedList inputStack = new LinkedList();
19 |
20 | int len = inputString.length();
21 | for (int i = 0; i < len; i++) {
22 | inputStack.add(String.valueOf(inputString.charAt(i)));
23 | }
24 |
25 | return inputStack;
26 | }
27 |
28 | /**
29 | * Prints the output string
30 | * @param stuff
31 | */
32 | public void printOutput(Queue stuff){
33 | StringBuilder string = new StringBuilder();
34 | // stuff.forEach(c -> {
35 | // string.append(c);
36 | // });
37 | System.out.println(string.toString());
38 | }
39 |
40 | /**
41 | * Parses the expression
42 | * @param incomingInput - input expression as a list
43 | * @return output list
44 | */
45 | public LinkedList parse(LinkedList incomingInput){
46 | LinkedList output = new LinkedList();
47 | Stack stack = new Stack();
48 | LinkedList input = (LinkedList) incomingInput.clone();
49 |
50 | while (!input.isEmpty()){
51 | String token = input.remove();
52 |
53 | if (Checker.isNumeric(token)){
54 | output.add(token);
55 | } else if (Checker.isFunction(token)){
56 | stack.push(token);
57 | } else if (Checker.isFunctionSeparator(token)){
58 | while (!stack.isEmpty() &&
59 | !stack.peek().equals("(")){
60 | output.add(stack.pop());
61 | }
62 | if (stack.peek().equals("(")){
63 |
64 | } else {
65 | System.err.println("The separator or parentheses were misplaced.");
66 | return null;
67 | }
68 | } else if (Checker.isOperator(token)){
69 | while (!stack.isEmpty() && Checker.isOperator(stack.peek()) &&
70 | ((Checker.isLeftAssociative(token) && (Checker.getPrecedence(token) <= Checker.getPrecedence(stack.peek())) ||
71 | (!Checker.isLeftAssociative(token) && (Checker.getPrecedence(token) < Checker.getPrecedence(stack.peek())))))
72 | ){
73 | output.add(stack.pop());
74 | }
75 | stack.push(token);
76 | } else if (token.equals("(")){
77 | stack.push(token);
78 | } else if (token.equals(")")){
79 | while (!stack.isEmpty() && !stack.peek().equals("(")){
80 | output.add(stack.pop());
81 | }
82 | if (stack.peek().equals("(")){
83 | stack.pop();
84 | if (!stack.isEmpty() && Checker.isFunction(stack.peek())){
85 | output.add(stack.pop());
86 | }
87 | } else {
88 | System.err.println("There are mismatched parentheses.");
89 | return null;
90 | }
91 |
92 | }
93 | }
94 |
95 | if (input.isEmpty()){
96 | while (!stack.isEmpty()){
97 | if (stack.peek().equals("(") || stack.peek().equals(")")){
98 | System.err.println("There are mismatched parentheses.");
99 | return null;
100 | }
101 | output.add(stack.pop());
102 | }
103 | }
104 | return output;
105 | }
106 |
107 | public static void main(String[] args) {
108 |
109 | Parser test = new Parser();
110 | String s1 = "1+((2*5)^(3/5))";
111 | LinkedList s1q = test.stringToQueue(s1);
112 |
113 | LinkedList s1test = test.parse(s1q);
114 | test.printOutput(s1test);
115 |
116 | }
117 |
118 | }
119 |
--------------------------------------------------------------------------------
/app/src/main/java/calculator/xzya/ro/calculator/Checker.java:
--------------------------------------------------------------------------------
1 | package calculator.xzya.ro.calculator;
2 |
3 | public class Checker {
4 |
5 | public static boolean isNumeric(String str) {
6 | try {
7 | double d = Double.parseDouble(str);
8 | } catch (NumberFormatException nfe) {
9 | return false;
10 | }
11 | return true;
12 | }
13 |
14 | public static boolean isFunction(String token) {
15 | if (
16 | token.equals("sqrt") ||
17 | token.equals("sin") ||
18 | token.equals("asin") ||
19 | token.equals("cos") ||
20 | token.equals("acos") ||
21 | token.equals("tan") ||
22 | token.equals("atan") ||
23 | token.equals("log") ||
24 | token.equals("log2")) {
25 | return true;
26 | }
27 | return false;
28 | }
29 |
30 | public static boolean isFunctionSeparator(String token) {
31 | if (
32 | token.equals(",")) {
33 | return true;
34 | }
35 | return false;
36 | }
37 |
38 | public static boolean isOperator(String token) {
39 | if (
40 | token.equals("+") ||
41 | token.equals("-") ||
42 | token.equals("*") ||
43 | token.equals("/") ||
44 | token.equals("^") ||
45 | token.equals("%") ||
46 | token.equals("!")) {
47 | return true;
48 | }
49 | return false;
50 | }
51 |
52 | public static boolean isLeftAssociative(String token) {
53 | if (
54 | token.equals("*") ||
55 | token.equals("/") ||
56 | token.equals("+") ||
57 | token.equals("-") ||
58 | token.equals("!")) {
59 | return true;
60 | }
61 | return false;
62 | }
63 |
64 | public static int getPrecedence(String token) {
65 | switch (token) {
66 | case "+":
67 | return 2;
68 | case "-":
69 | return 2;
70 | case "*":
71 | return 3;
72 | case "/":
73 | return 3;
74 | case "%":
75 | return 3;
76 | case "^":
77 | return 4;
78 | case "!":
79 | return 5;
80 | default:
81 | return 1;
82 | }
83 | }
84 |
85 | public static int numberOfParameters(String token) {
86 | switch (token) {
87 | case "+":
88 | return 2;
89 | case "-":
90 | return 2;
91 | case "*":
92 | return 2;
93 | case "/":
94 | return 2;
95 | case "^":
96 | return 2;
97 | case "%":
98 | return 2;
99 | case "!":
100 | return 1;
101 | case "sqrt":
102 | return 1;
103 | case "sin":
104 | return 1;
105 | case "asin":
106 | return 1;
107 | case "cos":
108 | return 1;
109 | case "acos":
110 | return 1;
111 | case "tan":
112 | return 1;
113 | case "atan":
114 | return 1;
115 | case "log":
116 | return 1;
117 | case "log2":
118 | return 1;
119 | default:
120 | return -1;
121 | }
122 | }
123 |
124 | }
125 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/app/src/main/java/calculator/xzya/ro/calculator/Postfix.java:
--------------------------------------------------------------------------------
1 | package calculator.xzya.ro.calculator;
2 |
3 | import java.util.LinkedList;
4 | import java.util.Stack;
5 |
6 | /**
7 | * This is a simple postfix calculator implementation.
8 | *
9 | * @author Dumitru Mihail Cristian
10 | * @see //http://en.wikipedia.org/wiki/Reverse_Polish_notation
11 | */
12 |
13 | public class Postfix {
14 |
15 | private Parser parser;
16 |
17 | public Postfix() {
18 | this.parser = new Parser();
19 | }
20 |
21 | /**
22 | * Evaluates the expression
23 | *
24 | * @param input - input linked list, each operator/number is
25 | * stored in the list as an element
26 | * @return string - result as a string
27 | */
28 | public String evaluate(LinkedList input) {
29 | String result = null;
30 | LinkedList output = parser.parse(input);
31 | if (output == null){
32 | return "ERROR";
33 | }
34 |
35 | result = evaluater(output);
36 |
37 | return result;
38 | }
39 |
40 | /**
41 | * Helper function
42 | *
43 | * @param incomingOutput - output from the parser
44 | * @return string - result as a string
45 | */
46 | private String evaluater(LinkedList incomingOutput) {
47 | Stack stack = new Stack();
48 | LinkedList output = (LinkedList) incomingOutput.clone();
49 |
50 | while (!output.isEmpty()) {
51 | String token = output.removeFirst();
52 |
53 | if (Checker.isNumeric(token)) {
54 | stack.push(token);
55 | } else {
56 | int n = Checker.numberOfParameters(token);
57 | if (stack.size() < n) {
58 | System.err.println("There are insufficient values in the expression.");
59 | } else {
60 | LinkedList arguments = new LinkedList();
61 | for (int i = 0; i < n; i++) {
62 | arguments.push(stack.pop());
63 | }
64 | Double result = evaluateOperator(token, arguments);
65 | if (result != null){
66 | stack.push(result.toString());
67 | }
68 | }
69 | }
70 | }
71 | if (stack.size() == 1) {
72 | if (!stack.isEmpty()){
73 | return stack.pop();
74 | }
75 | } else {
76 | System.err.println("The user input has too many values.");
77 | System.out.println(stack.toString());
78 | }
79 |
80 | return "ERROR";
81 | }
82 |
83 | /**
84 | * Helper function
85 | * Checks the operator and applies the corresponding operation
86 | *
87 | * @param token - the operator
88 | * @param arguments - list of arguments for the operator
89 | * @return
90 | */
91 | private Double evaluateOperator(String token, LinkedList arguments) {
92 | Double result = null;
93 |
94 | switch (token) {
95 | case "+":
96 | result = add(arguments.pop(), arguments.pop());
97 | break;
98 | case "-":
99 | result = subtract(arguments.pop(), arguments.pop());
100 | break;
101 | case "*":
102 | result = multiply(arguments.pop(), arguments.pop());
103 | break;
104 | case "/":
105 | result = divide(arguments.pop(), arguments.pop());
106 | break;
107 | case "^":
108 | result = pow(arguments.pop(), arguments.pop());
109 | break;
110 | case "!":
111 | Integer temp = factorial(arguments.pop());
112 | if (temp != null){
113 | result = temp.doubleValue();
114 | } else {
115 | return null;
116 | }
117 | break;
118 | case "sqrt":
119 | result = sqrt(arguments.pop());
120 | break;
121 | case "sin":
122 | result = sin(arguments.pop());
123 | break;
124 | case "asin":
125 | result = asin(arguments.pop());
126 | break;
127 | case "cos":
128 | result = cos(arguments.pop());
129 | break;
130 | case "acos":
131 | result = acos(arguments.pop());
132 | break;
133 | case "tan":
134 | result = tan(arguments.pop());
135 | break;
136 | case "atan":
137 | result = atan(arguments.pop());
138 | break;
139 | case "log":
140 | result = log(arguments.pop());
141 | break;
142 | case "log2":
143 | result = log2(arguments.pop());
144 | break;
145 | }
146 |
147 |
148 | return result;
149 | }
150 |
151 | /**
152 | * Adds two numbers together
153 | *
154 | * @param a first number
155 | * @param b second number
156 | * @return result
157 | */
158 | private double add(String a, String b) {
159 | Double valA = Double.parseDouble(a);
160 | Double valB = Double.parseDouble(b);
161 |
162 | return valA + valB;
163 | }
164 |
165 | /**
166 | * Subtracts two numbers
167 | *
168 | * @param a first number
169 | * @param b second number
170 | * @return result
171 | */
172 | private double subtract(String a, String b) {
173 | Double valA = Double.parseDouble(a);
174 | Double valB = Double.parseDouble(b);
175 |
176 | return valA - valB;
177 | }
178 |
179 | /**
180 | * Multiply two numbers together
181 | *
182 | * @param a first number
183 | * @param b second number
184 | * @return result
185 | */
186 | private double multiply(String a, String b) {
187 | Double valA = Double.parseDouble(a);
188 | Double valB = Double.parseDouble(b);
189 |
190 | return valA * valB;
191 | }
192 |
193 | /**
194 | * Divides two numbers
195 | *
196 | * @param a first number
197 | * @param b second number
198 | * @return result
199 | */
200 | private double divide(String a, String b) {
201 | Double valA = Double.parseDouble(a);
202 | Double valB = Double.parseDouble(b);
203 |
204 | return valA / valB;
205 | }
206 |
207 | /**
208 | * Raises the first number to the power of the second number
209 | *
210 | * @param a first number
211 | * @param b second number
212 | * @return result
213 | */
214 | private double pow(String a, String b) {
215 | Double valA = Double.parseDouble(a);
216 | Double valB = Double.parseDouble(b);
217 |
218 | return Math.pow(valA, valB);
219 | }
220 |
221 | /**
222 | * Gives the square root of the number
223 | *
224 | * @param a the number
225 | * @return result
226 | */
227 | private double sqrt(String a) {
228 | Double valA = Double.parseDouble(a);
229 |
230 | return Math.sqrt(valA);
231 | }
232 |
233 | /**
234 | * Gives the sine
235 | * @param a the number
236 | * @return result
237 | */
238 | private double sin(String a){
239 | Double valA = Double.parseDouble(a);
240 |
241 | return Math.sin(valA);
242 | }
243 |
244 | /**
245 | * Gives the arcsine
246 | * @param a the number
247 | * @return result
248 | */
249 | private double asin(String a){
250 | Double valA = Double.parseDouble(a);
251 |
252 | return Math.asin(valA);
253 | }
254 |
255 | /**
256 | * Gives the cosine
257 | * @param a the number
258 | * @return result
259 | */
260 | private double cos(String a){
261 | Double valA = Double.parseDouble(a);
262 |
263 | return Math.cos(valA);
264 | }
265 |
266 | /**
267 | * Gives the arccosine
268 | * @param a the number
269 | * @return result
270 | */
271 | private double acos(String a){
272 | Double valA = Double.parseDouble(a);
273 |
274 | return Math.acos(valA);
275 | }
276 |
277 | /**
278 | * Gives the tangent
279 | * @param a the number
280 | * @return result
281 | */
282 | private double tan(String a){
283 | Double valA = Double.parseDouble(a);
284 |
285 | return Math.tan(valA);
286 | }
287 |
288 | /**
289 | * Gives the arctangent
290 | * @param a the number
291 | * @return result
292 | */
293 | private double atan(String a){
294 | Double valA = Double.parseDouble(a);
295 |
296 | return Math.atan(valA);
297 | }
298 |
299 | /**
300 | * Gives the log
301 | * @param a the number
302 | * @return result
303 | */
304 | private Double log(String a){
305 | Double valA = Double.parseDouble(a);
306 |
307 | return Math.log10(valA);
308 | }
309 |
310 | /**
311 | * Gives the log2
312 | * @param a the number
313 | * @return result
314 | */
315 | private double log2(String a){
316 | Double valA = Double.parseDouble(a);
317 |
318 | return Math.log(valA)/Math.log(2);
319 | }
320 |
321 |
322 | private Integer factorial(String a) {
323 | Integer n = null;
324 | try {
325 | n = Integer.parseInt(a);
326 | } catch(NumberFormatException e){
327 | e.printStackTrace();
328 | return null;
329 | }
330 |
331 | int p = 1;
332 | while (n != 0) {
333 | p *= n;
334 | n -= 1;
335 | }
336 | return p;
337 | }
338 |
339 | public static void main(String[] args) {
340 |
341 | Postfix evaluator = new Postfix();
342 |
343 | String s1 = "1+((2*5)^(3/5)/sqrt(5))";
344 |
345 | }
346 |
347 | }
348 |
--------------------------------------------------------------------------------
/app/src/main/java/calculator/xzya/ro/calculator/MainActivity.java:
--------------------------------------------------------------------------------
1 | package calculator.xzya.ro.calculator;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.ActionBarActivity;
5 | import android.view.Menu;
6 | import android.view.MenuItem;
7 | import android.view.View;
8 | import android.widget.TextView;
9 |
10 | import java.util.LinkedList;
11 |
12 |
13 | public class MainActivity extends ActionBarActivity {
14 |
15 | private android.support.v7.widget.Toolbar toolbar;
16 | private TextView expression;
17 | private TextView result;
18 | private TextView one;
19 | private TextView two;
20 | private TextView three;
21 | private TextView four;
22 | private TextView five;
23 | private TextView six;
24 | private TextView seven;
25 | private TextView eight;
26 | private TextView nine;
27 | private TextView zero;
28 | private TextView plus;
29 | private TextView minus;
30 | private TextView multiply;
31 | private TextView divide;
32 | private TextView pow;
33 | private TextView square_root;
34 | private TextView factorial;
35 | private TextView dot;
36 | private TextView left_parentheses;
37 | private TextView right_parentheses;
38 | private TextView backspace;
39 | private TextView erase;
40 | private TextView equals;
41 | private TextView answer;
42 | private TextView change_sign;
43 | private TextView shift;
44 | private TextView sin;
45 | private TextView cos;
46 | private TextView tan;
47 | private TextView log;
48 |
49 | private Postfix postfix;
50 | private String ans;
51 | private LinkedList input;
52 | private boolean shiftPressed = false;
53 |
54 | @Override
55 | protected void onCreate(Bundle savedInstanceState) {
56 | super.onCreate(savedInstanceState);
57 | setContentView(R.layout.activity_main);
58 |
59 | toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.app_bar);
60 |
61 | setSupportActionBar(toolbar);
62 |
63 | postfix = new Postfix();
64 | input = new LinkedList();
65 |
66 | expression = (TextView) findViewById(R.id.expression);
67 | result = (TextView) findViewById(R.id.result);
68 | one = (TextView) findViewById(R.id.one);
69 | two = (TextView) findViewById(R.id.two);
70 | three = (TextView) findViewById(R.id.three);
71 | four = (TextView) findViewById(R.id.four);
72 | five = (TextView) findViewById(R.id.five);
73 | six = (TextView) findViewById(R.id.six);
74 | seven = (TextView) findViewById(R.id.seven);
75 | eight = (TextView) findViewById(R.id.eight);
76 | nine = (TextView) findViewById(R.id.nine);
77 | zero = (TextView) findViewById(R.id.zero);
78 | plus = (TextView) findViewById(R.id.plus);
79 | minus = (TextView) findViewById(R.id.subtract);
80 | multiply = (TextView) findViewById(R.id.multiply);
81 | divide = (TextView) findViewById(R.id.divide);
82 | pow = (TextView) findViewById(R.id.pow);
83 | square_root = (TextView) findViewById(R.id.square_root);
84 | factorial = (TextView) findViewById(R.id.factorial);
85 | dot = (TextView) findViewById(R.id.dot);
86 | backspace = (TextView) findViewById(R.id.backspace);
87 | erase = (TextView) findViewById(R.id.erase);
88 | equals = (TextView) findViewById(R.id.equals);
89 | change_sign = (TextView) findViewById(R.id.change_sign);
90 | answer = (TextView) findViewById(R.id.answer);
91 | shift = (TextView) findViewById(R.id.shift);
92 | sin = (TextView) findViewById(R.id.sin);
93 | cos = (TextView) findViewById(R.id.cos);
94 | tan = (TextView) findViewById(R.id.tan);
95 | log = (TextView) findViewById(R.id.log);
96 | left_parentheses = (TextView) findViewById(R.id.left_parentheses);
97 | right_parentheses = (TextView) findViewById(R.id.right_parentheses);
98 |
99 | one.setOnClickListener(new View.OnClickListener() {
100 | @Override
101 | public void onClick(View view) {
102 | addElement("1");
103 | }
104 | });
105 |
106 | two.setOnClickListener(new View.OnClickListener() {
107 | @Override
108 | public void onClick(View view) {
109 | addElement("2");
110 | }
111 | });
112 |
113 | three.setOnClickListener(new View.OnClickListener() {
114 | @Override
115 | public void onClick(View view) {
116 | addElement("3");
117 | }
118 | });
119 |
120 | four.setOnClickListener(new View.OnClickListener() {
121 | @Override
122 | public void onClick(View view) {
123 | addElement("4");
124 | }
125 | });
126 |
127 | five.setOnClickListener(new View.OnClickListener() {
128 | @Override
129 | public void onClick(View view) {
130 | addElement("5");
131 | }
132 | });
133 |
134 | six.setOnClickListener(new View.OnClickListener() {
135 | @Override
136 | public void onClick(View view) {
137 | addElement("6");
138 | }
139 | });
140 |
141 | seven.setOnClickListener(new View.OnClickListener() {
142 | @Override
143 | public void onClick(View view) {
144 | addElement("7");
145 | }
146 | });
147 |
148 | eight.setOnClickListener(new View.OnClickListener() {
149 | @Override
150 | public void onClick(View view) {
151 | addElement("8");
152 | }
153 | });
154 |
155 | nine.setOnClickListener(new View.OnClickListener() {
156 | @Override
157 | public void onClick(View view) {
158 | addElement("9");
159 | }
160 | });
161 |
162 | zero.setOnClickListener(new View.OnClickListener() {
163 | @Override
164 | public void onClick(View view) {
165 | addElement("0");
166 | }
167 | });
168 |
169 | dot.setOnClickListener(new View.OnClickListener() {
170 | @Override
171 | public void onClick(View view) {
172 | if (!input.isEmpty()) {
173 | if (!input.peekLast().contains(".")){
174 | addElement(".");
175 | }
176 | } else {
177 | addElement(".");
178 | }
179 | }
180 | });
181 |
182 | plus.setOnClickListener(new View.OnClickListener() {
183 | @Override
184 | public void onClick(View view) {
185 | addElement("+");
186 | }
187 | });
188 |
189 | minus.setOnClickListener(new View.OnClickListener() {
190 | @Override
191 | public void onClick(View view) {
192 | addElement("-");
193 | }
194 | });
195 |
196 | multiply.setOnClickListener(new View.OnClickListener() {
197 | @Override
198 | public void onClick(View view) {
199 | addElement("*");
200 | }
201 | });
202 |
203 | divide.setOnClickListener(new View.OnClickListener() {
204 | @Override
205 | public void onClick(View view) {
206 | addElement("/");
207 | }
208 | });
209 |
210 | pow.setOnClickListener(new View.OnClickListener() {
211 | @Override
212 | public void onClick(View view) {
213 | addElement("^");
214 | }
215 | });
216 |
217 | square_root.setOnClickListener(new View.OnClickListener() {
218 | @Override
219 | public void onClick(View view) {
220 | addFunctionHelper("sqrt");
221 | }
222 | });
223 |
224 | factorial.setOnClickListener(new View.OnClickListener() {
225 | @Override
226 | public void onClick(View view) {
227 | addElement("!");
228 | }
229 | });
230 |
231 | left_parentheses.setOnClickListener(new View.OnClickListener() {
232 | @Override
233 | public void onClick(View view) {
234 | addElement("(");
235 | }
236 | });
237 |
238 | right_parentheses.setOnClickListener(new View.OnClickListener() {
239 | @Override
240 | public void onClick(View view) {
241 | addElement(")");
242 | }
243 | });
244 |
245 | erase.setOnClickListener(new View.OnClickListener() {
246 | @Override
247 | public void onClick(View view) {
248 | input.clear();
249 | expression.setText("");
250 | result.setText("");
251 | }
252 | });
253 |
254 | backspace.setOnClickListener(new View.OnClickListener() {
255 | @Override
256 | public void onClick(View view) {
257 | if (!input.isEmpty()) {
258 | if (Checker.isNumeric(input.peekLast())) {
259 | String temp = input.removeLast();
260 | temp = temp.substring(0, temp.length() - 1);
261 | if (!temp.isEmpty()) {
262 | input.addLast(temp);
263 | }
264 | } else {
265 | input.removeLast();
266 | }
267 | StringBuilder tempText = new StringBuilder();
268 | for (String s : input) {
269 | tempText.append(s);
270 | }
271 | expression.setText(tempText.toString());
272 | }
273 | }
274 | });
275 |
276 | answer.setOnClickListener(new View.OnClickListener() {
277 | @Override
278 | public void onClick(View view) {
279 | if (ans != null) {
280 | if (!input.isEmpty()) {
281 | if (!Checker.isNumeric(input.peekLast())) {
282 | input.addLast(ans);
283 | setExpressionText();
284 | }
285 | } else {
286 | input.addLast(ans);
287 | setExpressionText();
288 | }
289 | }
290 | }
291 | });
292 |
293 | change_sign.setOnClickListener(new View.OnClickListener() {
294 | @Override
295 | public void onClick(View view) {
296 | if (!input.isEmpty() && Checker.isNumeric(input.peekLast())) {
297 | Double temp = Double.parseDouble(input.removeLast());
298 | temp *= -1;
299 | input.addLast(temp.toString());
300 | setExpressionText();
301 | }
302 | }
303 | });
304 |
305 | shift.setOnClickListener(new View.OnClickListener() {
306 | @Override
307 | public void onClick(View view) {
308 | if (shiftPressed == false) {
309 | shiftPressed = true;
310 |
311 | sin.setText(R.string.sin2);
312 | cos.setText(R.string.cos2);
313 | tan.setText(R.string.tan2);
314 | log.setText(R.string.log2);
315 |
316 | } else {
317 | shiftPressed = false;
318 |
319 | sin.setText(R.string.sin1);
320 | cos.setText(R.string.cos1);
321 | tan.setText(R.string.tan1);
322 | log.setText(R.string.log1);
323 | }
324 | }
325 | });
326 |
327 | sin.setOnClickListener(new View.OnClickListener() {
328 | @Override
329 | public void onClick(View view) {
330 | if (shiftPressed) {
331 | addFunctionHelper("asin");
332 | } else {
333 | addFunctionHelper("sin");
334 | }
335 | }
336 | });
337 |
338 | cos.setOnClickListener(new View.OnClickListener() {
339 | @Override
340 | public void onClick(View view) {
341 | if (shiftPressed) {
342 | addFunctionHelper("acos");
343 | } else {
344 | addFunctionHelper("cos");
345 | }
346 | }
347 | });
348 |
349 | tan.setOnClickListener(new View.OnClickListener() {
350 | @Override
351 | public void onClick(View view) {
352 | if (shiftPressed) {
353 | addFunctionHelper("atan");
354 | } else {
355 | addFunctionHelper("tan");
356 | }
357 | }
358 | });
359 |
360 | log.setOnClickListener(new View.OnClickListener() {
361 | @Override
362 | public void onClick(View view) {
363 | if (shiftPressed) {
364 | addFunctionHelper("log2");
365 | } else {
366 | addFunctionHelper("log");
367 | }
368 | }
369 | });
370 |
371 | equals.setOnClickListener(new View.OnClickListener() {
372 | @Override
373 | public void onClick(View view) {
374 | if (!input.isEmpty()) {
375 | Thread t = new Thread(new Runnable() {
376 | @Override
377 | public void run() {
378 | final String tempResult = postfix.evaluate(input);
379 | if (!tempResult.equals("ERROR") && !tempResult.equals("NaN")) {
380 | ans = tempResult.toString();
381 | }
382 |
383 | runOnUiThread(new Runnable() {
384 | @Override
385 | public void run() {
386 | result.setText(tempResult);
387 | }
388 | });
389 | }
390 | });
391 | t.start();
392 | }
393 | }
394 | });
395 |
396 |
397 | }
398 |
399 | private void addFunctionHelper(String element){
400 | if (!input.isEmpty()){
401 | if (Checker.isNumeric(input.peekLast()) || input.peekLast().equals(")")){
402 | addElement("*");
403 | addElement(element);
404 | addElement("(");
405 | } else {
406 | addElement(element);
407 | addElement("(");
408 | }
409 | } else {
410 | addElement(element);
411 | addElement("(");
412 | }
413 | }
414 |
415 | private void addElement(final String element) {
416 | if (!input.isEmpty()) {
417 | if ((Checker.isNumeric(element) || element.equals(".")) && (Checker.isNumeric(input.peekLast()) || input.peekLast().equals("."))) {
418 | String token = input.removeLast();
419 | token += element;
420 | input.addLast(token);
421 | } else if (Checker.isOperator(element) && Checker.isOperator(input.peekLast())) {
422 | input.removeLast();
423 | input.addLast(element);
424 | } else {
425 | input.addLast(element);
426 | }
427 | } else {
428 | input.addLast(element);
429 | }
430 |
431 | setExpressionText();
432 | }
433 |
434 | private void setExpressionText() {
435 | StringBuilder text = new StringBuilder();
436 | for (String s : input) {
437 | text.append(s);
438 | }
439 | expression.setText(text.toString());
440 | }
441 |
442 |
443 | @Override
444 | public boolean onCreateOptionsMenu(Menu menu) {
445 | // Inflate the menu; this adds items to the action bar if it is present.
446 | getMenuInflater().inflate(R.menu.menu_main, menu);
447 | return true;
448 | }
449 |
450 | @Override
451 | public boolean onOptionsItemSelected(MenuItem item) {
452 | // Handle action bar item clicks here. The action bar will
453 | // automatically handle clicks on the Home/Up button, so long
454 | // as you specify a parent activity in AndroidManifest.xml.
455 | int id = item.getItemId();
456 |
457 | //noinspection SimplifiableIfStatement
458 | if (id == R.id.action_settings) {
459 | return true;
460 | }
461 |
462 | return super.onOptionsItemSelected(item);
463 | }
464 | }
465 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
10 |
11 |
20 |
21 |
29 |
30 |
37 |
38 |
39 |
40 |
46 |
47 |
52 |
53 |
62 |
63 |
72 |
73 |
82 |
83 |
92 |
93 |
102 |
103 |
104 |
105 |
110 |
111 |
120 |
121 |
130 |
131 |
140 |
141 |
150 |
151 |
160 |
161 |
162 |
163 |
168 |
169 |
170 |
179 |
180 |
189 |
190 |
199 |
200 |
209 |
210 |
219 |
220 |
221 |
222 |
223 |
228 |
229 |
238 |
239 |
248 |
249 |
258 |
259 |
268 |
269 |
270 |
279 |
280 |
281 |
282 |
283 |
288 |
289 |
298 |
299 |
308 |
309 |
318 |
319 |
328 |
329 |
338 |
339 |
340 |
341 |
342 |
347 |
348 |
349 |
358 |
359 |
368 |
369 |
378 |
379 |
388 |
389 |
398 |
399 |
400 |
401 |
402 |
403 |
404 |
405 |
--------------------------------------------------------------------------------