【C#】20. AssocMatrix:输出矩阵至Excel

之前对于AssocMatrix的理解有误,修改了一下原来的代码:根据输入的行和列key,可以找到对应的元素!

using System;using System.Collections.Generic;using System.Collections;using System.Linq;using System.Text;namespace FinMktReverseEngineering{//1. Set#region Set 类型public class Set<T> : IEnumerable<T>{// 成员protected Dictionary<T, int> dict;//构造器1public Set(){dict = new Dictionary<T, int>();}//构造器2public Set(Set<T> set){dict = new Dictionary<T, int>();foreach (T key in set.dict.Keys){dict[key]=0;}}//构造器3public Set(Dictionary<T,int> Dict){this.dict = new Dictionary<T, int>(Dict);}//GetEnumerator迭代器IEnumerator<T> IEnumerable<T>.GetEnumerator(){foreach (KeyValuePair<T, int> item in dict){yield return item.Key;}}// 【修改】IEnumerator IEnumerable.GetEnumerator(){foreach (KeyValuePair<T, int> item in dict){yield return item.Key;}}//Insert方法public void Insert(T key){dict[key]=0;}//Remove方法public void Remove(T key){dict.Remove(key);}//Contains方法public bool Contains(T key){if (dict.ContainsKey(key)){return true;}else return false;}//Replace方法public void Replace(T key1,T key2){foreach (T key in dict.Keys){if (key.Equals(key1)){Remove(key1);Insert(key2);break;}}}//Intersection方法public static Set<T> Intersection(Set<T> s1, Set<T> s2){Set<T> setIntsec = new Set<T>();foreach (T key1 in s1.dict.Keys){foreach (T key2 in s2.dict.Keys){if (key1.Equals(key2)) setIntsec.Insert(key1);}}return setIntsec;}//Union方法public static Set<T> Union(Set<T> s1, Set<T> s2){Set<T> setUnion = new Set<T>(s1);foreach (T key2 in s2.dict.Keys){setUnion.Insert(key2);}return setUnion;}//Difference方法public static Set<T> Difference(Set<T> s1, Set<T> s2){Set<T> setDiff = new Set<T>(s1);foreach (T key1 in s1.dict.Keys){foreach (T key2 in s2.dict.Keys){if (key1.Equals(key2)) setDiff.Remove(key1);}}return setDiff;}//SymmetricDifference方法public static Set<T> SymmetricDifference(Set<T> s1, Set<T> s2){Set<T> setSymDiff = Set<T>.Union(s1,s2);foreach (T key1 in s1.dict.Keys){foreach (T key2 in s2.dict.Keys){if (key1.Equals(key2)) setSymDiff.Remove(key1);}}return setSymDiff;}//print()方法public void print(){foreach (KeyValuePair<T,int> item in dict){Console.Write("{0}, ", item.Key);}Console.WriteLine();}//Size()方法public int Size(){return dict.Count;}//count属性public int count{get { return dict.Count; }}//是否是空集public bool IsEmpty{get { return dict.Count==0?true:false; }}//两个集合是否相同public bool IsEqual(Set<T> newSet){if (this.dict.Count!=newSet.count){return false;}else{foreach (T key in newSet){if (!this.dict.ContainsKey(key)) { return false; }}return true;}}}#endregion//2. AssocArray#region AssocArray 类型public class AssocArray<Key, Value> : IEnumerable<KeyValuePair<Key, Value>>{//成员Dictionary<Key, Value> str; //这个字典的主键数量可能比Set中的要大。Set<Key> keys;// 构造器1:Default constructorpublic AssocArray(){str = new Dictionary<Key, Value>();keys = new Set<Key>();}// 构造器2:Copy constructorpublic AssocArray(AssocArray<Key, Value> arr2){str = new Dictionary<Key, Value>(arr2.str);// Dictionary的copy构造器keys = new Set<Key>(arr2.keys);}// 构造器3:初始化关键字和值public AssocArray(Set<Key> keys, Value val){this.keys = keys;str = new Dictionary<Key, Value>();foreach (Key k in this.keys){str[k] = val;}}// 构造器4public AssocArray(Set<Key> keys, Dictionary<Key, Value> str){this.keys = keys;this.str = str;}//this索引public Value this[Key k] // k in Set keys{set{keys.Insert(k);str[k] = value; //str Dictionary}get { return str[k]; }}//Keys Set 属性public Set<Key> Keys{set { this.keys = value; }get { return keys; }}//Dictionary 属性public Dictionary<Key,Value> Dict{set { this.str = value; }get { return this.str; }}//Count 属性【只读】public int Count{get { return keys.count; }}//在console中打印public void print(){foreach (Key key in keys){if (str.ContainsKey(key)){Console.Write("{0}, {1} ", key, str[key]);}}Console.WriteLine();}// 返回Dictionary和Set中都有的主键对应的keyvaluepairIEnumerator<KeyValuePair<Key, Value>> IEnumerable<KeyValuePair<Key, Value>>.GetEnumerator(){for (int i = 0; i < this.str.Count; i++){yield return this.str.ElementAt(i);}}IEnumerator IEnumerable.GetEnumerator(){for (int i = 0; i < this.keys.count; i++){yield return this.str.ElementAt(i);}}}#endregion//3. AssocMatrix#region AssocMatrix 类型public class AssocMatrix<K1, K2, V>{// The essential data, public for conveniencepublic NumericMatrix<V> numericmat; // The real datapublic AssocArray<K1, int> r; // Rowspublic AssocArray<K2, int> c; // Columns// Redundant information for performancepublic Set<K1> keyRows;public Set<K2> keyColumns;//1. 构造函数public AssocMatrix(AssocArray<K1, int> rArray, AssocArray<K2, int> cArray, NumericMatrix<V> NMat){if (rArray.Count == NMat.Rows && cArray.Count == NMat.Columns){this.r = rArray;this.c = cArray;this.numericmat = NMat;this.keyRows = new Set<K1>(rArray.Keys);this.keyColumns = new Set<K2>(cArray.Keys);int i = NMat.MinRowIndex;// r c 分别与numericmat相关联!# region 关联foreach (K1 key1 in keyRows){r[key1] = i++;}int j = NMat.MinColumnIndex;foreach (K2 key2 in keyColumns){c[key2] = j++;}#endregion}else { Console.WriteLine("AssocMatrix 构建失败:因为对应的行或者列的数量不相等"); }}//2 返回numMatrix元素public V this[int posr, int posc]{ // User-defined positionget{return numericmat[posr, posc];}set{numericmat[posr, posc] = value;}}public V this[K1 keyR, K2 keyC]{ // User-defined positionget{int posr, posc;posr = r[keyR];posc = c[keyC];return numericmat[posr, posc];}set{int posr, posc;posr = r[keyR];posc = c[keyC];numericmat[posr, posc] = value;}}//3. 打印行已经对应的序号public void PrintRowKeys(){foreach (KeyValuePair<K1,int> kvp in r){Console.WriteLine("{0} : {1}", kvp.Key, kvp.Value);}}//4. 打印列已经对应的序号public void PrintColKeys(){foreach (KeyValuePair<K2, int> kvp in c){Console.WriteLine("{0} : {1}", kvp.Key, kvp.Value);}}//5. 打印整个矩阵public void PrintAllElements(){//打印列标题Console.Write("\t");foreach (K2 kc in c.Keys){Console.Write("{0}\t", kc.ToString());}Console.WriteLine();//打印行标题和每个元素foreach (K1 kr in r.Keys){Console.Write("{0}:\t",kr.ToString());foreach (K2 kc in c.Keys){Console.Write("{0}\t",this[kr,kc].ToString());}Console.WriteLine();}}//***********************************************************************************//2. 得到行标题rowpublic K1 getRowKey(int row){return r.Keys.ElementAt(row);}//3. 得到列标题colpublic K2 getColumnKey(int col){return c.Keys.ElementAt(col );}//4. 属性numMatMinRowIndexpublic int numMatMinRowIndex{get { return numericmat.MinRowIndex; }}//5. 属性numMatMinColumnIndexpublic int numMatMinColumnIndex{get { return numericmat.MinColumnIndex; }}//6. 属性numMatMaxRowIndexpublic int numMatMaxRowIndex{get { return numericmat.MaxRowIndex; }}//7. 属性numMatMaxColumnIndexpublic int numMatMaxColumnIndex{get { return numericmat.MaxColumnIndex; }}//8. 函数Vector<V> rowVectorpublic Array<V> rowVector(int numrow){return (Vector<V>)numericmat.getRow(numrow);}//9. 函数Vector<V> columnVectorpublic Array<V> columnVector(int numcol){return numericmat.getColumn(numcol);}}#endregion}using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;using System.Globalization;namespace FinMktReverseEngineering{class test{static void Main(string[] args){// 先生成集合Set<string> set1 = new Set<string>();set1.Insert("A1");set1.Insert("A2");set1.Insert("A3");set1.Insert("A4");Set<string> set2 = new Set<string>();set2.Insert("B1"); set2.Insert("B2"); set2.Insert("B3");// 通过集合生成AssocArrayAssocArray<string, int> aa1 = new AssocArray<string, int>(set1,0);AssocArray<string, int> aa2 = new AssocArray<string, int>(set2, 0);// 通过AssocArray和NumericaMatrix生成AssocMatrixNumericMatrix<double> nmat= new NumericMatrix<double>(4,3,0,0);for (int i = nmat.MinRowIndex; i <=nmat.MaxRowIndex; i++){for (int j = nmat.MinColumnIndex; j <= nmat.MaxColumnIndex; j++){nmat[i,j]=i*j/2.0+3;}}AssocMatrix<string, string, double> am = new AssocMatrix<string, string, double>(aa1, aa2, nmat);// 打印出来行、列(AssocArray中对应的序号)以及所有元素Console.WriteLine("行Key以及序号");am.PrintRowKeys();Console.WriteLine("列Key以及序号");am.PrintColKeys();Console.WriteLine("打印所有元素以及标题");am.PrintAllElements();ExcelDriver exl = new ExcelDriver();exl.AddAssocMatrix<string, string, double>(am, "打印矩阵工作表");exl.MakeVisible(true);Console.ReadLine();}}}

,痛苦留给的一切,请细加回味!苦难一经过去,苦难就变为甘美。

【C#】20. AssocMatrix:输出矩阵至Excel

相关文章:

你感兴趣的文章:

标签云: