ОФП 2 лаба (12) (2)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
class Number (Sign: Boolean, v1: Int, v2: Int, v3: Int, v4: Int, v5: Int) {
val sum: (List[Int], List[Int], Int => Boolean, Boolean) => (List[Int]) = {
case (Nil, Nil, p, sign) =>(Nil)
case (x :: xs, qx ::qxs, p, sign) if(p(x+qx)) => {
val end = sum(xs,qxs,p,sign)
((x+qx) :: end)
}
case (x :: xs, qx :: qxs, p,sign) => {
val (end) = sum(xs,qxs,p,sign)
if (end==Nil) {
println ("error")
((x+qx) :: end)
} else {
val buf :: buf1 = end
((x+qx-10) :: (buf+1) :: buf1)
}
}
}
val dif: (List[Int], List[Int], Int => Boolean) => (List[Int]) = {
case (Nil, Nil, p) =>(Nil)
case (x :: xs, qx :: qxs, p) if(x>=qx) => {
val end = dif(xs,qxs, p)
((x-qx) :: end)
}
case (x :: xs, qx :: qxs, p) => {
val (end) = dif(xs,qxs, p)
if (end==Nil) {
((x-qx) :: end)
} else {
val buf :: buf1 = end
((x-qx+10) :: (buf-1) :: buf1)
}
}
}
val (sign, n) = (Sign, v5 :: v4 :: v3 :: v2 :: v1 :: Nil)
val V1 = v1
val V2 = v2
val V3 = v3
val V4 = v4
val V5 = v5
def unary_- () = new Number (!sign,V1,V2,V3,V4,V5)
def + (q: Number) = {
if ((sign == q.sign)) {
val x1 :: x2 :: x3 :: x4 :: x5 ::Nil = sum(n,q.n,_<10,sign)
new Number (sign, x5,x4,x3,x2,x1)
} else {
if (V1!=q.V1) {
if (V1>q.V1) {
val x1 :: x2 :: x3 :: x4 :: x5 ::Nil = dif(n,q.n,_<10)
new Number (sign, x5,x4,x3,x2,x1)
} else {
val x1 :: x2 :: x3 :: x4 :: x5 ::Nil = dif(q.n,n,_<10)
new Number (q.sign, x5,x4,x3,x2,x1)
}
} else {
if (V2!=q.V2){
if (V2>q.V2) {
val x1 :: x2 :: x3 :: x4 :: x5 ::Nil = dif(n,q.n,_<10)
new Number (sign, x5,x4,x3,x2,x1)
} else {
val x1 :: x2 :: x3 :: x4 :: x5 ::Nil = dif(q.n,n,_<10)
new Number (q.sign, x5,x4,x3,x2,x1)
}
} else {
if (V3!=q.V3){
if (V3>q.V3) {
val x1 :: x2 :: x3 :: x4 :: x5 ::Nil = dif(n,q.n,_<10)
new Number (sign, x5,x4,x3,x2,x1)
} else {
val x1 :: x2 :: x3 :: x4 :: x5 ::Nil = dif(q.n,n,_<10)
new Number (q.sign, x5,x4,x3,x2,x1)
}
} else {
if (V4!=q.V4){
if (V4>q.V4) {
val x1 :: x2 :: x3 :: x4 :: x5 ::Nil = dif(n,q.n,_<10)
new Number (sign, x5,x4,x3,x2,x1)
} else {
val x1 :: x2 :: x3 :: x4 :: x5 ::Nil = dif(q.n,n,_<10)
new Number (q.sign, x5,x4,x3,x2,x1)
}
} else {
if (V5!=q.V5){
if (V5>q.V5) {
val x1 :: x2 :: x3 :: x4 :: x5 ::Nil = dif(n,q.n,_<10)
new Number (sign, x5,x4,x3,x2,x1)
} else {
val x1 :: x2 :: x3 :: x4 :: x5 ::Nil = dif(q.n,n,_<10)
new Number (q.sign, x5,x4,x3,x2,x1)
}
} else {
new Number (sign, 0,0,0,0,0)
}
}
}
}
}
}
}
def - (q: Number) = this + (-q)
def * (i:Int) = {
if (i==0) {
new Number (sign,0,0,0,0,0)
} else {
if (i==1) {
new Number (sign,V1,V2,V3,V4,V5)
} else {
if (i==(-1)) {
new Number (!sign,V1,V2,V3,V4,V5)
} else {
if (i>1) {
new Number (sign,V1+V1*(i-1),V2+V2*(i-1),V3+V3*(i-1),V4+V4*(i-1),V5+V5*(i-1))
} else {
new Number (!sign,V1+V1*(i+1),V2+V2*(i+1),V3+V3*(i+1),V4+V4*(i+1),V5+V5*(i+1))
}
}
}
}
}
}
val a = new Number (false, 0,0,9,3,7)
val b = new Number (true, 0,0,4,5,4)
val c = new Number (false, 0,0,4,8,3)
val d = a+b
println(d.sign,d.n)
println(c.sign,c.n)
val g = d-c
println(g.sign,g.n)
val h = c*2
println(h.sign,h.n)