-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.2.bas
52 lines (39 loc) · 1.42 KB
/
3.2.bas
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
Sub quadraticequation()
Dim Document
Dim selection
Dim splited
Dim a, b, c, d, disc, x1, x2 As Integer
Set Document = Application.ActiveDocument
Set selection = Document.Content
Dim stringOne As String
Dim regexOne As Object
Set regexOne = New RegExp
regexOne.Pattern = "([+|-]?\d+)\*x2+([+|-]?\d+)\*x([+|-]?\d+)=([+|-]?\d+)"
regexOne.Global = True
regexOne.IgnoreCase = IgnoreCase
stringOne = selection
Set theMatches = regexOne.Execute(stringOne)(0)
a = CInt(theMatches.SubMatches(0))
b = CInt(theMatches.SubMatches(1))
c = CInt(theMatches.SubMatches(2))
d = CInt(theMatches.SubMatches(3))
Debug.Print a
Debug.Print b
Debug.Print c
Debug.Print d
c = c - d
disc = b ^ 2 - 4 * a * c
selection.Font.name = "Arial"
selection.Font.Size = 16
selection.Font.Italic = True
If disc = 0 Then
x1 = -b / (2 * a)
selection.InsertAfter text:=vbNewLine & Replace("x = %1", "%1", CStr(x1))
ElseIf disc > 0 Then
x1 = (-b + disc ^ (1 / 2)) / (2 * a)
x2 = (-b - disc ^ (1 / 2)) / (2 * a)
selection.InsertAfter text:=vbNewLine & Replace(Replace("x1 = %1, x2 = %2", "%1", CStr(x1)), "%2", CStr(x2))
Else
selection.InsertAfter text:=vbNewLine & "Данное уравнение не имеет действительных чисел."
End If
End Sub