vb电脑编程 编写一个程序,根据本金a、存款年数n和年利率p计算到期利息。提示:到期利息计

vb电脑编程 编写一个程序,根据本金a、存款年数n和年利率p计算到期利息。提示:到期利息计,第1张

'放四个text1-text4,四个label1-label4,一个command1,调整好大小和位置

'代码如下:

private sub form_load()

label1caption ="本金"

label2caption="存款年数"

label3caption="年利率"

label4caption="到期利息"

endsub

 

private sub command1_click()

dim a as single

dim n as integer

dim p as single

dim Lx as single

a=val(text1text)

n=val(text2text)

p=val(text3text)

Lx=a(1+p)^n-a

text4text=Lx

end sub

有什么疑问请留言。

Private Sub Command1_Click()

Text4Text = Text1Text  Text2Text  Text3Text

End Sub

Private Sub Command2_Click()

Text1Text = ""

Text2Text = ""

Text3Text = ""

Text4Text = ""

End Sub

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)

Text1Text = Text1Text & Command1(Index)Caption ’将command1的单击事件与文本框显示的内容连接

End Sub

Private Sub Command2_Click()

Text1Text = Text1Text + “。”

If (InStr(Text1Text, “。”) = 1) Then ‘第一位不能为小数

Text1Text = “”

End If

If InStr(Text1Text, “。”) 《 Len(Text1Text) Then ’防止出现两个小数点

Text1Text = Left

(Text1Text, Len(Text1Text) - 1)

End If

End Sub

Private Sub

Command3_Click()

s2 = Val(Text1Text) ‘开始加减乘除运算

Select Case ysf Case “+”

Text1Text = s1 + s2

Case “-”

Text1Text = s1 - s2

Case “”

Text1Text = s1 s2

Case “/”

If s2 = 0 Then

MsgBox “分母不能为零!”

Text1Text = “”

Else

Text1Text = s1 / s2 End If End Select

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

这个很关键,如果没有这个的话,得出小于1的小数前面没有0

End Sub

Private Sub Command4_Click()

If Text1Text = “” Then ’文本为空就结束

Exit Sub

End If

Text1Text = Left(Text1Text, Len(Text1Text) - 1) ‘文本退一格

End Sub

Private Sub Command5_Click()

Text1Text = “” ’清除当前框内文本

End Sub

Private Sub Command6_Click(Index As Integer)

s1 = Val(Text1Text) ‘将s1隐藏起来 ysf = Command6(Index)Caption

Text1Text = “”

End Sub

Private Sub Command7_Click()

If Left(Text1Text, 1) 《》 “-” Then ’判断作为负数

Text1Text = “-” & Text1Text

Else

Text1Text = Right(Text1Text, Len(Text1Text) - 1)

End If

End Sub

Private Sub Command8_Click()

Text1Text = Text1Text Text1Text ‘平方

End Sub

Visual Basic(VB)是由微软公司开发的包含环境的事件驱动编程语言。它源自于BASIC编程语言。VB拥有图形用户界面(GUI)和快速应用程序开发(RAD)系统,可以轻易的使用DAO、RDO、ADO连接数据库,或者轻松的创建ActiveX控件。程序员可以轻松地使用VB提供的组件快速创建一个应用程序。

参考链Visual Basic——百度百科接

Private Sub Command1_Click()

lblLX = txtA (1 + txtP) ^ txtN - txtA

End Sub

注:其中 txtA、txtP 和 txtN为输入本金、利率和存期的文本框,lblLX 为显示利息的标签。

运行界面为:

分类: 电脑/网络 >> 程序设计 >> 其他编程语言

解析:

其实比较简单啦,用一个窗体就可以实现啦!

我自己写的,你可以看看

Option Explicit

Dim strNumber As String

Dim strPoint As String

Dim dblNum1 As Double

Dim intOperator As Integer

'清除结果

Private Sub cmdGT_Click()

txtDisplayText = "0"

strNumber = ""

strPoint = ""

intOperator = 7

End Sub

'输入数字

Private Sub cmdNumber_Click(Index As Integer)

strNumber = strNumber & cmdNumber(Index)Caption

txtDisplayText = strNumber & strPoint

End Sub

Private Sub cmdOnOff_Click()

End

End Sub

'运算过程

Private Sub cmdOperator_Click(Index As Integer)

Dim dblnum2 As Double

'是第一次单击运算符时,将输入的值先赋给第一个数,否则赋值给第二个数进行运算

If intOperator = 7 Then

dblNum1 = CDbl(txtDisplayText)

Else

dblnum2 = CDbl(Val(txtDisplayText))

'根据输入的符号进行运算

'求普通运算

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

txtDisplayText = txtDisplayText & ""

End If

End Sub

Private Sub cmdOtherOper_Click(Index As Integer)

Dim dblNum As Double

'求平方根,平方,

dblNum = CDbl(Val(txtDisplayText))

Select Case Index

Case 0

'验证数据是否有效

If dblNum >= 0 Then

txtDisplayText = CStr(Sqr(dblNum))

Else

MsgBox "负数不能开平方根!", _

vbOKOnly + vbCritical, "开平方根错误"

End If

Case 1

txtDisplayText = CStr(dblNum ^ 2)

End Select

'判断是否为文本框中的数字加点

If Not txtDisplay Like "" Then

txtDisplayText = txtDisplayText & ""

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

cmdGTSetFocus

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 事件,因此在这里设置文本框的焦点

txtDisplaySetFocus

Case Else

KeyAscii = 0

End Select

End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)

txtDisplaySetFocus

End Sub

Private Sub Form_Load()

strNumber = ""

strPoint = ""

intOperator = 7

End Sub

Private Sub Command1_Click()

Dim h As Integer, c As Single

h = Val(InputBox("请输入上网时间", "输入"))

Select Case h

Case -1

End

Case Is < 10

c = 25

Case Is < 50

c = 2 h

Case Is < 100

c = 15 h

Case Is < 200

c = 1 h

Case Is > 200

c = 200

End Select

msgbox "上网费用为" & c & "元", vbOKOnly, "输出"

End Sub

以上就是关于vb电脑编程 编写一个程序,根据本金a、存款年数n和年利率p计算到期利息。提示:到期利息计全部的内容,包括:vb电脑编程 编写一个程序,根据本金a、存款年数n和年利率p计算到期利息。提示:到期利息计、VB课后题、用VB编写一个计算器程序的代码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10098065.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存