C # Научный калькулятор - PullRequest
       9

C # Научный калькулятор

0 голосов
/ 01 декабря 2018

Здравствуйте, ребята, я пытаюсь создать Научный калькулятор, но моя проблема в том, что мой "=" возвращает только 2-значные операции ... Как я собираюсь работать со стеком и создавать большие формулы?

Например: 2 + 4 работает нормально , но 2 + 4 * 5-6 не работает , и моя программа падает. Я знаю, что оператор switch работает только для 2 цифр, но я хочу научитьсякак использовать стек. Заранее спасибо!

Это мой код.

namespace clcProject
{
    public partial class Form1 : Form
    {
        double num1;
        double num2, result;
        string operation;
        string Multiple;
        Stack<int> mystack = new Stack<int>();






    public Form1()
    {
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void button18_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "First you have to input the number.")
        {
            textBox1.Text = String.Empty;

        }
        textBox1.Text += "8";
    }

    private void number9_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "First you have to input the number.")
        {
            textBox1.Text = String.Empty;

        }
        textBox1.Text += "9";
    }

    private void button34_Click(object sender, EventArgs e)
    {
        textBox1.Text += ")";
    }

    private void input(string a)
    {
        if (textBox1.Text == "0")
            textBox1.Text = a;
        else
            textBox1.Text += a;
    }

    private void Clear_Click(object sender, EventArgs e)
    {

        textBox1.Text = "";
    }

    private void button0_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "First you have to input the number.")
        {
            textBox1.Text = String.Empty;

        }
        textBox1.Text += "0";
    }

    private void number1_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "First you have to input the number.")
        {
            textBox1.Text = String.Empty;

        }
        textBox1.Text += "1";
    }

    private void number2_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "First you have to input the number.")
        {
            textBox1.Text = String.Empty;

        }
        textBox1.Text += "2";
    }

    private void number3_Click(object sender, EventArgs e)
    {
        if(textBox1.Text == "First you have to input the number.")
        {
            textBox1.Text = String.Empty;

        }
        textBox1.Text += "3";
    }

    private void number4_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "First you have to input the number.")
        {
            textBox1.Text = String.Empty;

        }
        textBox1.Text += "4";
    }

    private void number5_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "First you have to input the number.")
        {
            textBox1.Text = String.Empty;

        }
        textBox1.Text += "5";
    }

    private void number6_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "First you have to input the number.")
        {
            textBox1.Text = String.Empty;

        }
        textBox1.Text += "6";
    }

    private void number7_Click(object sender, EventArgs e)
    {
          if (textBox1.Text == "First you have to input the number.")
          {
              textBox1.Text = String.Empty;

          }
          textBox1.Text += "7";

    }

    private void Backspace_Click(object sender, EventArgs e)
    {
        if(textBox1.Text == "First you have to input the number.")
        {
            textBox1.Text = String.Empty;
        }
        int textLenght = textBox1.Text.Length;
        if (textBox1.Text.Length > 0)
        {
            textBox1.Text = textBox1.Text.Substring(0, textLenght - 1);

        }

        textBox1.Focus();
        textBox1.SelectionStart = textLenght;

    }

    private void divide_Click(object sender, EventArgs e)
    {
        num1 = double.Parse(textBox1.Text);
        operation = "/";
        textBox1.Text = String.Empty;
    }

    private void plus_Click(object sender, EventArgs e)
    {
        num1 = double.Parse(textBox1.Text);
        operation = "+";
        textBox1.Text = String.Empty;
    }

    private void minus_Click(object sender, EventArgs e)
    {
        num1 = double.Parse(textBox1.Text);
        operation = "-";
        textBox1.Text = String.Empty;
    }

    private void multiple_Click(object sender, EventArgs e)
    {
        num1 = double.Parse(textBox1.Text);
        operation = "*";
        textBox1.Text = String.Empty;
    }





    private void equals_Click(object sender, EventArgs e)
    {
        num2 = double.Parse(textBox1.Text);
        mystack.push(num1);
        switch (operation)
        {
            case "+":
                textBox1.Text = (num1 + num2).ToString();
                break;
            case "-":
                textBox1.Text = (num1 - num2).ToString();
                break;
            case "*":
                textBox1.Text = (num1 * num2).ToString();
                break;
            case "/":
                if (num2 == 0)
                    textBox1.Text = "DIVIDED WITH 0";
                else
                    textBox1.Text = (num1 / num2).ToString();
                break;
            case "x^y":
                result = Math.Pow(Convert.ToDouble(num1), Convert.ToDouble(num2));
                textBox1.Text = result.ToString();
                break;


            default:
                break;

        }


    }
   /* private void multiple_acts(object sender, EventArgs e)
    {
        double[] num3 = new double[100];
        operation = "++";

    }*/

    private void CE_Click(object sender, EventArgs e)
    {
        num1 = double.Parse(textBox1.Text);

        if (textBox1.Text.Length == 0)
        {

            textBox1.Text = "First you have to input the number.";

        }
        else
        {

            textBox1.Text = (Math.Sqrt(double.Parse(textBox1.Text))).ToString();
        }
    }

    private void log_Click(object sender, EventArgs e)
    {

        if (textBox1.Text.Length == 0)
        {


            textBox1.Text = "First you have to input the number.";

        }
        else


            textBox1.Text = (Math.Log10(double.Parse(textBox1.Text))).ToString();

    }

    private void tan_Click(object sender, EventArgs e)
    {
        if (textBox1.Text.Length == 0)
        {

            textBox1.Text = "First you have to input the number.";

        }
        else


            textBox1.Text = (Math.Tan(double.Parse(textBox1.Text))).ToString();

    }

    private void cos_Click(object sender, EventArgs e)
    {

        if (textBox1.Text.Length == 0)
        {

            textBox1.Text = "First you have to input the number.";

        }
        else


            textBox1.Text = (Math.Cos(double.Parse(textBox1.Text))).ToString();

    }

    private void sin_Click(object sender, EventArgs e)
    {

        if (textBox1.Text.Length == 0)
        {

            textBox1.Text = "First you have to input the number.";

        }
        else


            textBox1.Text = (Math.Sin(double.Parse(textBox1.Text))).ToString();


    }

    private void upNumber_Click(object sender, EventArgs e)
    {
        num1 = double.Parse(textBox1.Text);
        if (textBox1.Text.Length == 0)
        {

            textBox1.Text = "First you have to input the number.";

        }
        else
        {
            operation = "x^y";
            textBox1.Text = String.Empty;
        }
    }

    private void factorial_Click(object sender, EventArgs e)
    {            
        long f = 1;
        for (int i = 1; i <= long.Parse(textBox1.Text); i++)
            f = f * i;

        textBox1.Text = f.ToString();

    }

    private void Pi_Click(object sender, EventArgs e)
    {
        double pi = 3.14159265359;
        textBox1.Text = pi.ToString();

    }

    private void x10_Click(object sender, EventArgs e)
    {
        textBox1.Text = (Math.Log(double.Parse(textBox1.Text))).ToString();
    }

    private void square_Click(object sender, EventArgs e)
    {
        result = Convert.ToDouble(textBox1.Text) * Convert.ToDouble(textBox1.Text);
        textBox1.Text = result.ToString();

    }

    private void left_Click(object sender, EventArgs e)
    {
        textBox1.Text += "(";
    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void dot_Click(object sender, EventArgs e)
    {
        textBox1.Text += ",";
    }


}

}

Извините за этот беспорядок!

...