📄 form1.cs
字号:
this.button2.Location = new System.Drawing.Point(552, 128);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(40, 23);
this.button2.TabIndex = 11;
this.button2.Text = "关闭";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// ds
//
this.ds.DataSetName = "NewDataSet";
this.ds.Locale = new System.Globalization.CultureInfo("zh-CN");
this.ds.Tables.AddRange(new System.Data.DataTable[] {
this.dataTable1,
this.dataTable2});
//
// dataTable1
//
this.dataTable1.TableName = "Table1";
//
// dataTable2
//
this.dataTable2.TableName = "Table2";
//
// splitter1
//
this.splitter1.Location = new System.Drawing.Point(0, 0);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(3, 549);
this.splitter1.TabIndex = 13;
this.splitter1.TabStop = false;
//
// listBox2
//
this.listBox2.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
this.listBox2.ColumnWidth = 170;
this.listBox2.HorizontalScrollbar = true;
this.listBox2.ItemHeight = 12;
this.listBox2.Location = new System.Drawing.Point(32, 160);
this.listBox2.MultiColumn = true;
this.listBox2.Name = "listBox2";
this.listBox2.Size = new System.Drawing.Size(928, 352);
this.listBox2.TabIndex = 16;
this.listBox2.SelectedIndexChanged += new System.EventHandler(this.listBox2_SelectedIndexChanged);
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(712, 16);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.TabIndex = 17;
this.richTextBox1.Text = "richTextBox1";
//
// button3
//
this.button3.Location = new System.Drawing.Point(624, 128);
this.button3.Name = "button3";
this.button3.TabIndex = 18;
this.button3.Text = "fuzhi";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// label13
//
this.label13.Location = new System.Drawing.Point(32, 56);
this.label13.Name = "label13";
this.label13.TabIndex = 2;
this.label13.Text = "输入松弛因子";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(176, 56);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(56, 21);
this.textBox2.TabIndex = 3;
this.textBox2.Text = "";
//
// label14
//
this.label14.Location = new System.Drawing.Point(816, 120);
this.label14.Name = "label14";
this.label14.TabIndex = 19;
this.label14.Text = "label14";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(944, 549);
this.Controls.Add(this.label14);
this.Controls.Add(this.button3);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.listBox2);
this.Controls.Add(this.splitter1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.label11);
this.Controls.Add(this.label10);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "松弛因子法";
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.ds)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataTable1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataTable2)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
/// <summary>
/// 初始化区定义属性值
/// </summary>
int n,i,l,gamafirst;
double [,]a;
double [ ]b;
double [ ]gama;
double [,]x;
string str;
FileStream fs =new FileStream("c:\\text.txt",FileMode.OpenOrCreate);
public void dingyi()
{
a = new double[n,n]; ///初始化数组A的值
b=new double[n]; ///B的初始化
gama=new double[]{1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9};
x=new double[n,1000];
int i,j;
for ( i =0 ;i<n;i++)
{
a[i,i]=-4;
}
for ( i =0 ;i<n-1;i++)
{
a[i,i+1]=1;
}
for ( i =0 ;i<n-1;i++)
{
a[i+1,i]=1;
}
b[0]=b[n-1]=-3;
for(j=1;j<n-1;j++)
{
b[j]=-2;
}
//==========初始化数据结束===========
for(int m =0;m<n;m++)
{
x[m,0]=1;
}
}
public void jisuan_sub2(double num2)
{
double gamatext=double.Parse(textBox2.Text ) ; ///设定的是松弛因子的值共9个
{
str="\n"+"当松弛因子为"+gamatext.ToString()+"时:";
listBox2.Items.Add("\n"+str);
for( l=0;l<999;l++) /// 设定的是0~n-1个循环,是设定x的循环值
{
//========================
x[0,l+1]=(1-gamatext)*x[0,l]+gamatext*(b[0]-sum1(0,1,n-1,l))/a[0,0]; //第一个式子的x1值
str ="第 "+(l+1).ToString()+" 次循环值";
listBox2.Items.Add(str);
str="x[1]=:"+x[0,l+1].ToString();
//richTextBox1.Text+=str;
listBox2.Items.Add(str);
for( i=1;i<n-1;i++) /// 设定的是2~n个循环,是设定x的下标值
{
x[i,l+1]=(1-gamatext)*x[i,l]+gamatext*(b[i]-sum1(i, 0,i-1,l)-sum1(i, i+1,n-1,l))/a[i,i];//i,j分别为a的下标
str = "x["+(i+1).ToString()+"]为:"+x[i,l+1].ToString();
//richTextBox1.Text+=str;
listBox2.Items.Add(str+"\n\r");
}
x[n-1,l+1]=(1-gamatext)*x[n-1,l]+gamatext*(b[n-1]-sum1(n-1, 1,n-2,l))/a[n-1,n-1]; //第一个式子的x1值
str ="x["+(n).ToString()+"]为:"+x[n-1,l+1].ToString();
// richTextBox1.Text+=str;
listBox2.Items.Add("\n"+str);
// listBox1.Items.Add(str);
if(Math.Abs(x[i,l]-x[i,l+1])< Math.Pow(10,-6))
{
label14.Text="k="+l.ToString();
break;
}
}
//listBox1.Items.Add(str);
//textBox2.Text=str;
}
}
public void jisuan_sub1(int num)
{ //==========初始化数据开始===========
for( gamafirst=0;gamafirst<9;gamafirst++) ///设定的是松弛因子的值共9个
{
str="\n"+"当松弛因子为"+gama[gamafirst].ToString()+"时:";
listBox2.Items.Add("\n"+str);
for( l=0;l<999;l++) /// 设定的是0~n-1个循环,是设定x的循环值
{
//========================
x[0,l+1]=(1-gama[gamafirst])*x[0,l]+gama[gamafirst]*(b[0]-sum1(0,1,n-1,l))/a[0,0]; //第一个式子的x1值
str ="第 "+(l+1).ToString()+" 次循环值";
listBox2.Items.Add(str);
str="x[1]=:"+x[0,l+1].ToString();
//richTextBox1.Text+=str;
listBox2.Items.Add(str);
for( i=1;i<n-1;i++) /// 设定的是2~n个循环,是设定x的下标值
{
x[i,l+1]=(1-gama[gamafirst])*x[i,l]+gama[gamafirst]*(b[i]-sum1(i, 0,i-1,l)-sum1(i, i+1,n-1,l))/a[i,i];//i,j分别为a的下标
str = "x["+(i+1).ToString()+"]为:"+x[i,l+1].ToString();
//richTextBox1.Text+=str;
listBox2.Items.Add(str+"\n\r");
}
x[n-1,l+1]=(1-gama[gamafirst])*x[n-1,l]+gama[gamafirst]*(b[n-1]-sum1(n-1, 1,n-2,l))/a[n-1,n-1]; //第一个式子的x1值
str ="x["+(n).ToString()+"]为:"+x[n-1,l+1].ToString();
// richTextBox1.Text+=str;
listBox2.Items.Add("\n"+str);
// listBox1.Items.Add(str);
if(Math.Abs(x[i,l]-x[i,l+1])< Math.Pow(10,-6))
{ label14.Text="k="+l.ToString();
break;
}
}
//listBox1.Items.Add(str);
//textBox2.Text=str;
}
}
public double sum1(int i ,int n ,int m,int p)//分别为|a的行|,|循环的开始量|,|循环的结束量|,x的循环值
{ double result=0;
for(int ii=n;ii<m;ii++)
{
result+=a[i,ii]*x[ii,p];
}
return(result);
}
private void button1_Click(object sender, System.EventArgs e)
{
listBox2.Items.Clear();
if(textBox1.Text!="")
{
n=int.Parse(textBox1.Text) ; ///设定阶数,有输入框输入
dingyi();
if(textBox2.Text=="")
{ jisuan_sub1(int.Parse(textBox1.Text ));}
else
{jisuan_sub2(double.Parse(textBox2.Text ));
// label14.Text=Math.Pow(10,-3).ToString();
}
}
else
{MessageBox.Show("输入框不能为空!");}
}
private void Form1_Load(object sender, System.EventArgs e)
{ listBox2.Width=this.Width-5;
listBox2.Height=this.Height-50;
}
private void button2_Click(object sender, System.EventArgs e)
{
Close();
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void listBox2_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void button3_Click(object sender, System.EventArgs e)
{
for(int iii=0;iii<listBox2.Items.Count-1;iii++)
{ listBox2.SelectedIndex=iii;
richTextBox1.Text+=listBox2.SelectedItem.ToString();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -