Задача: написать класс, реализующий основные арифметические действия (+, -, *, /) с БОЛЬШИМ целым (число может быть произвольной длины, не ограничено 64 битами)
Основная идея: хранить число в виде строки
осталось доделать сравнение
import java.lang.Exception
import kotlin.math.abs
class BigNumber(var value:String="0"){
var negative:Boolean=false
var _number:String=""
init {
val r=Regex("""(-)?(\d+)""")
val mr=r.find(value.trim())
if(mr==null)
throw Exception("неверная строка")
negative=mr.groupValues[1]=="-"
_number=mr.groupValues[2]
}
constructor(number: Any): this(){
when(number){
is Int, is Long,is Short,is Byte->{
this.negative = ((number as Number).toLong()<0)
this._number = abs((number as Number).toLong()).toString()
return
}
is String->{
val r=Regex("""(-)?(\d+)""")
val mr=r.find(number.trim())
if(mr==null)
throw Exception("неверная строка")
negative=mr.groupValues[1]=="-"
_number=mr.groupValues[2]
return
}
}
throw Exception("c таким типом не работаю")
}
private fun add(number: BigNumber):BigNumber{
var tempValue=_number
var tempNumber=number._number
if (tempValue.length>tempNumber.length)
tempNumber=tempNumber.padStart(
tempValue.length,'0')
else
tempValue=_number.padStart(tempValue.
length,'0')
if(number.negative==this.negative) {
var temp = 0
var result = ""
for (i in tempValue.indices.reversed()) {
temp = tempNumber[i].toString().toInt() +
tempValue[i].toString().toInt() +
temp
result = (temp % 10).toString() + result
temp /= 10
}
if (temp > 0)
result = temp.toString() + result
if (number.negative or this.negative)
return BigNumber("-"+result)
else return BigNumber(result)
}
else return sub(number)
}
private fun sub(number: BigNumber):BigNumber {
var ymenshaemoe=_number
var vichitaemoe=number._number
if (ymenshaemoe.length>vichitaemoe.length)
vichitaemoe=vichitaemoe.padStart(
ymenshaemoe.length,'0')
else
ymenshaemoe=ymenshaemoe.padStart(vichitaemoe.
length,'0')
if (ymenshaemoe.compareTo(vichitaemoe)<0) {
val temp=ymenshaemoe
ymenshaemoe=vichitaemoe
vichitaemoe=temp
}
var temp=0
var result=""
for (i in ymenshaemoe.indices.reversed()) {
temp=(ymenshaemoe[i].toInt()-0x30) -
(vichitaemoe[i].toInt()-0x30) -
temp
if (temp<0){
result = (temp + 10).toString() + result
temp=1
}
else {
result = temp.toString() + result
temp=0
}
}
if (number.negative or this.negative)
return BigNumber("-"+result)
else
return BigNumber(result)
}
override fun toString(): String =
"BigNumber(${if (negative) "-" else ""}$_number)"
private fun mul(number: BigNumber):BigNumber{
var mnozhitel=number
var result=BigNumber()
// while(mnozhitel != BigNumber(0)){
while(mnozhitel._number != "0"){
result = result.add(_number)
mnozhitel = mnozhitel.sub(1)
}
result.negative=number.negative!=this.negative
return result
}
private fun div(number: BigNumber):BigNumber{
var chastnoe=BigNumber()
var delimoe=this
while (delimoe._number.compareTo(number._number)>=0){
chastnoe+=1
delimoe=delimoe.sub(number._number)
}
chastnoe.negative=number.negative!=delimoe.negative
return chastnoe }
override fun equals(value2: Any?):Boolean{
if(value2!=null) {
val tmp = BigNumber(value2)
return (this.negative==tmp.negative) and
(this._number.compareTo(tmp._number)==0)
}
else
return false
}
operator fun plus(slagaemoe: Any): BigNumber = add(slagaemoe)
operator fun minus(vichitaemoe: Any): BigNumber = sub(vichitaemoe)
operator fun times(mnozitel: Any): BigNumber = mul(mnozitel)
operator fun div(delitel: Any): BigNumber = division(delitel)
fun add(number:Any): BigNumber = add(BigNumber(number))
fun division(number: Any):BigNumber = div(BigNumber(number))
fun mul(number: Any):BigNumber = mul(BigNumber(number))
fun sub(number: Any):BigNumber = sub(BigNumber(number))
}
fun main(){
try {
var bn = BigNumber(5)
println(bn + 5)
println(bn * 5)
println(bn - 5)
println(bn / 5)
}catch (e:Exception){
println(e)
}
}