diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..2b06303 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,36 @@ +apply plugin: 'com.android.application' +apply plugin: 'kotlin-android' +apply plugin: 'kotlin-android-extensions' + +android { + compileSdkVersion 30 + + defaultConfig { + applicationId "com.justaloli.masscalc" + minSdkVersion 26 + targetSdkVersion 30 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + implementation fileTree(dir: "libs", include: ["*.jar"]) + implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + implementation 'androidx.core:core-ktx:1.3.2' + implementation 'androidx.appcompat:appcompat:1.2.0' + implementation 'androidx.constraintlayout:constraintlayout:2.0.2' + testImplementation 'junit:junit:4.12' + androidTestImplementation 'androidx.test.ext:junit:1.1.2' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' + +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..64b4a05 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# 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 *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/app/release/app-release.apk b/app/release/app-release.apk new file mode 100644 index 0000000..557aca3 Binary files /dev/null and b/app/release/app-release.apk differ diff --git a/app/release/output-metadata.json b/app/release/output-metadata.json new file mode 100644 index 0000000..71f03b9 --- /dev/null +++ b/app/release/output-metadata.json @@ -0,0 +1,20 @@ +{ + "version": 3, + "artifactType": { + "type": "APK", + "kind": "Directory" + }, + "applicationId": "com.justaloli.masscalc", + "variantName": "release", + "elements": [ + { + "type": "SINGLE", + "filters": [], + "attributes": [], + "versionCode": 1, + "versionName": "1.0", + "outputFile": "app-release.apk" + } + ], + "elementType": "File" +} \ No newline at end of file diff --git a/app/src/androidTest/java/com/justaloli/masscalc/ExampleInstrumentedTest.kt b/app/src/androidTest/java/com/justaloli/masscalc/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..9c0d446 --- /dev/null +++ b/app/src/androidTest/java/com/justaloli/masscalc/ExampleInstrumentedTest.kt @@ -0,0 +1,24 @@ +package com.justaloli.masscalc + +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.ext.junit.runners.AndroidJUnit4 + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + assertEquals("com.justaloli.masscalc", appContext.packageName) + } +} \ 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..0906ccb --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/justaloli/masscalc/MainActivity.kt b/app/src/main/java/com/justaloli/masscalc/MainActivity.kt new file mode 100644 index 0000000..b4bc473 --- /dev/null +++ b/app/src/main/java/com/justaloli/masscalc/MainActivity.kt @@ -0,0 +1,119 @@ +package com.justaloli.masscalc + +import android.graphics.Color +import android.os.Bundle +import android.view.inputmethod.EditorInfo +import android.widget.ArrayAdapter +import androidx.appcompat.app.AppCompatActivity +import kotlinx.android.synthetic.main.activity_main.* + +class MainActivity : AppCompatActivity() { + var mm = mutableMapOf()//保存原子质量的map + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main)//家在layout + window.navigationBarColor = Color.TRANSPARENT //设置导航栏沉浸 + //状态栏和自动夜间模式沉浸写在styles.xml里 + + initMassMap()//加载原子质量map + + //输入框的相关绑定 + editText.setOnEditorActionListener { view, i, event -> + if(i==EditorInfo.IME_ACTION_SEARCH){ //将输入法回车事件绑定给处理函数 + processInput() + } + true //这是个什么参数,不知道 + } + + //配置输入时的候选框 + val adapter = ArrayAdapter(this, + R.layout.support_simple_spinner_dropdown_item, + mm.keys.toList()) //定义候选框 adapter + editText.setAdapter(adapter) //设置输入框的候选为adapter(上一行的) + editText.setOnItemClickListener { _, _, _, _ -> //这四个参数目前用不到 + processInput()}//当点击候选框中item的时候 直接进行processInput()方法,无需再次回车 + + //按钮的相关绑定 + button.setOnClickListener { + myprint(mm.toList().toString()) + }//设置点按打印全部原子质量 + button.setOnLongClickListener{ + textView.text = "";true //这个true是这个长按监听需要的一个值,没啥用,就true吧 + }//设置长按清屏 + + } + private fun processInput(){ //处理 + val input = editText.text.toString() //获取输入框的输入 + val mass = calcMain(input) //调用计算函数 + myprint("$input : $mass") //输出结果 + } + + // 两个用于相对分子质量的函数 + private fun calcMass(li:Array):Double{ //主要用于匹配每个原子和对应的系数,进行相乘再相加的运算 + var r=0.0 + val re = "[A-Z][a-z]*|[0-9]*" + for (i in li){ + var relist = emptyArray() + Regex(re).findAll(i).forEach {relist+=it.value} + if(relist[1]==""){ + relist[1] = "1" + } + mm[relist[0]]?.also { r+=it*relist[1].toInt()} + } + return r + } + private fun calcMain(input:String):Double{ //主要用于识别、拆分输入中的括号,并累加全部计算结果 + mm[input]?.also { return it } + var res = 0.0 + var allMatch = "" + Regex("\\([^()]*(((\\()[^()]*)+((\\))[^()]*)+)*\\)[0-9]*|[A-Z][a-z]*[0-9]*").findAll(input).forEach { + val i = it.value + allMatch+=i + if(i!=""){ + val r: Double +// val t: Int + if(i[0]=='('){ + var inp_li = emptyArray() + Regex(".*\\)|[0-9]*").findAll(i).forEach { inp_li+=it.value } + if(inp_li[1]==""){inp_li[1] = "1"} +// myprint(inp_li.toList().toString()) +// myprint(inp_li[0].substring(1,inp_li[0].lastIndex)) + r = calcMain(inp_li[0].substring(1,inp_li[0].lastIndex))*inp_li[1].toInt() + } + else{ + r = calcMass(arrayOf(i)) + } + res+=r + } + } + if(allMatch!=input){ + myprint("input incorrect. matched:$allMatch ; input:$input") + res = 0.0 + } + //写入历史部分 目前停用 +// if(isOutLayer){ +// if(res!=0.0 && switchHistory.isChecked) { +// openFileOutput(input, MODE_PRIVATE).use { +// it.write(res.toString().toByteArray()) +// } +// } +// } + return res + } + //初始化原子质量map + private fun initMassMap(){ + mm.putAll(listOf(Pair("H",1.008),Pair("He",4.0026),Pair("Li",6.94),Pair("Be",9.0122),Pair("B",10.81),Pair("C",12.011),Pair("N",14.007),Pair("O",15.999),Pair("F",18.998),Pair("Ne",20.18),Pair("Na",22.99),Pair("Mg",24.305),Pair("Al",26.982),Pair("Si",28.085),Pair("P",30.974),Pair("S",32.06),Pair("Cl",35.45),Pair("Ar",39.948),Pair("K",39.098),Pair("Ca",40.078),Pair("Sc",44.956),Pair("Ti",47.867),Pair("V",50.942),Pair("Cr",51.996),Pair("Mn",54.938),Pair("Fe",55.845),Pair("Co",58.933),Pair("Ni",58.693),Pair("Cu",63.546),Pair("Zn",65.38),Pair("Ga",69.723),Pair("Ge",72.63),Pair("As",74.922),Pair("Se",78.971),Pair("Br",79.904),Pair("Kr",83.798),Pair("Rb",85.468),Pair("Sr",87.62),Pair("Y",88.906),Pair("Zr",91.224),Pair("Nb",92.906),Pair("Mo",95.95),Pair("Tc",98.0),Pair("Ru",101.07),Pair("Rh",102.91),Pair("Pd",106.42),Pair("Ag",107.87),Pair("Cd",112.41),Pair("In",114.82),Pair("Sn",204.38),Pair("Sb",121.76),Pair("Te",127.6),Pair("I",126.9),Pair("Xe",131.29),Pair("Cs",132.91),Pair("Ba",137.33),Pair("La",138.91),Pair("Ce",140.12),Pair("Pr",140.91),Pair("Nd",144.24),Pair("Pm",144.24),Pair("Sm",150.36),Pair("Eu",151.96),Pair("Gd",157.25),Pair("Tb",158.93),Pair("Dy",162.5),Pair("Ho",164.93),Pair("Er",167.26),Pair("Tm",168.93),Pair("Yb",173.05),Pair("Lu",174.97),Pair("Hf",178.49),Pair("Ta",180.95),Pair("W",183.84),Pair("Re",186.21),Pair("Os",190.23),Pair("Ir",192.22),Pair("Pt",195.08),Pair("Au",196.97),Pair("Hg",200.59),Pair("Tl",204.38),Pair("Pb",207.2),Pair("Bi",208.98),Pair("Po",209.0),Pair("At",210.0),Pair("Rn",222.0),Pair("Fr",223.0),Pair("Ra",226.0),Pair("Ac",227.0),Pair("Th",232.04),Pair("Pa",231.04),Pair("U",238.03),Pair("Np",237.0),Pair("Pu",244.0),Pair("Am",243.0),Pair("Cm",247.0),Pair("Bk",247.0),Pair("Cf",251.0),Pair("Es",252.0),Pair("Fm",257.0),Pair("Md",258.0),Pair("No",259.0),Pair("Lr",266.0),Pair("Rf",267.0),Pair("Db",268.0),Pair("Sg",269.0),Pair("Bh",270.0),Pair("Hs",277.0),Pair("Mt",278.0),Pair("Ds",281.0),Pair("Rg",282.0),Pair("Cn",282.0),Pair("Nh",286.0),Pair("Fl",289.0),Pair("Mc",290.0),Pair("Lv",293.0),Pair("Ts",294.0),Pair("Og",294.0),)) + //把应用数据里面的历史记录存起来 也放到mm字典里面 目前停用 +// var historyList = arrayOf>() +// for(fn in fileList()){ +// val mass = openFileInput(fn).bufferedReader().readText().toDouble() +// historyList+=Pair(fn,mass) +// } +// mm.putAll(historyList) + } + //打印输出结果函数 + private fun myprint(text:String, isclear:Boolean=false){ + if(isclear){textView.text=""} + textView.append("\noutput:$text\n") + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..cc14f03 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..a4f78de --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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..eda1d08 --- /dev/null +++ b/app/src/main/res/layout-land/activity_main.xml @@ -0,0 +1,73 @@ + + + + + + + +