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

时间:2015-10-25 17:22:08

标签: c# function memory calculator

我是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());
        }
    }
}

问题是,我得错了结果:this is my result but the answers must be different!! !!!!!! BUT, It should be like this 有些方法应具备这些功能:
以下是一些重要问题:

Add()方法添加Number1和Number2并返回结果。

Subtract()方法从Number1中减去Number2并返回结果。

Multiply()方法将Number1和Number2相乘并返回结果。

Divide()方法检查Number2是否为零。如果Number2为零,它应抛出一个异常,并显示消息“由于Number2为零,因此无法完成分区!”。如果Number2不为零,则应该返回除法结果。

SquareRootNumber1()和SquareRootNumber2()方法分别返回Number1和Number2的平方根。

如果计算器的内存中存在存储的值,则isMemoryUsed()方法应返回true,如果内存中没有存储的值,则返回false。

MemoryStore(double)方法将参数值存储到内存中。

MemoryRecall()方法返回内存的值。如果内存中没有存储值,则应返回零。

MemoryAdd(double)方法将参数值添加到内存中。

MemorySubtract(double)方法从内存中减去参数值。

MemoryClear()方法清除内存中的值。

调用MemoryStore,MemoryAdd和MemorySubtract方法时,值存储在内存中,然后isMemoryUsed()方法应返回true。调用MemoryClear()方法后,isMemoryUsed()方法应返回false。 MemoryRecall()方法不会更改isMemoryUsed()方法返回的值。

您的程序应该为上面给出的程序提供相同的输出。

仔细指定成员变量及其可见性(公共或私人)。

1 个答案:

答案 0 :(得分:1)

意外的结果是你测试一下内存,对吧?

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

2)记忆回忆如下:

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

它与:

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