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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
| Option Explicit
Public Const cP$ = "+": Public Const cD$ = "/": Public Const cM$ = "_": Public Const cO$ = "(": Public Const cF$ = ")"
Public Const cD0$ = "Bad Entry"
Public Const cR$ = "?"
Function SmQ$(x$, y$)
'addition Q->Q
Dim p$(1 To 2, 1 To 3), b%(1 To 2, 1 To 2), c$(1 To 5), i%
For i = 1 To 3
p(1, i) = IdnQ(x, i): p(2, i) = IdnQ(y, i)
Next i
b(1, 1) = SgnQ(x): b(2, 1) = SgnQ(y)
c(1) = PrdN(p(1, 2), p(2, 3)): c(2) = PrdN(p(1, 3), p(2, 2)): c(3) = PrdN(p(1, 3), p(2, 3))
If b(1, 1) = b(2, 1) Then
c(4) = SmmN(c(1), c(2)): b(1, 2) = b(1, 1): c(5) = SmmN(p(1, 1), p(2, 1)): b(2, 2) = b(1, 1)
If XsYn(c(4), c(3)) Then
c(4) = DffN(c(4), c(3)): c(5) = SmmN(c(5), 1)
End If
Else
c(4) = DffN(c(1), c(2)): b(1, 2) = IIf(XsYn(c(1), c(2)), b(1, 1), b(2, 1))
c(5) = DffN(p(1, 1), p(2, 1)): b(2, 2) = IIf(XsYn(p(1, 1), p(2, 1)), b(1, 1), b(2, 1))
End If
If Not CBool(c(4)) Then
SmQ = IIf(b(2, 2), c(5), IIf(Not CBool(c(5)), 0, cM & c(5)))
Else
c(1) = c(3): c(3) = SmpN(c(4), c(3), 2): c(4) = SmpN(c(4), c(1), 1)
If Not CBool(c(5)) Then
SmQ = IIf(b(1, 2), 0 & cP & c(4) & cD & c(3), cM & cO & 0 & cP & c(4) & cD & c(3) & cF)
Else
If b(2, 2) = b(1, 2) Then
SmQ = IIf(b(2, 2), c(5) & cP & c(4) & cD & c(3), cM & cO & c(5) & cP & c(4) & cD & c(3) & cF)
Else
If b(2, 2) Then
SmQ = DffN(c(5), 1) & cP & DffN(c(3), c(4)) & cD & c(3)
Else
SmQ = cM & cO & DffN(c(5), 1) & cP & DffN(c(3), c(4)) & cD & c(3) & cF
End If
End If
End If
End If
End Function
Function PrQ$(x$, y$)
'multiplication Q->Q
Dim p$(1 To 2, 1 To 3), b%, c$(1 To 4), i%
For i = 1 To 3
p(1, i) = IdnQ(x, i): p(2, i) = IdnQ(y, i)
Next i
b = SgnQ(x) Eqv SgnQ(y)
c(1) = PrdN(p(1, 3), p(2, 3))
c(2) = SmmN(SmmN(SmmN(PrdN(PrdN(p(1, 1), p(2, 1)), c(1)), PrdN(PrdN(p(1, 1), p(2, 2)), p(1, 3))), _
PrdN(PrdN(p(2, 1), p(1, 2)), p(2, 3))), PrdN(p(1, 2), p(2, 2)))
c(3) = DvnN(c(2), c(1), 1): c(2) = DvnN(c(2), c(1), 2)
If XsYn(c(2), c(1)) Then
c(3) = SmmN(c(3), 1): c(2) = DffN(c(2), c(1))
End If
If b Then
If Not CBool(c(2)) Then
PrQ = c(3)
Else
c(4) = c(1): c(1) = SmpN(c(2), c(1), 2): c(2) = SmpN(c(2), c(4), 1)
PrQ = c(3) & cP & c(2) & cD & c(1)
End If
Else
If Not CBool(c(2)) Then
PrQ = IIf(c(3) <> 0, cM & c(3), c(3))
Else
c(4) = c(1): c(1) = SmpN(c(2), c(1), 2): c(2) = SmpN(c(2), c(4), 1)
PrQ = cM & cO & c(3) & cP & c(2) & cD & c(1) & cF
End If
End If
End Function
Function SsQ$(x$, y$)
'soustraction Q->Q
SsQ = SmQ(x, PrQ(y, "_1"))
End Function
Function DvQ$(x$, y$)
'division Q->Q
Dim p$(1 To 3), b%, i%
If y = "0" Then
DvQ = cD0
Else
For i = 1 To 3
p(i) = IdnQ(y, i)
Next i
b = SgnQ(y): p(1) = SmmN(PrdN(p(1), p(3)), p(2)): p(2) = p(3)
If b Then
DvQ = PrQ(x, 0 & cP & p(2) & cD & p(1))
Else
DvQ = PrQ(x, cM & cO & 0 & cP & p(2) & cD & p(1) & cF)
End If
End If
End Function
Private Function SgnQ%(x$)
'signe (true=0,+,false=-) Q
If Left(x, 1) <> cM Then SgnQ = True
End Function
Private Function Sp0n$(x$)
'supprime les 0 à gauche N
Dim i%
i = 1
Do Until x = "" Or Left(x, 1) <> 0
x = Right(x, Len(x) - 1): i = i + 1
Loop
Sp0n = IIf(x <> "", x, 0)
End Function
Private Function IdnQ$(x$, Arg%)
'partie entiere (1), numérateur (2) diviseur (3) Q
Dim pD&, pA&
pD = InStr(1, x, cD): pA = InStr(1, x, cP)
Select Case Arg
Case 1
If SgnQ(x) Then
If pD > 0 Then
IdnQ = Left(x, pA - 1)
Else
IdnQ = x
End If
Else
If pD > 0 Then
IdnQ = Right(Left(x, pA - 1), pA - 3)
Else
IdnQ = Right(x, Len(x) - 1)
End If
End If
Case 2
If pD = 0 Then
IdnQ = 0
Else
IdnQ = Right(Left(x, pD - 1), pD - 1 - pA)
End If
Case 3
If pD = 0 Then
IdnQ = 1
Else
If SgnQ(x) Then
IdnQ = Right(x, Len(x) - pD)
Else
IdnQ = Left(Right(x, Len(x) - pD), Len(x) - pD - 1)
End If
End If
End Select
IdnQ = Sp0n(IdnQ)
End Function
Private Function XsYn%(x$, y$)
'N->x>=y?, suppose Sp0n fait
Dim Cx%, Cy%, i%
Select Case Len(x)
Case Is > Len(y)
XsYn = True
Case Len(y)
For i = 1 To Len(x)
Cx = Right(Left(x, i), 1): Cy = Right(Left(y, i), 1)
If Cx > Cy Then
XsYn = True: Exit Function
End If
If Cy > Cx Then Exit Function
Next i
XsYn = True
End Select
End Function
Private Function SmmN$(x$, y$)
'N->x+y, suppose Sup0 fait
Dim r%, i%, Cx%, Cy%, Cz%
For i = 1 To WorksheetFunction.Max(Len(x), Len(y))
Cx = Left(Right(x, i), 1): If i > Len(x) Then Cx = 0
Cy = Left(Right(y, i), 1): If i > Len(y) Then Cy = 0
Cz = Cx + Cy + r
If Cz < 10 Then
r = 0
Else
Cz = Cz - 10: r = 1
End If
SmmN = Cz & SmmN
Next i
SmmN = Sp0n(r & SmmN)
End Function
Private Function DffN$(ByVal x$, ByVal y$)
'N->Abs(x-y), suppose Sup0 fait
Dim z$, Cx%, Cy%, r%, i%, Cz%
If Not XsYn(x, y) Then
z = x: x = y: y = z
End If
For i = 1 To WorksheetFunction.Max(Len(x), Len(y))
Cx = Left(Right(x, i), 1): Cy = Left(Right(y, i), 1)
If i > WorksheetFunction.Min(Len(x), Len(y)) Then Cy = 0
Cz = Cx - Cy - r
If Cx < Cy + r Then
Cz = Cz + 10: r = 1
Else
r = 0
End If
DffN = Cz & DffN
Next i
DffN = Sp0n(DffN)
End Function
Private Function PrdN$(ByVal x$, ByVal y$)
'N->x*y, suppose Sup0 fait
Dim z$, r%, Cx%, Cy%, i%, j%, Cz%, k%, Rs1$, Rs2$
PrdN = 0
If Not XsYn(x, y) Then
z = x: x = y: y = z
End If
For i = 1 To WorksheetFunction.Min(Len(x), Len(y))
Rs1 = "": r = 0: Cx = Left(Right(y, i), 1)
For j = 1 To WorksheetFunction.Max(Len(x), Len(y))
Cy = Left(Right(x, j), 1): Cz = (Cx * Cy) + r: r = Int(Cz / 10): Cz = Cz - 10 * r: Rs1 = Cz & Rs1
Next j
Rs2 = IIf(r > 0, r & Rs1, Rs1)
For k = 2 To i
Rs2 = Rs2 & 0
Next k
PrdN = SmmN(Rs2, PrdN)
Next i
PrdN = Sp0n(PrdN)
End Function
Private Function DvnN$(x$, y$, Arg%)
'(1) int(x/y), (2) x mod y, suppose Sup0 fait (N)
Dim Cx$, i%, j%, v$, RsE$, RsR$
If Not XsYn(x, y) Then
DvnN = IIf(Arg = 1, 0, x): Exit Function
End If
Cx = Left(x, Len(y))
For i = Len(y) To Len(x)
For j = 0 To 9
If Not XsYn(Cx, PrdN(j + 1, y)) Then
RsE = RsE & j: v = DffN(Cx, PrdN(j, y))
If i < Len(x) Then Cx = Sp0n(v & Right(Left(x, i + 1), 1))
Exit For
End If
Next j
Next i
RsE = Sp0n(RsE)
DvnN = Sp0n(IIf(Arg = 1, RsE, v))
End Function
Private Function SmpN$(x$, y$, Arg%)
'(1) x/pgcd(x,y),(2) y/pgcd(x,y), suppose Sup0 fait (N)
Dim a$, b$, c$
a = x: b = y
Do While DvnN(a, b, 2) <> 0
c = a: a = b: b = DvnN(c, b, 2)
Loop
Select Case Arg
Case 1
SmpN = DvnN(x, b, 1)
Case 2
SmpN = DvnN(y, b, 1)
End Select
End Function |
Partager