简体   繁体   中英

How can I ensure that I can continue to calculate with the outcome of a sum?

I am making a calculator in C#. I have now basic functions in it. For example: When I do 20 + 10 , the outcome is 30 . When I then press the plus-button again and the 6 , you would say that the outcome will be 36 . This is not the case for me yet. The result for me is then 22 . So my question is also: How can I ensure that I can continue to calculate with the result of a calculation? Does anyone have an idea?

This is my code:

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 test
{
    public partial class Form1 : Form
    {
        string first = "";
        string second = "";
        string userInput = "";
        string space = " ";
        char function;
        double result = 0.0;
        int times;

        public Form1()
        {
            InitializeComponent();
        }
        private void number1_Click(object sender, EventArgs e)
        {
            calculatorDisplay.Text = "";
            userInput += "1";
            calculatorDisplay.Text += userInput;
        }

        private void number2_Click(object sender, EventArgs e)
        {
            calculatorDisplay.Text = "";
            userInput += "2";
            calculatorDisplay.Text += userInput;
        }

        private void number3_Click(object sender, EventArgs e)
        {
            calculatorDisplay.Text = "";
            userInput += "3";
            calculatorDisplay.Text += userInput;
        }

        private void number4_Click(object sender, EventArgs e)
        {
            calculatorDisplay.Text = "";
            userInput += "4";
            calculatorDisplay.Text += userInput;
        }

        private void number5_Click(object sender, EventArgs e)
        {
            calculatorDisplay.Text = "";
            userInput += "5";
            calculatorDisplay.Text += userInput;
        }

        private void number6_Click(object sender, EventArgs e)
        {
            calculatorDisplay.Text = "";
            userInput += "6";
            calculatorDisplay.Text += userInput;
        }

        private void number7_Click(object sender, EventArgs e)
        {
            calculatorDisplay.Text = "";
            userInput += "7";
            calculatorDisplay.Text += userInput;
        }

        private void number8_Click(object sender, EventArgs e)
        {
            calculatorDisplay.Text = "";
            userInput += "8";
            calculatorDisplay.Text += userInput;
        }

        private void number9_Click(object sender, EventArgs e)
        {
            calculatorDisplay.Text = "";
            userInput += "9";
            calculatorDisplay.Text += userInput;
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            first = "";
            second = "";
            userInput = "";
            result = 0.0;
            calculatorDisplay.Text = "0";
            feedback.Text = "";
            times = 0;

        }

        private void divideButton_Click(object sender, EventArgs e)
        {
            function = '/';
            first = userInput;
            userInput = "";
            feedback.Text = first + space + "÷";
            
        }

        private void multiplyButton_Click(object sender, EventArgs e)
        {
            function = '*';
            first = userInput;
            userInput = "";
            feedback.Text = first + space + "*";
        }

        private void plusButton_Click(object sender, EventArgs e)
        {
            function = '+';
            first = userInput;
            userInput = "";
            feedback.Text = first + space + "+";
        }

        private void minusButton_Click(object sender, EventArgs e)
        {
            function = '-';
            first = userInput;
            userInput = "";
            feedback.Text = first + space + "-";
 
        }

        private void equalButton_Click(object sender, EventArgs e)
        {
            times++;
            second = userInput;
            double firstNum, secondNum;
            firstNum = Convert.ToDouble(first);
            secondNum = Convert.ToDouble(second);
            
            if (function =='+')
            {
                result = firstNum + (secondNum * times);
                calculatorDisplay.Text = result.ToString();
                feedback.Text = (result-secondNum) +  space + "+" + space + second + space + "=";
            }
            else if(function == '-')
            {
                result = firstNum - (secondNum * times);
                calculatorDisplay.Text = result.ToString();
                feedback.Text = (result+secondNum) + space + "-" + space + second + space + "=";
            }
            else if (function == '/')
            {
                if(secondNum == 0)
                {
                    calculatorDisplay.Text = "Error";
                }
                else
                {
                    result = firstNum / Math.Pow(secondNum, times);
                    calculatorDisplay.Text = result.ToString();
                    feedback.Text = result*secondNum + space + "÷" + space + second + space + "=";
                }
            }
            else if (function == '*')
            {
                result = firstNum * Math.Pow(secondNum, times);
                calculatorDisplay.Text = result.ToString();
                feedback.Text = result/secondNum + space + "*" + space + second + space + "=";
            }
        }
        
        private void decimalButton_Click(object sender, EventArgs e)
        {
            calculatorDisplay.Text += ".";
        }

        private void zeroButton_Click(object sender, EventArgs e)
        {
            calculatorDisplay.Text = "";
            userInput += "0";
            calculatorDisplay.Text += userInput;
        }
    }
}

My guess is:

First call to equalButton_Click

 result = firstNum + (secondNum * times);
 result = 10 + (20 * 1); // = 30

second time you call it

times++; // = 2
...
result = 10 + (6 * 2); // = 22

I think you have to assing firstNum the result variable after calculating the result.

firstNum = result;

Try that:

        if (function =='+')
        {
            result = firstNum + (secondNum * times);
            firstNum = result; // Notice this
            calculatorDisplay.Text = result.ToString();
            feedback.Text = (result-secondNum) +  space + "+" + space + second + space + "=";
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM