-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
960 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} |
24 changes: 24 additions & 0 deletions
24
app/src/androidTest/java/com/justaloli/masscalc/ExampleInstrumentedTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.justaloli.masscalc"> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme"> | ||
<activity android:name=".MainActivity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
119 changes: 119 additions & 0 deletions
119
app/src/main/java/com/justaloli/masscalc/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String,Double>()//保存原子质量的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<String>(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<String>):Double{ //主要用于匹配每个原子和对应的系数,进行相乘再相加的运算 | ||
var r=0.0 | ||
val re = "[A-Z][a-z]*|[0-9]*" | ||
for (i in li){ | ||
var relist = emptyArray<String>() | ||
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<String>() | ||
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<Pair<String,Double>>() | ||
// 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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:aapt="http://schemas.android.com/aapt" | ||
android:width="108dp" | ||
android:height="108dp" | ||
android:viewportWidth="108" | ||
android:viewportHeight="108"> | ||
<path android:pathData="M31,63.928c0,0 6.4,-11 12.1,-13.1c7.2,-2.6 26,-1.4 26,-1.4l38.1,38.1L107,108.928l-32,-1L31,63.928z"> | ||
<aapt:attr name="android:fillColor"> | ||
<gradient | ||
android:endX="85.84757" | ||
android:endY="92.4963" | ||
android:startX="42.9492" | ||
android:startY="49.59793" | ||
android:type="linear"> | ||
<item | ||
android:color="#44000000" | ||
android:offset="0.0" /> | ||
<item | ||
android:color="#00000000" | ||
android:offset="1.0" /> | ||
</gradient> | ||
</aapt:attr> | ||
</path> | ||
<path | ||
android:fillColor="#FFFFFF" | ||
android:fillType="nonZero" | ||
android:pathData="M65.3,45.828l3.8,-6.6c0.2,-0.4 0.1,-0.9 -0.3,-1.1c-0.4,-0.2 -0.9,-0.1 -1.1,0.3l-3.9,6.7c-6.3,-2.8 -13.4,-2.8 -19.7,0l-3.9,-6.7c-0.2,-0.4 -0.7,-0.5 -1.1,-0.3C38.8,38.328 38.7,38.828 38.9,39.228l3.8,6.6C36.2,49.428 31.7,56.028 31,63.928h46C76.3,56.028 71.8,49.428 65.3,45.828zM43.4,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2c-0.3,-0.7 -0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C45.3,56.528 44.5,57.328 43.4,57.328L43.4,57.328zM64.6,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2s-0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C66.5,56.528 65.6,57.328 64.6,57.328L64.6,57.328z" | ||
android:strokeWidth="1" | ||
android:strokeColor="#00000000" /> | ||
</vector> |
Oops, something went wrong.