北影新生:c#和VB.net问提(急)

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/05 04:45:57
这样用c#或VB.net实现
1。在textBox1控件中输入一个数
2。在textBox2控件中输入任意多个数用“-”隔开
3。把大于textBox1控件中的数从小到大进行排序在textBox3控件中(数与数用“-”隔开)
4。把小于textBox1控件中的数从小到大进行排序并放在大于textBox1控件中的数的排序的后面(数与数用“-”隔开)
谢谢那位大哥帮帮我,我才学,点都不懂
如果可以请解释每步好不

在把textBox3控件中第1个与第2个,第2个与第3个,第3个与第4,第4个与第5个.......(也就是相邻2个数)个的所有数相减取绝对值,把所有绝对值相加除以textBox3中数的个数

最好告诉QQ号吗,我想在以后学习中请教一下你们
我的QQ是43206810
谢谢
想问问你们学习时用的什么资料
我没钱买书就只好在网上找了,但是看不懂

/*
*楼主可以copy过去运行...主要的代码都在比较下面,这里发了
*排版都破坏了 真郁闷
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txt1;
private System.Windows.Forms.TextBox txt2;
private System.Windows.Forms.TextBox txtXiao;
private System.Windows.Forms.Button btnJiSuan;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Label label8;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.txt1 = new System.Windows.Forms.TextBox();
this.txt2 = new System.Windows.Forms.TextBox();
this.txtXiao = new System.Windows.Forms.TextBox();
this.btnJiSuan = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// txt1
//
this.txt1.Location = new System.Drawing.Point(104, 24);
this.txt1.Name = "txt1";
this.txt1.Size = new System.Drawing.Size(160, 21);
this.txt1.TabIndex = 0;
this.txt1.Text = "10";
//
// txt2
//
this.txt2.Location = new System.Drawing.Point(104, 80);
this.txt2.Name = "txt2";
this.txt2.Size = new System.Drawing.Size(160, 21);
this.txt2.TabIndex = 1;
this.txt2.Text = "1-4-2-100-45-90";
//
// txtXiao
//
this.txtXiao.Location = new System.Drawing.Point(104, 192);
this.txtXiao.Name = "txtXiao";
this.txtXiao.Size = new System.Drawing.Size(160, 21);
this.txtXiao.TabIndex = 2;
this.txtXiao.Text = "";
//
// btnJiSuan
//
this.btnJiSuan.Location = new System.Drawing.Point(216, 224);
this.btnJiSuan.Name = "btnJiSuan";
this.btnJiSuan.TabIndex = 5;
this.btnJiSuan.Text = "计算";
this.btnJiSuan.Click += new System.EventHandler(this.btnJiSuan_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(280, 16);
this.label1.TabIndex = 6;
this.label1.Text = "在textBox1控件中输入一个数";
this.label1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// label2
//
this.label2.Location = new System.Drawing.Point(0, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(288, 23);
this.label2.TabIndex = 7;
this.label2.Text = "在textBox2控件中输入任意多个数用“-”隔开";
this.label2.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// label3
//
this.label3.Location = new System.Drawing.Point(0, 112);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(288, 32);
this.label3.TabIndex = 8;
this.label3.Text = "3。把大于textBox1控件中的数从小到大进行排序在textBox3控件中(数与数用“-”隔开) \r\n";
this.label3.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// label4
//
this.label4.Location = new System.Drawing.Point(0, 144);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(288, 40);
this.label4.TabIndex = 9;
this.label4.Text = "4。把小于textBox1控件中的数从小到大进行排序并放在大于textBox1控件中的数的排序的后面(数与数用“-”隔开)";
this.label4.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// label5
//
this.label5.Location = new System.Drawing.Point(8, 224);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(208, 32);
this.label5.TabIndex = 10;
this.label5.Text = "说明:如果与第一个值一样,将放到最后面,本程序暂时不处理相等的数字";
//
// label6
//
this.label6.Location = new System.Drawing.Point(24, 24);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(56, 16);
this.label6.TabIndex = 11;
this.label6.Text = "第一个值";
//
// label7
//
this.label7.Location = new System.Drawing.Point(16, 80);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(88, 32);
this.label7.TabIndex = 12;
this.label7.Text = "要处理的值,请使用这个格式";
//
// label8
//
this.label8.Location = new System.Drawing.Point(24, 192);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(64, 23);
this.label8.TabIndex = 13;
this.label8.Text = "处理之后";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 256);
this.Controls.Add(this.label8);
this.Controls.Add(this.label7);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnJiSuan);
this.Controls.Add(this.txtXiao);
this.Controls.Add(this.txt2);
this.Controls.Add(this.txt1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

//********************************************************************/
//*****************************主要代码段*****************************/
//********************************************************************/
//********************************************************************/
//********************************************************************/
/// <summary>
/// 处理格式化的字符串,将其转为数组
/// </summary>
/// <param name="str">使用'-'分隔的数字序列</param>
/// <returns>int []</returns>
private int [] ZhuanHua(string str)
{
string [] strTmp = str.Split('-');
int [] num = new int[strTmp.Length];
for(int i = 0; i < strTmp.Length; i++)
{
num[i] = Convert.ToInt32(strTmp[i]);
}
return num;
}

