2013-08-07 2 views
-2

동일한 연산자 (*, /, +, -)를 사용하면 계산기가 정상적으로 작동하지만 예를 들어 두 개의 숫자가 합쳐지면 잘못된 답을 줄 수 있습니다. 솔루션을 찾았지만 찾을 수없는 것 같습니다. 당신의 btnEquals_Click 행사에서C# 계산기가 올바르게 작동하지 않습니다.

public partial class frmMain : Form 
    { 
    public frmMain() 
    { 
     InitializeComponent(); 
    } 

    bool multiply = false; 
    bool divide = false; 
    bool add = false; 
    bool subtract = false; 

    private void btnOne_Click(object sender, EventArgs e) 
    { 
     txtDisplay.Text = txtDisplay.Text + "1"; 
    } 

    private void btnTwo_Click(object sender, EventArgs e) 
    { 
     txtDisplay.Text = txtDisplay.Text + "2"; 
    } 

    private void btnThree_Click(object sender, EventArgs e) 
    { 
     txtDisplay.Text = txtDisplay.Text + "3"; 
    } 

    private void btnFour_Click(object sender, EventArgs e) 
    { 
     txtDisplay.Text = txtDisplay.Text + "4"; 
    } 

    private void btnFive_Click(object sender, EventArgs e) 
    { 
     txtDisplay.Text = txtDisplay.Text + "5"; 
    } 

    private void btnSix_Click(object sender, EventArgs e) 
    { 
     txtDisplay.Text = txtDisplay.Text + "6"; 
    } 

    private void btnSeven_Click(object sender, EventArgs e) 
    { 
     txtDisplay.Text = txtDisplay.Text + "7"; 
    } 

    private void btnEight_Click(object sender, EventArgs e) 
    { 
     txtDisplay.Text = txtDisplay.Text + "8"; 
    } 

    private void btnNine_Click(object sender, EventArgs e) 
    { 
     txtDisplay.Text = txtDisplay.Text + "9"; 
    } 

    private void btnZero_Click(object sender, EventArgs e) 
    { 
     if (txtDisplay.Text.Length > 0) 
     { 
      txtDisplay.Text = txtDisplay.Text + "0"; 
     } 
    } 

    private void btnClear_Click(object sender, EventArgs e) 
    { 
     txtDisplay.Clear(); 
    } 

    private void btnDecimalPoint_Click(object sender, EventArgs e) 
    { 
     if (txtDisplay.Text.Contains(".")) 
     { 
      return; 
     } 
     else 
     { 
      txtDisplay.Text = txtDisplay.Text + "."; 
     } 
    } 

    private void btnNegative_Click(object sender, EventArgs e) 
    { 
     if (txtDisplay.Text.Contains("-")) 
     { 
      txtDisplay.Text = txtDisplay.Text.Remove(0,1); 
     } 
     else 
     { 
      txtDisplay.Text = "-" + txtDisplay.Text; 
     } 
    } 

    private void btnMultiply_Click(object sender, EventArgs e) 
    { 
     if (txtDisplay.Text == "") 
     { 
      return; 
     } 
     else 
     { 
      multiply = true; 
      txtDisplay.Tag = txtDisplay.Text; 
      txtDisplay.Text = ""; 
     } 
    } 


    private void btnAdd_Click(object sender, EventArgs e) 
    { 
     if (txtDisplay.Text == "") 
     { 
      return; 
     } 
     else 
     { 
      add = true; 
      txtDisplay.Tag = txtDisplay.Text; 
      txtDisplay.Text = ""; 
     } 
    } 

    private void btnSubtract_Click(object sender, EventArgs e) 
    { 
     if (txtDisplay.Text == "") 
     { 
      return; 
     } 
     else 
     { 
      subtract = true; 
      txtDisplay.Tag = txtDisplay.Text; 
      txtDisplay.Text = ""; 
     } 
    } 
    private void btnEquals_Click(object sender, EventArgs e) 
    { 
     if (multiply) 
     { 
      decimal dec = Convert.ToDecimal(txtDisplay.Tag) * Convert.ToDecimal(txtDisplay.Text); 
      txtDisplay.Text = dec.ToString(); 
     } 
     if (divide) 
     { 
      decimal dec = Convert.ToDecimal(txtDisplay.Tag)/Convert.ToDecimal(txtDisplay.Text); 
      txtDisplay.Text = dec.ToString(); 
     } 
     if (add) 
     { 
      decimal dec = Convert.ToDecimal(txtDisplay.Tag) + Convert.ToDecimal(txtDisplay.Text); 
      txtDisplay.Text = dec.ToString(); 
     } 
     if (subtract) 
     { 
      decimal dec = Convert.ToDecimal(txtDisplay.Tag) - Convert.ToDecimal(txtDisplay.Text); 
      txtDisplay.Text = dec.ToString(); 
     } 
     else 
     { 
      return; 
     } 
    } 

    private void btnDivide_Click(object sender, EventArgs e) 
    { 
     if (txtDisplay.Text == "") 
     { 
      return; 
     } 
     else 
     { 
      divide = true; 
      txtDisplay.Tag = txtDisplay.Text; 
      txtDisplay.Text = ""; 
     } 
    } 

} 
+0

