成语| 古诗大全| 教学资源| 作文| 扒知识| 扒知识繁体

當前位置:首页 > 趣味生活

編寫一個簡單的計算器程式

Q1:用VB編寫一個計算器程式的代碼

1、創建控件組的方法
首先創建一個命令按鈕,調整其大小(覺得合適就行),名稱為Command1,Caption 屬性為數字 0 ;然後進行“復制”和“粘貼”,當選擇“粘貼”時,出現對話框提示已有一個同名控件,詢問是否創建控件組,選擇“是”後,即創建了一個名為“Command”的控件組。

這時,第一個按鈕的Index屬性值默認為“0”,第二個的Index屬性值自動設為“1”,並且大小與第一個按鈕相同,只需修改其 Caption 屬性為數字“1”並將其拖至合適位置即可。此後繼續使用“粘貼”的方法建立其他控件組中其余按鈕,共20個按鈕,每建立一個,就將它拖到合適處,並修改相應的Caption屬性值。

2、各控件組其屬性設置如下:


設置效果如下圖所示:

二、編寫代碼

Dim s1 As Single, s2 As Single, ysf As String

‘定義兩個單精度數變數用與存放參與運算的數,一個字符型存放運算符

Private Sub Command1_Click(Index As Integer)

Text1.Text = Text1.Text & Command1(Index).Caption ’將command1的單擊事件與文本框顯示的內容連接

End Sub

Private Sub Command2_Click()

Text1.Text = Text1.Text + “。”

If (InStr(Text1.Text, “。”) = 1) Then ‘第一位不能為小數

Text1.Text = “”

End If

If InStr(Text1.Text, “。”) 《 Len(Text1.Text) Then ’防止出現兩個小數點

Text1.Text = Left

(Text1.Text, Len(Text1.Text) - 1)

End If

End Sub

Private Sub

Command3_Click()

s2 = Val(Text1.Text) ‘開始加減乘除運算

Select Case ysf Case “+”

Text1.Text = s1 + s2、

Case “-”

Text1.Text = s1 - s2、

Case “*”

Text1.Text = s1 * s2、

Case “/”

If s2 = 0 Then

MsgBox “分母不能為零!”

Text1.Text = “”

Else

Text1.Text = s1 / s2 End If End Select

Text1 = IIf(Left(Text1.Text, 1) = “。”, 0 & Text1.Text, Text1.Text) ‘

這個很關鍵,如果沒有這個的話,得出小於1的小數前面沒有0

End Sub

Private Sub Command4_Click()

If Text1.Text = “” Then ’文本為空就結束

Exit Sub

End If

Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) ‘文本退一格

End Sub

Private Sub Command5_Click()

Text1.Text = “” ’清除當前框內文本

End Sub

Private Sub Command6_Click(Index As Integer)

s1 = Val(Text1.Text) ‘將s1隱藏起來 ysf = Command6(Index).Caption

Text1.Text = “”

End Sub

Private Sub Command7_Click()

If Left(Text1.Text, 1) 《》 “-” Then ’判斷作為負數

Text1.Text = “-” & Text1.Text

Else

Text1.Text = Right(Text1.Text, Len(Text1.Text) - 1)

End If

End Sub

Private Sub Command8_Click()

Text1.Text = Text1.Text * Text1.Text ‘平方

End Sub

拓展資料

Visual Basic(VB)是由微軟公司開發的包含環境的事件驅動編程語言。它源自於BASIC編程語言。VB擁有圖形用戶界面(GUI)和快速應用程式開發(RAD)系統,可以輕易的使用DAO、RDO、ADO連接資料庫,或者輕松的創建ActiveX控件。程式員可以輕松地使用VB提供的組件快速創建一個應用程式。

參考鏈Visual Basic——百度百科接

WwW.※BaZHiSHi.Com

Q2:用c語言編寫一個簡單計算器程式

double a,b;
char c;
scanf("%lf%c%lf",&a,&c,&b);
switch(c)
{case +:printf("%g%c%g=%g",a,c,b,a+b);break; case -:printf("%g%c%g=%g",a,c,b,a-b);break; case *:printf("%g%c%g=%g",a,c,b,a*b);break; case /:b?printf("%g%c%g=%g",a,c,b,a/b):puts("error");break; default:printf("error");break;}

Q3:求用vb編寫一個簡易計算器的程式代碼

界面:
text1
1 2 3 +
4 5 6 -
7 8 9 *
0 = AC /
代碼:
dim newnum as boolean
dim n1,n2 op as integer
private sub ac_click()
text1.text=""
end sub
private sub command1_click(index as integer)
if newnum=true then text1=""
text1=text1&index
newnum=false
end sub
private sub command1_click(index as integer)
if op=0 then
n1=val(text1)
else
n2=val(text1)
select case op
case 1
text1=n1+n2
case 2
text1=n1-n2
case 3
text1=n1*n2
case 4
if n2<>0 then text1=n1/n2
end select
op=0
end if
if index>0 then op=index
n1=val(text1)
newnum=true
end sub

WWW.bAZH&Ishi.COM

Q4:用C語言編寫程式:一個簡單的計算器

#include

voidmain()

