diff --git a/My_Calculator.iml b/My_Calculator.iml new file mode 100644 index 0000000..098970b --- /dev/null +++ b/My_Calculator.iml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/app.iml b/app/app.iml new file mode 100644 index 0000000..c56c8f4 --- /dev/null +++ b/app/app.iml @@ -0,0 +1,97 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..74e0b0f --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,26 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 23 + buildToolsVersion "23.0.2" + + defaultConfig { + applicationId "ru.kozlov_ea.my_calculator" + 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.1' +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..41afecb --- /dev/null +++ b/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 D:\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/app/src/androidTest/java/ru/kozlov_ea/my_calculator/ApplicationTest.java b/app/src/androidTest/java/ru/kozlov_ea/my_calculator/ApplicationTest.java new file mode 100644 index 0000000..ed592b4 --- /dev/null +++ b/app/src/androidTest/java/ru/kozlov_ea/my_calculator/ApplicationTest.java @@ -0,0 +1,13 @@ +package ru.kozlov_ea.my_calculator; + +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/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..d3d1121 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + diff --git a/app/src/main/java/ru/kozlov_ea/my_calculator/MainActivity.java b/app/src/main/java/ru/kozlov_ea/my_calculator/MainActivity.java new file mode 100644 index 0000000..447c830 --- /dev/null +++ b/app/src/main/java/ru/kozlov_ea/my_calculator/MainActivity.java @@ -0,0 +1,114 @@ +package ru.kozlov_ea.my_calculator; + +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +public class MainActivity extends AppCompatActivity implements View.OnClickListener { + + Button bAC, bC, bAns; + + TextView Ans; + String inStr = ""; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + // определяем элементы + + bAC = (Button) findViewById(R.id.bAC); + bC = (Button) findViewById(R.id.bC); + bAns = (Button) findViewById(R.id.bAns); + + Ans = (TextView) findViewById(R.id.textView); + + int[] digits = new int[] {R.id.b1, R.id.b2, R.id.b3, R.id.b4, R.id.b5, R.id.b6, R.id.b7, R.id.b8, R.id.b9, R.id.b0, R.id.bdot}; + int[] operations = new int[] {R.id.bplus, R.id.bminus, R.id.bmul, R.id.bdiv}; + + for(int number: digits) { + findViewById(number).setOnClickListener(digitsListener); + } + + for(int op: operations) { + findViewById(op).setOnClickListener(operationsListener); + } + + bAC.setOnClickListener(this); + bC.setOnClickListener(this); + bAns.setOnClickListener(this); + } + + private boolean isBinaryOperation (char ch) { + return (ch == '+') || (ch == '-') || (ch == '*') || (ch == '/'); + } + + View.OnClickListener digitsListener = new View.OnClickListener() { + @Override + public void onClick(View view) { + Button inBtn = (Button) view; + inStr += inBtn.getText(); + Ans.setText(inStr); + } + }; + + View.OnClickListener operationsListener = new View.OnClickListener() { + @Override + public void onClick(View view) { + Button inBtn = (Button) view; + if (inStr.length() > 0) { + int curLen = inStr.length() - 1; + char cur = inStr.charAt(curLen); + if (isBinaryOperation(cur)) { + inStr = inStr.substring(0, curLen); + } + } + + inStr += inBtn.getText(); + Ans.setText(inStr); + } + }; + + + // нажатая пользователем кнопка определяет текущую операцию + public void onClick(View v) { + try { + switch (v.getId()) { + + case R.id.bAC: + inStr = ""; + Ans.setText(inStr); + break; + case R.id.bC: + inStr = inStr.substring(0, inStr.length() - 1); + Ans.setText(inStr); + break; + + case R.id.bAns: + double t = new ParserModel(inStr).Third(); + String s = Double.toString(t); + + Ans.setText(s); + inStr = s; + break; + } + } catch (Exception e) { + Ans.setText("Invalid input"); + inStr = ""; + } + } + + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + outState.putString("Input data", inStr); + } + + protected void onRestoreInstanceState(Bundle savedInstanceState) { + super.onRestoreInstanceState(savedInstanceState); + inStr = savedInstanceState.getString("Input data"); + Ans.setText(inStr); + } +} diff --git a/app/src/main/java/ru/kozlov_ea/my_calculator/ParserModel.java b/app/src/main/java/ru/kozlov_ea/my_calculator/ParserModel.java new file mode 100644 index 0000000..6dd9294 --- /dev/null +++ b/app/src/main/java/ru/kozlov_ea/my_calculator/ParserModel.java @@ -0,0 +1,102 @@ +package ru.kozlov_ea.my_calculator; + +public class ParserModel { + + private String inStr; + private int count; + + private static final int maxStringLen = 15; + + ParserModel(String in) { + inStr = in; + count = 0; + } + + + //проверяем корретность строки на длину + private boolean CheckStr(String cur) { + return cur.length() > 0 && cur.length() < maxStringLen; + } + + // если исходная строчка задана корректно, то разбираем её + private String getCur() { + String curS = ""; + int m = count; + if (CheckStr(inStr)) { + while (count < inStr.length()) { + char ch = inStr.charAt(count); + if ((ch == '.') || (ch >= '0' && ch <= '9')) { + count++; + } else { + break; + } + } + } + if (m < count) { + curS = inStr.substring(m, count); + } + return curS; + } + + private double First() { + double curAns = 0; + boolean flag = false; + + if (inStr.charAt(count) == '-') { + count++; + flag= true; + } + + String q = getCur(); + if (flag) { + q = '-' + q; + } + + if (CheckStr(q)) { + curAns = Double.parseDouble(q); + } + return curAns; + } + + private Double Second() { + double t = First(); + while (count < inStr.length()) { + char ch = inStr.charAt(count); + switch (ch) { + case '*': + count++; + t = t * First(); + break; + case '/': + count++; + t = t / First(); + break; + default: + return t; + } + } + return t; + } + + public Double Third() { + double t = Second(); + while (count < inStr.length()) { + char ch = inStr.charAt(count); + switch (ch) { + case '+': + count++; + t += Second(); + + break; + case '-': + count++; + t -= Second(); + + break; + default: + return t; + } + } + return t; + } +} diff --git a/app/src/main/res/layout-land/activity_main.xml b/app/src/main/res/layout-land/activity_main.xml new file mode 100644 index 0000000..d77be7d --- /dev/null +++ b/app/src/main/res/layout-land/activity_main.xml @@ -0,0 +1,191 @@ + + + + + + + + + +