在4个TextBox中输入4个大小不同的数,将它们从大到小排序后的结果用Msgbox函数输出。
发布网友
发布时间:2022-05-16 20:20
我来回答
共4个回答
热心网友
时间:2024-03-01 15:45
Form1.cs内容:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
ArrayList al = new ArrayList();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
al.Clear();
int a = Convert.ToInt32(textBox1.Text);
int b = Convert.ToInt32(textBox2.Text);
int c = Convert.ToInt32(textBox3.Text);
int d = Convert.ToInt32(textBox4.Text);
al.Add(a);
al.Add(b);
al.Add(c);
al.Add(d);
al.Sort();
label1.Text = al[0] + "," + al[1] + "," + al[2] + "," + al[3];
}
}
}
Form1.Designer.cs 内容:
namespace WindowsFormsApplication2
{
partial class Form1
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(345, 314);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(72, 55);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 1;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(72, 94);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 21);
this.textBox2.TabIndex = 2;
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(72, 134);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(100, 21);
this.textBox3.TabIndex = 3;
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(72, 177);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(100, 21);
this.textBox4.TabIndex = 4;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(72, 242);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.TabIndex = 5;
this.label1.Text = "label1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(797, 546);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Label label1;
}
}
热心网友
时间:2024-03-01 15:46
取数的过程就算了,假设取到的四个数分别赋值给a,b,c,d四个变量,数据类型为double 则有:
sub swap(byref d1 as double,byref d2 as double)'交换数据,如果是vb6,可以去掉这个方法。
dim t as double
t=d1:d1=d2:d2=t
end sub
'简化的冒泡排序
function sort(byval a as double,byval b as double,byval c as double,byval d as double) as string
if a>b then swap a,b
if b>c then swap b,c
if c>d then swap c,d
if a>b then swap a,b
if b>c then swap b,c
if c>d then swap c,d
if a>b then swap a,b
if b>c then swap b,c
if c>d then swap c,d
sort = d &"," & c &"," & b &"," & a
end function
热心网友
时间:2024-03-01 15:46
分都没有~~调用的话onClick="doSort() js写的~~至于html 怎么写 应该会把
function doSort() {
nanExists = false;
var Array1=new Array();
Array1[0]=document.getElementById("s1").value;
Array1[1]=document.getElementById("s2").value;
Array1[2]=document.getElementById("s3").value;
Array1[3]=document.getElementById("s4").value;
for (var i = 0; i < Array1.length; i++) {
Array1[i] = parseInt(Array1[i], 10);
if (isNaN(Array1[i])) {
nanExists = true;
break;
}
}
Array1 = bubbleSort(Array1, 0, Array1.length - 1);
if (nanExists)
alert("请输如数字");
else
alert(resultString(Array1, 0));
}
function resultString(inputArray, num) {
if ((inputArray.length - 1) >= num)
return (inputArray[num] + "," + resultString(inputArray,(num + 1)));
else return "";
}
function bubbleSort(inputArray, start, rest) {
for (var i = rest - 1; i >= start; i--) {
for (var j = start; j <= i; j++) {
if (inputArray[j+1] > inputArray[j]) {
var tempValue = inputArray[j];
inputArray[j] = inputArray[j+1];
inputArray[j+1] = tempValue;
}
}
}
return inputArray;
}
热心网友
时间:2024-03-01 15:47
int main()
{
int i,j,t,a[10];
printf("Please input 10 integers:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<9;i++) /* 冒泡法排序 */
for(j=0;j<10-i-1;j++)
if(a[j]>a[j+1])
{t=a[j];/* 交换a[i]和a[j] */
a[j]=a[j+1];
a[j+1]=t;