C# Calculator Class

前端 未结 4 864
太阳男子
太阳男子 2021-01-17 00:36

I was given this homework assignment, which I have been having a hard time with. The form was written and we had to write the class. Currently when I run the program my equa

相关标签:
4条回答
  • 2021-01-17 01:10

    Let's look at the requirement:

    "The results of multipication should show no more decimal places than the result of the first number than in the second number. Example 55.5*89.68 = 4977.240"

    The natural of multiplication in basic arithmetic means that this happens by default anyway. Eg. you're never going to multiply X.X by X.XX and get result X.XXXXXXX... - it just can't happen.

    Formatting is therefore easy - if you need to explicitly format then format as num.ToString("#.##############") which allows for a while bunch of numbers.

    You haven't outlined any requirements for Division, so I can't comment.

    BTW - if you say x = 1 / 3 and y = z * x, then you are going to get a lot of numbers after the decimal place, because x is .333333333... to start with. This is within the requirement AFAICT.

    0 讨论(0)
  • 2021-01-17 01:15

    In the main code, you have called the Equal() method like this:

    if (newValue)
        calc.Equals();
    else
        calc.Equals(displayValue); //Your class do not have this.
    

    So, you should do this first

    //I am not sure why you need to pass in the displayValue parameter, so I presume it would not return anything.
    public void Equal(Decimal displayValue)
    {
            //Do the things you suppose to do
    }
    

    And for your 9 + 9 = 9 problem, it is simply because in your code, you only press your click event Add_Button for once. Make a break point at your Add() method. Then try to do like this:

    9 --> press your Add_Button --> 9 --> press your Add_Button --> check your currentValue

    0 讨论(0)
  • 2021-01-17 01:23

    displayValue is both a class field and a method argument. Is that your intention? You need to make this.displayValue = ... when you assign to the field argument in order to make it clear what you are doing. Currently you are overwriting the local copy of the argument and the field value is always 0.

    Just delete the decimal displayValue; declaration (and from the Clear() function) and then store the displayValue outside of the class in the form.

    public class Calculator
    {
    
        //public Decimal displayValue;
        public Decimal currentValue;
    
        public void Add(Decimal displayValue)
        {
    
            currentValue+=displayValue;
    
        }
        ...
        public void Clear()
        {
            currentValue=0;
            //displayValue=0;
        }
    
        public decimal CurrentValue
        {
            get { return currentValue; }
    
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Calculator calc=new Calculator();
            calc.Add(1000m);
            calc.Divide(25m);
            calc.Subtract(8m);
    
            Console.WriteLine(calc.CurrentValue);
            // (1000/25)-8 = 32
        }
    }
    
    0 讨论(0)
  • 2021-01-17 01:24

    The main problem I see with your code is that you are only adding one of your operands, So for example 8 + 8 will always equal 8 for example.You have to do something like this ( included Add and reciprocal functions only):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Calculator
    {
    public class Calculator
    {
    
        public Decimal displayValue;
        public Decimal currentValue;
    
        private Decimal Operand_1;
        private Decimal Operand_2;
    
        private bool Operand_1_Added = false;
        private bool Operand_2_Added = false;
    
        private string Operation = "";
    
       private void AddOperand(Decimal Operand)
        {
                if(Operand_1_Added) 
                    {
                      Operand_2 = Operand;
                      Operand_2_Added = true;
                    }
                else {
                      Operand_1 = Operand;
                      Operand_1_Added = true;
                      currentValue = Operand_1;
                     }
         }
    
    
    
       public void Add(Decimal Arg1)
        {  
            this.AddOperand(Arg1);
            Operation = "Addition";
        }
    
       public void Reciprocal(Decimal Arg)
        {
         this.AddOperand(Arg);
         Operation = "Reciprocal";
        }
    
    
         public void Clear()
         {
             currentValue = 0;
             displayValue = 0;
             Operand_1 = 0;
             Operand_2 = 0;
         }
    
         public void Equals()
          {
             switch(Operation)
                 {
                     case "Addition": 
                              currentValue = Operand_1 + Operand_2;
                              break;
                     case "Reciprocal":
                              currentValue = 1/Operand_1;
                              break;
                     default: break; 
                  }
          }
    
       public void Equal(Decimal displayValue)
       {
        currentValue = displayValue;
       }
         public decimal CurrentValue
         {
           get
              {
               return currentValue;
              }
    
         }
    }
    

    Haven't tested code but this should work with the form class.

    0 讨论(0)
提交回复
热议问题