예제 #1
0
 private void btnOperator_Click(object sender, EventArgs e)
 {
     if (((Button)sender).Text == "%")
     {
         percen = true;
     }
     if (has2 == true)
     {
         string secondOperand = lblDisplay.Text;
         if (percen == true)
         {
             secondOperand = (Convert.ToDouble(secondOperand) * Convert.ToDouble(firstOperand) / 100).ToString();
         }
         string result = CalculateEngine.calculate(operate, firstOperand, secondOperand); ///3232332323232323232
         double m      = Convert.ToDouble(result);
         percen = false;
         if (result is "E" || result.Length > 8)
         {
             lblDisplay.Text = "Error";
         }
         else
         {
             lblDisplay.Text = result;
         }
         has2            = false;
         isAfterOperater = false;
     }
예제 #2
0
        static public string calculate(string operate, string firstOperand, string secondOperand, int maxOutputSize = 8)
        {
            MainForm.keep2 = secondOperand;



            switch (operate)
            {
            case "+":
                return((Convert.ToDouble(firstOperand) + Convert.ToDouble(secondOperand)).ToString());

            case "-":
                return((Convert.ToDouble(firstOperand) - Convert.ToDouble(secondOperand)).ToString());

            case "X":
                return((Convert.ToDouble(firstOperand) * Convert.ToDouble(secondOperand)).ToString());

            case "÷":
                // Not allow devide be zero
                if (secondOperand != "0")
                {
                    double   result;
                    string[] parts;
                    int      remainLength;

                    result = (Convert.ToDouble(firstOperand) / Convert.ToDouble(secondOperand));
                    // split between integer part and fractional part
                    parts = result.ToString().Split('.');
                    // if integer part length is already break max output, return error
                    if (parts[0].Length > maxOutputSize)
                    {
                        return("E");
                    }
                    // calculate remaining space for fractional part.
                    remainLength = maxOutputSize - parts[0].Length - 1;
                    // trim the fractional part gracefully. =
                    return(result.ToString("N" + remainLength));
                }
                break;

            case "%":
                secondOperand = ((Convert.ToDouble(secondOperand) * Convert.ToDouble(firstOperand)) / 100).ToString();

                string res = CalculateEngine.calculate(operate, firstOperand, secondOperand);
                return(res);

                break;
            }
            return("E");


            // return "E";
            // percen = true;
        }