表示式計算器的設計與實現VB

時間 2022-03-10 18:45:27

1樓:

option explicit

public function str18(num as string) as double

dim i&, l&, lstart&

dim stemp$, dtemp#, dtemp2#

if num = vbnullstring then exit function

lstart = instr(1, num, "(")

do while lstart > 0

if lstart > 1 then

if instr(1, "(*+-/", mid$(num, lstart - 1, 1)) <= 0 then goto 10

end if

stemp = getinners(num, lstart + 1)

l = len(stemp)

stemp = cstr(str18(stemp))

num = mid$(num, 1, lstart - 1) & stemp & mid$(num, lstart + l + 2)

10: lstart = instr(lstart + 1, num, "(")

loop

lstart = instrrev(num, "int(")

do while lstart > 0

stemp = getinners(num, lstart + 4)

l = len(stemp)

stemp = cstr(int(str18(stemp)))

num = mid$(num, 1, lstart - 1) & stemp & mid$(num, lstart + l + 5)

lstart = instrrev(num, "int(", lstart)

loop

lstart = instr(2, num, "+")

if lstart > 0 then

dtemp = str18(mid$(num, lstart + 1))

dtemp2 = str18(left$(num, lstart - 1))

str18 = dtemp + dtemp2: exit function

end if

lstart = instr(2, num, "-")

if lstart > 0 then

if instr(1, "(*+-/", mid$(num, lstart - 1, 1)) <= 0 then

dtemp = str18(mid$(num, lstart + 1))

dtemp2 = str18(left$(num, lstart - 1))

str18 = dtemp2 - dtemp: exit function

end if

end if

lstart = instr(1, num, "*")

if lstart > 0 then

dtemp = str18(mid$(num, lstart + 1))

dtemp2 = str18(left$(num, lstart - 1))

str18 = dtemp * dtemp2: exit function

end if

lstart = instr(1, num, "/")

if lstart > 0 then

dtemp = str18(mid$(num, lstart + 1))

'if dtemp = 0 then exit function

dtemp2 = str18(left$(num, lstart - 1))

str18 = dtemp2 / dtemp: exit function

end if

lstart = instr(1, num, "^")

if lstart > 0 then

dtemp = str18(mid$(num, lstart + 1))

dtemp2 = str18(left$(num, lstart - 1))

str18 = dtemp2 ^ dtemp: exit function

end if

if left$(num, 1) = "-" then

str18 = -1 * val(mid$(num, 2))

else

str18 = val(num)

end if

end function

private function getinners(str$, lstart&) as string

'獲取()內容,str(lstart-1)="("

dim i&, stemp as string * 1, k%, l&

l = len(str)

for i = lstart to l

stemp = mid$(str, i, 1)

select case stemp

case "(": k = k + 1

case ")": k = k - 1

end select

if k = -1 then exit for

next

if i <= l then getinners = mid$(str, lstart, i - lstart)

end function

最好寫在模組中 哦~~~~~[注,來自vbgood 很久前的了 誰的我也忘了]

利用vb製作一個簡單科學計算器 設計與實現 (1) 完成加、減、乘、除、乘方、開方和求倒數的計算 5

2樓:伽

用vb製作一個簡單科學計算器

比較,肯定知道的確

3樓:匿名使用者

vb製作一個簡單科學計算器 。。我能搞定。。

vb設計一個簡易計算器

4樓:韓晨伊

private sub command1_click(index as integer)

select case index

case 0 to 9

if firstnum then

strnum1 = str(index)

firstnum = false

else

strnum1 = strnum1 & trim(str(index))

end if

text1.text = strnum1

case 10

if not pointflag thenif firstnum = true thenstrnum1 = "0."

firstnum = false

else

strnum1 = strnum1 + "."

end if

pointflag = true

text1.text = strnum1

end if

case 11

if not signflag then

strnum1 = text1.text

equal = val(strnum1)

else

call run

end if

firstnum = true

pointflag = false

signflag = false

case 12 to 15

firstnum = true

pointflag = false

if signflag then

call run

else

signflag = true

strnum2 = strnum1

strnum1 = ""

end if

runsign = index - 11

case else

call cleardata

end select

end sub

怎麼用vb程式做計算器啊???

5樓:子小了了

其實比較簡單啦,用一個窗體就可以實現啦!

我自己寫的,你可以看看

option explicit

dim strnumber as string

dim strpoint as string

dim dblnum1 as double

dim intoperator as integer

'清除結果

private sub cmdgt_click()

txtdisplay.text = "0."

strnumber = ""

strpoint = "."

intoperator = 7

end sub

'輸入數字

private sub cmdnumber_click(index as integer)

strnumber = strnumber & cmdnumber(index).caption

txtdisplay.text = strnumber & strpoint

end sub

private sub cmdonoff_click()

endend sub

'運算過程

private sub cmdoperator_click(index as integer)

