資源描述:
《vb代碼——計算器》由會員上傳分享,免費在線閱讀,更多相關內(nèi)容在教育資源-天天文庫。
1、第三題OptionExplicitPrivateStoredValueAsDouble'中間計算值PrivateStore1AsDoublePrivateConstopNone=0'代表沒有運算符PrivateConstopAdd=1'代表加法常量PrivateConstopSubtract=2'代表減法常量PrivateConstopMultiply=3'代表乘法常量PrivateConstopDivide=4'代表除法常量PrivateOperatorAsInteger'計算符號因子PrivateNewEntryAsBoolean'新數(shù)據(jù)項邏輯變量,若為True,說
2、明無新數(shù)據(jù)PrivatejscsAsIntegerPrivateConstpi=3.1415926PrivateSubCmdBack_Click()'刪除最后一個字符.DimtxtAsStringDimmin_lenAsIntegertxt=txtDisplay.TextIfLeft$(txt,1)="-"Then'假如txt中的第一個字符是負號,只有兩個字符,刪除就會使之為0min_len=2Else'假如txt是正數(shù),只有一個字符,刪除就會使之為0min_len=1EndIfIfLen(txt)>min_lenThentxtDisplay.Text=Left$(tx
3、t,Len(txt)-1)ElsetxtDisplay.Text="0"EndIfEndSubPrivateSubCmdCal_Click(IndexAsInteger)'SelectCaseIndex'Case0'Operator=opAdd'Case1'Operator=opSubtract'Case2'Operator=opMultiply'Case3'Operator=opDivide'EndSelect'NewEntry=True'這種方法正確,但顯得比較煩瑣,下面的方法顯得更為簡單,不但調(diào)用了CmdEqual_Click程序'且運算符號可以直接計算jscs=
4、jscs+1'Ifjscs>1Then'執(zhí)行前面的操作cmdEqual_Click'EndIf'存儲操作符Operator=Index+1'開始新數(shù)據(jù).NewEntry=TrueEndSubPrivateSubcmdClear_Click()cmdClearEntry_ClickStoredValue=0jscs=0Operator=opNoneEndSubPrivateSubcmdClearEntry_Click()txtDisplay.Text="0"EndSubPrivateSubcmdNum_Click(IndexAsInteger)IfNewEntryThen
5、txtDisplay.Text=Format$(Index)NewEntry=FalseElseIftxtDisplay.Text="0"ThentxtDisplay.Text=Format$(Index)ElsetxtDisplay.Text=txtDisplay.Text&Format$(Index)EndIfEndIfEndSubPrivateSubcmdOFF_Click()UnloadMeEndSubPrivateSubcmdPn_Click()'正負鍵輸入IfNewEntryThen'假如是新數(shù)據(jù),則敲"+/-"鍵為負號txtDisplay.Text="-"
6、ElseIfLeft$(txtDisplay.Text,1)="-"Then'假若數(shù)據(jù)本身為負值,則按此鍵變?yōu)檎祎xtDisplay.Text=Right$(txtDisplay.Text,2)ElsetxtDisplay.Text="-"&txtDisplay.Text'若數(shù)據(jù)為正值,則轉(zhuǎn)化為負值EndIfEndSubPrivateSubcmdDecimal_Click()'小數(shù)點輸入IfInStr(txtDisplay.Text,".")Then'若數(shù)據(jù)中已有小數(shù)點,則發(fā)出Beep的聲音BeepElseIfNewEntryThentxtDisplay.Text="
7、."'若為新數(shù)據(jù),則直接輸入小數(shù)點,數(shù)據(jù)變?yōu)榕f數(shù)據(jù)NewEntry=FalseElsetxtDisplay.Text=txtDisplay.Text&"."'若為舊數(shù)據(jù),則將以前數(shù)據(jù)與小數(shù)點相連EndIfEndIfEndSub'計算上述計算操作的結(jié)果PrivateSubcmdEqual_Click()Dimnew_valueAsDoubleIftxtDisplay.Text=""Then'若數(shù)據(jù)框中無數(shù)據(jù)則為0,否則將該數(shù)轉(zhuǎn)換為雙精度數(shù)new_value=0Elsenew_value=CDbl(txtDisplay.Text)EndIfS