c#控制台应用程序中的简单计算器类

本文关键字:简单 计算器 控制台 应用程序 | 更新日期: 2023-09-27 18:19:13

我是c#的初学者,我有一个家庭作业,所以我需要你的帮助,这是我现在所做的,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SimpleCalculator{
    class SimpleCalculator {
        double number1;
        double number2;
        double result;
        double stored;
        Boolean isStored; 
        public SimpleCalculator()  {}
        public double Add()
        {
            result = (this.number1 + this.number2);
            return result;
        }
        public double Subtract()
        {
            result = (this.number1 - this.number2);
            return result;
        }
        public double Multiply()
        {
            result = (this.number1 * this.number2);
            return result;
        }
        public double Divide()
        {
            result = ((this.number1) /( this.number2));
            return result;
        }
        public double SquareRootNumber1()
        {
            result = Math.Sqrt(this.number1);
            return result;
        }
        public double SquareRootNumber2()
        {
            result = Math.Sqrt(this.number2);
            return result;
        }
        public Boolean isMemoryUsed()
        {
            if (this.stored == 0){
                this.isStored=false ;
                return isStored;
            }
            else              
                this.isStored = true;          
            return this.isStored;
        }
        public double  MemoryStore(double paramater_store1) {
            this.stored = this.stored + paramater_store1;
            return this.stored;        
        }
        public double MemoryRecall()
        {       
            if (MemoryStore(this.stored)==0)
                return 0;  
            else 
                return MemoryStore(this.stored);       
        }
        public double MemoryAdd(double paramater_store2)
        {
            this.stored = this.stored + paramater_store2 ;
            return this.stored;
        }
        public double MemorySubtract(double paramater_store3)
        {
            this.stored = this.stored - paramater_store3;
            return this.stored;
        }
        public double MemoryClear()
        {
            return this.stored = 0;
        }
        static void Main(string[] args)
        {
            SimpleCalculator calc = new SimpleCalculator();
            calc.number1 = 3.14; 
            calc.number2 = 5.78;
            Console.WriteLine("A is {0}", calc.number1);
            Console.WriteLine("B is {0}", calc.number2);
            double result = calc.Add();
            Console.WriteLine("A + B = {0}", result);
            result = calc.Subtract();
            Console.WriteLine("A - B = {0}", result);
            result = calc.Multiply();
            Console.WriteLine("A * B = {0}", result);
            Console.WriteLine("Sqrt(A) = {0}", calc.SquareRootNumber1());
            Console.WriteLine("Sqrt(B) = {0}", calc.SquareRootNumber2());
            Console.WriteLine("A = {0}", calc.number1);
            Console.WriteLine("B = {0}", calc.number2);
            try
            {
                result = calc.Divide(); 
                Console.WriteLine("A / B = {0}", result);
            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex.Message);
            }
            calc.number1=3.14;
            calc.number2=0 ;
            Console.WriteLine("A is {0} ", calc.number1);
            Console.WriteLine("B is {0} ", calc.number2);
            result = calc.Divide();
            try { 
                result = calc.Divide();
                Console.WriteLine("A / B = {0}", result);
            }
            catch (Exception ex) {
                Console.WriteLine("The divisor can’t be zero ",ex.Message);
            }
            Console.WriteLine("Is memory being used at the beginning? {0}", calc.isMemoryUsed());
            calc.MemoryStore(3.16);
            Console.WriteLine("Is memory being used after MemoryStore? {0}", calc.isMemoryUsed()); 
            Console.WriteLine("Memory = {0}", calc.MemoryRecall());
            calc.MemoryAdd(5.67);
            Console.WriteLine("Is memory being used after MemoryAdd? {0}", calc.isMemoryUsed()); 
            Console.WriteLine("Memory = {0}", calc.MemoryRecall());
            calc.MemorySubtract(7.89);
            Console.WriteLine("Is memory being used after MemorySubtract? {0}", calc.isMemoryUsed());
            Console.WriteLine("Memory = {0}", calc.MemoryRecall());
            calc.MemoryClear();
            Console.WriteLine("Is memory being used after MemoryClear? {0}", calc.isMemoryUsed());
            calc.MemoryAdd(5.67);
            Console.WriteLine("Is memory being used after MemoryAdd? {0}", calc.isMemoryUsed());
            Console.WriteLine("Memory = {0}", calc.MemoryRecall());
            calc.MemoryClear();
            Console.WriteLine("Is memory being used after MemoryClear? {0}",      calc.isMemoryUsed());
            calc.MemorySubtract(7.89);
            Console.WriteLine("Is memory being used after MemorySubtract? {0}", calc.isMemoryUsed()); 
            Console.WriteLine("Memory = {0}", calc.MemoryRecall());
        }
    }
}

问题是,我得到错误的结果:这是我的结果,但答案必须是不同的!!!!!!!!但是,应该是这样的有一些方法应该具有这些特性:
以下是一些重要的问题:

Add()方法将Number1和Number2相加并返回结果。 Subtract()方法从Number1中减去Number2并返回结果。 乘法()方法将Number1和Number2相乘并返回结果。 Divide()方法检查Number2是否为零。如果Number2为零,它应该抛出一个异常,消息为"不能进行除法,因为Number2为零!"如果Number2不为零,它应该返回除法结果。 SquareRootNumber1()和SquareRootNumber2()方法分别返回Number1和Number2的平方根。 如果计算器的内存中存在存储值,isMemoryUsed()方法应该返回true,如果内存中没有存储值,则返回false。 MemoryStore(double)方法将参数值存储到内存中。 MemoryRecall()方法返回内存的值。如果内存中没有存储值,它应该返回0。 MemoryAdd(double)方法将参数值添加到内存中。 MemorySubtract(double)方法从内存中减去参数值。 MemoryClear()方法清除内存中的值。 当MemoryStore, MemoryAdd和MemorySubtract方法被调用时,一个值被存储在内存中,然后isMemoryUsed()方法应该返回true。调用MemoryClear()方法后,isMemoryUsed()方法应该返回false。MemoryRecall()方法不会改变isMemoryUsed()方法返回的值。 你的程序应该给出与上面程序相同的输出。 仔细指定成员变量及其可见性(public或private)。

c#控制台应用程序中的简单计算器类

意外的结果是一旦你测试内存,对吗?

1) MemoryStore不存储参数,它将参数添加到当前内存内容,这是你想要的吗?

2) Memory recall是这样写的

public double MemoryRecall()
{       
    if (MemoryStore(this.stored)==0)
        return 0;  
    else 
        return MemoryStore(this.stored);       
}

相同
public double MemoryRecall()
{ 
    return MemoryStore(this.stored);       
}