Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _6211Assignment01
- {
- class Program
- {
- static string input;
- static bool exit;
- static int selection;
- static void Main(string[] args)
- {
- exit = false;
- //loop main menu until selection 5 is made
- while (!exit)
- {
- Console.Clear();
- Console.WriteLine("/~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\");
- Console.WriteLine("| |");
- Console.WriteLine("| Main Menu |");
- Console.WriteLine("| |");
- Console.WriteLine("| 1: queue menu |");
- Console.WriteLine("| 2: palindrome checker |");
- Console.WriteLine("| 3: convert infix to postfix |");
- Console.WriteLine("| 4: evaluate postfix equation |");
- Console.WriteLine("| |");
- Console.WriteLine("| 5: quit to menu |");
- Console.WriteLine("| |");
- Console.WriteLine("\\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/");
- Console.WriteLine();
- Console.Write("Please enter selection:");
- input = Console.ReadLine();
- //prevent crashing on invalid input and allow reselection
- while (!Int32.TryParse(input, out selection))
- {
- Console.WriteLine();
- Console.WriteLine("Invalid input, please try again");
- Console.Write("Please enter selection:");
- input = Console.ReadLine();
- }
- Console.Clear();
- switch (selection)
- {
- case 1:
- Question1.menu();
- break;
- case 2:
- Question2.checker();
- Console.ReadKey();
- break;
- case 3:
- Question3.converter();
- Console.ReadKey();
- break;
- case 4:
- Question4.start();
- Console.ReadKey();
- break;
- case 5:
- exit = true;
- break;
- default:
- Console.WriteLine("Invalid number, please try again");
- Console.ReadKey();
- break;
- }
- }
- }
- }
- public static class Question1
- {
- static Queue<string> Queue1 = new Queue<string>();
- static Array Array1;
- static string input;
- static int selection;
- static string check;
- static bool exit;
- public static void menu()
- {
- exit = false;
- while (!exit)
- {
- Console.Clear();
- Console.WriteLine("/~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\");
- Console.WriteLine("| |");
- Console.WriteLine("| Queue Menu |");
- Console.WriteLine("| |");
- Console.WriteLine("| 1: print queue |");
- Console.WriteLine("| 2: add to queue |");
- Console.WriteLine("| 3: remove from queue |");
- Console.WriteLine("| 4: check if queue contains item |");
- Console.WriteLine("| 5: convert queue to array and print contents |");
- Console.WriteLine("| |");
- Console.WriteLine("| 6: quit to menu |");
- Console.WriteLine("| |");
- Console.WriteLine("\\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/");
- Console.WriteLine();
- Console.Write("Please enter selection:");
- input = Console.ReadLine();
- while (!Int32.TryParse(input, out selection))
- {
- Console.WriteLine();
- Console.WriteLine("Invalid input, please try again");
- Console.Write("Please enter selection:");
- input = Console.ReadLine();
- }
- Console.Clear();
- switch (selection)
- {
- case 1:
- printQueue();
- Console.ReadKey();
- break;
- case 2:
- Console.Write("please enter item to add:");
- input = Console.ReadLine();
- enqueue(input);
- Console.ReadKey();
- break;
- case 3:
- dequeue();
- Console.ReadKey();
- break;
- case 4:
- Console.Write("please enter item to check for:");
- input = Console.ReadLine();
- Contains(input);
- Console.ReadKey();
- break;
- case 5:
- ToArray();
- Console.ReadKey();
- break;
- case 6:
- exit = true;
- break;
- default:
- Console.WriteLine("Invalid number, please try again");
- Console.ReadKey();
- break;
- }
- }
- }
- static void printQueue()
- {
- Console.WriteLine("The queue currently contains:");
- Console.WriteLine();
- foreach (string x in Queue1)
- {
- Console.WriteLine($"- {x}");
- }
- }
- static void enqueue(string input)
- {
- Queue1.Enqueue(input);
- Console.WriteLine();
- Console.WriteLine($"{input} has been added to the queue");
- }
- static void dequeue()
- {
- if (Queue1.Count > 0)
- {
- check = Queue1.Dequeue();
- Console.WriteLine($"{check} has been dequeued from the queue");
- }
- else
- {
- Console.WriteLine("Queue is already empty");
- }
- }
- static void Contains(string item)
- {
- if (Queue1.Contains(item))
- {
- Console.WriteLine($"Queue contains {item}");
- }
- else
- {
- Console.WriteLine($"Queue does not contain {item}");
- }
- }
- static void ToArray()
- {
- if (Queue1.Count > 0)
- {
- Array1 = Queue1.ToArray();
- Console.WriteLine("Queue has been converted to an array");
- Console.WriteLine();
- Console.WriteLine("Array contents:");
- Console.WriteLine();
- foreach (var x in Array1)
- {
- Console.WriteLine($"- {x}");
- }
- }
- else
- {
- Console.WriteLine("No items in queue to convert to array");
- }
- }
- }
- public static class Question2
- {
- static string input;
- static string check;
- static Stack<Char> palinStack = new Stack<Char>();
- public static void checker()
- {
- palinStack.Clear();
- check = null;
- Console.Write("Please enter word: ");
- input = Console.ReadLine().ToLower();
- input = input.Replace(" ", "");
- foreach (char c in input)
- {
- if (Char.IsPunctuation(c))
- {
- input = input.Replace(c.ToString(), "");
- }
- else
- {
- palinStack.Push(c);
- }
- }
- foreach (char c in palinStack)
- {
- check += c;
- }
- if (check == input)
- {
- Console.WriteLine($"{input} is a palindrome");
- }
- else
- {
- Console.WriteLine($"{input} is not a palindrome");
- }
- }
- }
- public static class Question3
- {
- static Stack<char> stack = new Stack<char>();
- static string input;
- static string postfix;
- static char ch;
- static int left;
- static int right;
- static int prio;
- public static void converter()
- {
- stack.Clear();
- postfix = "";
- Console.Write("please enter infix equation to convert:");
- input = Console.ReadLine();
- if (checkParenthesis(input))
- {
- Console.WriteLine(convert(input));
- }
- else
- {
- Console.WriteLine("parenthesis unbalanced, equation invalid");
- }
- }
- static string convert(string infix)
- {
- postfix = "";
- stack.Clear();
- stack.Push('(');
- infix += ')';
- for (int i = 0; i < infix.Length; i++)
- {
- ch = infix[i];
- if (char.IsDigit(ch))
- {
- postfix += ch;
- }
- else if (ch == '(')
- {
- stack.Push(ch);
- }
- else if (isOperator(ch))
- {
- prio = precedence(ch);
- while(precedence(stack.Peek()) >= prio)
- {
- postfix += stack.Pop();
- }
- stack.Push(ch);
- }
- else if (ch == ')')
- {
- while(stack.Peek() != '(')
- {
- postfix += stack.Pop();
- }
- stack.Pop();
- }
- }
- return (postfix);
- }
- static int precedence(char c)
- {
- if(c == '^')
- {
- return (3);
- }
- else if ("*/%".Contains(c))
- {
- return (2);
- }
- else if ("+-".Contains(c))
- {
- return (1);
- }
- else
- {
- return (0);
- }
- }
- static bool checkParenthesis(string test)
- {
- right = 0;
- left = 0;
- foreach (char c in test)
- {
- if (c == '(')
- {
- left++;
- }
- else if (c == ')')
- {
- right++;
- }
- }
- if (left == right)
- {
- return (true);
- }
- else
- {
- return (false);
- }
- }
- static bool isOperator(char c)
- {
- if ("^/%*+-".Contains(c))
- {
- return (true);
- }
- else
- {
- return (false);
- }
- }
- }
- public static class Question4
- {
- static string input;
- static int answer;
- static Stack<string> stack = new Stack<string>();
- static int num1;
- static int num2;
- static bool invalid = false;
- public static void start()
- {
- Console.Write("Please enter postfix equation:");
- input = Console.ReadLine();
- EvaluatePostfixExpression(input);
- }
- public static void EvaluatePostfixExpression(string postfix)
- {
- invalid = false;
- stack.Clear();
- Console.WriteLine();
- foreach(char c in postfix)
- {
- if (char.IsNumber(c))
- {
- stack.Push(c.ToString());
- }
- else
- {
- if (stack.Count > 1)
- {
- //put into numbers (reverse order due to stack)
- num2 = Int32.Parse(stack.Pop());
- num1 = Int32.Parse(stack.Pop());
- stack.Push(calculate(num1, num2, c).ToString());
- }
- else
- {
- Console.WriteLine("Invalid postfix expression");
- invalid = true;
- }
- }
- if (invalid)
- {
- break;
- }
- }
- if (stack.Count != 1)
- {
- invalid = true;
- Console.WriteLine("invalid postfix expression");
- }
- if (!invalid)
- {
- Console.WriteLine($"{postfix} = {stack.Pop()}");
- }
- }
- static int calculate(int a, int b, char op)
- {
- switch (op)
- {
- case '+':
- answer = a + b;
- break;
- case '-':
- answer = a - b;
- break;
- case '*':
- answer = a * b;
- break;
- case '/':
- answer = a / b;
- break;
- case '%':
- answer = a % b;
- break;
- case '^':
- answer = (int)Math.Pow(a, b);
- break;
- default:
- Console.WriteLine("Invalid postfix expression");
- invalid = true;
- break;
- }
- return (answer);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement