Min ta moduldan iborat dastur tuzing(dasturlash tili va mavzu erkin)


binding.divideButton.setOnClickListener {



Download 432,8 Kb.
bet2/3
Sana31.12.2021
Hajmi432,8 Kb.
#234192
1   2   3
Bog'liq
DTST


binding.divideButton.setOnClickListener { btnMath(binding.divideButton) }
binding.resultButton.setOnClickListener { btnEquel() }
binding.resultTextView.setOnClickListener{resultTextView()}
binding.ceButton.setOnClickListener {ceButton() }
binding.percentButton.setOnClickListener { percentButton() }

}

fun onClick(view: View) {


val id = view.id
var value = binding.editText.text.toString()
when (id) {
binding.oneButton.id -> {
value += "1"
stringBuilder.append("1")
tempStringBuilder.append("1")
}
binding.twoButton.id -> {
value += "2"
stringBuilder.append("2")
tempStringBuilder.append("2")

}
binding.threeButton.id -> {


value += "3"
stringBuilder.append("3")
tempStringBuilder.append("3")

}
binding.fourButton.id -> {


value += "4"
stringBuilder.append("4")
tempStringBuilder.append("4")

}
binding.fiveButton.id -> {


value += "5"
stringBuilder.append("5")
tempStringBuilder.append("5")
}
binding.sixButton.id -> {
value += "6"
stringBuilder.append("6")
tempStringBuilder.append("6")
}
binding.sevenButton.id -> {
value += "7"
stringBuilder.append("7")
tempStringBuilder.append("7")
}
binding.eightButton.id -> {
value += "8"
stringBuilder.append("8")
tempStringBuilder.append("8")
}
binding.nineButton.id -> {
value += "9"
stringBuilder.append("9")
tempStringBuilder.append("9")

}
binding.zeroButton.id -> {


value += "0"
stringBuilder.append("0")
tempStringBuilder.append("0")
}
binding.pointButton.id -> {
value += "."
stringBuilder.append(".")
tempStringBuilder.append(".")

}
binding.cButton.id -> {

value = ""
stringBuilder.clear()
tempStringBuilder.clear();
}
binding.deleteButton.id -> {
value = binding.editText.text.toString()

if (value.isNotEmpty()) {


value = value.substring(0, value.length - 1)
}
if (stringBuilder.isNotEmpty()) {
stringBuilder.deleteCharAt(stringBuilder.length - 1)
if (stringBuilder.contains("=")) {
value = ""
stringBuilder.clear()
}
}

}
}
binding.editText.setText(value)


}

Bu onclick funksiya bizdagi buttonlarni bosilganda qayerga malumto jonatish va nimalarni chiqarish bilan shugulanadi.

OnViewCreat ni ichida pastrog’gida matematik ammalarni bajarish uchun funksiyalr yozilgan .

private fun isMathOperation(c: Char):Boolean {


if(c == '+' || c=='-' || c == '/' || c == '*') return true;
return false;
}

bu funksiya bizga operatsion belgini qaytaradi shu belgimi yoqmi deb.

fun btnMath(view: View) {
val id = view.id
when (id) {
binding.multiplyButton.id -> {
if(stringBuilder.isNotEmpty() && !isMathOperation(stringBuilder[stringBuilder.length - 1])) {
stringBuilder.append("*");
}
}
binding.divideButton.id -> {
if(stringBuilder.isNotEmpty() && !isMathOperation(stringBuilder[stringBuilder.length - 1])) {
stringBuilder.append("/");
}
}
binding.minusButton.id -> {
if(stringBuilder.isNotEmpty() && !isMathOperation(stringBuilder[stringBuilder.length - 1])) {
stringBuilder.append("-")
}
if(stringBuilder.isEmpty()) stringBuilder.append("-")
}
binding.plusButton.id -> {
if(stringBuilder.isNotEmpty() && !isMathOperation(stringBuilder[stringBuilder.length - 1])) {
stringBuilder.append("+")
}
}
}
if(stringBuilder.isNotEmpty()) {
binding.editText.setText(stringBuilder.toString())
}
}

Bu funksiya esa ammalardan bir bosilganda bu stringbuilderga yig’adi yani bizga bu uzun ammalarni hisoblashga kerak boladi

private fun getCharCode(a: Char):Int {
return a.toInt()
}

private fun btnEquel(){


if(stringBuilder.isEmpty()) return
if(stringBuilder.isNotEmpty() && isMathOperation(stringBuilder[stringBuilder.length - 1])) return
binding.resultTextView.text = stringBuilder.toString()
if ( stringBuilder.isNotEmpty() && !isMathOperation(stringBuilder[0])) {
stringBuilder.insert(0, '+');
}
stringBuilder.append('+');
Log.d("stringEqual: ", stringBuilder.toString());
var a: MutableList = arrayListOf()
var num = 0.0;
var sign = 1;
var lastOperation = '+';
for(i in stringBuilder.indices) {
if (isMathOperation(stringBuilder[i])) {
if(i > 0) {
if(lastOperation == '-') {
a.add(-1 * num);
} else if(lastOperation == '+') {
a.add(num)
} else if(lastOperation == '*') {
a[a.size-1] *= num;
} else if(lastOperation == '/') {
a[a.size-1] /= num;
}
}
num = 0.0
lastOperation = stringBuilder[i];
} else {
num = num * 10 + getCharCode(stringBuilder[i]) - 48;
}
}
var result = 0.0
for(item in a) {
result+=item
}
Log.d("arrayresult", a.toString())
Log.d("result", result.toString())

stringBuilder.append(result)


binding.editText.setText(" = ${result.toString()}")
tempStringBuilder.clear()

}

private fun resultTextView(){


binding.editText.setText("")
binding.resultTextView.setOnClickListener


Download 432,8 Kb.

Do'stlaringiz bilan baham:
1   2   3




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish