我自己写的第一个vb程序(原程序)请大家指点指点!帮我看看,有什么错的,对的?

earphone 2000-06-09 09:54:00
form1.Visual Basic Form File文件

VERSION 5.00
Begin VB.Form Form1
BorderStyle = 1 'Fixed Single
Caption = "计算器"
ClientHeight = 3480
ClientLeft = 5070
ClientTop = 2085
ClientWidth = 3960
LinkTopic = "Form1"
MaxButton = 0 'False
ScaleHeight = 3480
ScaleWidth = 3960
Begin VB.CommandButton cmd
Caption = "="
Height = 375
Left = 3120
TabIndex = 22
Top = 600
Width = 495
End
Begin VB.CommandButton CmdC
Caption = "C"
Height = 375
Left = 2280
TabIndex = 21
Top = 600
Width = 495
End
Begin VB.CommandButton cmdCE
Caption = "CE"
Height = 375
Left = 1440
TabIndex = 20
Top = 600
Width = 495
End
Begin VB.CommandButton Command2
Caption = "Backspace"
Height = 375
Left = 240
TabIndex = 19
Top = 600
Width = 975
End
Begin VB.TextBox Text1
Alignment = 1 'Right Justify
Height = 285
Left = 240
TabIndex = 0
Top = 120
Width = 3375
End
Begin VB.Frame Frame2
Height = 2175
Left = 2520
TabIndex = 12
Top = 1080
Width = 1215
Begin VB.CommandButton Command3
Caption = "/"
Height = 375
Index = 4
Left = 120
TabIndex = 16
Top = 1680
Width = 975
End
Begin VB.CommandButton Command3
Caption = "*"
Height = 375
Index = 3
Left = 120
TabIndex = 15
Top = 1200
Width = 975
End
Begin VB.CommandButton Command3
Caption = "-"
Height = 375
Index = 2
Left = 120
TabIndex = 14
Top = 720
Width = 975
End
Begin VB.CommandButton Command3
Caption = "+"
Height = 375
Index = 1
Left = 120
TabIndex = 13
Top = 240
Width = 975
End
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 375
Index = 9
Left = 1800
TabIndex = 11
Top = 2280
Width = 495
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 375
Index = 8
Left = 1080
TabIndex = 10
Top = 2280
Width = 495
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 375
Index = 7
Left = 360
TabIndex = 9
Top = 2280
Width = 495
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 375
Index = 6
Left = 1800
TabIndex = 8
Top = 1800
Width = 495
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 375
Index = 5
Left = 1080
TabIndex = 7
Top = 1800
Width = 495
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 375
Index = 4
Left = 360
TabIndex = 6
Top = 1800
Width = 495
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 375
Index = 3
Left = 1800
TabIndex = 5
Top = 1320
Width = 495
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 375
Index = 2
Left = 1080
TabIndex = 4
Top = 1320
Width = 495
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 375
Index = 1
Left = 360
TabIndex = 3
Top = 1320
Width = 495
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 375
Index = 0
Left = 360
TabIndex = 2
Top = 2760
Width = 495
End
Begin VB.Frame Frame1
Height = 2175
Left = 240
TabIndex = 1
Top = 1080
Width = 2175
Begin VB.CommandButton Command1
Caption = "."
Height = 375
Index = 11
Left = 1560
TabIndex = 18
Top = 1680
Width = 495
End
Begin VB.CommandButton Command1
Caption = "+/-"
Height = 375
Index = 10
Left = 840
MaskColor = &H80000000&
TabIndex = 17
Top = 1680
Width = 495
End
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim i As Integer
Dim var1, var2 As Double
Dim flag As String
Dim addindex As Integer
Dim jieguo As Variant
Dim fuhao1 As Integer

Private Sub cmd_Click()
yunsuan
Text1.Text = jieguo
var1 = jieguo
End Sub

Private Sub Command1_Click(index As Integer)
Select Case index
Case 0
addindex = index
fuhao
Case 1
addindex = index
fuhao
Case 2
addindex = index
fuhao
Case 3
addindex = index
fuhao
Case 4
addindex = index
fuhao
Case 5
addindex = index
fuhao
Case 6
addindex = index
fuhao
Case 7
addindex = index
fuhao
Case 8
addindex = index
fuhao
Case 9
addindex = index
fuhao
Case 10
zengfuhao
Case 11
If fuhao1 = 1 Then
var2 = Val(Text1.Text)
xiaoshudian2
Else
var1 = Val(Text1.Text)
xiaoshudian1
End If
End Select
End Sub

Private Sub Command3_Click(index As Integer)
var1 = Val(Text1.Text)
Select Case index
Case 1
flag = "+"
yunsuan1
Case 2
flag = "-"
yunsuan1
Case 3
flag = "*"
yunsuan1
Case 4
flag = "/"
yunsuan1
End Select
End Sub

Private Sub Cmdce_Click()
var1 = Val(Text1.Text)
var2 = 0
Text1.Text = 0
End Sub

Private Sub Cmdc_Click()
var1 = 0
var2 = 0
Text1.Text = 0
flag = 0
i = 0
jieguo = 0
fuhao1 = 0
End Sub

