创建运行总计

时间:2019-06-10 22:12:30

标签: c#

这是一个简单的计算器。它执行计算。但是,每次计算时,我都想保存总计并将其添加到运行总计中。创建运行总计是我遇到的问题。有人可以帮帮我吗?假定是纪念品格式。因此,当我撤消操作时,将从先前的计算中删除堆栈或总计。我为此感到挣扎。

class Calculator
{
    public Stack<double> result= new Stack<double>();
    double total;

    public void Add(double a, double b)
    {
        total = a + b;
        Console.WriteLine("Sum:{0}", total);
        result.Push(total);
    }
    public void Sub(double a, double b)
    {
        total = a - b;
        Console.WriteLine("Difference:{0}", total);
        result.Push(total);
    }
    public void Mul(double a, double b)
    {
        total = a * b;
        Console.WriteLine("Product:{0} ", total);
        result.Push(total);
    }
    public void Div(double a, double b)
    {
        if (b!=0)
        {
            total = a / b;
            Console.WriteLine("Quotient:{0}", total);
            result.Push(total);
        }
        else
        {
            Console.WriteLine("Error: Cannot divide by 0");
        }
    }

    double GetTotal()
    {
        return total;

    }
    void Undo()
    {
        if (result.Count==0)
        {
            Console.WriteLine("UNDO IS NOT AVAILABLE");
        }
        result.Pop();
        total = result.Pop();
        Console.WriteLine("Running total:{0}", total);
    }
    void clear()
    {
        while (result.Count !=0)
            result.Pop();
        total = 0;
        Console.WriteLine("Running total:{0}", total);
    }
    static int Main()
    {
        Calculator cal=new Calculator();
        string line="";
        while (true)
        {
            Console.WriteLine("Enter (Clear, Undo, Exit, Expression):");

            if (line.ToLower() == "exit")
                break;
            else if (line.ToLower() == "undo")
                cal.Undo();
            else if (line.ToLower() == "clear")
                cal.clear();
            else
            {
                double a, b;



                Console.WriteLine("Write the first number");
                double.TryParse(Console.ReadLine(), out a);

                Console.WriteLine("Write the second number");
                double.TryParse(Console.ReadLine(), out b);

                Console.WriteLine("Write the operand (+, -, /, *)");
                char.TryParse(Console.ReadLine(), out char c);

                Console.WriteLine("Total:{0}", cal.total);

                if (c == '+')
                    cal.Add(a, b);
                if (c == '-')
                    cal.Sub(a, b);
                if (c == '*')
                    cal.Mul(a, b);
                if (c == '/')
                    cal.Div(a, b);
            }
        }
        return 0;

1 个答案:

答案 0 :(得分:0)

首先,初始化您的total

double total = 0;

然后,执行total,而不是简单地分配给total += 另外,您需要先执行push,然后再将新结果添加到运行总计。

public void Add(double a, double b)
{
    var temp = a + b;
    Console.WriteLine("Sum:{0}", temp);
    result.Push(temp);
    total += temp;
}

对除法和乘法之类的其他运算遵循相同的想法。

编辑:

再次阅读您的问题,我不清楚您是否要将每个结果存储在堆栈中,或者是否要存储每个累进总数。上面的示例存储了每个单独操作的结果。如果您只想将每个新总计存储在堆栈中:

public void Add(double a, double b)
{
    total += a + b;
    Console.WriteLine("Sum:{0}", total);
    result.Push(total);
}