{

inta[4],b[4],c[4],i;

charop[4];

for(i=0;i<4;i++)

{

scanf("%1d%1c%1d",&a[i],&op[i],&b[i]);

}

for(i=0;i<4;i++)

{

switch(op[i])

{

case+:c[i]=a[i]+b[i];break;

case-:c[i]=a[i]-b[i];break;

case*:c[i]=a[i]*b[i];break;

case/:c[i]=a[i]/b[i];break;

}

printf("%d%c%d=%d\n",a[i],op[i],b[i],c[i]);

}

}

Q5:用vb編寫一個簡單計算器程式 可以進行基本四則基本運算即可 謝謝各位

------------------------------------------------------------------------
版權所有 (C) 1994 Microsoft Corporation

您可以免費以任何方式使用、修改、復制並分發您認為有用的
示例應用程式文件 (或任何修改過的版本)。Microsoft 對任何
示例應用程式文件不做任何保證,不負任何責任和義務。
------------------------------------------------------------------------
Option Explicit
Dim Op1, Op2 預先輸入操作數。
Dim DecimalFlag As Integer 小數點存在嗎?
Dim NumOps As Integer 操作數個數。
Dim LastInput 指示上一次按鍵事件的類型。
Dim OpFlag 指示未完成的操作。
Dim TempReadout
C (取消) 按鈕的 Click 事件過程
重新設置顯示並初始化變數。
Private Sub Cancel_Click()
Readout = Format(0, "0.")
Op1 = 0
Op2 = 0
Form_Load
End Sub
CE (取消輸入) 按鈕的 Click 事件過程。
Private Sub CancelEntry_Click()
Readout = Format(0, "0.")
DecimalFlag = False
LastInput = "CE"
End Sub
小數點 (.) 按鈕的 Click 事件過程
如果上一次按鍵為運算符,初始化 readout 為 "0.";
否則顯示時追加一個小數點。
Private Sub Decimal_Click()
If LastInput = "NEG" Then
Readout = Format(0, "-0.")
ElseIf LastInput <> "NUMS" Then
Readout = Format(0, "0.")
End If
DecimalFlag = True
LastInput = "NUMS"
End Sub
窗體的初始化過程
設置所有變數為其初始值。
Private Sub Form_Load()
DecimalFlag = False
NumOps = 0
LastInput = "NONE"
OpFlag = " "
Readout = Format(0, "0.")
Decimal.Caption = Format(0, ".")
End Sub
數字鍵 (0-9) 的 Click 事件過程
向顯示中的數追加新數。
Private Sub Number_Click(Index As Integer)
If LastInput <> "NUMS" Then
Readout = Format(0, ".")
DecimalFlag = False
End If
If DecimalFlag Then
Readout = Readout + Number(Index).Caption
Else
Readout = Left(Readout, InStr(Readout, Format(0, ".")) - 1) + Number(Index).Caption + Format(0, ".")
End If
If LastInput = "NEG" Then Readout = "-" & Readout
LastInput = "NUMS"
End Sub
運算符 (+, -, x, /, =) 的 Click 事件過程
如果接下來的按鍵是數字鍵,增加 NumOps。
如果有一個操作數,則設置 Op1。
如果有兩個操作數,則將 Op1 設置為 Op1 與
當前輸入字符串的運算結果,並顯示結果。
Private Sub Operator_Click(Index As Integer)
TempReadout = Readout
If LastInput = "NUMS" Then
NumOps = NumOps + 1、End If
Select Case NumOps
Case 0
If Operator(Index).Caption = "-" And LastInput <> "NEG" Then
Readout = "-" & Readout
LastInput = "NEG"
End If
Case 1、Op1 = Readout
If Operator(Index).Caption = "-" And LastInput <> "NUMS" And OpFlag <> "=" Then
Readout = "-"
LastInput = "NEG"
End If
Case 2、Op2 = TempReadout
Select Case OpFlag
Case "+"
Op1 = CDbl(Op1) + CDbl(Op2)
Case "-"
Op1 = CDbl(Op1) - CDbl(Op2)
Case "X"
Op1 = CDbl(Op1) * CDbl(Op2)
Case "/"
If Op2 = 0 Then
MsgBox "除數不能為零", 48, "計算器"
Else
Op1 = CDbl(Op1) / CDbl(Op2)
End If
Case "="
Op1 = CDbl(Op2)
Case "%"
Op1 = CDbl(Op1) * CDbl(Op2)
End Select
Readout = Op1、NumOps = 1、End Select
If LastInput <> "NEG" Then
LastInput = "OPS"
OpFlag = Operator(Index).Caption
End If
End Sub
百分比鍵 (%) 的 Click 事件過程
計算並顯示第一個操作數的百分數。
Private Sub Percent_Click()
Readout = Readout / 100
LastInput = "Ops"
OpFlag = "%"
NumOps = NumOps + 1、DecimalFlag = True
End Sub

Q6:編寫一個簡易計算器程式,根據用戶輸入的運算符做兩個數的加,減,乘或除運算

#include
main()
{double a,b; double result: char operator;printf("Pleasr input two number:");scanf("%f,%f",&a,&b);printf("please input the operator:");scanf(%c",&operator);if(opreator==+)printf("result=%d\n",a+b);else if(opreator==-)printf("result=%d\n",a-b);else if(opreator==*)printf("result=%d\n",a*b);else if(opreator==/)printf("result=%d\n",a/b);else printf("Wrong!");getch();}

Q7:求助用C#編寫一個簡單計算器程式

這個很簡單的就就是拖拽跟多的button過來
寫在他的事件就可以了
在用if條件判斷就可以了

猜你喜歡

更多