Private Sub Form_Load()
Text1.Text = 0
i = 0
For i = 0 To 9
Command1(i).Caption = i
Next
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode >= 48 And KeyCode <= 57 Or KeyCode = 8 Or KeyCode = 46 Then
Text1.Locked = False
Else
Text1.Locked = True
End If
End Sub

Sub fuhao()
If fuhao1 = 0 Then '判断是否点击运算符号
If flag <> "" Then
Text1.Text = 0 '如果点击运算符号文本框清0
fuhao1 = 1 '符值fuhao为是
End If
End If
If fuhao1 = 1 Then
add2
Else
add1
End If
End Sub

Sub add1()
If var1 = 0 Then
Text1.Text = addindex
var1 = Val(Text1.Text)
Else
Text1.Text = Text1.Text & addindex
End If
End Sub

Sub add2()
If var2 = 0 Then
Text1.Text = addindex
var2 = Val(Text1.Text)
Else
Text1.Text = Text1.Text & addindex
End If
End Sub

Sub yunsuan()
Select Case flag '判断运算符进行加减乘除运算
Case "+"
jieguo = var1 + var2
Case "-"
jieguo = var1 - var2
Case "*"
jieguo = var1 * var2
Case "/"
jieguo = var1 / var2
End Select
var2 = 0
End Sub

Sub yunsuan1()
If var2 <> 0 Then
yunsuan
Text1.Text = jieguo
var1 = Val(jieguo)
End If
End Sub

Function zengfuhao()
If Text1.Text > 0 Then
Text1.Text = -Val(Text1.Text)
Else
Text1.Text = -Val(Text1.Text)
End If
End Function

Sub xiaoshudian1()
Dim x, y As String
x = "."
y = Str$(var1)
If InStrRev(x, var1) <> x Then
var1 = var1 & x
Text1.Text = var1
End If
End Sub

Sub xiaoshudian2()
Dim x, y As String
x = "."
y = Str$(var2)
If InStrRev(x, var2) <> x Then
var2 = var2 & x
Text1.Text = var2
End If
End Sub



帮我看看,有什么错的,对的?

...全文
299 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
gogogo 2000-06-19
  • 打赏
  • 举报
回复
你的确实是初学者,看看你的select case语句就知道了,一样的内容就不要再写一遍,写这么多次,控件数组的优势都被你吃了,好差!
gogogo
Gansen 2000-06-19
  • 打赏
  • 举报
回复
Private Sub Command1_Click(index As Integer)
Select Case index
Case 0
addindex = index
fuhao
Case 1
addindex = index
fuhao
Case 2
addindex = index
fuhao
Case 3
addindex = index
fuhao
Case 4
addindex = index
fuhao
Case 5
addindex = index
fuhao
Case 6
addindex = index
fuhao
Case 7
addindex = index
fuhao
Case 8
addindex = index
fuhao
Case 9
addindex = index
fuhao
Case 10
zengfuhao
Case 11
If fuhao1 = 1 Then
var2 = Val(Text1.Text)
xiaoshudian2
Else
var1 = Val(Text1.Text)
xiaoshudian1
End If
End Select
End Sub
可以改为
Private Sub Command1_Click(index As Integer)
Select Case index
Case 0,1,2,3,4,5,6,7,8,9
addindex = index
fuhao
Case 10
zengfuhao
Case 11
If fuhao1 = 1 Then
var2 = Val(Text1.Text)
xiaoshudian2
Else
var1 = Val(Text1.Text)
xiaoshudian1
End If
End Select
End Sub

这样子好看多了。
Wonny 2000-06-19
  • 打赏
  • 举报
回复
第一次能写出这样的程序的确不错了。我想您应该也是第一次编程吧。其实一个语句能解决的就不必拉长到十几句(比如说 SELECT 程序段中的)。命名要规范一点,多用点英语其实也无所谓的,能看得了程序的人英语应该不是大问题,用拼音看起来实在太难受。正确与否运行一下便知。
Tyro 2000-06-19
  • 打赏
  • 举报
回复
主要是命名不规范
1 2000-06-19
  • 打赏
  • 举报
回复
我和你一样是初学者,希望能共同探讨。联系方法95828-61077
xuayo 2000-06-18
  • 打赏
  • 举报
回复
这种写法,叫人看的很累,最好培养以下自己的写程序习惯和vb的命名
Janven 2000-06-12
  • 打赏
  • 举报
回复
阁下的命名太不规范了,最好不要用VB默认的,程序一大起来,看都看不清,别说调试了。
yby 2000-06-10
  • 打赏
  • 举报
回复
感谢neulf,你的回答对我也有帮助。
zzj_mjz 2000-06-10
  • 打赏
  • 举报
回复
如想声明两个String变量,
Dim x,y as String 这样的写法是不对的,这样x是Variant类型的,应写成:
Dim x as String,y as String
neulf 2000-06-09
  • 打赏
  • 举报
回复
最大的错误就是编程习惯不好!不要用拼音,尽量用英文和英文词组!
aleck 2000-06-09
  • 打赏
  • 举报
回复
有没有错,运行一下不就知道了?
CCL 2000-06-09
  • 打赏
  • 举报
回复
强烈赞同tbmac
tbmac 2000-06-09
  • 打赏
  • 举报
回复
去问计算机

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