数字化变电站应用程序,关于字典方面的应用内容。字典加队列方式进行相应的存储,操作内容。速度快啊....
前边两个程序先练练,后边的一个程序是实际内容。没调试。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
public class cache
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
Dictionary<ulong, List<string>> index = new Dictionary<ulong, List<string>>();
4
public cache()
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
6
7
}
8
public void add(int FactoryID, int TypeID, string Series)
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
10
ulong bi1 = FactoryID;
11![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
bi1=bi1 << 32;
13
bi1 += TypeID;
14![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
if(!index.ContainsKey(bi1))
16![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
17
index.Add(bi1,new List<string>());
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
}
20
index[bi1].Add(Series);
21![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
}
23![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
public List<string> GetData(int FactoryID, int TypeID)
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26
ulong bi1 = FactoryID;
27![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
bi1 = bi1 << 32;
29
bi1 += TypeID;
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
if (index.ContainsKey(bi1))
32![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
33
return index[bi1];
34![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
}
36
else
37![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
38
return null;
39
}
40
41
}
42![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
public class FactoryCache
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
Dictionary<string, int> index = new Dictionary<string, int>();
4![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
public void add(int FactoryID,string name)
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
7
8
if (!index.ContainsKey(bi1))
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
10
index.Add(name, FactoryID);
11![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
}
13
14![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
}
16![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
public int GetData(string name)
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
if (index.ContainsKey(name))
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
23
return index[bi1];
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
}
26
else
27![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28
return -1;
29
}
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
}
32![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
}
数字化变电站应用程序。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
class Program
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Field#region Field
4![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
//初始化用到的录用三次的数据.一开始首先录用三遍的数据,然后再进行相应的逻辑保护
6
static int init = 0;
7![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
//标记是否要进行相应的逻辑,隔一次进行一次保护逻辑
9
static int nu2=0;
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
//标记有多少个sno与id的对
12
static int num = 0;
13
14
//用来存储平均值。一个id对应一个平均值
15
static int[] intArray;
16![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
//用来表示进行隔一次进行相应的逻辑判断
18
static bool[] boolArray;
19![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
//sno与id的对应表
21
static Dictionary<int, int> myDic1 = new Dictionary<int, int>();
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
//存储320个值,key为id
24
static Dictionary<int, Queue<int>> myDic2 = new Dictionary<int, Queue<int>>();
25![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
static Queue<int> q = null;
27![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
#endregion
29![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
static void Main(string[] args)
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32
//bool []m=new bool [3];
33
//Console.WriteLine("{0},{1},{2}",m[0],m[2],m[1]);
34
}
35![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
37
/// 读取文件信息,初始化相应的dictionary,数组
38
/// </summary>
39
static void ReadFile()
40![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
41
FileStream fs = File.OpenRead("data.f");
42
byte[] myByte = new byte[6];
43
while (fs.Read(myByte, 0, 6) > 0)
44![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
45
int id = myByte[0] * 128 + myByte[1];
46
int sno = myByte[4] + 128 + myByte[5];
47
myDic1.Add(sno,id);
48
q=new Queue<int>();
49
//每个id都存有320个数据,初始化为0
50
for (int i = 0; i < 320; i++)
51![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
52
q.Enqueue(0);
53
}
54
myDic2.Add(id, q);
55
//一共多少个对
56
num++;
57
}
58
//初始化数组,数组初始化时为0.bool数组初始化时为False
59
intArray=new int[num];
60
boolArray = new bool[num];
61
fs.Close();
62
return;
63
}
64![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
66
/// 对相应的sno以及。收上来的四个数据进行相应的处理
67
/// </summary>
68
static void Data(int sno, int data1, int data2, int data3, int data4)
69![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
70
bool myBool;
71![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
//获取四个数字的平均值
73
int average = (data1 + data2 + data3 + data4) / 4;
74
75
//获取id,及id处的队列
76
int id=myDic1[sno];
77
Queue<int> q=myDic2[id];
78
intArray[id] = average;
79![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80
//如果nu进行了两遍,就把nu变为0。判断是否已经两遍。循环进行了两次判断。
81
if (nu2 == 2 * num)
82![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
83
nu2 = 0;
84
myBool = Logic(average, id);
85
}
86
else
87![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
88
myBool = Logic(average, id);
89
}
90![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91
if (myBool)
92![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
93
//移除末尾处的四个值
94
for (int i = 0; i < 4; i++)
95![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
96
q.Dequeue();
97
}
98![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
99
//向队列插入四个值
100
q.Enqueue(data1);
101
q.Enqueue(data2);
102
q.Enqueue(data3);
103
q.Enqueue(data4);
104
}
105
}
106![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
107
static bool Logic(int average,int id)
108![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
109![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
110
//初始化时先录三个数据,然后再进行相应的逻辑。
111
//int nu = 0;
112
if (init <= 3*num)
113![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
114
//初始化工作。先录三个数据
115
init++;
116
return true;
117
}
118
else
119![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
120
//判断是否已经出错
121
if (!boolArray[id])
122![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
123
if (nu2 < num)
124![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
125
nu2++;
126
return true;
127
}
128
else
129![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
130
nu2++;
131
//逻辑判断
132
return true;
133
}
134
}
135
else
136![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
137
nu2++;
138
return false;
139
}
140
}
141
}
142
}