2013-09-03 6 views
-1

1 + 1 = 2과 같은 입력을 표시하도록이 계산기를 계속 가져 오려고합니다. 코드가 정확하다고 생각하지만 + -/*을 실행하면 구문 분석이 필요합니다. 나는 이것을 할 수있는 가장 가까운 아이디어가 없다.Windows 응용 프로그램에서 char을 구문 분석하는 방법은 무엇입니까?

나는 꽤 오랫동안 노력했다. 그래서 char.Parse와 만약 그렇다면 어디에서?

public partial class Form1 : Form 
{ 
    char c; 
    double num1; 
    double num2; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 
    private void btn0_Click(object sender, EventArgs e) 
    { 
     txtBox.Text += 0; 
    } 
    private void btn1_Click(object sender, EventArgs e) 
    { 
     txtBox.Text += 1; 
    } 
    private void btn2_Click(object sender, EventArgs e) 
    { 
     txtBox.Text += 2; 
    } 
    private void btn3_Click(object sender, EventArgs e) 
    { 
     txtBox.Text += 3; 
    } 
    private void btn4_Click(object sender, EventArgs e) 
    { 
     txtBox.Text += 4; 
    } 
    private void btn5_Click(object sender, EventArgs e) 
    { 
     txtBox.Text += 5; 
    } 
    private void btn6_Click(object sender, EventArgs e) 
    { 
     txtBox.Text += 6; 
    } 
    private void btn7_Click(object sender, EventArgs e) 
    { 
     txtBox.Text += 7; 
    } 
    private void btn8_Click(object sender, EventArgs e) 
    { 
     txtBox.Text += 8; 
    } 
    private void btn9_Click(object sender, EventArgs e) 
    { 
     txtBox.Text += 9; 
    } 
    private void btnDecimal_Click(object sender, EventArgs e) 
    { 
     if (!txtBox.Text.Contains('.')) 
      txtBox.Text += '.'; 
    } 
    private void btnAddition_Click(object sender, EventArgs e) 
    { 
     c = '+'; 
     num1 = double.Parse(txtBox.Text); 
     txtBox.Text = string.Empty; // txtBox.Text += c; 
    } 
    private void btnSubtraction_Click(object sender, EventArgs e) 
    { 
     c = '-'; 
     num1 = double.Parse(txtBox.Text); 
     txtBox.Text = string.Empty; 
    } 
    private void btnMultiplication_Click(object sender, EventArgs e) 
    { 
     c = '*'; 
     num1 = double.Parse(txtBox.Text); 
     txtBox.Text = string.Empty; 
    } 
    private void btnDivision_Click(object sender, EventArgs e) 
    { 
     c = '/'; 
     num1 = double.Parse(txtBox.Text); 
     txtBox.Text = string.Empty; 
    } 
    private void btnClear_Click(object sender, EventArgs e) 
    { 
     txtBox.Clear(); 
    } 
    private void btnEqual_Click(object sender, EventArgs e) 
    { 
     double result; 
     num2 = double.Parse(txtBox.Text); 
     switch (c) 
     { 
      case '+': 
       result = num1 + num2; 
       txtBox.Text = result.ToString(); // txtBox.Text += result.ToString(); 
       break; 
      case '-': 
       result = num1 - num2; 
       txtBox.Text = result.ToString(); 
       break; 
      case '/': 
       if (num2 != 0) 
       { 
        result = num1/num2; 
        txtBox.Text = result.ToString(); 
       } 
       else 
       { 
        txtBox.Text = "You can't divide by zero... sign up for Math 100 please =)"; 
       } 
       break; 
      case '*': 
       result = num1 * num2; 
       txtBox.Text = result.ToString(); 
       break; 
     } 
    } 
} 

}

+3

같은 뭔가를 찾고 있다고 생각하면 "문자를 구문 분석"이란 무엇을 의미합니까? "parse"는 문자열의 내부 구조를 발견하는 것을 의미합니다. - "+"에 구조가 없으며, 단일 문자이고, 그것이 무엇 인지도 알 수 있습니다. – Amadan

답변

2

논리는 전적으로 결함이있다.

당신은 세 가지 작업을 수행 할 수 있습니다

첫째, 당신이 무슨 일을하는지 스크랩하고 당신을 위해이 작업을 수행 도서관 .. 등 NCalc을 함께 갈 수 있습니다.

또는 당신이 무슨 일을하는지 스크랩과 같은 Shunting Yard algorithm.

으로이 작업을 수행하는 알고리즘을 .. 조회 또는, 당신은 당신이 무엇을 수정하려고 유지할 수 있습니다 ..하지만 가장있을 수 있습니다 이렇게하는 다른 방법을 찾아야합니다. 코드보고에서

0

은, 코드의 당신의 문제 라인은 다음과 같습니다

txtBox.Text = result.ToString(); // txtBox.Text += result.ToString(); 

당신은 당신의 계산 (A + B, A의 결과로 텍스트 상자의 텍스트를 설정하는 - B, A * B를, 또는 a/b)를 사용하십시오. 그 결과

, 난 당신이

txtBox.Text = num1 + " + " + num2 + " = " + result.ToString(); 
+0

문제는 코드가 여전히 + =/* 문자를보고 충돌하는 것입니다. – Zoro

관련 문제