역방향입니다. 계산을 수행하기 위해 숫자로 변환하는 UI 값이 있습니다. UI 값을 작성하는 데 사용되는 숫자가 있어야합니다. – asawyer

+0

woah. 나는 대부분의 이벤트를 하나의 이벤트로 결합하여 스크롤 휠을 위해 발신자를 결정합니다. – Jonesopolis

+1

'1 + 2 * 3'과 같은 시퀀스를 입력 할 때 '9'가 나오지만, '7'을 원해? –

답변

2

봐. 누군가가 추가하기를 원하므로 add = true 블록 만 실행하면 if 블록 만 실행됩니다.

는 그런 사람이 증식 선택, 이제 multiply = true하지만 add = true뿐만 아니라, 이제 당신은 추가 을 곱한 것입니다. 누군가가 모든 연산자를 통과하면 연산자 플래그를 지우지 않으므로 그 이후의 모든 숫자가 곱 해져서 나누어 져서 추가되고 마지막으로 빼기됩니다.

를 해결하려면, 당신은 운영자 지우는 방법을 만들 수 있습니다 귀하의 btnEquals_Click 경우에,

private void btnMultiply_Click(object sender, EventArgs e) 
{ 
    if (txtDisplay.Text == "") 
     return; 

    ResetOperatorFlags(); 

    multiply = true; 
    txtDisplay.Tag = txtDisplay.Text; 
    txtDisplay.Text = ""; 
} 

마지막 :

private void ResetOperatorFlags() 
{ 
    multiply = false; 
    divide = false; 
    add = false; 
    subtract = false; 
} 
다음

당신이 수에 대해 작업을 수행하기 전에 전화를 , else if을 사용하십시오. 플래그를 지운 후에 실제로 필요하지는 않지만 한 번에 하나만 설정할 수 있다면 모든 플래그를 테스트하는 것은 의미가 없습니다 :

private void btnEquals_Click(object sender, EventArgs e) 
{ 
    if (multiply) 
    { 
     ... 
    } 
    else if (divide) 
    { 
     ... 
+0

@ chancea, 내 문제를 해결해 주셔서 감사 드리며, 도움을 준 다른 사람들 덕분에. – DiegoAR

+1

NP @DiegoAR. 이 중 하나가 문제를 해결하면 "대답"으로 표시 할 수 있습니다. –

2

문제는 사용자가 작업 명령을 지우지 않는 것 같습니다. 즉, Add로 설정하지만, Multiply로 설정하면 Add를 지우지 않습니다. btnEquals_Click 메소드를 살펴보면 한 번에 여러 작업을 활성화 할 수 있으며 모든 작업이 실행됩니다.

+1

예, 또한 그가 열거 형과 switch 문 대신 4 개의 bool과 많은 ifs를 사용해야한다고 덧붙일 것입니다 –

+0

동의했습니다. 문제가 발생하기 전에 많은 코드를 살펴 보았습니다 :) –

+0

예, 미안합니다. 코드의 긴 블록.여전히 C# – DiegoAR

관련 문제