jhylands

Calculator

Jan 3rd, 2012
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 25.14 KB | None | 0 0
  1. Module Module1
  2.     'problem when numbers that are negative
  3.     Public Structure lastnumber
  4.         Dim number As Int64
  5.         Dim power As Integer
  6.     End Structure
  7.     Sub Main()
  8. start:  'incase a link back to the start is needed
  9.         Dim equation As String
  10.         Console.WriteLine("Welcome to your calculator.")
  11.         Console.Write("Start: ")
  12.         equation = removespace(Console.ReadLine())
  13.         equation = dofunctionsuntil(equation)
  14.         equation = constantsToNumbers(equation)
  15.         equation = errorhandle(equation)
  16.         equation = dobracuntil(equation)
  17.         equation = dorununtil(equation)
  18.         Console.WriteLine(equation)
  19. show:
  20.         Console.ReadKey()
  21.         GoTo start
  22.     End Sub
  23.     Public Function removespace(ByVal equation)
  24.         'do until there are no longer spaces in the string
  25.         Do Until InStr(equation, " ") = 0
  26.             'remove the first space in the string
  27.             equation = Mid(equation, 1, InStr(equation, " ") - 1) & Mid(equation, InStr(equation, " ") + 1, Len(equation) - InStr(equation, " "))
  28.         Loop
  29.         'return a value
  30.         removespace = equation
  31.     End Function
  32.    
  33.     Public Function dofunctionsuntil(ByVal equation)
  34.         'call the dofunction function until there are no longer functions in the string
  35.         Do Until InStr(equation, "sin") = 0 And InStr(equation, "cos") = 0 And InStr(equation, "tan") = 0 And InStr(equation, "log") = 0 And InStr(equation, "pri") = 0 And InStr(equation, "sca") = 0 And InStr(equation, "sol") = 0 And InStr(equation, "der") = 0
  36.             equation = FindFunction(equation)
  37.         Loop
  38.         'return a value
  39.         dofunctionsuntil = equation
  40.     End Function
  41.    
  42.     Public Function errorhandle(ByVal equation)
  43.         'define i as integer as it will count through things
  44.         Dim I As Integer
  45.         'find out if the brackets are even
  46.         'level increaces and decreaces depending on whether the scanner finds an open
  47.         'bracket or a close bracket. If at the end of the string level is what it started on then
  48.         'the brackets are equale
  49.         Dim level As Integer
  50.         level = 0
  51.         'go through each caractor of the string
  52.         For I = 1 To Len(equation)
  53.             'if it's an open bracket then increace the level
  54.             If Mid(equation, I, 1) = "(" Then
  55.                 level = level + 1
  56.             ElseIf Mid(equation, I, 1) = ")" Then
  57.             'else decreace the level
  58.                 level = level - 1
  59.             End If
  60.         Next
  61.         'if the level is less than it started then there are to many close brackets
  62.         If level < 0 Then
  63.             Console.WriteLine("Too many close brackets!")
  64.             Call Main()
  65.         ElseIf level > 0 Then
  66.         'else if level is more than it started then there are too many open brackets
  67.             Console.WriteLine("Too many open brackets!")
  68.             Call Main()
  69.         End If
  70.         'remove bad symbols
  71.         'scan through the string
  72.         For I = 1 To Len(equation)
  73.             'if the current charactor is not a number and its not one of the signs and it's not an acceptable charactor in another way then:
  74.             If ifnumber(Mid(equation, I, 1)) = False And ifsign(Mid(equation, I, 1)) = False And ifexeption(Mid(equation, I, 1)) = False Then
  75.                 'get rid of it
  76.                 equation = Mid(equation, 1, I - 1) & Mid(equation, I + 1, Len(equation) - I)
  77.                 'correct for the fact that the string is now smaller
  78.                 i=i-1
  79.             End If
  80.         Next
  81.        
  82.         'identifying empty brackets
  83.         For I = 1 To Len(equation)
  84.             If Mid(equation, I, 2) = "()" Then
  85.                 Console.WriteLine("Error open bracket leads on to close brackets directly!")
  86.                 Call Main()
  87.             End If
  88.         Next
  89.         errorhandle = equation
  90.     End Function
  91.    
  92.     Public Function ifconst1(ByVal letter) As String
  93.         letter = UCase(letter)
  94.         If letter = "C" Then
  95.             ifconst1 = 299792458
  96.         ElseIf letter = "G" Then
  97.             ifconst1 = 6.673 * 10 ^ -11
  98.         ElseIf letter = "E" Then
  99.             ifconst1 = 2.71828183
  100.         ElseIf letter = "A" Then
  101.             ifconst1 = 1
  102.         Else
  103.             ifconst1 = 0
  104.         End If
  105.     End Function 'identify 1 charactor constants
  106.     Public Function ifconst2(ByVal letter)
  107.         letter = UCase(letter)
  108.         If letter = "KE" Then
  109.             ifconst2 = 8.9875517873681758 * 10 ^ 9
  110.         ElseIf letter = "PI" Then
  111.             ifconst2 = Math.PI
  112.         Else
  113.             ifconst2 = 0
  114.         End If
  115.  
  116.     End Function'identify 2 charactor constants
  117.     Public Function constantsToNumbers(ByVal equation)
  118.         'define i as a counting number
  119.         Dim i As Integer
  120.         'go throught the string looking for double charactors first
  121.         'this is done because a signle charactor constant could make up a double charactor constant
  122.         'for example in ke is e
  123.         'only go to the second last charactor because you can't have a two charactor word starting at the last charactor
  124.         For i = 1 To Len(equation) - 1
  125.             If ifconst2(Mid(equation, i, 2)) <> 0 Then
  126.                 'if it is a constant then replace it with its numerical value
  127.                 equation = Mid(equation, 1, i - 1) & ifconst2(Mid(equation, i, 2)) & Mid(equation, i + 2, Len(equation) - i)
  128.             End If
  129.         Next
  130.         'look for single charactor constants
  131.         For i = 1 To Len(equation)
  132.             If ifconst1(Mid(equation, i, 1)) <> 0 Then
  133.                 'if it's a charactor then replace it
  134.                 equation = Mid(equation, 1, i - 1) & ifconst1(Mid(equation, i, 1)) & Mid(equation, i + 1, Len(equation) - i)
  135.             End If
  136.         Next
  137.         constantsToNumbers = equation
  138.     End Function
  139.     Public Function isolatebrackets(ByVal equation)
  140.         Dim i As Integer
  141.         Dim layer As Integer
  142.         layer = 0 'set layer
  143.         For i = 1 To Len(equation) 'set up scan of equation from the charactor after the trig function
  144.             If Mid(equation, i, 1) = "(" Then 'if open brackets go in a  layer
  145.                 layer = layer + 1
  146.             ElseIf Mid(equation, i, 1) = ")" Then ' if close brackets go out a layer
  147.                 layer = layer - 1
  148.                 If layer = 0 Then ' if layer is on the layer we started at then finish
  149.                     GoTo isolated
  150.                 End If
  151.             End If
  152.         Next
  153. isolated:
  154.         isolatebrackets = Mid(equation, 2, i - 2) ' remove the first bracket and last bracket and submit code
  155.     End Function
  156.     Public Function ifsign(ByVal chr)
  157.         If chr = "-" Or chr = "/" Or chr = "+" Or chr = "*" Or chr = "^" Then
  158.             ifsign = True
  159.         Else
  160.             ifsign = False
  161.         End If
  162.     End Function
  163.     Public Function ifnumber(ByVal charactor As String)
  164.         If charactor = "0" Or charactor = "1" Or charactor = "2" Or charactor = "3" Or charactor = "4" Or charactor = "5" Or charactor = "6" Or charactor = "7" Or charactor = "8" Or charactor = "9" Or charactor = "." Then
  165.             ifnumber = True
  166.         Else
  167.             ifnumber = False
  168.         End If
  169.     End Function
  170.     Public Function iffunction(ByVal function_)
  171.         function_ = UCase(function_)
  172.         If function_ = "SIN" Or function_ = "COS" Or function_ = "TAN" Or function_ = "LOG" Or function_ = "PRI" Or function_ = "SCA" Or function_ = "SOL" Or function_ = "DER" Then
  173.             iffunction = True
  174.         Else
  175.             iffunction = False
  176.         End If
  177.     End Function
  178.     Public Function ifexeption(ByVal charactor As String)
  179.         If charactor = ")" Or charactor = "(" Or charactor = "^" Then
  180.             ifexeption = True
  181.         Else
  182.             ifexeption = False
  183.         End If
  184.     End Function
  185.     Public Function FindFunction(ByVal equation)
  186.         Dim I As Integer
  187.         Dim bracsum As String
  188.         Dim expantion As String
  189.         Dim FunctionName As String
  190.         ' find the trig function closest to the right
  191.         For I = Len(equation) - 3 To 1 Step -1
  192.  
  193.             If iffunction(Mid(equation, I, 3)) Then
  194.                 FunctionName = Mid(equation, I, 3)
  195.                 If Mid(equation, I + 3, 1) = "(" Then 'if there are brackets after the equation
  196.                     bracsum = isolatebrackets(Mid(equation, I + 3, Len(equation) - (I + 3))) 'find which brackets are after the function
  197.                 Else
  198.                     Console.WriteLine("The function must be contained in brackets e.g. sin(30)")
  199.                     Call Main()
  200.                     Exit Function
  201.                 End If
  202.  
  203.                 expantion = doFunction(FunctionName, bracsum)
  204.                 equation = Mid(equation, 1, I - 1) & expantion & Mid(equation, I + Len(bracsum) + 5, Len(equation) - (I + Len(bracsum) + 1)) ' compleat equation
  205.                 GoTo badger  'exit for loop when brackets compleat
  206.             End If ' end if a trig function
  207.         Next
  208. badger:
  209.  
  210.         FindFunction = equation ' return function value
  211.  
  212.     End Function
  213.     Public Function doFunction(ByVal Function_name, ByVal equation)
  214.         Function_name = UCase(Function_name)
  215.         'functions that contain iligal charactors
  216.         If Function_name = "SCA" Then
  217.             Do Until Len(equation) = 1
  218.                 If Len(equation) = 2 Then
  219.                     equation = signmath(equation)
  220.                 Else
  221.                     equation = signmath(equation) & Mid(equation, 3, Len(equation) - 3)
  222.                 End If
  223.             Loop
  224.             doFunction = equation
  225.             Exit Function
  226.         ElseIf Function_name = "DER" Then
  227.             On Error GoTo iferrorondev
  228.             If InStr(equation, "x") = 0 Then
  229.                 doFunction = 0
  230.             Else
  231.                 doFunction = der(equation)
  232.             End If
  233.             Exit Function
  234. iferrorondev:
  235.             Console.WriteLine("A user caused error has occured, you must type in an algibaic equation")
  236.             Exit Function
  237.         ElseIf Function_name = "SOL" Then
  238.             doFunction = solvealgibra(equation)
  239.             Exit Function
  240.         ElseIf Function_name = "LOG" Then
  241.             Dim numbers() As String = Split(equation, ",")
  242.             Dim number1 As Integer
  243.             Dim number2 As Double
  244.             If numbers.GetLength(0) = 1 Then
  245.                 number2 = dobracuntil(dobracuntil(numbers(0)))
  246.                 doFunction = Math.Log(number2, 10)
  247.             Else
  248.                 number1 = Int(dobracuntil(dobracuntil(numbers(0))))
  249.                 number2 = dobracuntil(dobracuntil(numbers(1)))
  250.                 doFunction = Math.Log(number2, number1)
  251.             End If
  252.             Exit Function
  253.         End If
  254.         'clearing illigal charactors
  255.         equation = constantsToNumbers(equation)
  256.         equation = errorhandle(equation)
  257.         equation = dobracuntil(equation)
  258.         'functions that use numbers
  259.         If Function_name = "SIN" Then
  260.             doFunction = Math.Sin(equation)
  261.         ElseIf Function_name = "COS" Then
  262.             doFunction = Math.Cos(equation)
  263.         ElseIf Function_name = "TAN" Then
  264.             doFunction = Math.Tan(equation)
  265.         ElseIf Function_name = "PRI" Then
  266.             Dim number As Int64
  267.             Dim i As Int64
  268.             Dim lastnumber As lastnumber
  269.             number = equation
  270.             i = number
  271.             lastnumber.number = 0
  272.             lastnumber.power = 1
  273.             Do Until i = 1
  274.                 i = factor(i)
  275.                 If lastnumber.number = number / i Then
  276.                     lastnumber.power = lastnumber.power + 1
  277.                 Else
  278.                     If lastnumber.number <> 0 Then
  279.                         If lastnumber.power = 1 Then
  280.                             Console.WriteLine(lastnumber.number)
  281.                         Else
  282.                             Console.WriteLine(lastnumber.number & "^" & lastnumber.power)
  283.                         End If
  284.                     End If
  285.                     lastnumber.number = number / i
  286.                     lastnumber.power = 1
  287.                 End If
  288.                 number = i
  289.  
  290.  
  291.             Loop
  292.             If lastnumber.power = 1 Then
  293.                 Console.WriteLine(lastnumber.number)
  294.             Else
  295.                 Console.WriteLine(lastnumber.number & "^" & lastnumber.power)
  296.             End If
  297.             doFunction = 1
  298.         End If
  299.     End Function
  300.     Public Function factor(ByVal number)
  301.         Dim i As Int64
  302.         Dim n As Int64
  303.         For i = Int(number / 2) To 1 Step -1
  304.             If number Mod i = 0 Then
  305.                 n = i
  306.                 Exit For
  307.             End If
  308.         Next
  309.         factor = n
  310.     End Function
  311.     Public Function signmath(ByVal equation)
  312.         Dim sign1 As String
  313.         Dim sign2 As String
  314.         sign1 = Mid(equation, 1, 1)
  315.         sign2 = Mid(equation, 2, 1)
  316.         If sign1 = "+" And sign2 = "+" Then
  317.             signmath = "+"
  318.         ElseIf sign1 = "+" And sign2 = "-" Then
  319.             signmath = "-"
  320.         ElseIf sign1 = "-" And sign2 = "+" Then
  321.             signmath = "-"
  322.         Else
  323.             signmath = "+"
  324.         End If
  325.     End Function
  326.     Public Function reaplace(ByVal equation, ByVal number)
  327.  
  328.         Dim i As Integer
  329.         i = 1
  330.         Do Until InStr(equation, "x") = 0
  331.             If Mid(equation, i, 1) = "x" Then
  332.                 If i <> 1 Then
  333.                     If Mid(equation, i - 1, 1) = "-" Or Mid(equation, i - 1, 1) = "+" Or Mid(equation, i - 1, 1) = "(" Or Mid(equation, i - 1, 1) = "/" Or Mid(equation, i - 1, 1) = "*" Or Mid(equation, i - 1, 1) = "^" Then
  334.                         equation = Mid(equation, 1, i - 1) & number & Mid(equation, i + 1, Len(equation) - i)
  335.                     Else
  336.                         equation = Mid(equation, 1, i - 1) & "*" & number & Mid(equation, i + 1, Len(equation) - i)
  337.                     End If
  338.                 Else
  339.                     equation = number & Mid(equation, 2, Len(equation) - 1)
  340.                 End If
  341.  
  342.             End If
  343.             i = i + 1
  344.  
  345.         Loop
  346.  
  347.         reaplace = equation
  348.     End Function
  349.     Public Function solvealgibra(ByVal original) As Double
  350.         Dim derivitive As String
  351.         Dim i As Integer = 0
  352.         derivitive = dev(original)
  353.         Dim x, x0 As Double
  354.         x = 1
  355.         Dim equation As String
  356.         equation = "x-((" & original & ")/(" & derivitive & "))"
  357.         Do Until x = x0 Or i > 10000
  358.             x0 = x
  359.             x = dorununtil(dobracuntil(reaplace(equation, x0)))
  360.             i = i + 1
  361.         Loop
  362.         If i > 10000 Then
  363.             solvealgibra = 0 / 0
  364.         Else
  365.             solvealgibra = x
  366.         End If
  367.         Exit Function
  368. errorhandle:
  369.         Console.WriteLine("The function sol can be used to solve non liner algibreic equations!")
  370.         Console.WriteLine("You must first munipulat the equation so that 'something = 0'")
  371.         Console.WriteLine("You then write in the calculator program 'sol(something)'")
  372.         solvealgibra = 0 / 0
  373.     End Function
  374.     Public Function dev(ByVal origin)
  375.         Dim i As Integer
  376.         Dim bits(scan(origin))
  377.         If bits.GetLength(0) <> 1 Then
  378.             Dim n As Integer
  379.             Dim carry As Integer
  380.             n = 0
  381.             carry = 1
  382.             For i = 1 To Len(origin)
  383.                 If Mid(origin, i, 1) = "-" Then
  384.                     bits(n) = Mid(origin, carry, i - carry)
  385.                     carry = i
  386.                     n = n + 1
  387.                 ElseIf Mid(origin, i, 1) = "+" Then
  388.                     bits(n) = Mid(origin, carry, i - carry)
  389.                     carry = i
  390.                     n = n + 1
  391.                 End If
  392.             Next
  393.         Else
  394.             bits(0) = origin
  395.         End If
  396.         Dim derivitive As String
  397.         Dim power As String
  398.  
  399.         Dim constant As Double
  400.         derivitive = ""
  401.         For i = 0 To bits.GetLength(0) - 1
  402.             If InStr(bits(i), "x") <> 0 Then
  403.                 If InStr(bits(i), "^") = 0 Then
  404.                     power = 1
  405.                 Else
  406.                     power = Mid(bits(i), InStr(bits(i), "^") + 1, Len(bits(i)) - (InStr(bits(i), "^")))
  407.                 End If
  408.  
  409.                 If Mid(bits(i), 1, InStr(bits(i), "x") - 1) = "" Then
  410.                     constant = 1
  411.                 ElseIf Mid(bits(i), 1, InStr(bits(i), "x") - 1) = "-" Then
  412.                     constant = -1
  413.                 Else
  414.                     constant = Mid(bits(i), 1, InStr(bits(i), "x") - 1)
  415.                 End If
  416.                 If power <> "1" Then
  417.                     derivitive = derivitive & "+(" & power & "*" & constant & "x^(" & power & "- 1))"
  418.                 Else
  419.                     derivitive = derivitive & "+" & constant
  420.                 End If
  421.             End If
  422.         Next
  423.         dev = Mid(derivitive, 2, Len(derivitive) - 1)
  424.     End Function
  425.     Public Function scan(ByVal text)
  426.         Dim i, number As Integer
  427.         number = 0
  428.         For i = 1 To Len(text)
  429.             If Mid(text, i, 1) = "-" Then
  430.                 number = number + 1
  431.             ElseIf Mid(text, i, 1) = "+" Then
  432.                 number = number + 1
  433.             End If
  434.         Next
  435.         scan = number
  436.     End Function
  437.     Public Function der(ByVal origin)
  438.         Dim i As Integer
  439.         Dim bits(scan(origin))
  440.         If bits.GetLength(0) <> 1 Then
  441.             Dim n As Integer
  442.             Dim carry As Integer
  443.             n = 0
  444.             carry = 1
  445.             For i = 1 To Len(origin)
  446.                 If Mid(origin, i, 1) = "-" Then
  447.                     bits(n) = Mid(origin, carry, i - carry)
  448.                     carry = i
  449.                     n = n + 1
  450.                 ElseIf Mid(origin, i, 1) = "+" Then
  451.                     bits(n) = Mid(origin, carry, i - carry)
  452.                     carry = i
  453.                     n = n + 1
  454.                 End If
  455.             Next
  456.         Else
  457.             bits(0) = origin
  458.         End If
  459.         Dim derivitive As String
  460.         Dim power As Integer
  461.  
  462.         Dim constant As Double
  463.         derivitive = ""
  464.         For i = 0 To bits.GetLength(0) - 1
  465.             If InStr(bits(i), "x") <> 0 Then
  466.                 If InStr(bits(i), "^") = 0 Then
  467.                     power = 1
  468.                 Else
  469.                     power = Int(Mid(bits(i), InStr(bits(i), "^") + 1, Len(bits(i)) - (InStr(bits(i), "^"))))
  470.                 End If
  471.  
  472.                 If Mid(bits(i), 1, InStr(bits(i), "x") - 1) = "" Then
  473.                     constant = 1
  474.                 ElseIf Mid(bits(i), 1, InStr(bits(i), "x") - 1) = "-" Then
  475.                     constant = -1
  476.                 Else
  477.                     constant = Mid(bits(i), 1, InStr(bits(i), "x") - 1)
  478.                 End If
  479.                 If power <> "1" Then
  480.                     derivitive = derivitive & "+" & power * constant & "x^" & power - 1
  481.                 Else
  482.                     derivitive = derivitive & "+" & constant
  483.                 End If
  484.             End If
  485.         Next
  486.         der = Mid(derivitive, 2, Len(derivitive) - 1)
  487.     End Function
  488.     Public Function dorununtil(ByVal equation)
  489.  
  490.         Do Until InStr(equation, "*") = 0 And InStr(equation, "/") = 0 And InStr(equation, "^") = 0 And answer(equation)
  491.             equation = run(equation)
  492.         Loop
  493.         dorununtil = equation
  494.     End Function
  495.     Public Function answer(ByVal equation)
  496.         Dim i, signs As Integer
  497.         signs = 0
  498.         For i = 2 To Len(equation)
  499.             If Mid(equation, i, 1) = "-" Then
  500.                 If Mid(equation, i - 1, 1) <> "E" Then
  501.                     signs = signs + 1
  502.                 End If
  503.             ElseIf Mid(equation, i, 1) = "+" Then
  504.                 If Mid(equation, i - 1, 1) <> "E" Then
  505.                     signs = signs + 1
  506.                 End If
  507.             End If
  508.         Next
  509.         If signs = 0 Then
  510.             answer = True
  511.         Else
  512.             answer = False
  513.         End If
  514.     End Function
  515.     Public Function dobracuntil(ByVal equation)
  516.         Do Until InStr(equation, "(") = 0
  517.             equation = dobrackets(equation)
  518.         Loop
  519.         dobracuntil = equation
  520.     End Function
  521.     Public Function run(ByVal number As String)
  522.         On Error GoTo errorhandels
  523.         Dim num(NoOfNumbers(number)) As String
  524.         Dim i As Integer
  525.         Dim sign(NoOfNumbers(number) - 1) As Char
  526.         Dim current As Integer
  527.         Dim strnext As String
  528.         strnext = ""
  529.         current = 0
  530.         'making the numbers = to "" so that when more digits are added to the end there isnt a 0 infront
  531.         For i = 0 To num.GetLength(0) - 1
  532.             num(i) = ""
  533.         Next
  534.         'splitting the string into numbers and calculations
  535.         'if the first number is negative then set it as negative
  536.         num(0) = Mid(number, 1, 1) ' putting the first charactor at the start of the first number
  537.         For i = 2 To Len(number) ' this loop looks at charactors 2 to the last and decids weather to put them in a number or sign
  538.             If ifsign(Mid(number, i, 1)) = False Then ' if the current charactor is not a sign
  539.                 If Mid(number, i, 1) = "E" Then ' if the current charactor is E then put the next charactor as it will be part of the next number but might be confused as a sign by the program
  540.                     num(current) = num(current) & Mid(number, i, 2) ' adding both charactors to number
  541.                     i = i + 1 ' including the fact that two charactors where added
  542.                 Else
  543.                     num(current) = num(current) & Mid(number, i, 1) 'stooringcharactor to number
  544.                 End If
  545.             ElseIf ifsign(Mid(number, i - 1, 1)) = True Then 'if the sign shows the charge of the number then ...
  546.                 num(current) = num(current) & Mid(number, i, 1) ' put this number at the start of the number
  547.             Else ' ie the sign is what we want to calculate
  548.  
  549.                 sign(current) = Mid(number, i, 1) 'storing sign
  550.                 current = current + 1   'changing the number that digits are stoored to after a sign
  551.             End If
  552.         Next
  553.         'writing numbers before calculation
  554.         For i = 0 To signpick(sign) - 1
  555.             strnext = strnext & num(i) & sign(i)
  556.         Next
  557.         'writing calculation
  558.         strnext = strnext & sum(num(signpick(sign)), sign(signpick(sign)), num(signpick(sign) + 1))
  559.         'writing numbers after calculation
  560.         For i = signpick(sign) + 1 To sign.GetLength(0) - 1
  561.             strnext = strnext & sign(i) & num(i + 1)
  562.         Next
  563.  
  564.         run = Mid(strnext, 1, Len(strnext) - 1)
  565.         Exit Function
  566. errorhandels:
  567.  
  568.         Console.WriteLine("More than two signs in a row!!!")
  569.         Console.WriteLine("Programs progress:")
  570.         Console.WriteLine(number)
  571.         Console.WriteLine("This may be the answer to a sum inside some brackets! So please take mesures to avoid this such as;")
  572.         Console.WriteLine("If you type in 1+-(8-9) please insted type 1-(8-9)")
  573.         Console.WriteLine("Or you can use the function sca() for example sca(--+) would give the answer +")
  574.         Console.WriteLine("You can also use it in an equation for example 5sca(--++-+)7 which would give you 12")
  575.     End Function
  576.     Public Function signpick(ByVal signs() As Char)
  577.         Dim i As Integer
  578.  
  579.         For i = 0 To signs.GetLength(0) - 1
  580.             If signs(i) = "^" Then
  581.                 signpick = i
  582.                 Exit Function
  583.             End If
  584.         Next
  585.         For i = 0 To signs.GetLength(0) - 1
  586.             If signs(i) = "*" Then
  587.                 signpick = i
  588.                 Exit Function
  589.             End If
  590.         Next
  591.         For i = 0 To signs.GetLength(0) - 1
  592.             If signs(i) = "/" Then
  593.                 signpick = i
  594.                 Exit Function
  595.             End If
  596.         Next
  597.         signpick = 0
  598.     End Function
  599.     Public Function dobrackets(ByVal equation)
  600.  
  601.         Dim I As Integer
  602.         Dim bracsum As String
  603.         Dim expantion As String
  604.         I = Len(equation)
  605.         ' find the open bracket closest to the right
  606.         For I = Len(equation) To 1 Step -1
  607.             If Mid(equation, I, 1) = "(" Then
  608.                 bracsum = Mid(equation, I + 1, Len(equation) - (I)) 'go on using instr' isolate sum
  609.                 bracsum = Mid(bracsum, 1, InStr(bracsum, ")") - 1)
  610.                 expantion = dorununtil(bracsum)
  611.                 equation = Mid(equation, 1, I - 1) & expantion & Mid(equation, I + Len(bracsum) + 2, Len(equation) - (I + Len(bracsum) + 1)) ' compleat equation
  612.                 GoTo badger  'exit for loop when brackets compleat
  613.             End If
  614.         Next
  615. badger:
  616.         dobrackets = equation ' return function value
  617.  
  618.     End Function
  619.     Public Function NoOfNumbers(ByVal number)
  620.         Dim i As Integer
  621.         Dim numbers As Integer
  622.         numbers = 1
  623.         For i = 2 To Len(number)
  624.             If ifsign(Mid(number, i, 1)) = True Then
  625.                 numbers = numbers + 1
  626.             End If
  627.         Next
  628.         NoOfNumbers = numbers
  629.     End Function
  630.     Public Function sum(ByVal a As Single, ByVal sign As Char, ByVal b As Single)
  631.         If sign = "*" Then
  632.             sum = a * b
  633.         ElseIf sign = "/" Then
  634.             sum = a / b
  635.         ElseIf sign = "+" Then
  636.             sum = a + b
  637.         ElseIf sign = "-" Then
  638.             sum = a - b
  639.         Else
  640.             sum = a ^ b
  641.         End If
  642.     End Function
  643.  
  644. End Module
Advertisement
Add Comment
Please, Sign In to add comment