2016-03-01 618 views
0

以下是c#中GPA計算器的代碼。我感覺我寫的代碼和格式都很長。我使用了DisplayOne()函數; DisplayTwo():等,根據單選按鈕禁用和啓用文本框。而不是使用這些函數並編寫所有代碼行「txtCCode1.Enabled = true; txtCCode2.Enabled = false; s txtCCode3.Enabled = false;」等我想知道是否有任何其他選項來編寫程序而不使用「.Enable = true/false」或僅使用「.Enable = true/false」。不要直到DisplayOne();到DisplaySix();.C#GPA計算器

public double gradePoint1, gradePoint2, gradePoint3, gradePoint4, gradePoint5, gradePoint6; 
    public double courseGP1, courseGP2, courseGP3, courseGP4, courseGP5, courseGP6; 
    int credHour1 = 0, credHour2 = 0, credHour3 = 0, credHour4 = 0, credHour5 = 0, credHour6 = 0; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     // make the display bigger 
     this.Font = new Font("Arial", 15); 
    } 

    private void btnDisplay_Click(object sender, EventArgs e) 
    { 
     // to disable certain text boxes according to the number of courses taken 
     if (rad1.Checked) 
      DisplayOne(); 
     else if (rad2.Checked) 
      DisplayTwo(); 
     else if (rad3.Checked) 
      DisplayThree(); 
     else if (rad4.Checked) 
      DisplayFour(); 
     else if (rad5.Checked) 
      DisplayFive(); 
     else 
      DisplaySix(); 
     InitializeLabels(); 
    } 

    private void InitializeLabels() 
    { 
     // initialize all Credit Hours labels 
     lblCH1.Text = "0"; 
     lblCH2.Text = "0"; 
     lblCH3.Text = "0"; 
     lblCH4.Text = "0"; 
     lblCH5.Text = "0"; 
     lblCH6.Text = "0"; 
    } 

    private void DisplayOne() 
    { 
     txtCCode1.Enabled = true; 
     txtCCode2.Enabled = false; 
     txtCCode3.Enabled = false; 
     txtCCode4.Enabled = false; 
     txtCCode5.Enabled = false; 
     txtCCode6.Enabled = false; 
     txtGrade1.Enabled = true; 
     txtGrade2.Enabled = false; 
     txtGrade3.Enabled = false; 
     txtGrade4.Enabled = false; 
     txtGrade5.Enabled = false; 
     txtGrade6.Enabled = false; 
    } 

    private void DisplayTwo() 
    { 
     txtCCode1.Enabled = true; 
     txtCCode2.Enabled = true; 
     txtCCode3.Enabled = false; 
     txtCCode4.Enabled = false; 
     txtCCode5.Enabled = false; 
     txtCCode6.Enabled = false; 
     txtGrade1.Enabled = true; 
     txtGrade2.Enabled = true; 
     txtGrade3.Enabled = false; 
     txtGrade4.Enabled = false; 
     txtGrade5.Enabled = false; 
     txtGrade6.Enabled = false; 
    } 

    private void DisplayThree() 
    { 
     txtCCode1.Enabled = true; 
     txtCCode2.Enabled = true; 
     txtCCode3.Enabled = true; 
     txtCCode4.Enabled = false; 
     txtCCode5.Enabled = false; 
     txtCCode6.Enabled = false; 
     txtGrade1.Enabled = true; 
     txtGrade2.Enabled = true; 
     txtGrade3.Enabled = true; 
     txtGrade4.Enabled = false; 
     txtGrade5.Enabled = false; 
     txtGrade6.Enabled = false; 
    } 

    private void DisplayFour() 
    { 
     txtCCode1.Enabled = true; 
     txtCCode2.Enabled = true; 
     txtCCode3.Enabled = true; 
     txtCCode4.Enabled = true; 
     txtCCode5.Enabled = false; 
     txtCCode6.Enabled = false; 
     txtGrade1.Enabled = true; 
     txtGrade2.Enabled = true; 
     txtGrade3.Enabled = true; 
     txtGrade4.Enabled = true; 
     txtGrade5.Enabled = false; 
     txtGrade6.Enabled = false; 
    } 

    private void DisplayFive() 
    { 
     txtCCode1.Enabled = true; 
     txtCCode2.Enabled = true; 
     txtCCode3.Enabled = true; 
     txtCCode4.Enabled = true; 
     txtCCode5.Enabled = true; 
     txtCCode6.Hide(); 
     txtGrade1.Enabled = true; 
     txtGrade2.Enabled = true; 
     txtGrade3.Enabled = true; 
     txtGrade4.Enabled = true; 
     txtGrade5.Enabled = true; 
     txtGrade6.Hide(); 
     lblCH6.Hide(); 
    } 

    private void DisplaySix() 
    { 
     txtCCode1.Enabled = true; 
     txtCCode2.Enabled = true; 
     txtCCode3.Enabled = true; 
     txtCCode4.Enabled = true; 
     txtCCode5.Enabled = true; 
     txtCCode6.Enabled = true; 
     txtGrade1.Enabled = true; 
     txtGrade2.Enabled = true; 
     txtGrade3.Enabled = true; 
     txtGrade4.Enabled = true; 
     txtGrade5.Enabled = true; 
     txtGrade6.Enabled = true; 
     txtCCode6.Show(); 
     txtGrade6.Show(); 
     lblCH6.Show(); 
    } 

    private void txtCCode1_Leave(object sender, EventArgs e) 
    { 
     // to find the right most character from the course code = CREDIT HOURS 
     string courseCode = txtCCode1.Text; 
     string rightMost = courseCode.Right(1); 
     lblCH1.Text = rightMost; 
     credHour1 = Convert.ToInt32(lblCH1.Text); 
    } 

    private void txtCCode2_Leave(object sender, EventArgs e) 
    { 
     string courseCode = txtCCode2.Text; 
     string rightMost = courseCode.Right(1); 
     lblCH2.Text = rightMost; 
     credHour2 = Convert.ToInt32(lblCH2.Text); 
    } 

    private void txtCCode3_Leave(object sender, EventArgs e) 
    { 
     string courseCode = txtCCode3.Text; 
     string rightMost = courseCode.Right(1); 
     lblCH3.Text = rightMost; 
     credHour3 = Convert.ToInt32(lblCH3.Text); 
    } 

    private void txtCCode4_Leave(object sender, EventArgs e) 
    { 
     string courseCode = txtCCode4.Text; 
     string rightMost = courseCode.Right(1); 
     lblCH4.Text = rightMost; 
     credHour4 = Convert.ToInt32(lblCH4.Text); 
    } 

    private void txtCCode5_Leave(object sender, EventArgs e) 
    { 
     string courseCode = txtCCode5.Text; 
     string rightMost = courseCode.Right(1); 
     lblCH5.Text = rightMost; 
     credHour5 = Convert.ToInt32(lblCH5.Text); 
    } 

    private void txtCCode6_Leave(object sender, EventArgs e) 
    { 
     string courseCode = txtCCode6.Text; 
     string rightMost = courseCode.Right(1); 
     lblCH6.Text = rightMost; 
     credHour6 = Convert.ToInt32(lblCH6.Text); 
    } 

    private void txtGrade1_Leave(object sender, EventArgs e) 
    { 
     gradePoint1 = FindGradePoint(txtGrade1.Text); 
    } 

    private void txtGrade2_Leave(object sender, EventArgs e) 
    { 
     gradePoint2 = FindGradePoint(txtGrade2.Text); 
    } 

    private void txtGrade3_Leave(object sender, EventArgs e) 
    { 
     gradePoint3 = FindGradePoint(txtGrade3.Text); 
    } 

    private void txtGrade4_Leave(object sender, EventArgs e) 
    { 
     gradePoint4 = FindGradePoint(txtGrade4.Text); 
    } 

    private void txtGrade5_Leave(object sender, EventArgs e) 
    { 
     gradePoint5 = FindGradePoint(txtGrade5.Text); 
    } 

    private void txtGrade6_Leave(object sender, EventArgs e) 
    { 
     gradePoint6 = FindGradePoint(txtGrade6.Text); 
    } 

    private double FindGradePoint(string grade) 
    { 
     double gradePt=0; 
     switch (grade) 
     { 
      case "A+": 
      case "A": 
       gradePt = 4.00; 
       break; 
      case "A-": 
       gradePt = 3.75; 
       break; 
      case "B+": 
       gradePt = 3.5; 
       break; 
      case "B": 
       gradePt = 3.0; 
       break; 
      case "B-": 
       gradePt = 2.75; 
       break; 
      case "C+": 
       gradePt = 2.5; 
       break; 
      case "C": 
       gradePt = 2.0; 
       break; 
      case "C-": 
       gradePt = 1.75; 
       break; 
      case "D+": 
       gradePt = 1.5; 
       break; 
      case "D": 
       gradePt = 1.0; 
       break; 
      case "E": 
       gradePt = 0.5; 
       break; 
      case "F": 
       gradePt = 0.0; 
       break; 
      default: 
       DialogResult x = new DialogResult(); 
       x = MessageBox.Show("Error in Alphabet grade", "Error"); 
       if (x == DialogResult.OK) 
        txtGrade1.Focus(); 
       break; 
     } 
     return gradePt; 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     int totalCredHours = 0; 
     CalcTotalCredHours(credHour1, credHour2, credHour3, credHour4, credHour5, credHour6, ref totalCredHours); 
     courseGP1 = CalcCourseGradePoint(credHour1, gradePoint1); 
     courseGP2 = CalcCourseGradePoint(credHour2, gradePoint2); 
     courseGP3 = CalcCourseGradePoint(credHour3, gradePoint3); 
     courseGP4 = CalcCourseGradePoint(credHour4, gradePoint4); 
     courseGP5 = CalcCourseGradePoint(credHour5, gradePoint5); 
     courseGP6 = CalcCourseGradePoint(credHour6, gradePoint6); 
     double totalCGP = CalcTotalCGP(courseGP1, courseGP2, courseGP3, courseGP4, courseGP5, courseGP6); 
     double gpa = CalcGPA(totalCGP, totalCredHours); 
     lblGPA.Text = gpa.ToString("N"); 
    } 

    private double CalcCourseGradePoint(int ch, double gp) 
    { 
     double cgp = ch * gp; 
     return cgp; 
    } 

    private double CalcGPA(double tcgp, int tch) 
    { 
     double gpa = tcgp/tch; 
     return gpa; 
    } 

    private double CalcTotalCGP(double cgp1, double cgp2, double cgp3, double cgp4, double cgp5, double cgp6) 
    { 
     double totCGP = cgp1 + cgp2 + cgp3 + cgp4 + cgp5 + cgp6; 
     return totCGP; 
    } 

    private void CalcTotalCredHours(int ch1, int ch2, int ch3, int ch4, int ch5, int ch6, ref int tch) 
    { 
     tch = ch1 + ch2 + ch3 + ch4 + ch5 + ch6; 
    } 

    private void textBox1_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void textBox1_DoubleClick(object sender, EventArgs e) 
    { 
     textBox2.Text = textBox1.Text.Right(4).ToLower(); 
    } 
} 

}

