原创--C#实例编程1
求椭圆的面积
使用的知识要点
1.errProvider控制,主要是其SetError方法
2.正则表达式,可参考Regular Expression
主程序页面:
面积的主算法:
double x=0,y=0;
//a 为长,b为宽
while(x<a)
{
y=y+b/a*Math.Sqrt(a*a-x*x)*c;
x=x+c;
}
y=4*y;
文本框的输入控制
长,宽的输入都得是整数,精度为浮点数
用正则表达式
Regex isno=new Regex("^(([0-9]+\\.[0-9]*[1-9]*[0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)){{QUOTE7487}}quot;);
完整代码
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace 经济管理模型
{
/// <summary>
/// tymj 的摘要说明。
/// </summary>
public class tymj : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.ErrorProvider errorProvider1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public tymj()
{
//
// 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()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(tymj));
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.listBox1 = new System.Windows.Forms.ListBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.label4 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.errorProvider1 = new System.Windows.Forms.ErrorProvider();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(16, 40);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(296, 208);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.Location = new System.Drawing.Point(168, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(105, 25);
this.label1.TabIndex = 1;
this.label1.Text = "求椭圆面积";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// listBox1
//
this.listBox1.BackColor = System.Drawing.Color.LemonChiffon;
this.listBox1.Location = new System.Drawing.Point(328, 40);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(144, 212);
this.listBox1.TabIndex = 2;
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 256);
this.label2.Name = "label2";
this.label2.TabIndex = 3;
this.label2.Text = "长:";
//
// label3
//
this.label3.Location = new System.Drawing.Point(16, 288);
this.label3.Name = "label3";
this.label3.TabIndex = 4;
this.label3.Text = "宽:";
//
// textBox1
//
this.textBox1.BackColor = System.Drawing.SystemColors.Info;
this.textBox1.Location = new System.Drawing.Point(136, 256);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(168, 20);
this.textBox1.TabIndex = 5;
this.textBox1.Text = "15";
//
// textBox2
//
this.textBox2.BackColor = System.Drawing.SystemColors.Info;
this.textBox2.Location = new System.Drawing.Point(136, 288);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(168, 20);
this.textBox2.TabIndex = 6;
this.textBox2.Text = "10";
//
// button1
//
this.button1.BackColor = System.Drawing.SystemColors.ActiveBorder;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Location = new System.Drawing.Point(352, 312);
this.button1.Name = "button1";
this.button1.TabIndex = 7;
this.button1.Text = "确定";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label4
//
this.label4.Location = new System.Drawing.Point(16, 320);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(104, 24);
this.label4.TabIndex = 8;
this.label4.Text = "精度:";
//
// textBox3
//
this.textBox3.BackColor = System.Drawing.SystemColors.Info;
this.textBox3.Location = new System.Drawing.Point(136, 320);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(168, 20);
this.textBox3.TabIndex = 9;
this.textBox3.Text = "0.01";
//
// label5
//
this.label5.Location = new System.Drawing.Point(328, 264);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(136, 40);
this.label5.TabIndex = 10;
this.label5.Text = "面积:0";
//
// errorProvider1
//
this.errorProvider1.ContainerControl = this;
//
// tymj
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(480, 341);
this.Controls.Add(this.label5);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.label4);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.pictureBox1);
this.Name = "tymj";
this.Text = "求椭圆的面积";
this.ResumeLayout(false);
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
Regex isno=new Regex("^(([0-9]+\\.[0-9]*[1-9]*[0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)){{CODE7487}}quot;);
if (this.textBox1.Text=="")
this.errorProvider1.SetError(this.textBox1,"没有输入数据");
else if(this.textBox2.Text=="")
this.errorProvider1.SetError(this.textBox2,"没有输入数据");
else if(this.textBox3.Text=="")
this.errorProvider1.SetError(this.textBox3,"没有输入数据");
else if (!isno.IsMatch(this.textBox1.Text))
this.errorProvider1.SetError(this.textBox1,"输入的不是数字");
else if (!isno.IsMatch(this.textBox2.Text))
this.errorProvider1.SetError(this.textBox2,"输入的不是数字");
else if (!isno.IsMatch(this.textBox3.Text))
this.errorProvider1.SetError(this.textBox3,"输入的不是数字");
else
{
this.errorProvider1.SetError(this.textBox1,"");
this.errorProvider1.SetError(this.textBox2,"");
this.errorProvider1.SetError(this.textBox3,"");
this.Calculate(Convert.ToDouble(this.textBox1.Text),Convert.ToDouble(this.textBox2.Text),Convert.ToDouble(this.textBox3.Text));
}
}
private void Calculate(double a,double b,double c)
{
double x=0,y=0;
//x=x+c;
while(x<a)
{
y=y+b/a*Math.Sqrt(a*a-x*x)*c;
x=x+c;
}
y=4*y;
this.label5.Text="面积:"+y.ToString();
this.listBox1.Items.Add("精度:"+c.ToString());
this.listBox1.Items.Add("面积:"+y.ToString());
}
}
}