Visual Basic Help for making a simple calculator - calculator

Code a Function procedure named CalculateResult that performs the requested operation and returns a decimal value. This function should accept the following parameters: decOperand1 As Decimal - The value entered for the first operand. strOperator As String - One of these four operators: +, -, *, or /. decOperand2 As Decimal - The value entered for the second operand.
This is what I need to code any help that could be provided would be appreciated

Possible coding solution:
REM Code a Function procedure named CalculateResult that performs the
REM requested operation and returns a decimal value. This function should
REM accept the following parameters: decOperand1 As Decimal - The value
REM entered for the first operand. strOperator As String - One of these
REM four operators: +, -, *, or /. decOperand2 As Decimal - The value
REM entered for the second operand.
PRINT "Enter value1";: INPUT decOperand1&
PRINT "Enter operand(+,-,*,/,^)";: INPUT strOperator$
PRINT "Enter value2";: INPUT decOperand2&
PRINT "Result"; CalculateResult(decOperand1&, strOperator$, decOperand2&)
END
FUNCTION CalculateResult (decOperand1&, strOperator$, decOperand2&)
SELECT CASE strOperator$
CASE "+", "+="
CalculateResult = decOperand1& + decOperand2&
CASE "-", "-="
CalculateResult = decOperand1& - decOperand2&
CASE "*", "*="
CalculateResult = decOperand1& * decOperand2&
CASE "/", "/="
CalculateResult = decOperand1& / decOperand2&
CASE "^", "^="
CalculateResult = decOperand1& ^ decOperand2&
CASE ELSE
PRINT "Unknown operator."
END SELECT
END FUNCTION

Additional parsing function:
REM Code a Function procedure named CalculateResult that performs the
REM requested operation and returns a decimal value. This function should
REM accept the following parameters: decOperand1 As Decimal - The value
REM entered for the first operand. strOperator As String - One of these
REM four operators: +, -, *, or /. decOperand2 As Decimal - The value
REM entered for the second operand.
PRINT "Enter value1";: INPUT decOperand1&
PRINT "Enter operand(+,-,*,/,^)";: INPUT strOperator$
PRINT "Enter value2";: INPUT decCalc2$
IF decCalc2$ = "" THEN
PRINT "Result:"; CalculateResult2(decOperand1&, strOperator$)
ELSE
decOperand2& = VAL(decCalc2$)
PRINT "Result:"; CalculateResult(decOperand1&, strOperator$, decOperand2&)
END IF
END
FUNCTION CalculateResult (decOperand1&, strOperator$, decOperand2&)
SELECT CASE strOperator$
CASE "+", "+="
CalculateResult = decOperand1& + decOperand2&
CASE "-", "-="
CalculateResult = decOperand1& - decOperand2&
CASE "*", "*="
CalculateResult = decOperand1& * decOperand2&
CASE "/", "/="
CalculateResult = decOperand1& / decOperand2&
CASE "^", "^="
CalculateResult = decOperand1& ^ decOperand2&
CASE ELSE
PRINT "Unknown operator."
END SELECT
END FUNCTION
FUNCTION CalculateResult2 (decOperand1&, strOperator$)
SELECT CASE strOperator$
CASE "++"
CalculateResult2 = decOperand1& + 1&
CASE "--"
CalculateResult2 = decOperand1& - 1&
CASE "//" ' shift-left
CalculateResult2 = decOperand1& * 2&
CASE "**" ' shift-right
CalculateResult2 = decOperand1& / 2&
CASE ELSE
PRINT "Unknown operator."
END SELECT
END FUNCTION