+4

你的問題更適合[codereview](http://codereview.stackexchange.com/),雖然在發佈之前閱讀了他們的指南 – Sayse

回答

1

首先,你應該添加一個參數,以這些方法; DisplayOne(),DisplayTwo()等

private void DisplayNumbers(int number) 
{ 
    txtCCode1.Enabled = (number <= 1); 
    txtCCode2.Enabled = (number <= 2); 
    txtCCode3.Enabled = (number <= 3); 
    txtCCode4.Enabled = (number <= 4); 
    txtCCode5.Enabled = (number <= 5); 
    txtCCode6.Enabled = (number <= 6); 
    txtGrade1.Enabled = (number <= 1); 
    // .... 
} 

如果要採取進一步您可以創建持有爲每個數字控件的引用的類。

class Number { 
    public int NumberValue { get; set; } 
    public RadioButton ShowVisible { get; set; } 
    public TextBox CodeTextBox { get; set; } 
    public TextBox GradeTextBox { get;set; } 

    public void SetVisible() 
    { 
     this.CodeTextBox.Visible = this.ShowVisible.Checked; 
     this.GradeTextBox.Visible = this.ShowVisible.Checked; 
    } 
} 

然後在您按一下按鈕:

for(int number = 0; number < this.Numbers.Length; number ++) 
    this.Numbers[number].SetVisible(); 

數字將需要數對象,你需要創建數組。