From 60af6890b55d054e32d5a9f2452829b74af461e5 Mon Sep 17 00:00:00 2001 From: timmurk Date: Tue, 15 Mar 2016 22:14:41 +0300 Subject: [PATCH 1/2] Initial commit --- EasyCalc/.gitignore | 7 + EasyCalc/.idea/.name | 1 + EasyCalc/.idea/compiler.xml | 22 ++ .../.idea/copyright/profiles_settings.xml | 3 + EasyCalc/.idea/encodings.xml | 6 + EasyCalc/.idea/gradle.xml | 18 + EasyCalc/.idea/misc.xml | 46 +++ EasyCalc/.idea/modules.xml | 9 + EasyCalc/.idea/runConfigurations.xml | 12 + EasyCalc/.idea/vcs.xml | 6 + EasyCalc/EasyCalc.iml | 19 ++ EasyCalc/app/.gitignore | 1 + EasyCalc/app/app.iml | 101 ++++++ EasyCalc/app/build.gradle | 27 ++ EasyCalc/app/proguard-rules.pro | 17 + .../easycalc/ApplicationTest.java | 13 + EasyCalc/app/src/main/AndroidManifest.xml | 23 ++ .../android_2015/easycalc/MainActivity.java | 217 ++++++++++++ .../main/res/layout-land/activity_main.xml | 322 ++++++++++++++++++ .../app/src/main/res/layout/activity_main.xml | 252 ++++++++++++++ EasyCalc/app/src/main/res/menu/menu_main.xml | 6 + .../src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 3418 bytes .../src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2206 bytes .../src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4842 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 7718 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 10486 bytes .../app/src/main/res/values-land/dimens.xml | 9 + .../app/src/main/res/values-ru/strings.xml | 6 + .../app/src/main/res/values-v21/styles.xml | 8 + .../app/src/main/res/values-w820dp/dimens.xml | 6 + EasyCalc/app/src/main/res/values/colors.xml | 7 + EasyCalc/app/src/main/res/values/dimens.xml | 9 + EasyCalc/app/src/main/res/values/strings.xml | 7 + EasyCalc/app/src/main/res/values/styles.xml | 16 + .../easycalc/ExampleUnitTest.java | 15 + EasyCalc/build.gradle | 23 ++ EasyCalc/gradle.properties | 18 + EasyCalc/gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 49896 bytes .../gradle/wrapper/gradle-wrapper.properties | 6 + EasyCalc/gradlew | 164 +++++++++ EasyCalc/gradlew.bat | 90 +++++ EasyCalc/settings.gradle | 1 + 42 files changed, 1513 insertions(+) create mode 100644 EasyCalc/.gitignore create mode 100644 EasyCalc/.idea/.name create mode 100644 EasyCalc/.idea/compiler.xml create mode 100644 EasyCalc/.idea/copyright/profiles_settings.xml create mode 100644 EasyCalc/.idea/encodings.xml create mode 100644 EasyCalc/.idea/gradle.xml create mode 100644 EasyCalc/.idea/misc.xml create mode 100644 EasyCalc/.idea/modules.xml create mode 100644 EasyCalc/.idea/runConfigurations.xml create mode 100644 EasyCalc/.idea/vcs.xml create mode 100644 EasyCalc/EasyCalc.iml create mode 100644 EasyCalc/app/.gitignore create mode 100644 EasyCalc/app/app.iml create mode 100644 EasyCalc/app/build.gradle create mode 100644 EasyCalc/app/proguard-rules.pro create mode 100644 EasyCalc/app/src/androidTest/java/ru/ifmo/android_2015/easycalc/ApplicationTest.java create mode 100644 EasyCalc/app/src/main/AndroidManifest.xml create mode 100644 EasyCalc/app/src/main/java/ru/ifmo/android_2015/easycalc/MainActivity.java create mode 100644 EasyCalc/app/src/main/res/layout-land/activity_main.xml create mode 100644 EasyCalc/app/src/main/res/layout/activity_main.xml create mode 100644 EasyCalc/app/src/main/res/menu/menu_main.xml create mode 100644 EasyCalc/app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 EasyCalc/app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 EasyCalc/app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 EasyCalc/app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 EasyCalc/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 EasyCalc/app/src/main/res/values-land/dimens.xml create mode 100644 EasyCalc/app/src/main/res/values-ru/strings.xml create mode 100644 EasyCalc/app/src/main/res/values-v21/styles.xml create mode 100644 EasyCalc/app/src/main/res/values-w820dp/dimens.xml create mode 100644 EasyCalc/app/src/main/res/values/colors.xml create mode 100644 EasyCalc/app/src/main/res/values/dimens.xml create mode 100644 EasyCalc/app/src/main/res/values/strings.xml create mode 100644 EasyCalc/app/src/main/res/values/styles.xml create mode 100644 EasyCalc/app/src/test/java/ru/ifmo/android_2015/easycalc/ExampleUnitTest.java create mode 100644 EasyCalc/build.gradle create mode 100644 EasyCalc/gradle.properties create mode 100644 EasyCalc/gradle/wrapper/gradle-wrapper.jar create mode 100644 EasyCalc/gradle/wrapper/gradle-wrapper.properties create mode 100644 EasyCalc/gradlew create mode 100644 EasyCalc/gradlew.bat create mode 100644 EasyCalc/settings.gradle diff --git a/EasyCalc/.gitignore b/EasyCalc/.gitignore new file mode 100644 index 0000000..9c4de58 --- /dev/null +++ b/EasyCalc/.gitignore @@ -0,0 +1,7 @@ +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures diff --git a/EasyCalc/.idea/.name b/EasyCalc/.idea/.name new file mode 100644 index 0000000..338c6c5 --- /dev/null +++ b/EasyCalc/.idea/.name @@ -0,0 +1 @@ +EasyCalc \ No newline at end of file diff --git a/EasyCalc/.idea/compiler.xml b/EasyCalc/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/EasyCalc/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/EasyCalc/.idea/copyright/profiles_settings.xml b/EasyCalc/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/EasyCalc/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/EasyCalc/.idea/encodings.xml b/EasyCalc/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/EasyCalc/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/EasyCalc/.idea/gradle.xml b/EasyCalc/.idea/gradle.xml new file mode 100644 index 0000000..8d2df47 --- /dev/null +++ b/EasyCalc/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/EasyCalc/.idea/misc.xml b/EasyCalc/.idea/misc.xml new file mode 100644 index 0000000..5d19981 --- /dev/null +++ b/EasyCalc/.idea/misc.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/EasyCalc/.idea/modules.xml b/EasyCalc/.idea/modules.xml new file mode 100644 index 0000000..64adf4d --- /dev/null +++ b/EasyCalc/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/EasyCalc/.idea/runConfigurations.xml b/EasyCalc/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/EasyCalc/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/EasyCalc/.idea/vcs.xml b/EasyCalc/.idea/vcs.xml new file mode 100644 index 0000000..6564d52 --- /dev/null +++ b/EasyCalc/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/EasyCalc/EasyCalc.iml b/EasyCalc/EasyCalc.iml new file mode 100644 index 0000000..fc40f88 --- /dev/null +++ b/EasyCalc/EasyCalc.iml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/EasyCalc/app/.gitignore b/EasyCalc/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/EasyCalc/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/EasyCalc/app/app.iml b/EasyCalc/app/app.iml new file mode 100644 index 0000000..049ac5d --- /dev/null +++ b/EasyCalc/app/app.iml @@ -0,0 +1,101 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/EasyCalc/app/build.gradle b/EasyCalc/app/build.gradle new file mode 100644 index 0000000..19f4d0a --- /dev/null +++ b/EasyCalc/app/build.gradle @@ -0,0 +1,27 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 23 + buildToolsVersion "23.0.1" + + defaultConfig { + applicationId "ru.ifmo.android_2015.easycalc" + minSdkVersion 15 + targetSdkVersion 23 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + testCompile 'junit:junit:4.12' + compile 'com.android.support:appcompat-v7:23.1.0' + compile 'com.android.support:design:23.1.0' +} diff --git a/EasyCalc/app/proguard-rules.pro b/EasyCalc/app/proguard-rules.pro new file mode 100644 index 0000000..70b9eba --- /dev/null +++ b/EasyCalc/app/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /Users/ruslanabdulhalikov/Library/Android/sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/EasyCalc/app/src/androidTest/java/ru/ifmo/android_2015/easycalc/ApplicationTest.java b/EasyCalc/app/src/androidTest/java/ru/ifmo/android_2015/easycalc/ApplicationTest.java new file mode 100644 index 0000000..f161eca --- /dev/null +++ b/EasyCalc/app/src/androidTest/java/ru/ifmo/android_2015/easycalc/ApplicationTest.java @@ -0,0 +1,13 @@ +package ru.ifmo.android_2015.easycalc; + +import android.app.Application; +import android.test.ApplicationTestCase; + +/** + * Testing Fundamentals + */ +public class ApplicationTest extends ApplicationTestCase { + public ApplicationTest() { + super(Application.class); + } +} \ No newline at end of file diff --git a/EasyCalc/app/src/main/AndroidManifest.xml b/EasyCalc/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..b76d9e5 --- /dev/null +++ b/EasyCalc/app/src/main/AndroidManifest.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + diff --git a/EasyCalc/app/src/main/java/ru/ifmo/android_2015/easycalc/MainActivity.java b/EasyCalc/app/src/main/java/ru/ifmo/android_2015/easycalc/MainActivity.java new file mode 100644 index 0000000..2e609fd --- /dev/null +++ b/EasyCalc/app/src/main/java/ru/ifmo/android_2015/easycalc/MainActivity.java @@ -0,0 +1,217 @@ +package ru.ifmo.android_2015.easycalc; + +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +import java.math.BigDecimal; + +public class MainActivity extends AppCompatActivity { + + BigDecimal firstArgument = null; + private TextView textView; + private Operation operation = Operation.INV; + private boolean needToClear = false; + private boolean decimalUsed = false; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + textView = (TextView) findViewById(R.id.textView); + } + + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + if (firstArgument != null) { + outState.putString("firstArgument", firstArgument.toString()); + } + outState.putBoolean("needToClear", needToClear); + outState.putBoolean("decimalUsed", decimalUsed); + outState.putInt("operation", operation.ordinal()); + } + + protected void onRestoreInstanceState(Bundle savedInstanceState) { + super.onRestoreInstanceState(savedInstanceState); + if (savedInstanceState.getString("firstArgument") != null) { + firstArgument = new BigDecimal(savedInstanceState.getString("firstArgument")); + } + needToClear = savedInstanceState.getBoolean("needToClear"); + decimalUsed = savedInstanceState.getBoolean("decimalUsed"); + operation = Operation.values()[savedInstanceState.getInt("operation")]; + } + + public void onClickDigit(View v) { + StringBuffer s = new StringBuffer(textView.getText()); + String digit = ((Button) v).getText().toString(); + Log.i("onClickDigit", " " + firstArgument); + if (needToClear) { + s.setLength(0); + decimalUsed = false; + } + if (check(s, digit)) { + if (needToClear) { + needToClear = false; + } + if (operation == Operation.ERR) { + operation = Operation.INV; + } + s.append(digit); + textView.setText(s.toString()); + } + } + + private boolean check(StringBuffer s, String digit) { + switch (digit) { + case ".": + if (decimalUsed || operation == Operation.POW || s.length() == 0) { + return false; + } else { + return decimalUsed = true; + } + case "0": + return (s.length() == 0 || s.charAt(0) != '0' || decimalUsed); + case "00": + return (s.length() > 0 && s.charAt(0) != '0' || decimalUsed); + default: + return !(s.length() == 1 && s.charAt(0) == '0'); + } + } + + public void onClickOperation(View v) { + StringBuffer s = new StringBuffer(textView.getText().toString()); + Log.i("onClickOperation", operation.toString() + " " + firstArgument); + if (R.id.buttonAllClear == v.getId()) { + firstArgument = null; + operation = Operation.INV; + decimalUsed = false; + s.setLength(0); + textView.setText(s); + return; + } + if (firstArgument == null && s.length() == 0 || operation == Operation.ERR) { + return; + } + + switch (v.getId()) { + case R.id.buttonCos: + operation = Operation.COS; + evaluate(); + break; + case R.id.buttonSin: + operation = Operation.SIN; + evaluate(); + break; + case R.id.buttonFact: + operation = Operation.FACT; + evaluate(); + break; + case R.id.buttonSquare: + operation = Operation.SQR; + evaluate(); + break; + case R.id.buttonPower: + operation = Operation.POW; + break; + case R.id.buttonAdd: + operation = Operation.ADD; + break; + case R.id.buttonSubtract: + operation = Operation.SUB; + break; + case R.id.buttonMultiply: + operation = Operation.MUL; + break; + case R.id.buttonDivision: + operation = Operation.DIV; + break; + case R.id.buttonChangeSign: + if (s.length() != 0) { + if (s.charAt(0) == '-') { + s.deleteCharAt(0); + } else { + s.insert(0, '-'); + } + textView.setText(s); + } + break; + case R.id.buttonEvaluate: + evaluate(); + break; + case R.id.buttonMod: + operation = Operation.MOD; + break; + } + if (operation != Operation.ERR && (firstArgument == null || needToClear)) { + firstArgument = new BigDecimal(s.toString()); + } + needToClear = true; + } + + private void evaluate() { + String s = textView.getText().toString(); + if (firstArgument == null || needToClear) { + firstArgument = new BigDecimal(s.toString()); + } + + switch (operation) { + case MUL: + firstArgument = firstArgument.multiply(new BigDecimal(s)); + break; + case DIV: + if (s.equals("0")) { + firstArgument = null; + } else { + firstArgument = firstArgument.divide(new BigDecimal(s), 6, BigDecimal.ROUND_CEILING); + } + break; + case ADD: + firstArgument = firstArgument.add(new BigDecimal(s)); + break; + case SUB: + firstArgument = firstArgument.subtract(new BigDecimal(s)); + break; + case MOD: + firstArgument = firstArgument.divideAndRemainder(new BigDecimal(s))[1]; + break; + case POW: + firstArgument = firstArgument.pow(new Integer(s).intValue()); + break; + case SQR: + firstArgument = firstArgument.pow(2); + break; + case SIN: + firstArgument = new BigDecimal(Math.sin(firstArgument.doubleValue() / 180 * Math.PI)); + firstArgument = firstArgument.setScale(6, BigDecimal.ROUND_HALF_UP); + break; + case COS: + firstArgument = new BigDecimal(Math.cos(firstArgument.doubleValue() / 180 * Math.PI)); + firstArgument = firstArgument.setScale(6, BigDecimal.ROUND_HALF_UP); + break; + case FACT: + if (firstArgument.compareTo(new BigDecimal(31)) == -1) { + int p = firstArgument.intValue(); + firstArgument = new BigDecimal(1); + for (int i = 1; i <= p; i++) { + firstArgument = firstArgument.multiply(new BigDecimal(i)); + } + } else { + firstArgument = null; + } + break; + } + if (firstArgument == null) { + textView.setText(getResources().getString(R.string.error)); + operation = Operation.ERR; + } else { + textView.setText(firstArgument.toString()); + } + } + + enum Operation { + MUL, DIV, ADD, SUB, MOD, INV, POW, SQR, SIN, COS, FACT, ERR + } +} diff --git a/EasyCalc/app/src/main/res/layout-land/activity_main.xml b/EasyCalc/app/src/main/res/layout-land/activity_main.xml new file mode 100644 index 0000000..2fea76a --- /dev/null +++ b/EasyCalc/app/src/main/res/layout-land/activity_main.xml @@ -0,0 +1,322 @@ + + + + + + + +