Simple operations
This commit is contained in:
Binary file not shown.
@@ -1,20 +0,0 @@
|
|||||||
{
|
|
||||||
"version": 3,
|
|
||||||
"artifactType": {
|
|
||||||
"type": "APK",
|
|
||||||
"kind": "Directory"
|
|
||||||
},
|
|
||||||
"applicationId": "meow.sweetbread.lincalc",
|
|
||||||
"variantName": "debug",
|
|
||||||
"elements": [
|
|
||||||
{
|
|
||||||
"type": "SINGLE",
|
|
||||||
"filters": [],
|
|
||||||
"attributes": [],
|
|
||||||
"versionCode": 1,
|
|
||||||
"versionName": "1.0",
|
|
||||||
"outputFile": "app-debug.apk"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"elementType": "File"
|
|
||||||
}
|
|
||||||
@@ -5,117 +5,382 @@ import androidx.appcompat.app.AppCompatActivity
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.widget.Button
|
import android.widget.Button
|
||||||
|
import android.widget.ImageButton
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
|
import kotlin.math.pow
|
||||||
|
import kotlin.math.sqrt
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
private var tmp_buf = ""
|
private var priority = HashMap<String, Int>()
|
||||||
var buffer = listOf<String>()
|
private var tokensType = HashMap<String, Tokens>()
|
||||||
private var act_ena = false
|
private val leftAssociative = arrayOf("^")
|
||||||
var priority = HashMap<String, Int>()
|
private var constants = HashMap<String, Double>()
|
||||||
|
|
||||||
@SuppressLint("MissingInflatedId", "CutPasteId")
|
@SuppressLint("MissingInflatedId", "CutPasteId")
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
|
|
||||||
priority.put("+", 1)
|
priority["("] = 0
|
||||||
priority.put("-", 1)
|
priority["+"] = 1
|
||||||
priority.put("*", 2)
|
priority["-"] = 1
|
||||||
priority.put("/", 2)
|
priority["*"] = 2
|
||||||
|
priority["/"] = 2
|
||||||
|
priority["^"] = 3
|
||||||
|
|
||||||
val input = findViewById<TextView>(R.id.input)
|
tokensType["+"] = Tokens.Operations
|
||||||
|
tokensType["-"] = Tokens.Operations
|
||||||
|
tokensType["*"] = Tokens.Operations
|
||||||
|
tokensType["/"] = Tokens.Operations
|
||||||
|
tokensType["^"] = Tokens.Operations
|
||||||
|
tokensType["+"] = Tokens.Operations
|
||||||
|
tokensType["+"] = Tokens.Operations
|
||||||
|
tokensType["("] = Tokens.BracketOpen
|
||||||
|
tokensType[")"] = Tokens.BracketClose
|
||||||
|
|
||||||
findViewById<Button>(R.id.btn_0).setOnClickListener { btnInt(it as Button) }
|
constants["π"] = Math.PI
|
||||||
findViewById<Button>(R.id.btn_1).setOnClickListener { btnInt(it as Button) }
|
constants["e"] = Math.E
|
||||||
findViewById<Button>(R.id.btn_2).setOnClickListener { btnInt(it as Button) }
|
|
||||||
findViewById<Button>(R.id.btn_3).setOnClickListener { btnInt(it as Button) }
|
|
||||||
findViewById<Button>(R.id.btn_4).setOnClickListener { btnInt(it as Button) }
|
|
||||||
findViewById<Button>(R.id.btn_5).setOnClickListener { btnInt(it as Button) }
|
|
||||||
findViewById<Button>(R.id.btn_6).setOnClickListener { btnInt(it as Button) }
|
|
||||||
findViewById<Button>(R.id.btn_7).setOnClickListener { btnInt(it as Button) }
|
|
||||||
findViewById<Button>(R.id.btn_8).setOnClickListener { btnInt(it as Button) }
|
|
||||||
findViewById<Button>(R.id.btn_9).setOnClickListener { btnInt(it as Button) }
|
|
||||||
|
|
||||||
findViewById<Button>(R.id.btn_add).setOnClickListener { btnAct(it as Button) }
|
findViewById<Button>(R.id.btn_0).setOnClickListener { btn_listener(it as Button) }
|
||||||
findViewById<Button>(R.id.btn_sub).setOnClickListener { btnAct(it as Button) }
|
findViewById<Button>(R.id.btn_1).setOnClickListener { btn_listener(it as Button) }
|
||||||
findViewById<Button>(R.id.btn_mul).setOnClickListener { btnAct(it as Button) }
|
findViewById<Button>(R.id.btn_2).setOnClickListener { btn_listener(it as Button) }
|
||||||
findViewById<Button>(R.id.btn_div).setOnClickListener { btnAct(it as Button) }
|
findViewById<Button>(R.id.btn_3).setOnClickListener { btn_listener(it as Button) }
|
||||||
|
findViewById<Button>(R.id.btn_4).setOnClickListener { btn_listener(it as Button) }
|
||||||
|
findViewById<Button>(R.id.btn_5).setOnClickListener { btn_listener(it as Button) }
|
||||||
|
findViewById<Button>(R.id.btn_6).setOnClickListener { btn_listener(it as Button) }
|
||||||
|
findViewById<Button>(R.id.btn_7).setOnClickListener { btn_listener(it as Button) }
|
||||||
|
findViewById<Button>(R.id.btn_8).setOnClickListener { btn_listener(it as Button) }
|
||||||
|
findViewById<Button>(R.id.btn_9).setOnClickListener { btn_listener(it as Button) }
|
||||||
|
|
||||||
findViewById<Button>(R.id.btn_equ).setOnClickListener { toRpn() }
|
findViewById<Button>(R.id.btn_dot).setOnClickListener { btn_listener(it as Button) }
|
||||||
|
findViewById<Button>(R.id.btn_add).setOnClickListener { btn_listener(it as Button) }
|
||||||
|
findViewById<Button>(R.id.btn_sub).setOnClickListener { btn_listener(it as Button) }
|
||||||
|
findViewById<Button>(R.id.btn_mul).setOnClickListener { btn_listener(it as Button) }
|
||||||
|
findViewById<Button>(R.id.btn_div).setOnClickListener { btn_listener(it as Button) }
|
||||||
|
findViewById<Button>(R.id.btn_bro).setOnClickListener { btn_listener(it as Button) }
|
||||||
|
findViewById<Button>(R.id.btn_brc).setOnClickListener { btn_listener(it as Button) }
|
||||||
|
findViewById<Button>(R.id.btn_pow).setOnClickListener { btn_listener(it as Button) }
|
||||||
|
findViewById<Button>(R.id.btn_sqrt).setOnClickListener { btn_listener(it as Button) }
|
||||||
|
|
||||||
|
findViewById<Button>(R.id.btn_pi).setOnClickListener { btn_listener(it as Button) }
|
||||||
|
findViewById<Button>(R.id.btn_elr).setOnClickListener { btn_listener(it as Button) }
|
||||||
|
|
||||||
|
findViewById<Button>(R.id.btn_x).setOnClickListener { btn_listener(it as Button) }
|
||||||
|
|
||||||
|
findViewById<Button>(R.id.btn_equ).setOnClickListener { btn_listener(it as Button) }
|
||||||
|
|
||||||
|
val image = findViewById<TextView>(R.id.input)
|
||||||
|
|
||||||
|
findViewById<ImageButton>(R.id.del).setOnClickListener {
|
||||||
|
image.text = image.text.dropLast(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
findViewById<Button>(R.id.btn_sbm).setOnClickListener {
|
||||||
|
findViewById<TextView>(R.id.last_exp_out).text = findViewById<TextView>(R.id.input).text.toString()
|
||||||
|
val equations = findViewById<TextView>(R.id.input).text.toString().split('=')
|
||||||
|
if (equations.size == 1) {
|
||||||
|
val answer = evaluate(toRpn(tokenizer(equations[0])))
|
||||||
|
val value = if (answer.value.toString().split('.')[1] == "0")
|
||||||
|
(answer.value as Double).toInt().toString()
|
||||||
|
else (answer.value as Double).toFloat().toString()
|
||||||
|
findViewById<TextView>(R.id.left_out).text = answer.toString()
|
||||||
|
findViewById<TextView>(R.id.last_exp_out).text =
|
||||||
|
findViewById<TextView>(R.id.last_exp_out).text.toString() + " = " + value
|
||||||
|
findViewById<TextView>(R.id.input).text = ""
|
||||||
|
} else {
|
||||||
|
var left = evaluate(toRpn(tokenizer(equations[0])))
|
||||||
|
var right = evaluate(toRpn(tokenizer(equations[1])))
|
||||||
|
|
||||||
|
if (((left.type == Tokens.Number) and (right.type != Tokens.Number)) or
|
||||||
|
(left.type == Tokens.Linear) and (right.type == Tokens.Quadratic)) {
|
||||||
|
val tmp = right
|
||||||
|
left = right
|
||||||
|
right = tmp
|
||||||
|
}
|
||||||
|
|
||||||
|
findViewById<TextView>(R.id.left_out).text = left.toString()
|
||||||
|
findViewById<TextView>(R.id.right_out).text = right.toString()
|
||||||
|
|
||||||
|
if (left.type == Tokens.Number) {
|
||||||
|
val value = if (left.value.toString().split('.')[1] == "0")
|
||||||
|
(left.value as Double).toInt().toString()
|
||||||
|
else left.value.toString()
|
||||||
|
findViewById<TextView>(R.id.last_exp_out).text =
|
||||||
|
findViewById<TextView>(R.id.last_exp_out).text.toString() + " = " + value
|
||||||
|
findViewById<TextView>(R.id.input).text = ""
|
||||||
|
} else if (left.type == Tokens.Linear) {
|
||||||
|
val answer: Double
|
||||||
|
|
||||||
|
answer = if (right.type == Tokens.Number)
|
||||||
|
(left.value as Linear - right.value as Double).answer()
|
||||||
|
else
|
||||||
|
(left.value as Linear - right.value as Linear).answer()
|
||||||
|
val value: String
|
||||||
|
value = if (answer.toString().split('.')[1] == "0")
|
||||||
|
answer.toInt().toString()
|
||||||
|
else answer.toString()
|
||||||
|
findViewById<TextView>(R.id.last_exp_out).text =
|
||||||
|
findViewById<TextView>(R.id.last_exp_out).text.toString() +
|
||||||
|
"\n${(left.value as Linear).name} = " + value
|
||||||
|
findViewById<TextView>(R.id.input).text = ""
|
||||||
|
} else {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fun btnInt(btn: Button) {
|
private fun tokenizer(exp: String): List<Token> {
|
||||||
val input = findViewById<TextView>(R.id.input)
|
var position = 0
|
||||||
input.text = input.text.toString() + btn.text
|
var tokens = listOf<Token>()
|
||||||
tmp_buf += btn.text
|
|
||||||
|
|
||||||
act_ena = true
|
while (position < exp.length) {
|
||||||
findViewById<TextView>(R.id.tmp_buf_out).text = tmp_buf
|
val chr = exp[position]
|
||||||
findViewById<TextView>(R.id.buf_out).text = buffer.toString()
|
|
||||||
|
if (chr == ' ') {
|
||||||
|
position++
|
||||||
|
continue
|
||||||
|
} else if ((chr in '0'..'9') or (chr == '.')) {
|
||||||
|
var pos = position
|
||||||
|
while (pos < exp.length) {
|
||||||
|
if (!((exp[pos] in '0'..'9') or (exp[pos] == '.')))
|
||||||
|
break
|
||||||
|
pos++
|
||||||
}
|
}
|
||||||
|
if (tokens.isNotEmpty()) {
|
||||||
fun btnAct(btn: Button) {
|
if (tokens.last().type in arrayOf(Tokens.BracketClose, Tokens.Constant, Tokens.Variable))
|
||||||
if (act_ena) {
|
tokens += Token(Tokens.Operations, "*")
|
||||||
val input = findViewById<TextView>(R.id.input)
|
|
||||||
input.text = input.text.toString() + btn.text
|
|
||||||
if (tmp_buf != "") {
|
|
||||||
buffer += tmp_buf
|
|
||||||
tmp_buf = ""
|
|
||||||
}
|
}
|
||||||
act_ena = false
|
tokens += Token(Tokens.Number, exp.substring(position, pos).toDouble())
|
||||||
buffer += btn.text.toString()
|
position = pos
|
||||||
|
} else if (chr == 'X') {
|
||||||
|
if (tokens.isNotEmpty()) {
|
||||||
|
if (tokens.last().type in arrayOf(Tokens.BracketClose, Tokens.Constant, Tokens.Number))
|
||||||
|
tokens += Token(Tokens.Operations, "*")
|
||||||
}
|
}
|
||||||
findViewById<TextView>(R.id.buf_out).text = buffer.toString()
|
tokens += Token(Tokens.Variable, chr.toString())
|
||||||
|
position++
|
||||||
|
} else if (chr.toString() in constants.keys){
|
||||||
|
if (tokens.isNotEmpty()) {
|
||||||
|
if (tokens.last().type in arrayOf(Tokens.BracketClose, Tokens.Constant, Tokens.Variable, Tokens.Number))
|
||||||
|
tokens += Token(Tokens.Operations, "*")
|
||||||
}
|
}
|
||||||
|
tokens += Token(Tokens.Constant, chr.toString())
|
||||||
fun toRpn() {
|
position++
|
||||||
if (tmp_buf != "") {
|
} else if (chr in 'a'..'z') {
|
||||||
buffer += tmp_buf
|
var pos = position
|
||||||
tmp_buf = ""
|
while (pos < exp.length) {
|
||||||
|
if (exp[pos] !in 'a'..'z')
|
||||||
|
break
|
||||||
|
pos++
|
||||||
}
|
}
|
||||||
findViewById<TextView>(R.id.tmp_buf_out).text = tmp_buf
|
if (tokens.isNotEmpty()) {
|
||||||
findViewById<TextView>(R.id.buf_out).text = buffer.toString()
|
if (tokens.last().type in arrayOf(Tokens.BracketClose, Tokens.Constant, Tokens.Variable))
|
||||||
|
tokens += Token(Tokens.Operations, "*")
|
||||||
var out_buf = listOf<String>()
|
}
|
||||||
var act_stc = listOf<String>()
|
tokens += Token(Tokens.Function, exp.substring(position, pos))
|
||||||
|
position = pos
|
||||||
Log.d("Meow!", buffer.toString())
|
}
|
||||||
|
else if (tokensType.containsKey(chr.toString())) {
|
||||||
for (token in buffer) {
|
if (tokens.isNotEmpty()) {
|
||||||
if (token.toBigDecimalOrNull() != null)
|
if ((tokensType[chr.toString()]!! == Tokens.BracketOpen) and (tokens.last().type == Tokens.Number))
|
||||||
out_buf += token
|
tokens += Token(Tokens.Operations, "*")
|
||||||
else if (token in listOf<String>("+", "-", "*", "/")) {
|
if ((chr == '-') and (tokens.last().type != Tokens.Number))
|
||||||
// if (act_stc.lastOrNull() == null)
|
tokens += Token(Tokens.Number, 0.toDouble())
|
||||||
// act_stc += token
|
} else if (chr == '-')
|
||||||
// else if (priority[token]!! > priority[act_stc.last()]!!)
|
tokens += Token(Tokens.Number, 0.toDouble())
|
||||||
// act_stc += token
|
tokens += Token(tokensType[chr.toString()]!!, chr.toString())
|
||||||
// else {
|
position++
|
||||||
// out_buf += act_stc.last()
|
} else {
|
||||||
// act_stc = act_stc.dropLast(1)
|
findViewById<TextView>(R.id.input).error = "Неизвестный символ: ${exp[position]}"
|
||||||
// act_stc += token
|
|
||||||
// }
|
|
||||||
|
|
||||||
if (act_stc.lastOrNull() != null) {
|
|
||||||
while (priority[token]!! <= priority[act_stc.last()]!!) {
|
|
||||||
out_buf += act_stc.last()
|
|
||||||
act_stc = act_stc.dropLast(1)
|
|
||||||
|
|
||||||
if (act_stc.isEmpty())
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
act_stc += token
|
|
||||||
|
var new_tokens = listOf<Token>()
|
||||||
|
|
||||||
|
for (token in tokens) {
|
||||||
|
if (token.type == Tokens.Constant) {
|
||||||
|
new_tokens += Token(Tokens.Number, constants[token.value]!!)
|
||||||
|
} else {
|
||||||
|
new_tokens += token
|
||||||
}
|
}
|
||||||
Log.d("Meow!", "Act: $act_stc\tOut: $out_buf")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
out_buf += act_stc.reversed()
|
Log.i("Meow!", new_tokens.toString())
|
||||||
|
return new_tokens
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun btn_listener(btn: Button) {
|
||||||
|
val input = findViewById<TextView>(R.id.input)
|
||||||
|
input.text = input.text.toString() + btn.text.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun toRpn(tokens: List<Token>): List<Token> {
|
||||||
|
var out_buf = listOf<Token>()
|
||||||
|
val act_buf = arrayListOf<Token>()
|
||||||
|
|
||||||
|
for (token in tokens) {
|
||||||
|
if (token.type in arrayOf(Tokens.Number, Tokens.Variable))
|
||||||
|
out_buf += token
|
||||||
|
else if (token.type == Tokens.Function)
|
||||||
|
act_buf += token
|
||||||
|
else if (token.type == Tokens.Operations) {
|
||||||
|
if (act_buf.lastOrNull() != null) {
|
||||||
|
while (
|
||||||
|
(priority[token.value]!! < priority[act_buf.last().value]!!) or
|
||||||
|
((priority[token.value]!! == priority[act_buf.last().value]!!) and
|
||||||
|
(token.value in leftAssociative))) {
|
||||||
|
out_buf += act_buf.removeLast()
|
||||||
|
if (act_buf.isEmpty())
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
act_buf += token
|
||||||
|
} else if (token.type == Tokens.BracketOpen)
|
||||||
|
act_buf += token
|
||||||
|
else if (token.type == Tokens.BracketClose){
|
||||||
|
while (act_buf.last().type != Tokens.BracketOpen)
|
||||||
|
out_buf += act_buf.removeLast()
|
||||||
|
act_buf.removeLast()
|
||||||
|
if (act_buf.lastOrNull() != null) {
|
||||||
|
if (act_buf.last().type == Tokens.Function)
|
||||||
|
out_buf += act_buf.removeLast()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.d("Meow!", "Act: $act_buf\tOut: $out_buf")
|
||||||
|
}
|
||||||
|
|
||||||
|
out_buf += act_buf.reversed()
|
||||||
findViewById<TextView>(R.id.RPN_out).text = out_buf.toString()
|
findViewById<TextView>(R.id.RPN_out).text = out_buf.toString()
|
||||||
|
|
||||||
tmp_buf = ""
|
|
||||||
buffer = listOf()
|
|
||||||
act_ena = false
|
|
||||||
findViewById<TextView>(R.id.input).text = ""
|
findViewById<TextView>(R.id.input).text = ""
|
||||||
|
|
||||||
|
Log.d("Meow!", "toRpn: $out_buf")
|
||||||
|
return out_buf
|
||||||
|
}
|
||||||
|
|
||||||
|
fun evaluate(ext: List<Token>): Token {
|
||||||
|
var position = 0
|
||||||
|
var buffer = ext
|
||||||
|
var tmp_buf = listOf<Token>()
|
||||||
|
|
||||||
|
while (buffer.size != 1) {
|
||||||
|
val token = buffer[position]
|
||||||
|
|
||||||
|
if (token.type == Tokens.Function) {
|
||||||
|
if (token.value == "sqrt") {
|
||||||
|
tmp_buf = buffer.dropLast(ext.size - position + 1)
|
||||||
|
var res = Token(Tokens.Number, sqrt(buffer[position - 1].value as Double))
|
||||||
|
tmp_buf += res
|
||||||
|
tmp_buf += buffer.drop(position+1)
|
||||||
|
}
|
||||||
|
} else if (token.type == Tokens.Operations) {
|
||||||
|
val first = buffer[position-2]
|
||||||
|
val second = buffer[position-1]
|
||||||
|
|
||||||
|
//TODO: make operations with other objects
|
||||||
|
if (token.value == "+") {
|
||||||
|
tmp_buf = buffer.dropLast(ext.size - position + 2)
|
||||||
|
var res: Token
|
||||||
|
if ((first.type == Tokens.Number) and (second.type == Tokens.Number))
|
||||||
|
res = Token(Tokens.Number, (first.value as Double + second.value as Double))
|
||||||
|
else if ((first.type == Tokens.Variable) and (second.type == Tokens.Number))
|
||||||
|
res = Token(Tokens.Linear, Linear(first.value as String, second.value as Double, 0.toDouble()))
|
||||||
|
else
|
||||||
|
res = Token(Tokens.Linear, Linear(second.value as String, first.value as Double, 0.toDouble()))
|
||||||
|
tmp_buf += res
|
||||||
|
tmp_buf += buffer.drop(position+1)
|
||||||
|
} else if (token.value == "-") {
|
||||||
|
tmp_buf = buffer.dropLast(ext.size - position + 2)
|
||||||
|
val res = Token(Tokens.Number, (first.value as Double - second.value as Double))
|
||||||
|
tmp_buf += res
|
||||||
|
tmp_buf += buffer.drop(position+1)
|
||||||
|
} else if (token.value == "*") {
|
||||||
|
tmp_buf = buffer.dropLast(ext.size - position + 2)
|
||||||
|
val res: Token
|
||||||
|
res = if ((first.type == Tokens.Number) and (second.type == Tokens.Number))
|
||||||
|
Token(Tokens.Number, first.value as Double*second.value as Double)
|
||||||
|
else if ((first.type == Tokens.Number) and (second.type == Tokens.Variable))
|
||||||
|
Token(Tokens.Linear, Linear(second.value as String, 0.toDouble(), first.value as Double))
|
||||||
|
else if ((first.type == Tokens.Variable) and (second.type == Tokens.Number))
|
||||||
|
Token(Tokens.Linear, Linear(first.value as String, 0.toDouble(), second.value as Double))
|
||||||
|
else //if ((first.type == Tokens.Variable) and (second.type == Tokens.Variable))
|
||||||
|
Token(Tokens.Quadratic, Quadratic(first.value as String, 0.toDouble(), 0.toDouble(), 2.toDouble()))
|
||||||
|
tmp_buf += res
|
||||||
|
tmp_buf += buffer.drop(position+1)
|
||||||
|
} else if (token.value == "/") {
|
||||||
|
tmp_buf = buffer.dropLast(ext.size - position + 2)
|
||||||
|
val res = Token(Tokens.Number, first.value as Double/second.value as Double)
|
||||||
|
tmp_buf += res
|
||||||
|
tmp_buf += buffer.drop(position+1)
|
||||||
|
} else if (token.value == "^") {
|
||||||
|
tmp_buf = buffer.dropLast(ext.size - position + 2)
|
||||||
|
val res = Token(
|
||||||
|
Tokens.Number, (first.value as Double)
|
||||||
|
.pow(second.value as Double)
|
||||||
|
)
|
||||||
|
tmp_buf += res
|
||||||
|
tmp_buf += buffer.drop(position + 1)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
position++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer = tmp_buf
|
||||||
|
position = 0
|
||||||
|
Log.d("Meow!", buffer.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
return buffer[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
enum class Tokens {
|
||||||
|
Number,
|
||||||
|
Operations,
|
||||||
|
Function,
|
||||||
|
BracketOpen,
|
||||||
|
BracketClose,
|
||||||
|
Constant,
|
||||||
|
Variable,
|
||||||
|
Linear,
|
||||||
|
Quadratic
|
||||||
|
}
|
||||||
|
|
||||||
|
class Token (val type: Tokens, var value: Any) {
|
||||||
|
override fun toString(): String {
|
||||||
|
return "<$type, $value>"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
open class MathObject(val value: Any) {
|
||||||
|
operator fun minus(num: MathObject): MathObject {
|
||||||
|
return MathObject(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Linear(val name: String, private val add: Double, private val mul: Double) : MathObject("$mul*$name+$add") {
|
||||||
|
//TODO: Implement more methods
|
||||||
|
override fun toString(): String {
|
||||||
|
return "$mul*$name+$add"
|
||||||
|
}
|
||||||
|
|
||||||
|
operator fun minus(num: Double): Linear {
|
||||||
|
return Linear(name, add-num, mul)
|
||||||
|
}
|
||||||
|
|
||||||
|
operator fun minus(lin: Linear): Linear {
|
||||||
|
// if (name != lin.name) throw IllegalArgumentException
|
||||||
|
return Linear(name, add-lin.add, mul-lin.mul)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun answer() = -add/mul
|
||||||
|
}
|
||||||
|
|
||||||
|
class Quadratic(name: String, add: Double, mul: Double, pow: Double) {
|
||||||
|
//TODO: Implement more methods
|
||||||
|
}
|
||||||
@@ -14,29 +14,117 @@
|
|||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:id="@+id/tmp_buf_out"
|
|
||||||
android:layout_width="0dp"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
|
||||||
app:layout_constraintTop_toBottomOf="@+id/input" />
|
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:id="@+id/buf_out"
|
|
||||||
android:layout_width="0dp"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
|
||||||
app:layout_constraintTop_toBottomOf="@+id/tmp_buf_out" />
|
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/RPN_out"
|
android:id="@+id/RPN_out"
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@+id/last_exp_out" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/left_out"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@+id/RPN_out" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/right_out"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@+id/left_out" />
|
||||||
|
|
||||||
|
<ScrollView
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
app:layout_constraintBottom_toTopOf="@+id/linearLayout2"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toBottomOf="@+id/buf_out" />
|
app:layout_constraintTop_toBottomOf="@+id/RPN_out">
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="vertical" >
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:orientation="vertical">
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btn_sbm"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="Вычислить" />
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:orientation="horizontal">
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btn_x"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:text="X" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btn_pi"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:text="π" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btn_elr"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:text="e" />
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:orientation="horizontal">
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btn_bro"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:text="(" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btn_brc"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:text=")" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btn_pow"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:text="^" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btn_sqrt"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:text="sqrt" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
</ScrollView>
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
android:id="@+id/linearLayout2"
|
android:id="@+id/linearLayout2"
|
||||||
@@ -49,7 +137,7 @@
|
|||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
android:id="@+id/linearLayout"
|
android:id="@+id/linearLayout"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="vertical">
|
android:orientation="vertical">
|
||||||
|
|
||||||
@@ -62,21 +150,18 @@
|
|||||||
android:id="@+id/btn_1"
|
android:id="@+id/btn_1"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_weight="1"
|
|
||||||
android:text="1" />
|
android:text="1" />
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/btn_2"
|
android:id="@+id/btn_2"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_weight="1"
|
|
||||||
android:text="2" />
|
android:text="2" />
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/btn_3"
|
android:id="@+id/btn_3"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_weight="1"
|
|
||||||
android:text="3" />
|
android:text="3" />
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
@@ -89,21 +174,18 @@
|
|||||||
android:id="@+id/btn_4"
|
android:id="@+id/btn_4"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_weight="1"
|
|
||||||
android:text="4" />
|
android:text="4" />
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/btn_5"
|
android:id="@+id/btn_5"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_weight="1"
|
|
||||||
android:text="5" />
|
android:text="5" />
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/btn_6"
|
android:id="@+id/btn_6"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_weight="1"
|
|
||||||
android:text="6" />
|
android:text="6" />
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
@@ -117,21 +199,18 @@
|
|||||||
android:id="@+id/btn_7"
|
android:id="@+id/btn_7"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_weight="1"
|
|
||||||
android:text="7" />
|
android:text="7" />
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/btn_8"
|
android:id="@+id/btn_8"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_weight="1"
|
|
||||||
android:text="8" />
|
android:text="8" />
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/btn_9"
|
android:id="@+id/btn_9"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_weight="1"
|
|
||||||
android:text="9" />
|
android:text="9" />
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
@@ -148,30 +227,19 @@
|
|||||||
android:text="0" />
|
android:text="0" />
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/btn_bro"
|
android:id="@+id/btn_dot"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="(" />
|
android:text="." />
|
||||||
|
|
||||||
<Button
|
|
||||||
android:id="@+id/btn_brc"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:text=")" />
|
|
||||||
|
|
||||||
<Button
|
|
||||||
android:id="@+id/btn_equ"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_marginBottom="8dp"
|
|
||||||
android:text="=" />
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="1"
|
||||||
android:orientation="vertical">
|
android:orientation="vertical">
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
@@ -203,8 +271,36 @@
|
|||||||
android:text="/" />
|
android:text="/" />
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:orientation="vertical">
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btn_equ"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginBottom="8dp"
|
||||||
|
android:text="=" />
|
||||||
|
|
||||||
|
<ImageButton
|
||||||
|
android:id="@+id/del"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
app:srcCompat="@android:drawable/ic_input_delete" />
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/last_exp_out"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@+id/input" />
|
||||||
|
|
||||||
<!-- <EditText-->
|
<!-- <EditText-->
|
||||||
<!-- android:id="@+id/function_in"-->
|
<!-- android:id="@+id/function_in"-->
|
||||||
<!-- android:layout_width="0dp"-->
|
<!-- android:layout_width="0dp"-->
|
||||||
|
|||||||
Reference in New Issue
Block a user