资源描述
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;
namespace 灵敏度分析尝试
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public bool jiansuo(double x, double[] y)
{
bool cunzai = false; ;
for (int i = 0; i < y.Length; i++)
if (y[i] == x)
cunzai = true;
return cunzai;
}
public double qiumax(List<double > array)
{
double max=-9999;
for (int i = 0; i <array.Count; i++)
{
if (array[i] > max)
max = array[i];
}
return max;
}
public double qiumin(List<double> array)
{
double min = 9999;
for (int i = 0; i <array.Count; i++)
{
if (array[i] < min)
min= array[i];
}
return min;
}
private void button1建立表格_Click(object sender, EventArgs e)
{
if (dataGridView1.Columns.Count > 0 || dataGridView2.Columns.Count > 0 || dataGridView3.Columns.Count > 0 || dataGridView4.Columns.Count > 0 || dataGridView5.Columns.Count > 0)
{
dataGridView1.Columns.Clear();
dataGridView2.Columns.Clear();
dataGridView3.Columns.Clear();
dataGridView4.Columns.Clear();
dataGridView5.Columns.Clear();
}
if (dataGridView1.Rows.Count > 0 || dataGridView2.Rows.Count > 0 || dataGridView4.Rows.Count > 0 || dataGridView5.Rows.Count > 0)
{
dataGridView1.Rows.Clear();
dataGridView2.Rows.Clear();
dataGridView3.Rows.Clear();
dataGridView4.Rows.Clear();
dataGridView2.Rows.Clear();
}
int m, n;
m = int.Parse(textBox1production.Text);//m=3
n = int.Parse(textBox2resource.Text);//n=3
for (int i = 1; i <= m + n; i++)
{
dataGridView5.Columns.Add(" ", "C" + i.ToString());
}
for (int i = 1; i <= m + n; i++)
{
dataGridView4.Columns.Add("", "X" + i.ToString());
}
dataGridView4.Rows.Add(n + 1);
dataGridView3.Columns.Add(" ", "C");
dataGridView1.Columns.Add("", "Cb");
dataGridView1.Columns.Add("", "Xb(下标)");
dataGridView1.Columns.Add("", "B-1b");
dataGridView1.Rows.Add(n);
dataGridView2.Columns.Add(" ", "C");
dataGridView2.Rows.Add(1);
}
private void splitContainer3_SplitterMoved(object sender, SplitterEventArgs e)
{
}
private void splitContainer6_Panel2_Paint(object sender, PaintEventArgs e)
{
}
private void button1产品_Click(object sender, EventArgs e)
{
double[] Xb = new double[dataGridView1.Rows.Count];//用于存放Xb的下标;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[1].Value != null)
Xb[i] = double.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString());
}
double[,] A = new double[dataGridView4.Rows.Count, dataGridView4.Columns.Count];//A矩阵存放后面的
for (int i = 0; i < dataGridView4.Rows.Count; i++)
for (int j = 0; j < dataGridView4.Columns.Count; j++)
{
if (dataGridView4.Rows[i].Cells[j].Value != null)
A[i, j] = double.Parse(dataGridView4.Rows[i].Cells[j].Value.ToString());
}
int pro=0;
if (textBox3输入产品.Text!="")
pro = int.Parse(textBox3输入产品.Text);
if (pro <= 0 || pro > int.Parse(textBox1production.Text)+int.Parse (textBox2resource .Text))
MessageBox.Show("输入的数据必须大于0小于产品总数+资源数!,请重新输入!");
else
{
if (jiansuo(pro, Xb))
{
List<double> zuo = new List<double>();
List<double> you = new List<double>();
double zuomax = 0, youmin = 0;
for (int j = 0; j < Xb.Length; j++)
{
if (pro == Xb[j])//pro是基变量,且第i列是非基变量
{
for (int i = 0; i < A.GetLength(1); i++)
{
if (jiansuo(i+1, Xb )==false )
{
double shang = 0;
if (A[j, i] > 0)
{
shang=A[A.GetLength(0) - 1, i] / A[j, i];
zuo.Add(Math.Round (shang,2)); //非基变量的检验数除以Cj(j就是pro)对应的变量Xj那一行
}
if (A[j, i] < 0)
{
shang = A[A.GetLength(0) - 1, i] / A[j, i];
you.Add(Math.Round (shang,2));
}
}
}
}
}
if (zuo.Count > 0)
zuomax = qiumax(zuo);
if (you.Count > 0)
youmin = qiumin(you);
if (zuo.Count == 0 && you.Count == 0)
textBox1产品结果.Text = "";
if (zuo.Count > 0 && you.Count == 0)
textBox1产品结果.Text = "产品" + pro + "的变化量>=" + Convert.ToString(zuomax);
if (zuo.Count == 0 && you.Count > 0)
textBox1产品结果.Text = "产品" + pro + "的变化量<=" + Convert.ToString(youmin);
if (zuo.Count > 0 && you.Count > 0)
textBox1产品结果.Text = "产品" + pro + "的变化范围为 " + Convert.ToString(zuomax) + "到" + Convert.ToString(youmin);
}
if (jiansuo(pro, Xb)==false )
{
int hangbiao=dataGridView4.Rows .Count-1;
if (dataGridView4.Rows[hangbiao].Cells[pro-1].Value != null)
{
double jieguo = -(double.Parse(dataGridView4.Rows[hangbiao].Cells[pro -1].Value.ToString()));
textBox1产品结果.Text = "产品" + pro + "的变化量<=" + Convert.ToString(jieguo);
}
}
}
}
private void button资源计算_Click(object sender, EventArgs e)
{
double[] B_1b = new double[dataGridView1.Rows.Count];//用于存放B-1b列;
for (int i = 0; i < dataGridView1.Rows.Count; i++) //给XbB_1b赋值
{
if (dataGridView1.Rows[i].Cells[2].Value != null)
B_1b[i] = double.Parse(dataGridView1.Rows[i].Cells[2].Value.ToString());
}
int Res = 0;
if (textBox3输入资源.Text != "")
Res = int.Parse(textBox3输入资源.Text);
if (Res <= 0 || Res > int.Parse(textBox2resource.Text))
MessageBox.Show("输入的数据必须大于0小于等于资源数!,请重新输入!");
else
{
double[] Xnaddi = new double[dataGridView4.Rows.Count-1];//用于存放Xnaddi列
for (int i = 0; i < dataGridView4.Rows.Count-1; i++)
if (dataGridView4.Rows[i].Cells[int.Parse(textBox1production.Text) + Res-1].Value != null)
Xnaddi[i] = double.Parse(dataGridView4.Rows[i].Cells[int.Parse(textBox1production.Text) + Res-1].Value.ToString());
double shang = 0;
List<double> zuo = new List<double>();
List<double> you = new List<double>();
double zuomax = 0, youmin = 0;
for (int i = 0; i < Xnaddi.Length; i++)
{
if (Xnaddi[i] > 0)
{
shang = -(B_1b[i]) / Xnaddi[i];
zuo.Add(Math.Round(shang, 2)); //非基变量的检验数除以Cj(j就是pro)对应的变量Xj那一行
}
if (Xnaddi[i ]< 0)
{
shang = -(B_1b[i]) / Xnaddi[i];
you.Add(Math.Round(shang, 2));
}
}
if (zuo.Count > 0)
zuomax = qiumax(zuo);
if (you.Count > 0)
youmin = qiumin(you);
if (zuo.Count == 0 && you.Count == 0)
textBox2资源结果.Text = "";
if (zuo.Count > 0 && you.Count == 0)
textBox2资源结果.Text = "资源" + Res + "的变化量>=" + Convert.ToString(zuomax);
if (zuo.Count == 0 && you.Count > 0)
textBox2资源结果.Text = "资源" + Res + "的变化量<=" + Convert.ToString(youmin);
if (zuo.Count > 0 && you.Count > 0)
textBox2资源结果.Text = "资源" + Res + "的变化范围为 " + Convert.ToString(zuomax) + "到" + Convert.ToString(youmin);
}
}
private void button1影子价格_Click(object sender, EventArgs e)
{
int m;
m = Convert.ToInt32(textBox1production.Text );
int n;
n = Convert.ToInt32(textBox2resource.Text );
double [] Aend = new double [dataGridView4.Columns.Count];//A矩阵存放后面的
for (int j = 0; j < dataGridView4.Columns.Count; j++)
if (dataGridView4.Rows[dataGridView4.Rows.Count - 1].Cells[j].Value != null)
Aend[j] = double.Parse(dataGridView4.Rows[dataGridView4.Rows.Count - 1].Cells[j].Value.ToString());
int resource = 0;
if (textBox1输入资源.Text != "")
{
resource = int.Parse(textBox1输入资源.Text);
if (resource <= 0 || resource > n)
MessageBox.Show("输入的数据必须大于0小于等于资源数!,请重新输入!");
if (resource > 0 && resource <= n)
{
int resource111 = resource + m-1;
double yinzi = Math.Abs(Aend[resource111]);
textBox2影子价格.Text = "资源" + resource111 + "的影子价格为" + Convert.ToString(yinzi);
}
}
}
private void textBox1影子价格_TextChanged(object sender, EventArgs e)
{
}
private void buttonNewSet_Click(object sender, EventArgs e)
{
if (dataGridView6.Rows.Count > 0 || dataGridView6.Columns.Count > 0)
{
dataGridView6.Rows.Clear();
dataGridView6.Columns.Clear();
}
dataGridView6.Columns.Add(" ", "单位利润 ");
for (int i = 1; i <= int.Parse(textBox2resource.Text); i++)
dataGridView6.Columns.Add(" ", "消耗资源 " + i.ToString());
dataGridView6.Rows.Add(1);
}
private void splitContainer9_Panel1_Paint(object sender, PaintEventArgs e)
{
}
private void buttonCount_Click(object sender, EventArgs e)
{
int m, n;
m = int.Parse(textBox1production.Text);//m=3
n = int.Parse(textBox2resource.Text);//n=3
double[] A = new double[n];//A矩阵存放后面的
for (int j = 0; j < n; j++)
if (dataGridView4.Rows[dataGridView4.Rows.Count - 1].Cells[j+m].Value != null)
A[j] = Math.Abs(double.Parse(dataGridView4.Rows[dataGridView4.Rows.Count - 1].Cells[j+m].Value.ToString()));
double[] XbN = new double[n+1];
for (int i = 0; i < n+1; i++)
{
if (dataGridView6.Rows[0].Cells[i].Value != null)
XbN[i] = double.Parse(dataGridView6.Rows[0].Cells[i].Value.ToString());
}
double z = XbN[0];
for (int i = 0; i < n; i++)
z = z- A[i] * XbN[i + 1];
if (z > 0)
textBox1.Text = "生产该商品盈利";
else
textBox1.Text = "生产该商品亏本";
}
private void button2_Click(object sender, EventArgs e)
{
int i = 0, j = 0;
if (textBox6i.Text != "" && textBox5j.Text != "")
{
i = int.Parse(textBox6i.Text);
j = int.Parse(textBox5j.Text);
}
if (j <= 0 || j > (int.Parse(textBox1production.Text) + int.Parse(textBox2resource.Text)) || i <= 0 || i > int.Parse(textBox2resource.Text))
MessageBox.Show("输入的产品必须大于0小于产品总数+资源数;输入的资源必须大于0小于资源数!,请输入正确的数字!");
else
{
double shang = 0;
double[] Azuihouyihang = new double[dataGridView4.Columns.Count];
for (int n = 0; n < dataGridView4.Columns.Count; n++)
{
if (dataGridView4.Rows[dataGridView4.Rows.Count - 1].Cells[n].Value != null)
Azuihouyihang[n] = double.Parse(dataGridView4.Rows[dataGridView4.Rows.Count - 1].Cells[n].Value.ToString());
}
double x = Azuihouyihang[int.Parse(textBox1production.Text) + i - 1];
double y = Azuihouyihang[j - 1];
if (Math.Abs(x) > 0)
{
i = int.Parse(textBox6i.Text);
j = int.Parse(textBox5j.Text);
shang = y / Math.Abs(x);
textBox4Aij结果.Text = "A" + i + j + "的变化量>=" + Convert.ToString(Math.Round(shang, 2));
i = int.Parse(textBox6i.Text);
j = int.Parse(textBox5j.Text);
}
else
textBox4Aij结果.Text = "A" + i + j + "的变化量为" + "R";
}
}
private void button1产品计算_Click(object sender, EventArgs e)
{
double[] Xb = new double[dataGridView1.Rows.Count];//用于存放Xb的下标;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[1].Value != null)
Xb[i] = double.Parse(dataGridView1.Rows[i].Cells[1].
展开阅读全文