dim dblnum2 as double

'是第一次單擊運算子時,將輸入的值先賦給第一個數,否則賦值給第二個數進行運算

if intoperator = 7 then

dblnum1 = cdbl(txtdisplay.text)

else

dblnum2 = cdbl(val(txtdisplay.text))

'根據輸入的符號進行運算

'求普通運算

select case intoperator

case 0

dblnum1 = dblnum1 + dblnum2

case 1

dblnum1 = dblnum1 - dblnum2

case 2

dblnum1 = dblnum1 * dblnum2

case 3

if dblnum2 <> 0 then

dblnum1 = dblnum1 / dblnum2

else

msgbox "除數不能為「0」!請重新輸入除數。", vbokonly + vbinformation, "除零錯誤"

index = intoperator

end if

case 6

dblnum1 = dblnum1 * dblnum2 / 100

end select

end if

'取得當前輸入的運算子,以做下次運算

intoperator = index

strnumber = ""

txtdisplay = cstr(dblnum1)

'判斷是否為文字框中的數字加點

if not txtdisplay like "*.*" then

txtdisplay.text = txtdisplay.text & "."

end if

end sub

private sub cmdotheroper_click(index as integer)

dim dblnum as double

'求平方根,平方,

dblnum = cdbl(val(txtdisplay.text))

select case index

case 0

'驗證資料是否有效

if dblnum >= 0 then

txtdisplay.text = cstr(sqr(dblnum))

else

msgbox "負數不能開平方根!", _

vbokonly + vbcritical, "開平方根錯誤"

end if

case 1

txtdisplay.text = cstr(dblnum ^ 2)

end select

'判斷是否為文字框中的數字加點

if not txtdisplay like "*.*" then

txtdisplay.text = txtdisplay.text & "."

end if

end sub

private sub cmdpoint_click()

strnumber = strnumber & strpoint

strpoint = ""

end sub

private sub form_keydown(keycode as integer, shift as integer)

'使被按下的數字鍵的對應按鈕取得焦點

select case keycode

case 48 to 57

cmdnumber(keycode - 48).setfocus

case 96 to 105

cmdnumber(keycode - 96).setfocus

case else

'使按下的符號鍵對應的按鈕取得焦點

if keycode = 107 or (shift = vbshiftmask and keycode = 187) then

cmdoperator(0).setfocus

cmdoperator_click (0)

elseif keycode = 109 or keycode = 189 then

cmdoperator(1).setfocus

cmdoperator_click (1)

elseif keycode = 106 or (shift = vbshiftmask and keycode = 56) then

cmdoperator(2).setfocus

cmdoperator_click (2)

elseif keycode = 111 or keycode = 191 then

cmdoperator(3).setfocus

cmdoperator_click (3)

elseif keycode = 13 then

cmdoperator(7).setfocus

cmdoperator_click (7)

elseif keycode = 8 then

cmdgt.setfocus

call cmdgt_click

end if

end select

end sub

private sub form_keypress(keyascii as integer)

'將合法的資料輸入到文字框

select case keyascii

case 48 to 58

'呼叫數字鍵點選處理程式

cmdnumber_click keyascii - 48

keyascii = 0

case 46

'呼叫小數點輸入

cmdpoint_click

keyascii = 0

case 13

'當敲擊回車時,不能觸發form的 keyup 事件,因此在這裡設定文字框的焦點

txtdisplay.setfocus

case else

keyascii = 0

end select

end sub

private sub form_keyup(keycode as integer, shift as integer)

txtdisplay.setfocus

end sub

private sub form_load()

strnumber = ""

strpoint = "."

intoperator = 7

end sub

關於VB表示式運算的,VB中 表示式運算順序

在表示式中,當運算子不止一種時,要先處理算術運算子,接著處理比較運算子,然後再處理邏輯運算子。算術 比較 邏輯 指數運算 相等 not 負數 不等 and 乘法和除法 小於 or 整數除法 大於 xor 求模運算 mod 小於或相等 eqv 加法和減法 大於或相等 imp 字串連線 like is ...

VB裡的表示式是什麼意思,vb字串表示式是什麼意思

表示式 就是指有兩個或兩個以上的式子,通過一種或多種運算子連線起來如 a 5 a a 1 a not a if a b 基本上 所有的你都可以理解為 表示式 with語句就是指可以在同一個物件中設定屬性,也就是少打物件名 就好像是一個公式!vb字串表示式是什麼意思 上面這個就是一個字串表示式 vb字...

VB中邏輯表示式怎麼表示,請問,VB中的邏輯運算子都是什麼意思?

不周期彗星 1 x y小於10 且 x y要大於0 x y 10 and x y 0 2 x.y都是正整數或都是負整數 int x x and int y y and sng x sng y 3 a.b之一為零但不得同時為零 a 0 or b 0 and a b 4 c1 c2 c3大於等於255或...