C#使用堆栈创建一个简单的计算器

时间:2019-06-09 01:11:19

标签: c#

我正在尝试使用堆栈创建一个具有撤消功能的简单计算器。如果有人可以告诉我我的代码哪里出了问题,那真是太棒了。看来我的某些变量未正确说明。我从未使用过堆栈进行编码。我阅读了有关它的文章,但我需要代码帮助以朝正确的方向发展。

 class Calculator
   {
    public Stack<double> result;
    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.DefaultIfEmpty())
        {
            Console.WriteLine("UNDO IS NOT AVAILABLE");
        }
        result.Pop();
        total = result.Pop();
        Console.WriteLine("Running total:{0}", total);
    }
    void clear()
    {
        while (result.DefaultIfEmpty())
            result.Pop();
        total = 0;
        Console.WriteLine("Running total:{0}", total);
    }
    int main()
    {
        Calculator cal;
        string line;
        while (true)
        {
            Console.WriteLine("Enter (Clear, Undo, Exit, Expression):");

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

                char c;

                if (c == '+')
                    cal.add(a, b);
                if (c == '-')
                    cal.sub(a, b);
                if (c == '*')
                    cal.mul(a, b);
                if (c == '/')
                    cal.div(a, b);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

这段代码有几个问题,

  1. 最重要的是,您不初始化任何变量(结果,cal,line,a,b,c)c#需要知道它们具有什么值以便能够执行某些操作他们。解决该问题似乎可以使您的计算器正常工作。

  2. 第二件事,您应该在用户输入行(我添加的内容)中将其转换为小写字母,因此无论您如何编写“清除,撤消或退出”功能,

  3. 关于您需要在那里初始化变量的表达式(我也添加了一些内容)

  4. 主体必须是静态的,每当编写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):");
            line = Console.ReadLine();
            if (line.ToLower() == "exit")
                break;
            else if (line.ToLower() == "undo")
                cal.undo();
            else if (line.ToLower() == "clear")
                cal.clear();
            else
            {
                double a, b;
                char c;

                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 c);


                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;
    }
}