實現計算器

2021-04-12 19:58:31 字數 4395 閱讀 9846

dim

boldot as

boolean

dim dblacc1, dblacc2 as

double

dim dblacc3 as

double = 10

dim strop as

string

private

sub add_num(byval intnumber as

integer)

if boldot then

dblacc1 = dblacc1 + intnumber / dblacc3

dblacc3 *= 10

else

dblacc1 = dblacc1 * 10 + intnumber

endif

txtresult.text = cstr(dblacc1)

endsub

private

sub action(byval strcurrentop as

string)

select

case strop

case

is = "+"

dblacc2 = dblacc2 + dblacc1

dblacc1 = 0

case

is = "-"

dblacc2 = dblacc2 - dblacc1

case

is = "*"

dblacc2 = dblacc2 * dblacc1

dblacc1 = 0

case

is = "/"

dblacc2 = dblacc2 / dblacc1

dblacc1 = 0

case

is = "="

case

else

dblacc2 = dblacc1

dblacc1 = 0

endselect

strop = strcurrentop

txtresult.text = dblacc2

boldot = false

dblacc3 = 10

endsub

private

sub btn0_click(byval sender as system.object, byval e as system.eventargs) handles btn0.click

call add_num(0)

endsub

private

sub btn1_click(byval sender as system.object, byval e as system.eventargs) handles btn1.click

call add_num(1)

endsub

private

sub btn2_click(byval sender as system.object, byval e as system.eventargs) handles btn2.click

call add_num(2)

endsub

private

sub btn3_click(byval sender as system.object, byval e as system.eventargs) handles btn3.click

call add_num(3)

endsub

private

sub btn4_click(byval sender as system.object, byval e as system.eventargs) handles btn4.click

call add_num(4)

endsub

private

sub btn5_click(byval sender as system.object, byval e as system.eventargs) handles btn5.click

call add_num(5)

endsub

private

sub btn6_click(byval sender as system.object, byval e as system.eventargs) handles btn6.click

call add_num(6)

endsub

private

sub btn7_click(byval sender as system.object, byval e as system.eventargs) handles btn7.click

call add_num(7)

endsub

private

sub btn8_click(byval sender as system.object, byval e as system.eventargs) handles btn8.click

call add_num(9)

endsub

private

sub btn9_click(byval sender as system.object, byval e as system.eventargs) handles btn9.click

call add_num(9)

endsub

private

sub b***ot_click(byval sender as system.object, byval e as system.eventargs) handles b***ot.click

boldot = true

endsub

private

sub btnplus_click(byval sender as system.object, byval e as system.eventargs) handles btnplus.click

call action("+")

endsub

private

sub btnminus_click(byval sender as system.object, byval e as system.eventargs) handles btnminus.click

call action("-")

endsub

private

sub btntimes_click(byval sender as system.object, byval e as system.eventargs) handles btntimes.click

call action("*")

endsub

private

sub b***ivide_click(byval sender as system.object, byval e as system.eventargs) handles b***ivide.click

call action("/")

endsub

private

sub btnclear_click(byval sender as system.object, byval e as system.eventargs) handles btnclear.click

dblacc1 = 0

dblacc2 = 0

dblacc3 = 10

boldot = false

strop = ""

txtresult.text = ""

endsub

private

sub btnpi_click(byval sender as system.object, byval e as system.eventargs) handles btnpi.click

dblacc1 = math.pi

dblacc3 = 10

boldot = false

txtresult.text = dblacc1

endsub

private

sub btnsqrt_click(byval sender as system.object, byval e as system.eventargs) handles btnsqrt.click

dblacc1 = math.exp(math.log(dblacc1) / 2)

dblacc3 = 10

boldot = false

txtresult.text = dblacc1

endsub

endclass

計算器的實現

計算器?不是非常簡單嗎?宣告兩個float型變數,再加上操作符,自動計算結果出來!public static float calc float a,float b,int operator 呵呵,這麼簡單,那就沒必要寫成文章了 這裡要說的是 程式計算表示式的值,比如 1 2 3 5 9 就是簡單兩個...

QT實現計算器

1 新建calculatorstandard類和工程檔案 2 使用水平布局和把按鍵依次新增 ui介面如下圖所示 3 在calculatorstandard.h中宣告用到的槽函式 private slots void digitclicked 數字按鍵 void addandsubclicked 加減...

棧實現計算器

在表示式中的轉換規則 運算元 進棧 操作符 1 進棧 空棧 優先順序高 棧頂是 同時表示式不是 2 出棧並計算 表示式符號的優先順序不高於棧頂符號 表示式為 同時棧頂不為 表示式 0 同時棧不為空 3 出棧但不計算 表示式為 同時棧頂為 標頭檔案 標頭檔案 ifndef stack h define...