Answer the question
In order to leave comments, you need to log in
Calculator on mvc with transaction history?
In the task, you need to implement a calculator with a log that displays previous calculations. Can you tell me how to implement this log?
Answer the question
In order to leave comments, you need to log in
If for the web and specifically on ASP.NET , then you can:
1. Implement a structure or class to store a data element (history) and use a static class, property or field (for example, at the controller class level) for the entire history:
public class HistoryItem
{
public double X { get; set; }
public double Y { get; set; }
public string Operation { get; set; }
// предполагается, что журнал подразумевает хранение истории вычислений,
// и хотя можно повторно провести вычисления, в журнале такого по идее быть не должно,
public double Result { get; set; }
}
// ...
public static List<HistoryItem> History = new List<HistoryItem>();
// ...
History.Add(new HistoryItem { X = 123, Y = 456, Operation = "+", Result = 579 });
History.Add(new HistoryItem { X = 5, Y = 5, Operation = "*", Result = 25 });
public static List<string> History = new List<string>();
// ...
History.Add("2 * 2 = 4");
History.Add("3 + 4 = 7");
using System.Collections.Concurrent;
// ...
public static ConcurrentBag<string> History = new ConcurrentBag<string>();
// ...
History.Add("123 + 345 = 468");
History.Add("7 * 7 = 49");
// добавить
System.IO.File.AppendText("history.log", "5 * 5 = 25");
// прочитать историю
// System.IO.File.ReadAllText("history.log");
var historyData = window.localStorage.getItem('history');
var history = historyData ? JSON.parse(historyData) : [];
history.push('1 + 1 = 2');
history.push('2 + 2 = 4');
history.push('4 + 4 = 8');
window.localStorage.setItem('history', JSON.stringify(history));
For the log, you can use the Memento pattern: https://en.wikipedia.org/wiki/Memento_pattern
Well, I don’t know how others will say, I would use the structure to write everything as a large array
https://docs.microsoft.com/en-us/dotnet/csharp/pro...
public struct CoOrds
{
public int x, y;
public CoOrds(int p1, int p2)
{
x = p1;
y = p2;
}
}
class TestCoOrds
{
static void Main()
{
// Initialize:
CoOrds coords1 = new CoOrds();
CoOrds coords2 = new CoOrds(10, 10);
// Display results:
Console.Write("CoOrds 1: ");
Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y);
Console.Write("CoOrds 2: ");
Console.WriteLine("x = {0}, y = {1}", coords2.x, coords2.y);
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question