📄 waterstatus.cs
字号:
namespace WaterStatus
{
using System;
using System.Data;
using System.Data.OleDb;
class WaterStatus
{
private const int WATERPOLNUM=9; //地表水污染物的种类数目 DO,COD,BOD5,NO2-N,石油类这五种
private const int MAXPOLNUM=9; //定义大气,地表水,海水,噪声这四个方面当中每一方面污染物种类数目的最大值
private const int POLGRADENUM=5; //污染划分的等级数
private const int MAXRECORDNUM=800;
private int RECORDNUM=0;
//private OleDbConnection Myconnection;
private int [] PlaceID;//这个数组存放的是表格中存放的各个采样地点的ID
private double [,] WaterRecord;//这个数组存放的是表格中存放的各个地区地表水污染物的测量值
private double [,] RMatrixWater;//这个数组存放的是地表水污染物的测量值 关于 五个污染等级 的隶属度
//其中,数组的第一维~第五维存放的分别是 DO,COD,BOD5,NO2-N,石油类这五种污染物关于五个污染等级的隶属度
private double [,] WaterPolStandard={ {15,15,20,30,40},
{1.0,1.0,1.0,1.5,1.5},
{0.005,0.05,0.2,0.2,0.2},
{0.00005,0.00005,0.0001,0.001,0.001},
{0.05,0.05,0.05,0.1,0.1},
{0.01,0.05,0.05,0.05,0.1},
{0.001,0.005,0.005,0.005,0.01},
{0.01,0.05,0.05,0.05,0.1},
{0.05,0.05,0.05,0.5,1.0} };
//这个数组的第一维~第五维存放的分别是DO,COD,BOD5,NO2-N,石油类这五种地表水污染物的 五个污染等级 的划分标准
private double [] WaterPolValue;//这个数组存放的是五种地表水污染物的测量值
private double [] WaterPolWeight;//这个数组存放的是五种地表水污染物的归一化权值
private double [] BWater;//这个数组存放的是对地表水污染情况的评估结果
private string WaterPolGrade;//地表水的污染等级
private string WaterMainPol;//地表水的主要污染物
private string WaterPol;//地表水的超标污染物
private void InitializePara()
{
int i,j,MinPolID=21,MaxPolID=29;
string connectstring,sql;
RMatrixWater=new double [WATERPOLNUM,POLGRADENUM];
//WaterPolStandard=new double [WATERPOLNUM,POLGRADENUM];
WaterPolValue=new double [WATERPOLNUM];
WaterPolWeight=new double [WATERPOLNUM];
BWater=new double [POLGRADENUM];
PlaceID=new int [MAXRECORDNUM];//这个数组存放的是表格中存放的各个采样地点的ID
WaterRecord=new double [MAXRECORDNUM,WATERPOLNUM];
connectstring=@"Data Source=d:\environment.mdb;"+"Provider=Microsoft.Jet.OLEDB.4.0;";
OleDbConnection connection=new OleDbConnection(connectstring);
try
{
connection.Open();
//Console.WriteLine("Connection Established");
sql="select FirstGrade,SecondGrade,ThirdGrade,FourthGrade,FifthGrade from waterpolstandard where PollutantID<="+MaxPolID+" and PollutantID>="+MinPolID+" order by PollutantID";
OleDbCommand command = new OleDbCommand(sql,connection);
OleDbDataReader datareader=command.ExecuteReader();
//Console.WriteLine("\n\n");
i=0;
while(datareader.Read())
{
for(j=0;j<POLGRADENUM;j++)WaterPolStandard[i,j]=(double)datareader[j];
i++;
}
connection.Close();
}
catch(OleDbException e)
{
Console.WriteLine("An error occured!");
Console.WriteLine("Your error message is"+e.Message);
Console.ReadLine();
}
catch(SystemException e)
{
Console.WriteLine("An error occured!");
Console.WriteLine("Your error message is"+e.Message);
Console.ReadLine();
}
catch(Exception e)
{
Console.WriteLine("An error occured!");
Console.WriteLine("Your error message is"+e.Message);
Console.ReadLine();
}
try
{
connection.Open();
//Console.WriteLine("Connection Established");
sql="select ID,化学需氧量,氟化物,氰化物,总汞,总砷,总铅,总镉,六价铬,石油类 from water order by ID";
OleDbCommand command = new OleDbCommand(sql,connection);
OleDbDataReader datareader=command.ExecuteReader();
//Console.WriteLine("\n\n");
i=0;
while(datareader.Read())
{
PlaceID[i]=(int)datareader[0];
for(j=0;j<WATERPOLNUM;j++)WaterRecord[i,j]=(double)datareader[j+1];
i++;
}
RECORDNUM=i;
connection.Close();
}
catch(OleDbException e)
{
Console.WriteLine("An error occured!");
Console.WriteLine("Your error message is"+e.Message);
Console.ReadLine();
}
catch(SystemException e)
{
Console.WriteLine("An error occured!");
Console.WriteLine("Your error message is"+e.Message);
Console.ReadLine();
}
catch(Exception e)
{
Console.WriteLine("An error occured!");
Console.WriteLine("Your error message is"+e.Message);
Console.ReadLine();
}
/*for(i=0;i<WATERPOLNUM;i++)
{
for(j=0;j<POLGRADENUM;j++)Console.Write(WaterPolStandard[i,j]+" ");
Console.Write("\n");
}*/
}
private void GetRelative()//计算某一方面(大气,地表水,海水,噪声)的每种污染物的测量值 对于 五个污染级别 的 隶属度
{//WATERPOLNUM:该方面(大气,地表水,海水,噪声)的污染物种类的数目 WaterPolValue:该方面的各种污染物的测量值
//WaterPolStandard:该方面的各种污染物的五个污染等级的划分标准 RMatrixWater:该方面的各种污染物关于五个污染等级的隶属度
//kindofpol:污染的类型(大气,地表水,海水,噪声)
int i,j;
for(i=0;i<WATERPOLNUM;i++)//计算该方面的几种污染物的测量值 对于 五个污染级别 的 隶属度 在取区间的时候,取的都是左闭右开的区间,形如: a <= x < b
for(j=0;j<POLGRADENUM;j++)
{
if(j==0)//计算该方面的几种污染物的测量值 对于 污染级别第1级 的 隶属度
{
if(WaterPolValue[i]>=WaterPolStandard[i,j+1])RMatrixWater[i,j]=0;
else
if(WaterPolValue[i]<WaterPolStandard[i,j])RMatrixWater[i,j]=1;
else RMatrixWater[i,j]=(WaterPolStandard[i,j+1]-WaterPolValue[i])/(WaterPolStandard[i,j+1]-WaterPolStandard[i,j]);
}
else
if(j==POLGRADENUM-1)//计算该方面的几种污染物的测量值 对于 污染级别第5级 的 隶属度
{
if(WaterPolValue[i]>=WaterPolStandard[i,j])RMatrixWater[i,j]=1;
else
if(WaterPolValue[i]<WaterPolStandard[i,j-1])RMatrixWater[i,j]=0;
else RMatrixWater[i,j]=(WaterPolValue[i]-WaterPolStandard[i,j-1])/(WaterPolStandard[i,j]-WaterPolStandard[i,j-1]);
}//计算该方面的几种污染物的测量值 对于 污染级别第2,3,4级 的 隶属度
else
{
if( (WaterPolValue[i]>=WaterPolStandard[i,j+1])||(WaterPolValue[i]<WaterPolStandard[i,j-1]) )RMatrixWater[i,j]=0;
else
if( (WaterPolValue[i]>=WaterPolStandard[i,j-1])&&(WaterPolValue[i]<WaterPolStandard[i,j]) )RMatrixWater[i,j]=(WaterPolValue[i]-WaterPolStandard[i,j-1])/(WaterPolStandard[i,j]-WaterPolStandard[i,j-1]);
else RMatrixWater[i,j]=(WaterPolStandard[i,j+1]-WaterPolValue[i])/(WaterPolStandard[i,j+1]-WaterPolStandard[i,j]);
}
}
}
private void GetWeight()//计算该方面每种污染物的归一化权值
{//WATERPOLNUM:该方面(大气,地表水,海水,噪声)的污染物种类的数目 PolValue:该方面的各种污染物的测量值
//PolWeight:该方面的各种污染物的归一化权值
//PolStandard:该方面的各种污染物的五个污染等级的划分标准 kindofpol:污染的类型(大气,地表水,海水,噪声)
int i,j,maxindex,pollutantnum=0,tmp;
double sum,tmpmax;int [] pollutant;
sum=0.0;pollutant=new int [WATERPOLNUM];
for(i=0;i<WATERPOLNUM;i++)//计算该方面各种污染物的权值 此后,数组PolValue存放的就是该方面的各种污染物的权值
{
WaterPolWeight[i]=WaterPolValue[i]/WaterPolStandard[i,2];//计算其他因子的权值
sum=sum+WaterPolWeight[i];
if(WaterPolValue[i]>=WaterPolStandard[i,2])//将含量超标的地表水污染物的序号记录在pollutant数组当中,pollutantnum记录的是含量超标的地表水污染物的数目
{
pollutant[pollutantnum]=i;
pollutantnum++;
}
}
if(sum==0.0){WaterMainPol="没有明显的污染物";WaterPol="没有明显的污染物";return;}
for(i=0;i<WATERPOLNUM;i++)WaterPolWeight[i]=WaterPolWeight[i]/sum;//对该方面各种污染物的权值进行归一化
if(pollutantnum==0){WaterMainPol="没有明显的污染物";WaterPol="没有明显的污染物";return;}
for(i=0;i<pollutantnum;i++)//根据权值对超标的污染物进行排序
{
for(j=0;j<pollutantnum-i;j++)
{
if(WaterPolWeight[pollutant[j]]<=WaterPolWeight[pollutant[j+1]])
{
tmp=pollutant[j];
pollutant[j]=pollutant[j+1];
pollutant[j+1]=tmp;
}
}
}
WaterPol="";
for(i=0;i<pollutantnum;i++)
{
switch(pollutant[i])
{
case 0:WaterPol=WaterPol+"化学需氧量 ";break;
case 1:WaterPol=WaterPol+"氟化物 ";break;
case 2:WaterPol=WaterPol+"氰化物 ";break;
case 3:WaterPol=WaterPol+"总汞 ";break;
case 4:WaterPol=WaterPol+"总砷 ";break;
case 5:WaterPol=WaterPol+"总铅 ";break;
case 6:WaterPol=WaterPol+"总镉 ";break;
case 7:WaterPol=WaterPol+"六价铬 ";break;
case 8:WaterPol=WaterPol+"石油类 ";break;
}
}
tmpmax=WaterPolWeight[0];maxindex=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -