2009-01-29 64 views
0

我有一本字典,我正在使用它來定義銅纜的額定值。我想使用這些值來計算需要多少電纜才能達到某個連接評級。連接的大小,電纜類型和系統類型由名爲cb_amperage,cb_cable_size和cb_system_type的兩個ComboBox選擇。等式運行後找到的答案將顯示在名爲tb6_cable_qty的文本框中。我歡迎任何和所有意見和建議。預先感謝您的幫助。C#字典應用:通過選擇鍵應用值

數學很簡單:

decimal x, y, z; 
x = decimal.Parse(cb_amperage.); 
y = decimal.Parse();//<---- this value must come from the dictionary below 
a = decimal.Parse();//<---- this value must also come from a dictionary below 
z = (x/y) * a 
tb6_cable_qty.Text = Math.Round(z,2).ToString(); 


void Cb_amperageSelectedIndexChanged(object sender, EventArgs e)  
    { 
     if (!String.!IsNullOrEmpty(cb_amperage) & !String.IsNullOrEmpty(cb_cable_size)) 
     //not sure if the above is right but I think it coveys the idea 
     { 
     //function based on the values in the dictionary below 
     } 

     //Cable Dictionary 1 used for cable quantity calculation 
     Dictionary<string, int> cable_dictionary_1 = new Dictionary<string, int>(); 

     cable_dictionary_1.Add ("#1", 130); 
     cable_dictionary_1.Add ("1/0", 150); 
     cable_dictionary_1.Add ("2/0", 175); 
     cable_dictionary_1.Add ("3/0", 200); 
     cable_dictionary_1.Add ("4/0", 230); 
     cable_dictionary_1.Add ("250", 255); 
     cable_dictionary_1.Add ("300", 285); 
     cable_dictionary_1.Add ("400", 355); 
     cable_dictionary_1.Add ("500", 380); 
     cable_dictionary_1.Add ("600", 720); 
     cable_dictionary_1.Add ("750", 475); 


     //System Type Dictionary used for cable quantity calculation 
     Dictionary<string, int> system_type_dictionary = new Dictionary<string, int>(); 

     system_type_dictionary.Add ("3P 3W", 3); 
     system_type_dictionary.Add ("3P 4W", 4); 

EDIT 1: MMR;請看下面的代碼。我有一種感覺,我錯過了一些我會知道的事情,如果我稍微有些過期。以下是我對您建議的解決方案的第一部分的實施。我收到一個錯誤。我認爲這是因爲這兩個項目,字符串和字典不知道它們應該被鏈接。這是錯誤; *類,結構或接口成員聲明(CS1519)中的無效標記','。看看你能看到什麼。再次感謝。

//The following strings are used in the cable quantity calculation 
     private const string cblsize1 = "#1"; 
     private const string cblsize2 = "1/0"; 
     private const string cblsize3 = "2/0"; 
     private const string cblsize4 = "3/0"; 

//Cable Dictionary 1 used for cable quantity calculation 
     Dictionary<string, int> cable_dictionary_1 = new Dictionary<string, int>(); 

     cable_dictionary.Add(cblsize1, 130);//#1 Cable 
     cable_dictionary.Add(cblsize2, 150);//1/0 Cable 
     cable_dictionary.Add(cblsize3, 175);//2/0 Cable 
     cable_dictionary.Add(cblsize4, 200);//3/0 Cable 
+0

這裏有什麼問題? – Gishu 2009-01-29 05:22:41

+0

如何使用字典提供方程中的值是簡短版本。 – 2009-01-29 05:27:03

回答

0

解決!!!! 以下是代碼。感謝MSDN C#論壇的Rudedog2!

partial class MainForm 
{ 
    double x, y, z;  

    List<string> cable_size1; 
    List<string> system_type_list; 
    List<string> amperage; 

    Dictionary<string, double> cable_dictionary_1; 
    Dictionary<string, double> system_type; 
    Dictionary<string, double> amperage_dictionary; 

private void MainFormLoad(object sender, EventArgs e) 
    { 
      //Each of the following lists populate the ComboBoxes 
      //to be selected on the main form. 
      amperage = new List<string>(); 
      cable_size1 = new List<string>(); 
      system_type_list = new List<string>();   

      cable_dictionary_1 = new Dictionary<string, double>(); 
      system_type = new Dictionary<string, double>(); 
      amperage_dictionary = new Dictionary<string, double>(); 

      // 
      InitializeCurrentLoadCB(); 
      InitializeCableSizeCB(); 
      InitializeSystemTypeCB(); 

      //---------------Dictionaries--------------------------------------------------------- 
      InitializeSystemTypeLookup(); 
      InitializeCableLookup(); 
      InitializeAmperageLookup(); 
    } 
    private void InitializeCurrentLoadCB() 
    { 
      //Amperage List, No Exclusions----------------------------------------------------------- 
      amperage = new List<string>(); 
      amperage.Add("Please Select Amperage"); 
      amperage.Add("400"); 
      amperage.Add("800"); 
      amperage.Add("1000"); 
      amperage.Add("1200"); 
      amperage.Add("1600"); 
      amperage.Add("2000"); 
      amperage.Add("2500"); 
      amperage.Add("3000"); 
      amperage.Add("3200"); 
      amperage.Add("4000"); 
      amperage.Add("5000"); 
      amperage.Add("6000"); 

      cb_test_1.DataSource = amperage; 
      cb_test_1.SelectedIndex = 0; 
      cb_test_1.DropDownStyle = ComboBoxStyle.DropDownList; 
    } 
    private void InitializeCableSizeCB() 
    { 
      //Cable List, No Exclusions -------------------------------------------------------------- 
      cable_size1 = new List<string>(); 
      cable_size1.Add("Please Select Cable Size"); 
      cable_size1.Add ("#1"); 
      cable_size1.Add ("1/0"); 
      cable_size1.Add ("2/0"); 
      cable_size1.Add ("3/0"); 
      cable_size1.Add ("4/0"); 
      cable_size1.Add ("250"); 
      cable_size1.Add ("300"); 
      cable_size1.Add ("400"); 
      cable_size1.Add ("500"); 
      cable_size1.Add ("600"); 
      cable_size1.Add ("700"); 
      cable_size1.Add ("750"); 

      cb_test_2.DataSource = cable_size1; 
      cb_test_2.SelectedIndex = 0; 
      cb_test_2.DropDownStyle = ComboBoxStyle.DropDownList; 
      //Initial DataBind for cable size ComboBox 
    } 
    private void InitializeSystemTypeCB() 
    { 
      //System Type List 
      system_type_list = new List<string>(); 
      system_type_list.Add("Select System Type"); 
      system_type_list.Add("3 Phase 3 Wire"); 
      system_type_list.Add("3 Phase 4 Wire"); 

      cb_test_3.DataSource = system_type_list; 
      cb_test_3.SelectedIndex = 0; 
      cb_test_3.DropDownStyle = ComboBoxStyle.DropDownList; 
      //Initial DataBind for cb_system type ComboBox    
    } 


    private void Button1Click(object sender, System.EventArgs e) 
    { 

     if (!String.IsNullOrEmpty(cb_test_1.Text) && 
      (!String.IsNullOrEmpty(cb_test_2.Text) && 
      (!String.IsNullOrEmpty(cb_test_3.Text)))) 
      { 
       double a; 
       if (cb_test_1.SelectedIndex != 0) 
        { 
         x = amperage_dictionary[amperage[cb_test_1.SelectedIndex]];     
        }              

       if (cb_test_2.SelectedIndex != 0) 
        { 
         y = cable_dictionary_1[cable_size1[cb_test_2.SelectedIndex]];     
        } 

       if (cb_test_3.SelectedIndex != 0) 
        { 
         z = system_type[system_type_list[cb_test_3.SelectedIndex]]; 
        }   

       a = ((x/y)*z); 
       this.tb_1.Text = Math.Round(a,2).ToString(); 

      } 

    } 
    private void InitializeSystemTypeLookup() 
    { 
     //System Type Dictionary 
     this.system_type = new Dictionary<string, double>(); 
     this.system_type.Add(this.system_type_list[0], 0); 
     this.system_type.Add(this.system_type_list[1], 3); 
     this.system_type.Add(this.system_type_list[2], 4); 
    } 
    private void InitializeCableLookup() 
    { 
     //Cable Dictionary 1 used for cable quantity calculation 
     this.cable_dictionary_1 = new Dictionary<string, double>(); 
     this.cable_dictionary_1.Add (this.cable_size1[0], 0); 
     this.cable_dictionary_1.Add (this.cable_size1[1], 130); 
     this.cable_dictionary_1.Add (this.cable_size1[2], 150); 
     this.cable_dictionary_1.Add (this.cable_size1[3], 175); 
     this.cable_dictionary_1.Add (this.cable_size1[4], 200); 
     this.cable_dictionary_1.Add (this.cable_size1[5], 230); 
     this.cable_dictionary_1.Add (this.cable_size1[6], 255); 
     this.cable_dictionary_1.Add (this.cable_size1[7], 285); 
     this.cable_dictionary_1.Add (this.cable_size1[8], 355); 
     this.cable_dictionary_1.Add (this.cable_size1[9], 380); 
     this.cable_dictionary_1.Add (this.cable_size1[10], 720); 
     this.cable_dictionary_1.Add (this.cable_size1[11], 475); 
    } 
    private void InitializeAmperageLookup() 
    { 
     //Amperage Dictionary 
     this.amperage_dictionary = new Dictionary<string, double>(); 
     this.amperage_dictionary.Add(this.amperage[0], 0); 
     this.amperage_dictionary.Add(this.amperage[1], 400); 
     this.amperage_dictionary.Add(this.amperage[2], 800); 
     this.amperage_dictionary.Add(this.amperage[3], 1000); 
     this.amperage_dictionary.Add(this.amperage[4], 1200); 
     this.amperage_dictionary.Add(this.amperage[5], 1600); 
     this.amperage_dictionary.Add(this.amperage[6], 2000); 
     this.amperage_dictionary.Add(this.amperage[7], 2500); 
     this.amperage_dictionary.Add(this.amperage[8], 3000); 
     this.amperage_dictionary.Add(this.amperage[9], 3200); 
     this.amperage_dictionary.Add(this.amperage[10], 4000); 
     this.amperage_dictionary.Add(this.amperage[11], 5000); 
     this.amperage_dictionary.Add(this.amperage[12], 6000);  
    } 


} 
0

我不確定你的問題是什麼?聽起來你已經把它全部覆蓋了。

+0

我想我知道該怎麼做,而不是如何去做。 – 2009-01-29 03:40:10

0

編輯:等等,現在我已經得到了你的問題!

這是我會怎麼做:

首先,針對每一個琴鍵,使它們成爲一個字符串列表,所以:

private const string mFirst = "#1"; 

然後,添加到字典:

mDictionary.Add(mFirst, 130); //etc... 

然後,對於每個組合框,請添加您的字符串

comboBox1.Add(mFirst); 
comboBox1.Add(mSecond); //etc... 

然後,將事件添加到組合框中選擇的指數變化:

comboBox1_SelectedIndexChanged(SomeEventStuff I Forget) 

當值的變化還有,更新您的y值。對comboBox2執行相同的操作,並更新z的值。一旦更新了任何值,只需同時更新結果即可。

編輯2:

爲了做數學題,我有一個看起來像一個功能:

float CopperCalc(float inX, float inY, float inA){ 
    if (inY != 0){//avoid the division by zero 
     return (inX/inY) * inA; 
    } else { 
     return 0; //just assume it's zero here. 
    }//I'm not Jon Skeet, I can't divide by zero 
} 

然後,打電話給你的SelectedIndexChanged處理內部,該功能,並用它來更新你的文本框:

//put your mX, mY, and mA as member values of your class, and then 
//they get updated. For instance: 
void comboBox1_SelectedIndexChanged(Events){ 
    mX = mDictionary[comboBox1.SelectedItem]; 
    textBox1.Text = CopperCalc(mX, mY, mA).ToString("f1"); 
} 

並把相同功能的每個組合框,以便在組合框的更新,然後在文本框中的更新。 這個東西只有在你想嘗試自動確定電線組合應該自動的時候纔有意義:

這聽起來像是Knapsack Problem的一個變種 - 是對的嗎?爲了滿足連接要求,你需要弄清銅佈線的最小組合?是否有一些最小或最大或其他限制?爲什麼不只是扔大量的銅,以滿足最低要求?

這是一個非常漫長的說法,我想我需要更多的信息才能理解問題;但是,如果揹包問題是一個合理的類比,那麼就有解決方案,但它們是NP完全實現的。也就是說,他們不會「快速」運行,但如果您只有十二個線規(或其他),那麼您應該可以在速度上更好。