Untitled
unknown
plain_text
2 years ago
4.3 kB
6
Indexable
class Program { static void Main(string[] args) { double number; IUI ui = new ConsoleIO(); Stack<double> numberStack = new Stack<double>(); Controller controller = new Controller(); //finns en egent stack i .net DisplayMenuContent(); //DoubleStack stack = new DoubleStack(); while (true) { ui.WriteOutput("Välj några siffror, sedan kan du välja vad dessa ska göra\n"); number = controller.CheckValidDoubleInput(); AppendStack(number); DisplayStack(); string LetterOrArithmetic = controller.CheckValidLetterInput(ui.ReadInput()); //Om man skriver in fler än 1 char så blir det fel string input = Console.ReadLine().Trim(); //om man trycker enter blir värdet i command ett mellanslag annars första strecket if (input == "") input = " "; //kontrollerar om första tecknet är en digit //If the command is a digit, it is interpreted as a number, converted to double, and pushed onto the stack. char command = input[0]; if (Char.IsDigit(command)) { //Konverterar även fler tecken än det första. double value = Convert.ToDouble(input); numberStack.Push(value); } //If the command is '+', two values are popped from the stack, added together, and the result is pushed back onto the stack. //Tar de sista skrivna värderna i stacken och lägger ihop dem if (command == '+') { numberStack.Push(numberStack.Pop() + numberStack.Pop()); } //If the command is '*', two values are popped from the stack, multiplied together, and the result is pushed back onto the stack. else if (command == '*') { numberStack.Push(numberStack.Pop() * numberStack.Pop()); } //If the command is '-', two values are popped from the stack, subtracted from each other (order matters), and the result is pushed back onto the stack. //Tar översta värdet först och sparar det. Andra värdet subtraheras, dvs det sista in. Last in first ouy. else if (command == '-') { double d = numberStack.Pop(); numberStack.Push(numberStack.Pop() - d); } //If the command is '/', two values are popped from the stack, divided (order matters), and the result is pushed back onto the stack. else if (command == '/') { double d = numberStack.Pop(); numberStack.Push(numberStack.Pop() / d); } //If the command is 'c', the stack is cleared. //Sätter depth till noll else if (command == 'c') { numberStack.Clear(); } //If the command is 'q', the program breaks out of the loop and terminates. else if (command == 'q') { break; } else { //If the command is not recognized, an error message is displayed. Console.WriteLine("Illegal command, ignored"); } } void DisplayMenuContent() { ui.WriteOutput( "tryck - för att subthrahera\n" + "tryck + för att addera\n" + "tryck * för att multiplicera\n" + "tryck / för att dividera\n" + "tryck q för att stänga programmet\n" + "tryck c för att rensa\n" + "\n"); } void DisplayStack() { ui.WriteOutput("["); //Skriver ut en lista numberStack.ToList().ForEach(number => ui.WriteOutput(number.ToString()+ ",")); ui.WriteOutput("]\n"); } void AppendStack(double number) { numberStack.Push(number); } } }
Editor is loading...