Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 4.03 KB | None | 0 0
  1. import scala.collection.immutable.List
  2. import scala.collection.{TraversableLike, mutable}
  3. import scala.collection.mutable.ListBuffer
  4. import scala.reflect.internal.util.Collections
  5. import scala.runtime.RichChar
  6.  
  7. /**
  8.  * Created by user on 21.07.2015.
  9.  */
  10. class LongMath (v: String) {
  11.  
  12.   def value = v
  13.  
  14.   override def toString = v
  15.  
  16.   private def negative(num: LongMath) = num.value.charAt(0).equals("-")
  17.  
  18.   private def sub(num: LongMath) = {
  19.     num.value.substring(1)
  20.   }
  21.  
  22.   private def clear(str: String) = {
  23.     val neg = str.startsWith("-")
  24.     var i = if(neg) 1 else 0
  25.     var end = false
  26.     while (!end && i < str.length) {
  27.       if(str.charAt(i).equals("0")) i+=1 else end = true
  28.     }
  29.     (if(neg)"-") + str.substring(i+1)
  30.   }
  31.  
  32.   def +(num: LongMath): LongMath = {
  33.     val negative1 = negative(this)
  34.     val negative2 = negative(num)
  35.     if(negative1 || negative2) {
  36.       if(negative1 && negative2) {
  37.         return new LongMath("-" + add(sub(this), sub(num)))
  38.       } else {
  39.         return new LongMath(if(negative1) subtraction(num.value, sub(this)) else subtraction(this.value, sub(num)))
  40.       }
  41.     }
  42.  
  43.     new LongMath(add(this.value, num.value))
  44.   }
  45.  
  46.   private def add(v1: String, v2: String): String = {
  47.     val number = v1.toCharArray.map(_.asDigit)
  48.     val thisNumber = v2.toCharArray.map(_.asDigit)
  49.     var result = ListBuffer[Int]()
  50.     var supNum = 0
  51.  
  52.     val addition = (n: Int, m: Int) => {
  53.       val sum = n + m + supNum
  54.       supNum = 0
  55.       if(sum >= 10) {
  56.         supNum = sum/10
  57.         sum%10
  58.       } else {
  59.         sum
  60.       }
  61.     }
  62.  
  63.     val supplement = (number: Array[Int], indexStart: Int) => {
  64.       for(i <- indexStart to (0,-1)) {
  65.         result += addition(number(i), 0)
  66.       }
  67.     }
  68.  
  69.     for(i <- 0 until Math.min(number.length, thisNumber.length)) {
  70.       result += addition(number(number.length - i - 1), thisNumber(thisNumber.length - i - 1))
  71.     }
  72.  
  73.     if(number.length != thisNumber.length) {
  74.       if(number.length > thisNumber.length) {
  75.         supplement(number, Math.min(number.length, thisNumber.length) - 1)
  76.       } else {
  77.         supplement(thisNumber, Math.min(number.length, thisNumber.length) - 1)
  78.       }
  79.     }
  80.     if(supNum!=0) result+=supNum
  81.     clear(result.mkString("", "", ""))
  82.   }
  83.  
  84.   def -(num: LongMath): LongMath = {
  85.     val negative1 = negative(this)
  86.     val negative2 = negative(num)
  87.  
  88.     if(negative1 || negative2) {
  89.       if(negative1 && negative2) {
  90.         return new LongMath(subtraction(sub(num), sub(this)))
  91.       } else {
  92.         return new LongMath(if(negative1) "-" + add(sub(this), num.value) else add(this.value, sub(num)))
  93.       }
  94.     }
  95.  
  96.     new LongMath(subtraction(this.value, num.value))
  97.   }
  98.  
  99.   private def subtraction(v1: String, v2: String): String = {
  100.     var number = v2.toCharArray.map(_.asDigit)
  101.     var thisNumber = v1.toCharArray.map(_.asDigit)
  102.     var result = ListBuffer[Int]()
  103.     var negative = false
  104.  
  105.     if(number.length > thisNumber.length || number.length == thisNumber.length && number(0) > thisNumber(0)) {
  106.       negative = true
  107.       var tmp = thisNumber
  108.       thisNumber = number
  109.       number = tmp
  110.     }
  111.  
  112.     val sup: Int => Unit = (i: Int) => {
  113.       thisNumber(i) match {
  114.         case j if j > 0 =>
  115.           thisNumber.update(i, j-1)
  116.         case _ =>
  117.           thisNumber.update(i, 9)
  118.           sup(i-1)
  119.       }
  120.     }
  121.  
  122.     val minus = (i: Int, j: Int) => {
  123.       val res = thisNumber(i) - number(j)
  124.       res match {
  125.         case t if t >= 0 => thisNumber.update(i, t)
  126.         case _ =>
  127.           sup(i-1)
  128.           thisNumber.update(i, res + 10)
  129.       }
  130.     }
  131.  
  132.     for(i <- 0 until Math.min(thisNumber.length, number.length)) {
  133.       minus(thisNumber.length - i - 1, number.length - i - 1)
  134.     }
  135.     clear((if(negative)"-") + thisNumber.mkString("","",""))
  136.   }
  137. //
  138. //  def *(num: LongMath): LongMath = {
  139. //
  140. //  }
  141. //
  142. //  def /(num: LongMath) : LongMath = {
  143. //
  144. //  }
  145. //
  146. //  def >(num: LongMath) : LongMath = {
  147. //
  148. //  }
  149. //
  150. //  def <(num: LongMath) : LongMath = {
  151. //
  152. //  }
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement