如何使用多维数组向我的计算器程序添加历史记录功能?

时间:2017-05-03 18:39:17

标签: c#

我几天前开始学习C#,我现在正在玩简单的程序。目前,我使用Visual Studios 2015制作了一个简单的计算器。我想在我的程序中添加历史记录功能。我希望它通过使用多维数组存储过去10次计算的历史记录,并允许用户打印出他们的历史,但我对此缺乏经验。有人可以给我指点吗?这有助于进一步理解C#。

 class Program
    {
        static void Main(string[] args)
        {
            do
            {
                double result;

                Console.WriteLine("\nType your first number: ");
                string stringFirstNumber = Console.ReadLine();
                double firstNumber = Convert.ToDouble(stringFirstNumber);

                Console.WriteLine("Type your second number: ");
                string stringSecondNumber = Console.ReadLine();
                double secondNumber = Convert.ToDouble(stringSecondNumber);

                Console.WriteLine("Enter the operation + (addition), - (subtraction), * (multiplication), / (division), ^ (exponent), or % (modulus): ");
                string stringOperation = Console.ReadLine();

                switch (stringOperation)
                {
                    case "+":
                        result = firstNumber + secondNumber;
                        Console.WriteLine(firstNumber + " " + stringOperation + " " + secondNumber + " = " + result);
                        break;
                    case "addition":
                        result = firstNumber + secondNumber;
                        Console.WriteLine(firstNumber + " + " + secondNumber + " = " + result);
                        break;
                    case "-":
                        result = firstNumber - secondNumber;
                        Console.WriteLine(firstNumber + " " + stringOperation + " " + secondNumber + " = " + result);
                        break;
                    case "subraction":
                        result = firstNumber - secondNumber;
                        Console.WriteLine(firstNumber + " - " + secondNumber + " = " + result);
                        break;
                    case "*":
                        result = firstNumber * secondNumber;
                        Console.WriteLine(firstNumber + " " + stringOperation + " " + secondNumber + " = " + result);
                        break;
                    case "multiplication":
                        result = firstNumber * secondNumber;
                        Console.WriteLine(firstNumber + " * " + secondNumber + " = " + result);
                        break;
                    case "/":
                        result = firstNumber / secondNumber;
                        Console.WriteLine(firstNumber + " " + stringOperation + " " + secondNumber + " = " + result);
                        break;
                    case "division":
                        result = firstNumber / secondNumber;
                        Console.WriteLine(firstNumber + " / " + secondNumber + " = " + result);
                        break;
                    case "^":
                        result = Math.Pow(firstNumber, secondNumber);
                        Console.WriteLine(firstNumber + " " + stringOperation + " " + secondNumber + " = " + result);
                        break;
                    case "exponent":
                        result = Math.Pow(firstNumber, secondNumber);
                        Console.WriteLine(firstNumber + " ^ " + secondNumber + " = " + result);
                        break;
                    case "%":
                        result = firstNumber % secondNumber;
                        Console.WriteLine(firstNumber + " " + stringOperation + " " + secondNumber + " = " + result);
                        break;
                    case "modulus":
                        result = firstNumber % secondNumber;
                        Console.WriteLine(firstNumber + " % " + secondNumber + " = " + result);
                        break;
                    default:
                        Console.Write("Choose a valid option");
                        break;
                }
            } while (true);

        }
    }

2 个答案:

答案 0 :(得分:2)

你可以从这样的事情开始。设计一个简单的类来存储历史记录,其中应包含两个数字和操作。像这样:

public class CalculationHistory
{
    public double firstNumber { get; set; }
    public double secondNumber { get; set; }
    public string stringOperation { get; set; }

    public CalculationHistory(double firstNum, double secondNum, string op)
    {
        firstNumber = firstNum;
        secondNumber = secondNum;
        stringOperation = op;
    }
}

您可以维护这些集合,并根据您的要求将它们限制为10或任何其他数字。如果您只想维持有限数量的迭代,队列将更适合。

Queue<CalculationHistory> history = new Queue<CalculationHistory>();

每次执行操作时,请务必将其添加到队列中。

history.Enqueue(new CalculationHistory(firstNumber, secondNumber, stringOperation));

可能会在某处添加条件,检查队列是否已超出n个项目,然后根据需要检查.Dequeue()

PS:在您的代码中,您可能希望将case语句组合起来,而不是为同一操作分别使用。{/ p>

case "+":
case "addition":
    result = firstNumber + secondNumber;
    Console.WriteLine(firstNumber + " + " + secondNumber + " = " + result);
    break;

答案 1 :(得分:0)

@degant的答案导致了一个正确的解决方案,特别是从面向对象的视图。

然而,由于您似乎对使用数组实现问题感兴趣,这里有一个非常简单的示例,没有任何关于效率或OOP

// the number of history expressions to maintain
static readonly int MAX_HISTORY = 10;

// allocate the history as an array of objects (i.e. anything).
// this makes it easy to store multiple types (double, string, etc). 
static object[][] history = new object[MAX_HISTORY][];

// track the number of *valid* expressions in history.
static int historyCount = 0;

// add something to the history
static void historyAdd(double a, string op, double b)
{
    // make sure historyCount never exceeds MAX_HISTORY
    historyCount = Math.Min(historyCount + 1, MAX_HISTORY);
    // shift all expressions over 1 to make room for the new expression.
    // if the array is full, the oldest expression is removed.
    Array.Copy(history, 0, history, 1, historyCount - 1);

    // create a 1x3 array to hold the expression in slot 0, 
    // which was reserved by the preceding array shift         
    history[0] = new object[3];
    history[0][0] = a;
    history[0][1] = op;
    history[0][2] = b;
    // shorthand to do the above: history[0] = new object[] { a, op, b };
}
// demo the history
static void Main(string[] args)
{
    Random r = new Random();
    for (var i = 0; i < 20; i++)
        historyAdd(r.NextDouble(), "+", r.NextDouble());

    Console.WriteLine("History:");
    for (int i = 0; i < historyCount; i++)
    {
        object[] expression = history[i];
        Console.WriteLine("{0}) {1} {2} {3}", i + 1, expression[0], expression[1], expression[2]);
    }        
    Console.WriteLine();
    Console.WriteLine("Press a key to exit");
    Console.ReadKey();
}

请注意这是雇用boxing,我通常会避免使用。