Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import scala.collection.immutable.List
- import scala.collection.{TraversableLike, mutable}
- import scala.collection.mutable.ListBuffer
- import scala.reflect.internal.util.Collections
- import scala.runtime.RichChar
- /**
- * Created by user on 21.07.2015.
- */
- class LongMath (v: String) {
- def value = v
- override def toString = v
- private def negative(num: LongMath) = num.value.charAt(0).equals("-")
- private def sub(num: LongMath) = {
- num.value.substring(1)
- }
- private def clear(str: String) = {
- val neg = str.startsWith("-")
- var i = if(neg) 1 else 0
- var end = false
- while (!end && i < str.length) {
- if(str.charAt(i).equals("0")) i+=1 else end = true
- }
- (if(neg)"-") + str.substring(i+1)
- }
- def +(num: LongMath): LongMath = {
- val negative1 = negative(this)
- val negative2 = negative(num)
- if(negative1 || negative2) {
- if(negative1 && negative2) {
- return new LongMath("-" + add(sub(this), sub(num)))
- } else {
- return new LongMath(if(negative1) subtraction(num.value, sub(this)) else subtraction(this.value, sub(num)))
- }
- }
- new LongMath(add(this.value, num.value))
- }
- private def add(v1: String, v2: String): String = {
- val number = v1.toCharArray.map(_.asDigit)
- val thisNumber = v2.toCharArray.map(_.asDigit)
- var result = ListBuffer[Int]()
- var supNum = 0
- val addition = (n: Int, m: Int) => {
- val sum = n + m + supNum
- supNum = 0
- if(sum >= 10) {
- supNum = sum/10
- sum%10
- } else {
- sum
- }
- }
- val supplement = (number: Array[Int], indexStart: Int) => {
- for(i <- indexStart to (0,-1)) {
- result += addition(number(i), 0)
- }
- }
- for(i <- 0 until Math.min(number.length, thisNumber.length)) {
- result += addition(number(number.length - i - 1), thisNumber(thisNumber.length - i - 1))
- }
- if(number.length != thisNumber.length) {
- if(number.length > thisNumber.length) {
- supplement(number, Math.min(number.length, thisNumber.length) - 1)
- } else {
- supplement(thisNumber, Math.min(number.length, thisNumber.length) - 1)
- }
- }
- if(supNum!=0) result+=supNum
- clear(result.mkString("", "", ""))
- }
- def -(num: LongMath): LongMath = {
- val negative1 = negative(this)
- val negative2 = negative(num)
- if(negative1 || negative2) {
- if(negative1 && negative2) {
- return new LongMath(subtraction(sub(num), sub(this)))
- } else {
- return new LongMath(if(negative1) "-" + add(sub(this), num.value) else add(this.value, sub(num)))
- }
- }
- new LongMath(subtraction(this.value, num.value))
- }
- private def subtraction(v1: String, v2: String): String = {
- var number = v2.toCharArray.map(_.asDigit)
- var thisNumber = v1.toCharArray.map(_.asDigit)
- var result = ListBuffer[Int]()
- var negative = false
- if(number.length > thisNumber.length || number.length == thisNumber.length && number(0) > thisNumber(0)) {
- negative = true
- var tmp = thisNumber
- thisNumber = number
- number = tmp
- }
- val sup: Int => Unit = (i: Int) => {
- thisNumber(i) match {
- case j if j > 0 =>
- thisNumber.update(i, j-1)
- case _ =>
- thisNumber.update(i, 9)
- sup(i-1)
- }
- }
- val minus = (i: Int, j: Int) => {
- val res = thisNumber(i) - number(j)
- res match {
- case t if t >= 0 => thisNumber.update(i, t)
- case _ =>
- sup(i-1)
- thisNumber.update(i, res + 10)
- }
- }
- for(i <- 0 until Math.min(thisNumber.length, number.length)) {
- minus(thisNumber.length - i - 1, number.length - i - 1)
- }
- clear((if(negative)"-") + thisNumber.mkString("","",""))
- }
- //
- // def *(num: LongMath): LongMath = {
- //
- // }
- //
- // def /(num: LongMath) : LongMath = {
- //
- // }
- //
- // def >(num: LongMath) : LongMath = {
- //
- // }
- //
- // def <(num: LongMath) : LongMath = {
- //
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement