Problem z działaniem kalkulatora po pierwszym działaniu matematycznym.

0

Witam,
Mam problem z działaniem kalkulatora, nie potrafię tego przeskoczyć, pokaże to na przykładzie 10*8=80 <- teraz np. dzielę to, wpisuję liczbę, klikam równa się, a on wyświetla mi 0. Czyli mogę wykonać jedno działanie. Bardzo proszę o pomoc ;)

Oto kod:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp12
{
public partial class Form1 : Form
{

    double result = 0.0;
    char operation;
    string input = string.Empty;
    string operand1 = string.Empty;
    string operand2 = string.Empty;



    public Form1()
    {
        InitializeComponent();
    }
    
   



        private void button1_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            input += "7";
            this.textBox1.Text += input;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            input += "8";
            this.textBox1.Text += input;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            input += "9";
            this.textBox1.Text += input;

        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            input += "4";
            this.textBox1.Text = input;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            input += "5";
            this.textBox1.Text = input;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            input += "6";
            this.textBox1.Text = input;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            input += "1";
            this.textBox1.Text = input;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            input += "2";
            this.textBox1.Text = input;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            input += "3";
            this.textBox1.Text = input;
        }

        private void button14_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            input += "0";
            this.textBox1.Text = input;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operation = '+';
            input = string.Empty;
        }

        private void minus_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operation = '-';
            input = string.Empty;
        }

        private void divide_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operation = '/';
            input = string.Empty;
        }

        private void star_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operation = '*';
            input = string.Empty;

        }

        private void sin_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operation = 's';
            input = string.Empty;
        }

        private void cos_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operation = 'c';
            input = string.Empty;
        }

        private void sqrt_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operation = 'p';
            input = string.Empty;
        }

        private void kropka_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            input += ",";
            this.textBox1.Text = input;
        }
        private void PI_Click(object sender, EventArgs e)
        {
            double PI = Math.PI;
            string PII;
            PII = Convert.ToString(PI);
            this.textBox1.Text = "";
            input += PII;
            this.textBox1.Text = input;

        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            // Ln

            operand1 = input;
            operation = 'l';
            input = string.Empty;



        }

        private void clear_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            this.input = string.Empty;
            this.operand1 = string.Empty;
            this.operand2 = string.Empty;
        }


        private void wynik_Click(object sender, EventArgs e)
        {
        
            
            operand2 = input;
            double num1, num2;
            double.TryParse(operand1, out num1);
            double.TryParse(operand2, out num2);
            this.textBox1.Text = "";
            this.input = string.Empty;
            this.operand1 = string.Empty;
            this.operand2 = string.Empty;

            if (operation == '+')
            {
                result = num1 + num2;
                textBox1.Text = result.ToString();
               
                
        }

            else if (operation == '-')
            {
                result = num1 - num2;
                textBox1.Text = result.ToString();

            }
            else if (operation == '*')
            {
                result = num1 * num2;
                textBox1.Text = result.ToString();

            }
            else if (operation == '/')
            {
                if (num2 != 0)
                {
                    result = num1 / num2;
                    textBox1.Text = result.ToString();
                }

                else
                {
                    textBox1.Text = "Dzielisz przez 0 ! ";
                }


            }
            else if (operation == 'c')
            {

                result = Math.Cos(num1 + num2);
                textBox1.Text = result.ToString();

            }
            else if (operation == 's')
            {
                result = Math.Sin(num1 + num2);
                textBox1.Text = result.ToString();

            }
            else if (operation == 'p')
            {
                result = Math.Sqrt(num1 + num2);
                textBox1.Text = result.ToString();

            }

            else if (operation == 'l')
            {
                if (num1 + num2 > 0)
                {
                    result = Math.Log(num1 + num2);
                    textBox1.Text = result.ToString();
                }

                if (num1 + num2 == 0)
                {
                    textBox1.Text = "oo";
                }

                if (num1 + num2 < 0)
                {
                    textBox1.Text = "Nie istnieje";
                }

            }

        
        }
0

Po uzyskaniu pierwszego wyniku, w textbox'ie wyświetlasz wynik a zmiennym operand1, operand2 oraz input przypisujesz pustego stringa. Później, jak od razu klikniesz jakieś działanie to zmiennej operand1 przypisujesz wartość input, która w tym przypadku jest pusta.
No i sugeruję użyć switch/case zamiast tej rozbudowanej instrukcji if/else :)

0

Dzięki! ;)

1 użytkowników online, w tym zalogowanych: 0, gości: 1