This is a full-fledged recursive descent parser in QB45, QB64:
REM File: Recursive descent parser 2019.
DIM SHARED Out2 AS STRING
DIM SHARED Token AS INTEGER
DIM SHARED Token1 AS STRING
DIM SHARED Token2 AS STRING
DIM SHARED Token.Index AS INTEGER
DO
COLOR 15, 0
PRINT "Equation";: INPUT Out2
IF Out2 = "" THEN EXIT DO
COLOR 14, 0
PRINT "Equals:"; Equate(Out2)
LOOP
COLOR 7, 0
END
FUNCTION Equate (Out2$)
Temp# = DFalse
Var = INSTR(Out2$, " ")
WHILE Var
Out2$ = LEFT$(Out2$, Var - 1) + MID$(Out2$, Var + 1)
Var = INSTR(Out2$, " ")
WEND
IF Out2$ <> "" THEN
Out2$ = UCASE$(Out2$)
Token.Index = 1
CALL Get.Token
CALL Parse1(Temp#)
END IF
Equate = Temp#
END FUNCTION
' logical parser
SUB Parse1 (Temp#)
CALL Parse2(Temp#)
Token.Parsed$ = Token2$
DO
SELECT CASE Token.Parsed$
CASE "|", "&", "~", "?", ":"
Eat$ = ""
CASE ELSE
EXIT DO
END SELECT
Token.Stored$ = Token1$
CALL Get.Token
CALL Parse2(Temp2#)
Token1$ = Token.Stored$
CALL Arith(Token.Parsed$, Temp#, Temp2#)
Token.Parsed$ = Token2$
LOOP
END SUB
' relational parser
SUB Parse2 (Temp#)
CALL Parse3(Temp#)
Token.Parsed$ = Token2$
DO
SELECT CASE Token.Parsed$
CASE "<", ">", "=", "#", ">=", "<=", "<>", "^="
Eat$ = ""
CASE ELSE
EXIT DO
END SELECT
Token.Stored$ = Token1$
CALL Get.Token
CALL Parse3(Temp2#)
Token1$ = Token.Stored$
CALL Arith(Token.Parsed$, Temp#, Temp2#)
Token.Parsed$ = Token2$
LOOP
END SUB
' addition/subtraction parser
SUB Parse3 (Temp#)
CALL Parse4(Temp#)
Token.Parsed$ = Token2$
DO
SELECT CASE Token.Parsed$
CASE "+", "-"
Eat$ = ""
CASE ELSE
EXIT DO
END SELECT
Token.Stored$ = Token1$
CALL Get.Token
CALL Parse4(Temp2#)
Token1$ = Token.Stored$
CALL Arith(Token.Parsed$, Temp#, Temp2#)
Token.Parsed$ = Token2$
LOOP
END SUB
' modulo parser
SUB Parse4 (Temp#)
CALL Parse5(Temp#)
Token.Parsed$ = Token2$
DO
SELECT CASE Token.Parsed$
CASE "%"
Eat$ = ""
CASE ELSE
EXIT DO
END SELECT
Token.Stored$ = Token1$
CALL Get.Token
CALL Parse5(Temp2#)
Token1$ = Token.Stored$
CALL Arith(Token.Parsed$, Temp#, Temp2#)
Token.Parsed$ = Token2$
LOOP
END SUB
' integer division parser
SUB Parse5 (Temp#)
CALL Parse6(Temp#)
Token.Parsed$ = Token2$
DO
SELECT CASE Token.Parsed$
CASE "\"
Eat$ = ""
CASE ELSE
EXIT DO
END SELECT
Token.Stored$ = Token1$
CALL Get.Token
CALL Parse6(Temp2#)
Token1$ = Token.Stored$
CALL Arith(Token.Parsed$, Temp#, Temp2#)
Token.Parsed$ = Token2$
LOOP
END SUB
' multiplication/division parser
SUB Parse6 (Temp#)
CALL Parse7(Temp#)
Token.Parsed$ = Token2$
DO
SELECT CASE Token.Parsed$
CASE "*", "/"
Eat$ = ""
CASE ELSE
EXIT DO
END SELECT
Token.Stored$ = Token1$
CALL Get.Token
CALL Parse7(Temp2#)
Token1$ = Token.Stored$
CALL Arith(Token.Parsed$, Temp#, Temp2#)
Token.Parsed$ = Token2$
LOOP
END SUB
' power parser
SUB Parse7 (Temp#)
CALL Parse7a(Temp#)
Token.Parsed$ = Token2$
DO
SELECT CASE Token.Parsed$
CASE "^"
Eat$ = ""
CASE ELSE
EXIT DO
END SELECT
Token.Stored$ = Token1$
CALL Get.Token
CALL Parse7a(Temp2#)
Token1$ = Token.Stored$
CALL Arith(Token.Parsed$, Temp#, Temp2#)
Token.Parsed$ = Token2$
LOOP
END SUB
' dual-unary parser
SUB Parse7a (Temp#)
CALL Parse8(Temp#)
Token.Parsed$ = Token2$
DO
SELECT CASE Token.Parsed$
CASE "<<", ">>", "--", "++", "**", "//", "||", "##", "^^"
Eat$ = ""
CASE ELSE
EXIT DO
END SELECT
Token.Stored$ = Token1$
CALL Get.Token
' CALL Parse8(Temp2#) ' no secondary token
Token1$ = Token.Stored$
CALL Arith(Token.Parsed$, Temp#, Temp2#)
Token.Parsed$ = Token2$
LOOP
END SUB
' not/unary plus/unary negative parser
SUB Parse8 (Temp#)
Token.Negate$ = "" ' reset token storage
Token.Parsed$ = Token2$ ' store token
' process token
DO
SELECT CASE Token.Parsed$
CASE "!", "-", "+", "--", "++"
Eat$ = ""
CASE ELSE
EXIT DO
END SELECT
CALL Get.Token ' read next token
Token.Negate$ = Token.Negate$ + Token.Parsed$
Token.Parsed$ = Token2$ ' store token
LOOP
CALL Parse9(Temp#) ' get next operator
' process the combined operators
FOR Token.Type = LEN(Token.Negate$) TO 1 STEP -1
SELECT CASE MID$(Token.Negate$, Token.Type, 1) ' get next token
CASE "+"
Eat$ = ""
CASE "-"
Temp# = -Temp# ' perform negate
CASE "!" ' not
Temp# = NOT Temp# ' perform not calculation
END SELECT
NEXT
END SUB
SUB Parse9 (Temp#)
SELECT CASE Token
CASE 1
SELECT CASE Token2$
CASE "("
CALL Get.Token
IF Token2$ <> ")" THEN
DO
CALL Parse1(Temp#)
LOOP UNTIL Token2$ = ")" OR Token = False
CALL Get.Token
END IF
CASE ")"
CALL Get.Token
EXIT SUB
END SELECT
CASE 2
SELECT CASE RIGHT$(Token2$, 1)
CASE "H" ' hexidecimal
Temp# = DFalse
Var = False
FOR Var1 = LEN(Token2$) - 1 TO 1 STEP -1
Var2 = VAL("&H" + (MID$(Token2$, Var1, 1)))
Temp# = Temp# + Var2 * 16 ^ Var
Var = Var + 1
NEXT
CASE "O" ' octal
Temp# = DFalse
Var = False
FOR Var1 = LEN(Token2$) - 1 TO 1 STEP -1
Var2 = VAL(MID$(Token2$, Var1, 1))
Temp# = Temp# + Var2 * 8 ^ Var
Var = Var + 1
NEXT
CASE "B" ' binary
Temp# = DFalse
Var = False
FOR Var1 = LEN(Token2$) - 1 TO 1 STEP -1
IF MID$(Token2$, Var1, 1) = "1" THEN
Temp# = Temp# + 2 ^ Var
END IF
Var = Var + 1
NEXT
CASE ELSE ' decimal
Temp# = VAL(Token2$)
END SELECT
CALL Get.Token
CASE 3
SELECT CASE Token2$
CASE "RND"
Temp# = RND
CASE "ABS"
CALL Get.Token
CALL Get.Token
CALL Parse1(Temp#)
Temp# = ABS(Temp#)
CASE "NOT"
CALL Get.Token
CALL Get.Token
CALL Parse1(Temp#)
Temp# = NOT (Temp#)
CASE "SGN"
CALL Get.Token
CALL Get.Token
CALL Parse1(Temp#)
Temp# = SGN(Temp#)
CASE "SQR"
CALL Get.Token
CALL Get.Token
CALL Parse1(Temp#)
Temp# = SQR(Temp#)
CASE "OR"
CALL Get.Token
CALL Get.Token
CALL Parse1(Temp#)
Number# = Temp#
CALL Get.Token
CALL Parse1(Temp#)
Temp# = Number# OR Temp#
CASE "AND"
CALL Get.Token
CALL Get.Token
CALL Parse1(Temp#)
Number# = Temp#
CALL Get.Token
CALL Parse1(Temp#)
Temp# = Number# AND Temp#
CASE "EQV"
CALL Get.Token
CALL Get.Token
CALL Parse1(Temp#)
Number# = Temp#
CALL Get.Token
CALL Parse1(Temp#)
Temp# = Number# EQV Temp#
CASE "IMP"
CALL Get.Token
CALL Get.Token
CALL Parse1(Temp#)
Number# = Temp#
CALL Get.Token
CALL Parse1(Temp#)
Temp# = Number# IMP Temp#
CASE "MOD"
CALL Get.Token
CALL Get.Token
CALL Parse1(Temp#)
Number# = Temp#
CALL Get.Token
CALL Parse1(Temp#)
Temp# = Number# MOD Temp#
CASE "XOR"
CALL Get.Token
CALL Get.Token
CALL Parse1(Temp#)
Number# = Temp#
CALL Get.Token
CALL Parse1(Temp#)
Temp# = Number# XOR Temp#
END SELECT
CALL Get.Token
END SELECT
END SUB
SUB Arith (Token.Parsed$, Temp#, Temp2#)
SELECT CASE Token.Parsed$
CASE "+"
Temp# = Temp# + Temp2#
CASE "-"
Temp# = Temp# - Temp2#
CASE "/"
Temp# = Temp# / Temp2#
CASE "\"
Temp# = Temp# \ Temp2#
CASE "*"
Temp# = Temp# * Temp2#
CASE "^"
Temp# = Temp# ^ Temp2#
CASE "<"
Temp# = Temp# < Temp2#
CASE ">"
Temp# = Temp# > Temp2#
CASE "<<"
Temp# = Temp# * 2#
CASE ">>"
Temp# = Temp# \ 2#
CASE "||"
Temp# = Temp# * 10#
CASE "##"
Temp# = Temp# \ 10#
CASE "^^"
Temp# = Temp# ^ 10#
CASE "++"
Temp# = Temp# + 1#
CASE "--"
Temp# = Temp# - 1#
CASE "**"
Temp# = Temp# ^ 2#
CASE "//"
Temp# = SQR(Temp#)
CASE "="
Temp# = Temp# = Temp2#
CASE "<="
Temp# = Temp# <= Temp2#
CASE ">="
Temp# = Temp# >= Temp2#
CASE "#", "<>"
Temp# = Temp# <> Temp2#
CASE "^="
Temp# = Temp# ^ Temp2#
CASE "|"
Temp# = Temp# OR Temp2#
CASE "&"
Temp# = Temp# AND Temp2#
CASE "%"
Temp# = Temp# MOD Temp2#
CASE "~"
Temp# = Temp# XOR Temp2#
CASE "?"
Temp# = Temp# IMP Temp2#
CASE ":"
Temp# = Temp# EQV Temp2#
END SELECT
END SUB
SUB Get.Token
Token.List$ = " -+*/\^()[]{}<>=#|&!%~?:,"
Token2$ = ""
Token = False
IF Token.Index > LEN(Out2) THEN
Token2$ = ""
EXIT SUB
END IF
CALL Get.Token2(Token.Exists)
IF Token.Exists THEN
EXIT SUB
END IF
Token.Element$ = MID$(Out2, Token.Index, 1)
IF LEN(Token.Element$) THEN
IF INSTR(Token.List$, Token.Element$) THEN
Token = 1
Token2$ = Token.Element$
Token.Index = Token.Index + 1
EXIT SUB
END IF
END IF
Token.Element$ = MID$(Out2, Token.Index, 1)
IF (Token.Element$ >= "0" AND Token.Element$ <= "9") OR Token.Element$ = "." THEN
DO
IF LEN(Token.Element$) = False THEN
EXIT DO
END IF
IF INSTR(Token.List$, Token.Element$) THEN
EXIT DO
END IF
Token2$ = Token2$ + Token.Element$
Token.Index = Token.Index + 1
Token.Element$ = MID$(Out2, Token.Index, 1)
LOOP
Token = 2
EXIT SUB
END IF
Token.Element$ = MID$(Out2, Token.Index, 1)
IF Token.Element$ >= "A" AND Token.Element$ <= "Z" THEN
DO
IF LEN(Token.Element$) = False THEN
EXIT DO
END IF
IF INSTR(Token.List$, MID$(Out2, Token.Index, 1)) THEN
EXIT DO
END IF
Token2$ = Token2$ + Token.Element$
Token.Index = Token.Index + 1
Token.Element$ = MID$(Out2, Token.Index, 1)
LOOP
Token = 3
EXIT SUB
END IF
END SUB
' tokens of length 2
SUB Get.Token2 (Token.Exists)
Token.Exists = False
Next.Token$ = MID$(Out2, Token.Index, 2)
SELECT CASE Next.Token$
CASE "||"
Token = 1
Token2$ = "||"
Token.Index = Token.Index + 2
Token.Exists = True
CASE "##"
Token = 1
Token2$ = "##"
Token.Index = Token.Index + 2
Token.Exists = True
CASE "<<"
Token = 1
Token2$ = "<<"
Token.Index = Token.Index + 2
Token.Exists = True
CASE ">>"
Token = 1
Token2$ = ">>"
Token.Index = Token.Index + 2
Token.Exists = True
CASE "--"
Token = 1
Token2$ = "--"
Token.Index = Token.Index + 2
Token.Exists = True
CASE "++"
Token = 1
Token2$ = "++"
Token.Index = Token.Index + 2
Token.Exists = True
CASE "**"
Token = 1
Token2$ = "**"
Token.Index = Token.Index + 2
Token.Exists = True
CASE "//"
Token = 1
Token2$ = "//"
Token.Index = Token.Index + 2
Token.Exists = True
CASE ">=", "=>"
Token = 1
Token2$ = ">="
Token.Index = Token.Index + 2
Token.Exists = True
CASE "<=", "=<"
Token = 1
Token2$ = "<="
Token.Index = Token.Index + 2
Token.Exists = True
CASE "<>", "><"
Token = 1
Token2$ = "<>"
Token.Index = Token.Index + 2
Token.Exists = True
CASE "^="
Token = 1
Token2$ = "^="
Token.Index = Token.Index + 2
Token.Exists = True
CASE "^^"
Token = 1
Token2$ = "^^"
Token.Index = Token.Index + 2
Token.Exists = True
END SELECT
END SUB

Related

Math operations with one line input. Any suggestions to improve this code?

'''I wrote this program so I could do the math operations like I do in a terminal. just add the numbers and the operators and hit enter. that will give me the result.
With this code same can be done. I am sharing this so I can get some feedback on how to make this more efficient, add more functionality to it, etc...'''
# Basic operator functions
def add(num1, num2):
return num1 + num2
def sub(num1, num2):
return num1 - num2
def mul(num1, num2):
return num1 * num2
def dev(num1, num2):
return num1 / num2
operators = {'+': add, '-': sub, '*': mul, '/': dev}
def splitter(text):
"""Function to split an input string containing number and operators
in a list separating numbers and operators in given sequence"""
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', "."]
delimiter = ["+", "-", "*", "/"]
elist = []
num = ""
for char in text:
if char in numbers:
num += char
elif char in delimiter:
elist.append(float(num))
elist.append(char)
num = ""
elif char == " ":
continue
else:
print(f"Invalid input: {char}")
break
elist.append(float(num))
return elist
def calculator(splitter1):
result = splitter1
# print(result)
total = 0
while len(result) > 1:
if "/" in result:
o_index = result.index("/")
n1 = (o_index - 1)
n2 = (o_index + 1)
total = dev(result[n1], result[n2])
result[o_index] = total
result.pop(n1)
result.pop(o_index)
elif "*" in result:
o_index = result.index("*")
n1 = (o_index - 1)
n2 = (o_index + 1)
total = mul(result[n1], result[n2])
result[o_index] = total
result.pop(n1)
result.pop(o_index)
elif "+" in result:
o_index = result.index("+")
n1 = (o_index - 1)
n2 = (o_index + 1)
total = add(result[n1], result[n2])
result[o_index] = total
result.pop(n1)
result.pop(o_index)
elif "-" in result:
o_index = result.index("-")
n1 = (o_index - 1)
n2 = (o_index + 1)
total = sub(result[n1], result[n2])
result[o_index] = total
result.pop(n1)
result.pop(o_index)
else:
continue
# print(result)
return total
repeat = "y"
while repeat == "y":
cal = input('calc: ')
calculation = calculator(splitter(cal))
print(calculation)
repeat = input("Would you like to continue to a new calculation? type 'y' to continue: ").lower()
else:
print("Thank you!")

Alternative of ChrW function

Is there any alternative function/solution of the ChrW() which accepts value not in range is -32768–65535 like for character code 􀂇 which leads to "􀂇". Using ChrW() gives error
"Invalid procedure call or argument"
So I want an alternative solution to convert the charactercode to the actual character.
Code:
Function HTMLDecode(sText)
Dim regEx
Dim matches
Dim match
sText = Replace(sText, """, Chr(34))
sText = Replace(sText, "<" , Chr(60))
sText = Replace(sText, ">" , Chr(62))
sText = Replace(sText, "&" , Chr(38))
sText = Replace(sText, " ", Chr(32))
Set regEx= New RegExp
With regEx
.Pattern = "&#(\d+);" 'Match html unicode escapes
.Global = True
End With
Set matches = regEx.Execute(sText)
'Iterate over matches
For Each match In matches
'For each unicode match, replace the whole match, with the ChrW of the digits.
sText = Replace(sText, match.Value, ChrW(match.SubMatches(0)))
Next
HTMLDecode = sText
End Function
' https://en.wikipedia.org/wiki/UTF-16#Description
function Utf16Encode(byval unicode_code_point)
if (unicode_code_point >= 0 and unicode_code_point <= &hD7FF&) or (unicode_code_point >= &hE000& and unicode_code_point <= &hFFFF&) Then
Utf16Encode = ChrW(unicode_code_point)
else
unicode_code_point = unicode_code_point - &h10000&
Utf16Encode = ChrW(&hD800 Or (unicode_code_point \ &h400&)) & ChrW(&hDC00 Or (unicode_code_point And &h3FF&))
end if
end function
For Each match In matches
'For each unicode match, replace the whole match, with the ChrW of the digits.
sText = Replace(sText, match.Value, Utf16Encode(CLng(match.SubMatches(0))))
Next

LinkedStyle for the Word BuiltinStyles

I am extending the capability of the Word report writing from VSTO to consider different languages. Therefore, instead of using the headings like "Heading 1" etc, I have used wdStyleHeading1 etc. I have built a function to assign the style to the heading. The problem is that the line 1 and line 2 below are overwriting each other. If I call the function first, I loose list number and If I call function second, I loose the format. Can you please explain where I am going wrong?
I have imported the necessary references.
Call HeadingListLevel(wrdApp, 1)
wrdApp.Selection.ParagraphFormat.Style = Word.WdBuiltinStyle.wdStyleHeading1
Below is the sub function
Sub HeadingListLevel(wrdApp As Object, HeadingLvl As Integer)
'Dim wrdHeading As String
Dim wrdHeadingNr As String
Dim i As Integer
Dim ListTemp As Word.ListTemplate
wrdHeadingNr = "%" & 1
ListTemp = wrdApp.ListGalleries(Word.WdListGalleryType.wdOutlineNumberGallery).ListTemplates(1)
For i = 1 To HeadingLvl
If i > 1 Then
wrdHeadingNr = wrdHeadingNr & "." & "%" & i
End If
Next i
'wrdHeading = "Heading " & HeadingLvl
With ListTemp.ListLevels(1)
Select Case HeadingLvl
Case 1
.LinkedStyle = Word.WdBuiltinStyle.wdStyleHeading1
Case 2
.LinkedStyle = Word.WdBuiltinStyle.wdStyleHeading2
Case 3
.LinkedStyle = Word.WdBuiltinStyle.wdStyleHeading3
Case 4
.LinkedStyle = Word.WdBuiltinStyle.wdStyleHeading4
Case 5
.LinkedStyle = Word.WdBuiltinStyle.wdStyleHeading5
Case 6
.LinkedStyle = Word.WdBuiltinStyle.wdStyleHeading6
Case 7
.LinkedStyle = Word.WdBuiltinStyle.wdStyleHeading7
End Select
.NumberFormat = wrdHeadingNr
.NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabic
End With
wrdApp.Selection.Range.ListFormat.ApplyListTemplate(ListTemplate:=ListTemp)
ListTemp = Nothing
End Sub
I discovered the answer myself. Below is the changes to the sub function. It worked well for me.
Sub HeadingListLevel(wrdApp As Object, HeadingLvl As Integer)
'Dim wrdHeading As String
Dim wrdHeadingNr As String
Dim i As Integer
Dim ListTemp As Word.ListTemplate
wrdHeadingNr = "%" & 1
ListTemp = wrdApp.ListGalleries(Word.WdListGalleryType.wdOutlineNumberGallery).ListTemplates(5)
For i = 1 To HeadingLvl
If i > 1 Then
wrdHeadingNr = wrdHeadingNr & "." & "%" & 1
End If
Next i
'wrdHeading = "Heading " & HeadingLvl
With ListTemp.ListLevels(HeadingLvl)
Select Case HeadingLvl
Case 1
.LinkedStyle = wrdApp.ActiveDocument.Styles(Word.WdBuiltinStyle.wdStyleHeading1).NameLocal
Case 2
.LinkedStyle = wrdApp.ActiveDocument.Styles(Word.WdBuiltinStyle.wdStyleHeading2).NameLocal
Case 3
.LinkedStyle = wrdApp.ActiveDocument.Styles(Word.WdBuiltinStyle.wdStyleHeading3).NameLocal
Case 4
.LinkedStyle = wrdApp.ActiveDocument.Styles(Word.WdBuiltinStyle.wdStyleHeading4).NameLocal
Case 5
.LinkedStyle = wrdApp.ActiveDocument.Styles(Word.WdBuiltinStyle.wdStyleHeading5).NameLocal
Case 6
.LinkedStyle = wrdApp.ActiveDocument.Styles(Word.WdBuiltinStyle.wdStyleHeading6).NameLocal
Case 7
.LinkedStyle = wrdApp.ActiveDocument.Styles(Word.WdBuiltinStyle.wdStyleHeading7).NameLocal
End Select
'.LinkedStyle = wrdHeading
.NumberFormat = wrdHeadingNr
.NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabic
'.StartAt = 1
'.ResetOnHigher = False
End With
wrdApp.Selection.Range.ListFormat.ApplyListTemplate(ListTemplate:=ListTemp)
ListTemp = Nothing
End Sub

How can I camelCase a phrase with Dragon NaturallySpeaking's advanced scripting?

From time to time, typically when coding, I would like to dictate a phrase so that it is camelCased a phrase. For example, when I dictate sentence generator I would like Dragon NaturallySpeaking to write sentenceGenerator.
How can I camelCase a phrase with Dragon NaturallySpeaking's advanced scripting?
Same question for Dragon Dictate: How can I convert a series of words into camel case in AppleScript?
You can use this function:
' CamelCases the previous <1to10> words:
' Voice command name: CamelCase <1to10>
' Author: Edgar
' URL: https://www.knowbrainer.com/forums/forum/messageview.cfm?FTVAR_FORUMVIEWTMP=Linear&catid=12&threadid=14634&discTab=true
' URL mirror: https://web.archive.org/web/20170606015010/https://www.knowbrainer.com/forums/forum/messageview.cfm?FTVAR_FORUMVIEWTMP=Linear&catid=12&threadid=14634&discTab=true
' Tested with Dragon NaturallySpeaking 12.5 with Windows 7 SP1 x64 Ultimate
Sub Main
Dim camelVariable, dictate, firstCharacter As String
Dim wasSpace, isLower, trailingSpace As Boolean
Dim dictationLength As Integer
For increment = 1 To Val (ListVar1)
SendKeys "+^{Left}", 1
Next increment
Wait 0.2
SendKeys "^c", 1
Wait 0.3
dictate = Clipboard
Wait 0.3
dictationLength = Len (dictate)
If Mid (dictate, dictationLength, 1) = " " Then trailingSpace = True
'Dim testing As String
'testing = "#" + Mid (dictate, 1, dictationLength) + "#"
'MsgBox testing
dictate = Trim (dictate)
firstCharacter = Mid (dictate, 1, 1)
firstCharacter = LCase (firstCharacter)
camelVariable = firstCharacter
dictationLength = Len (dictate)
If dictationLength > 1 Then
For increment = 2 To dictationLength
firstCharacter = Mid (dictate, increment, 1)
If firstCharacter = " " Then
wasSpace = True
Else
If wasSpace = True Then firstCharacter = UCase (firstCharacter)
camelVariable = camelVariable + firstCharacter
wasSpace = False
End If
Next increment
End If
If leadingSpace = True Then camelVariable = " " + camelVariable
If trailingSpace = True Then camelVariable = camelVariable + " "
SendKeys camelVariable
End Sub
or
' CamelCases the previous dictated words:
' Voice command name: CamelCase that
' Author: Heather
' URL: https://www.knowbrainer.com/forums/forum/messageview.cfm?FTVAR_FORUMVIEWTMP=Linear&catid=12&threadid=14634&discTab=true
' URL mirror: https://web.archive.org/web/20170606015010/https://www.knowbrainer.com/forums/forum/messageview.cfm?FTVAR_FORUMVIEWTMP=Linear&catid=12&threadid=14634&discTab=true
' Tested with Dragon NaturallySpeaking 12.5 with Windows 7 SP1 x64 Ultimate
Option Explicit
Sub Main
Dim engCtrl As New DgnEngineControl
Dim Text As String
Dim VarText As String
HeardWord "cut","that"
Text = Clipboard
SendDragonKeys "" & CamelCase(Text)
End Sub
Public Function CamelCase(strInput As String) As String
Dim i As Integer
Dim sMid As String
Dim foundSpace As Boolean
For i = 1 To Len(strInput)
sMid = Mid(strInput, i, 1)
Select Case Asc(sMid)
Case 32:
foundSpace = True
Case 65 To 90:
If i = 1 Then
CamelCase = CamelCase + LCase(sMid)
Else
CamelCase = CamelCase + sMid
End If
foundSpace = False
Case 97 To 122:
If foundSpace Then
CamelCase = CamelCase + UCase(sMid)
Else
CamelCase = CamelCase + sMid
End If
foundSpace = False
Case Else:
CamelCase = CamelCase + sMid
foundSpace = False
End Select
Next i
End Function
or
' CamelCases the next dictated words:
' Voice command name: CamelCase <dictation>
' Author: Edgar
' URL: https://www.knowbrainer.com/forums/forum/messageview.cfm?FTVAR_FORUMVIEWTMP=Linear&catid=12&threadid=14634&discTab=true
' URL mirror: https://web.archive.org/web/20170606015010/https://www.knowbrainer.com/forums/forum/messageview.cfm?FTVAR_FORUMVIEWTMP=Linear&catid=12&threadid=14634&discTab=true
' Requires Dragon NaturallySpeaking 13 Professional or higher, because the variable <dictation> was introduced in Dragon NaturallySpeaking 13 Professional.
Sub Main
Dim camelVariable, dictate, firstCharacter As String
Dim wasSpace, isLower As Boolean
Dim dictationLength As Integer
dictate = ListVar1
dictate = Trim (dictate)' probably unnecessary
firstCharacter = Mid (dictate, 1, 1)
firstCharacter = LCase (firstCharacter)
camelVariable = firstCharacter
dictationLength = Len (dictate)
If dictationLength > 1 Then
For increment = 2 To dictationLength
firstCharacter = Mid (dictate, increment, 1)
If firstCharacter = " " Then
wasSpace = True
Else
If wasSpace = True Then firstCharacter = UCase (firstCharacter)
camelVariable = camelVariable + firstCharacter
wasSpace = False
End If
Next increment
End If
SendKeys " " + camelVariable + " "
End Sub
(source) (mirror)
The answer from 2017 has syntax issues. I just coded and tested this for Dragon 15:
'#Language "WWB-COM"
' Command name: case camel <dictation>
'
' Description:
' Applies camel case to provided phrase.
'
' Usage:
' "case camel looks good to me" -> "looksGoodToMe"
Option Explicit
Sub Main
Dim phrase As String
Dim result As String
Dim wasSpace As Boolean
Dim i As Integer
phrase = ListVar1
phrase = Trim(phrase)
wasSpace = False
For i = 0 To Len(phrase) - 1
If i = 0 Then
result = LCase(Mid(phrase,i + 1,1))
ElseIf Mid(phrase,i + 1,1) = " " Then
wasSpace = True
ElseIf wasSpace Then
result += UCase(Mid(phrase,i + 1,1))
wasSpace = False
Else
result += LCase(Mid(phrase,i + 1,1))
End If
Next
SendKeys result
End Sub

QBASIC Decimal to Binary conversion

I have converted a decimal number to binary using STR$() in QBASIC. But I need a way to convert decimal number to binary without using string functions. Thanks.
My Code :
CLS
INPUT N
WHILE N <> 0
E = N MOD 2
B$ = STR$(E)
N = FIX(N / 2)
C$ = B$ + C$
WEND
PRINT "Output "; C$
END
This code sample converts a numeric value to a binary string in Basic.
PRINT "Enter value";
INPUT Temp#
Out3$ = ""
IF Temp# >= False THEN
Digits = False
DO
IF 2 ^ (Digits + 1) > Temp# THEN
EXIT DO
END IF
Digits = Digits + 1
LOOP
FOR Power = Digits TO 0 STEP -1
IF Temp# - 2 ^ Power >= False THEN
Temp# = Temp# - 2 ^ Power
Out3$ = Out3$ + "1"
ELSE
Out3$ = Out3$ + "0"
END IF
NEXT
END IF
PRINT Out3$
END
When you want to display an integer value as binary, it seems logical to me to store it in a string variable, because it's only for display. So I'm not really sure what you are trying to do here.
Maybe you were looking for LTRIM$ so you would get outputs like 11010 instead of 1 1 0 1 0 ?
You could store it in an integer value like in the code below. But, although the integer value will look the same as the string variable, it will in fact be a completely different value.
CLS
INPUT "Type a decimal number:", N
S$ = ""
I = 0
P = 1
WHILE (N <> 0)
' get right most bit and shift right
E = N AND 1
N = INT(N / 2) ' bit shift right
' format for dsplay
S$ = LTRIM$(STR$(E)) + S$
I = I + (E * P)
P = P * 10
WEND
PRINT "Binary as string="; S$
PRINT "Binary as int="; I
END