/// <summary>
/// 排序 从小到大 暂时用插入排序,排序算法可以自己就该为其他的
/// </summary>
/// <param name="num">要排序的int []</param>
/// <returns>排序后的 int []</returns>
public int [] PaiXu(int [] num)
{
int iIn, iOut;
for(iOut = 1; iOut < num.Length; iOut++)
{
int temp = num[iOut];
iIn = iOut;
while(iIn > 0 && num[iIn - 1] >= temp)
{
num[iIn] = num[iIn - 1];
--iIn;
}
num[iIn] = temp;
}
return num;
}

/// <summary>
/// 比较大小
/// </summary>
/// <param name="snum">int 要进行比较的第一个数</param>
/// <param name="num">要比较的数字组</param>
/// <returns></returns>
public int [] BiJiao(int snum, int [] num)
{
int [] rtnNum = new int[num.Length];
int post = 0;
for(int i = 0; i < num.Length; i++)
{
if (num[i] > snum)
{
post = i; //找到大于snum的数字的位置
break;
}
}
int j = 0;
//将大于snum的值放到数组的前面
for (int i = post; i < num.Length; i++)
{
rtnNum[j++] = num[i];
}

//将小于snum的值放于大于snum的值后,并且这里没有对== snum的值进行处理进行,
//所以自然都放到小于snum的值后面了
for (int i = 0; i < post; i++)
{
rtnNum[j++] = num[i];
}
return rtnNum;
}

/// <summary>
/// 格式化输出
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public string NiZhuan(int [] num)
{

System.Text.StringBuilder strBud = new System.Text.StringBuilder();
for(int i = 0; i < num.Length; i++)
{
strBud.Append(num[i].ToString());
if(i != num.Length - 1)
strBud.Append("-");
}
return strBud.ToString();
}

/// <summary>
/// 计算
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public float JiSuan(int [] num)
{
int [] iTmp = new int[num.Length - 1];
for(int i = 0; i < num.Length; i++)
{
if (i + 1 == num.Length)
break;
iTmp[i] = Math.Abs(num[i] - num[i + 1]); //计算两两相减的绝对值
}
int iSum = 0;
for(int i = 0; i < iTmp.Length; i++)
{
iSum += iTmp[i]; //相加
}
return (float)iSum / num.Length;
}

private void btnJiSuan_Click(object sender, System.EventArgs e)
{
//转换这些格式化的字符串为数组
int [] iZhuanHuan = this.ZhuanHua(this.txt2.Text);
//对其进行排序 这里是从小到大
iZhuanHuan = this.PaiXu(iZhuanHuan);
//比较这里数字,然后输入符合要求的数组
iZhuanHuan = this.BiJiao(Convert.ToInt32(this.txt1.Text), iZhuanHuan);
//格式化数组 输入到文本框
this.txtXiao.Text = this.NiZhuan(iZhuanHuan);
MessageBox.Show("把textBox3控件中第1个与第2个,第2个与第3个,第3个与第4,第4个与第5个.......(也就是相邻2个数)个的所有数相减取绝对值,把所有绝对值相加除以textBox3中数的个数");
//计算...
MessageBox.Show("结果为:" + this.JiSuan(iZhuanHuan).ToString());
}
}
}

你可以考虑先把textbox里面的值取出来,然后通过字符串函数,比如对比,定位,判断 ‘-’ 的位置,然后取出数字,再进行运算