Jak zrobić historię operacji kalkulatora?

0

Witam mam pytanie jak zrobić historię 10 ostatnich operacji kalkulatora za pomocą typów generycznych próbowałem to zrobić za pomocą kolejki ale nie działało po mojej myśli

 class Program
    {
       
        static void Main(string[] args)
        {

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

            Console.WriteLine("Podaj liczbe");

            double x = Convert.ToDouble(Console.ReadLine());

            while (true)
            {


                Console.WriteLine("Wybierz rodzaj operacji:");
                string z = Console.ReadLine();

                 Console.WriteLine("Podaj liczbe");

                double y = Convert.ToDouble(Console.ReadLine());

                switch (z)
                {
                    case "+":
                        Console.WriteLine("+");
                        x = (x + y);  
                        Console.WriteLine("Wynik : " +x);
                        break;
                    case "-":
                        Console.WriteLine("-");
                        x = (x - y); 
                        Console.WriteLine("Wynik : " +x);
                        break;
                    case "*":
                        Console.WriteLine("*");
                        x = (x * y);  
                        Console.WriteLine("Wynik : " +x);
                        break;
                    case "/":
                        if (y == 0)
                        {
                            Console.WriteLine("Nie dziel przez 0");
                        }
                        else
                        {
                            Console.WriteLine("/");
                            x = (x / y);
                            Console.WriteLine("Wynik : " +x);
                        }
                        break;
                    case "Q":
                    case "q":

                        Console.WriteLine("Good Bye!");
                        Environment.Exit(1);
                        break;
                    case "p":
                    case "P":
                        Console.WriteLine("Lista ostatnich operacji : ");
                        break;
                }

            }
        }

       
    }

}

1

A gdzie Masz dodawanie do tej history?

0

I jak wygląda klasa CalculationHistory?

EDIT: znalazłem kod którego prawdopodobnie użyłeś

W takim razie jak chcesz dodać równanie do historii to w taki sposób:

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

history.add(new CalculationHistory(10, 20, "+"));
0
public class CalculationHistory
    {
        public double x { get; set; }
        public double y { get; set; }
        public string z { get; set; }

        public CalculationHistory(double first, double sec, string res)
        {
            x = first;
            y = sec;
            z = res;
        }

    }

Tak wygląda, ale nie działa tak jak powinna

0

A w jaki sposób ma działać?
Może napisz w pseudokodzie\c# w jaki sposób chciałbyś jej używać?

1
public class CalculationHistory
        {
            public CalculationHistory()
            {
                Items = new List<CalculationHistoryItem>();
            }

            private List<CalculationHistoryItem> Items { get; set; }
            public void Add(double x, double y, string z)
            {
                Items.Add(new CalculationHistoryItem(x, y, z));
            }
            public IEnumerable<CalculationHistoryItem> ReadAll
            {
                get
                {
                    foreach (var item in Items)
                        yield return item;
                }
            }
        }

        public class CalculationHistoryItem
        {
            public double x { get; set; }
            public double y { get; set; }
            public string z { get; set; }

            public CalculationHistoryItem(double first, double sec, string res)
            {
                x = first;
                y = sec;
                z = res;
            }
           public override string ToString()
            {
                return x + " " + z + " " + y;
            }
        }

Przykład użycia:

CalculationHistory history = new CalculationHistory();
            history.Add(0, 10, "+");
            history.Add(20, 10, "-");
            foreach (var item in history.ReadAll)
                Console.WriteLine(item);
1

@Grzegorz Świdwa: A po co takie cuda:

public IEnumerable<CalculationHistoryItem> ReadAll
            {
                get
                {
                    foreach (var item in Items)
                        yield return item;
                }
            }

?

1 użytkowników online, w tym zalogowanych: 0, gości: 1