用vb 把输入的10个数 从小到大排序
4个回答
推荐于2017-11-25
展开全部
在VB中演示过程冒泡法排序
要求把代码输入后能直接在VB环境中运行
在窗体中添加三个标签,一个按钮,一个框架在框架中添加一个文本框
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode < 48 Or KeyCode > 57 And KeyCode < 96 Or KeyCode > 150 And KeyCode <> 109 And _
KeyCode <> 189 Then Text1 = ""
End Sub
Private Sub Form_Load()
Label1(0).Visible = False
Label1(0).Alignment = 2
Label1(0).Top = 100
Label1(0).Left = 100
Label1(0).Height = 300
Label1(0).Width = 800
Label1(0).BackColor = QBColor(15)
Label1(0).FontSize = 12
Caption = "演示“起泡法”从小到大的排答睁序过程"
Top = 0
Left = 200
Width = 2 * Label1(0).Left + 12 * (Label1(0).Width + 100)
Height = 7000
Label2.Top = 5700
Label2.Left = 500
Label2.FontSize = 12
Label2.Width = Width - 2 * Label2.Left
Label2.WordWrap = True
Label2.AutoSize = True
a = Space(4) & "数从文本输入,每输完一个数后,按回车键,数的个数可变.排序开始之前所有"
a = a & "所有的数都是白底黑字).单击“开始”按钮,开始比较(红底)数与(黄底)数"
a = a & "若(红底)数大就交换两数的位置;否则,两数仍保持原位置.位置已确定不变的数为"
Label2 = a & "(底黑兄告字)."
Label3 = ""
Label3.FontSize = 15
Label3.Top = Label2.Top - 500
Label3.Left = 1300
Label3.AutoSize = True
Frame1.Top = Height - 2300
Frame1.Left = Width / 3
Frame1.Width = 2300
Frame1.Height = 850
Frame1.Caption = "输入-32768到32767的数"
Text1.Top = 300
Text1.Left = 500
Text1.Height = 370
Text1.Width = 1450
Text1.FontSize = 14
Text1.MaxLength = 6
Text1 = ""
Command1.Top = Frame1.Top + 200
Command1.Left = Frame1.Left + Frame1.Width + 100
Command1.Caption = "开始排序"
Command1.Enabled = False
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Static kx As Byte, ky As Byte
If KeyAscii = 13 Then
If Text1 <> "" Then
x = Val(Text1)
If x >= -32768 And x <= 32767 Then
If n = 0 Then
Label1(0) = x: Label1(0).Visible = True
Else
Load Label1(n): Label1(n) = x
Label1(n).Visible = True
kx = kx + 1
If kx Mod 12 = 0 Then ky = ky + 1: kx = 0
Label1(n).Left = Label1(0).Left + kx * (Label1(0).Width + 100)
Label1(n).Top = Label1(0).Top + ky * (Label1(0).Height + 100)
Command1.Enabled = True
End If
Text1 = ""羡举明: n = n + 1
End If
End If
End If
End Sub
要求把代码输入后能直接在VB环境中运行
在窗体中添加三个标签,一个按钮,一个框架在框架中添加一个文本框
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode < 48 Or KeyCode > 57 And KeyCode < 96 Or KeyCode > 150 And KeyCode <> 109 And _
KeyCode <> 189 Then Text1 = ""
End Sub
Private Sub Form_Load()
Label1(0).Visible = False
Label1(0).Alignment = 2
Label1(0).Top = 100
Label1(0).Left = 100
Label1(0).Height = 300
Label1(0).Width = 800
Label1(0).BackColor = QBColor(15)
Label1(0).FontSize = 12
Caption = "演示“起泡法”从小到大的排答睁序过程"
Top = 0
Left = 200
Width = 2 * Label1(0).Left + 12 * (Label1(0).Width + 100)
Height = 7000
Label2.Top = 5700
Label2.Left = 500
Label2.FontSize = 12
Label2.Width = Width - 2 * Label2.Left
Label2.WordWrap = True
Label2.AutoSize = True
a = Space(4) & "数从文本输入,每输完一个数后,按回车键,数的个数可变.排序开始之前所有"
a = a & "所有的数都是白底黑字).单击“开始”按钮,开始比较(红底)数与(黄底)数"
a = a & "若(红底)数大就交换两数的位置;否则,两数仍保持原位置.位置已确定不变的数为"
Label2 = a & "(底黑兄告字)."
Label3 = ""
Label3.FontSize = 15
Label3.Top = Label2.Top - 500
Label3.Left = 1300
Label3.AutoSize = True
Frame1.Top = Height - 2300
Frame1.Left = Width / 3
Frame1.Width = 2300
Frame1.Height = 850
Frame1.Caption = "输入-32768到32767的数"
Text1.Top = 300
Text1.Left = 500
Text1.Height = 370
Text1.Width = 1450
Text1.FontSize = 14
Text1.MaxLength = 6
Text1 = ""
Command1.Top = Frame1.Top + 200
Command1.Left = Frame1.Left + Frame1.Width + 100
Command1.Caption = "开始排序"
Command1.Enabled = False
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Static kx As Byte, ky As Byte
If KeyAscii = 13 Then
If Text1 <> "" Then
x = Val(Text1)
If x >= -32768 And x <= 32767 Then
If n = 0 Then
Label1(0) = x: Label1(0).Visible = True
Else
Load Label1(n): Label1(n) = x
Label1(n).Visible = True
kx = kx + 1
If kx Mod 12 = 0 Then ky = ky + 1: kx = 0
Label1(n).Left = Label1(0).Left + kx * (Label1(0).Width + 100)
Label1(n).Top = Label1(0).Top + ky * (Label1(0).Height + 100)
Command1.Enabled = True
End If
Text1 = ""羡举明: n = n + 1
End If
End If
End If
End Sub
2013-12-20
展开全部
Private Sub Form_Load()
''这是用VB给你一个冒泡最简单的例子,从大到毁简小排
Dim A(10) As Integer
Dim i, j, n, t As Integer
n = 10
Randomize
Debug.Print "初始的数组A"此物
For i = 1 To n
A(i) = Int(Rnd() * 5000)
Debug.Print A(i)
Next i
''''排序开始
For i = 1 To n - 1
For j = n To i + 1 Step -1
If A(j) > A(j - 1) Then'''判断a(j)与a(j-1)谁大,把大的放到前边
t = A(j)
A(j) = A(j - 1)
A(j - 1) = t
End If
Next j
Next i
''''森余液排序结束
Debug.Print "有序的数组A"
For i = 1 To n
Debug.Print A(i)
Next i
''如果要从小到大排就把"A(j) > A(j - 1)"改称"A(j) < A(j - 1)"
End Sub
''这是用VB给你一个冒泡最简单的例子,从大到毁简小排
Dim A(10) As Integer
Dim i, j, n, t As Integer
n = 10
Randomize
Debug.Print "初始的数组A"此物
For i = 1 To n
A(i) = Int(Rnd() * 5000)
Debug.Print A(i)
Next i
''''排序开始
For i = 1 To n - 1
For j = n To i + 1 Step -1
If A(j) > A(j - 1) Then'''判断a(j)与a(j-1)谁大,把大的放到前边
t = A(j)
A(j) = A(j - 1)
A(j - 1) = t
End If
Next j
Next i
''''森余液排序结束
Debug.Print "有序的数组A"
For i = 1 To n
Debug.Print A(i)
Next i
''如果要从小到大排就把"A(j) > A(j - 1)"改称"A(j) < A(j - 1)"
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-12-20
展开全部
冒泡法:
这是最原始,也是众所周知凳蠢的最慢昌神的算法了。他的名字的由来因为它的工作看来象是冒泡:
#include <iostream.h>
void BubbleSort(int* pData,int Count)
{
int iTemp;
for(int i=1;i<Count;i++)
{
for(int j=Count-1;j>=i;j--)
{
if(pData[j]<pData[j-1])
{
iTemp = pData[j-1];
pData[j-1] = pData[j];
pData[j] = iTemp;
}
}
}
}
void main()
{
int data[] = {10,9,8,7,6,5,4};
BubbleSort(data,7);
for (int i=0;i<7;i++)
cout<<data[i]<<" ";
cout<<"\n";
}
倒序(最糟情况)
第一轮:10,9,8,7->10,9,7,8->10,7,9,8->7,10,9,8(交换3次)
第二轮枣迅陪:7,10,9,8->7,10,8,9->7,8,10,9(交换2次)
第一轮:7,8,10,9->7,8,9,10(交换1次)
循环次数:6次
交换次数:6次
这是最原始,也是众所周知凳蠢的最慢昌神的算法了。他的名字的由来因为它的工作看来象是冒泡:
#include <iostream.h>
void BubbleSort(int* pData,int Count)
{
int iTemp;
for(int i=1;i<Count;i++)
{
for(int j=Count-1;j>=i;j--)
{
if(pData[j]<pData[j-1])
{
iTemp = pData[j-1];
pData[j-1] = pData[j];
pData[j] = iTemp;
}
}
}
}
void main()
{
int data[] = {10,9,8,7,6,5,4};
BubbleSort(data,7);
for (int i=0;i<7;i++)
cout<<data[i]<<" ";
cout<<"\n";
}
倒序(最糟情况)
第一轮:10,9,8,7->10,9,7,8->10,7,9,8->7,10,9,8(交换3次)
第二轮枣迅陪:7,10,9,8->7,10,8,9->7,8,10,9(交换2次)
第一轮:7,8,10,9->7,8,9,10(交换1次)
循环次数:6次
交换次数:6次
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-12-20
展开全部
冒配州泡,册祥最简单。
Dim a As String
Dim arr(1 To 10) As Integer
Dim i, j, t As Integer
Private Sub Command1_Click()
For i = 1 To 10
a = InputBox("请输入第" & i & "州卖搏个值")
arr(i) = Val(a)
Next i
For i = 1 To 9
For j = 1 To 9
If arr(j) > arr(j + 1) Then
t = arr(j + 1): arr(j + 1) = arr(j): arr(j) = t
End If
Next j
Next i
For i = 1 To 10
Print arr(i)
Next i
End Sub
Dim a As String
Dim arr(1 To 10) As Integer
Dim i, j, t As Integer
Private Sub Command1_Click()
For i = 1 To 10
a = InputBox("请输入第" & i & "州卖搏个值")
arr(i) = Val(a)
Next i
For i = 1 To 9
For j = 1 To 9
If arr(j) > arr(j + 1) Then
t = arr(j + 1): arr(j + 1) = arr(j): arr(j) = t
End If
Next j
Next i
For i = 1 To 10
Print arr(i)
Next i
End Sub
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询