已知一个哈希表如下:---------------------
背景颜色  |  文字颜色
--------------------
#F0F8FF   |  #00008B
#6495ED   |  #DC143C
#800000   |  #FFFFE0以下是数据行的文字,如何做到 “如果是相同的文字就把背景色设为一样”??????例:
str1 <-----背景颜色#F0F8FF ,文字颜色#00008B  
str1 <-----背景颜色#F0F8FF ,文字颜色#00008B 
str2 <-----背景颜色##6495ED ,文字颜色#DC143C 
str2 <-----背景颜色##6495ED ,文字颜色#DC143C 
str3 <-----背景颜色##6495ED ,文字颜色#DC143C 
str4 <-----背景颜色#800000 ,文字颜色#FFFFE0
str6 <-----背景颜色#F0F8FF,文字颜色#00008B<--[因为哈希表里只有三行颜色,所以用完了就重新从哈希表的第一行读取
str7 <-----背景颜色#6495ED,文字颜色#DC143C 
求一函数,如 Function_color("str")

解决方案 »

  1.   

    Dictionary<int, string> dic = new Dictionary<int, string>();
    int index = 0;
    foreach(object value in hashtable.Keys)
    {
    dic.Add(index++, value.ToString();
    }
    index = 0;
    Dictionary<string, index> dictxt = new Dictionary<string,index>();
    string txt = "";//txt的值
    string color = "";
    if (dictxt .ContainsKey(txt))
                {
    color = dic[dictxt[txt];
                }
    else
    {
    color = dic[index];
    dictxt.Add(txt, index++);
    }
      

  2.   

    Dictionary <int, string> dic = new Dictionary <int, string>(); 
    HashTable table=new HashTable();
    foreach(object obj in table.kesy)
    {
    if(obj.containskey(""))
    {}
    }
      

  3.   

    比较好的对你所有的字符串 进行排序(找一个排序算法,冒泡等等 网上很多)
    使 它们组合成   str1,str1,str1,str2,str2,str2,str3,str3
    这种 顺序规则!
    字符串放入数组,或字典,表当中
    以下列子是数组:for(int i=0;i< str.count;i++)
    {
      _color(str[i],i);
    }
    -----------------------------------------------
    string val=""; //全局变量
    int col = 0;
    Function _color(str,i)
    {
       if(i==0){
       取第一个颜色赋值给它;哈希表0位置的颜色(用col 去取值)
         val = str;
        }
    else
    {
        if(str == val)
        {
        取第一个颜色赋值给它;(用col 去取值)
        }
        else
        {
         col +=1;
        if(col >=3){col =0;}
         取第一个颜色赋值给它;(用col 去取值)
        }
    }
    }
      

  4.   


        class Program
        {
            BackForeColor[] Colors = new BackForeColor[]{
            new BackForeColor{Back="#F0F8FF",Fore="#00008B"},
            new BackForeColor{Back="#6495ED",Fore="#DC143C"},
            new BackForeColor{Back="#800000",Fore="#FFFFE0"},
            };
            Dictionary<string, int> dict = new Dictionary<string, int>();
            static void Main(string[] args)
            {            Program p = new Program();
                p.TestPrint("str1");
                p.TestPrint("str1");
                p.TestPrint("str2");
                p.TestPrint("str2");
                p.TestPrint("str3");
                p.TestPrint("str4");
                p.TestPrint("str6");
                p.TestPrint("str7");
                p.TestPrint("str1");        }
            int n = 0;
            //取下一个
            BackForeColor GetNextString(string s)
            {
                if (dict.Keys.Contains(s))
                {
                    return Colors[dict[s]];
                }else{
                    dict[s] = n % Colors.Length;
                    return Colors[(n++) % Colors.Length];
                }
            }
            //打印
            public void TestPrint(string str)
            {
                BackForeColor c=GetNextString(str);
                Console.WriteLine(str + "<-------背景颜色:" + c.Back + ",前景颜色:" + c.Fore);
            }    }   public struct BackForeColor
        {
          public string Back;
          public string Fore;
        }