Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _6211Assignment01
  8. {
  9. class Program
  10. {
  11. static string input;
  12. static bool exit;
  13. static int selection;
  14.  
  15. static void Main(string[] args)
  16. {
  17. exit = false;
  18. //loop main menu until selection 5 is made
  19. while (!exit)
  20. {
  21. Console.Clear();
  22. Console.WriteLine("/~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\");
  23. Console.WriteLine("| |");
  24. Console.WriteLine("| Main Menu |");
  25. Console.WriteLine("| |");
  26. Console.WriteLine("| 1: queue menu |");
  27. Console.WriteLine("| 2: palindrome checker |");
  28. Console.WriteLine("| 3: convert infix to postfix |");
  29. Console.WriteLine("| 4: evaluate postfix equation |");
  30. Console.WriteLine("| |");
  31. Console.WriteLine("| 5: quit to menu |");
  32. Console.WriteLine("| |");
  33. Console.WriteLine("\\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/");
  34. Console.WriteLine();
  35. Console.Write("Please enter selection:");
  36.  
  37. input = Console.ReadLine();
  38.  
  39. //prevent crashing on invalid input and allow reselection
  40. while (!Int32.TryParse(input, out selection))
  41. {
  42. Console.WriteLine();
  43. Console.WriteLine("Invalid input, please try again");
  44. Console.Write("Please enter selection:");
  45. input = Console.ReadLine();
  46. }
  47.  
  48. Console.Clear();
  49.  
  50. switch (selection)
  51. {
  52. case 1:
  53. Question1.menu();
  54. break;
  55.  
  56. case 2:
  57. Question2.checker();
  58. Console.ReadKey();
  59. break;
  60.  
  61. case 3:
  62. Question3.converter();
  63. Console.ReadKey();
  64. break;
  65.  
  66. case 4:
  67. Question4.start();
  68. Console.ReadKey();
  69. break;
  70.  
  71. case 5:
  72. exit = true;
  73. break;
  74.  
  75. default:
  76. Console.WriteLine("Invalid number, please try again");
  77. Console.ReadKey();
  78. break;
  79. }
  80.  
  81. }
  82. }
  83. }
  84.  
  85. public static class Question1
  86. {
  87. static Queue<string> Queue1 = new Queue<string>();
  88. static Array Array1;
  89.  
  90. static string input;
  91. static int selection;
  92. static string check;
  93. static bool exit;
  94.  
  95. public static void menu()
  96. {
  97. exit = false;
  98. while (!exit)
  99. {
  100. Console.Clear();
  101. Console.WriteLine("/~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\");
  102. Console.WriteLine("| |");
  103. Console.WriteLine("| Queue Menu |");
  104. Console.WriteLine("| |");
  105. Console.WriteLine("| 1: print queue |");
  106. Console.WriteLine("| 2: add to queue |");
  107. Console.WriteLine("| 3: remove from queue |");
  108. Console.WriteLine("| 4: check if queue contains item |");
  109. Console.WriteLine("| 5: convert queue to array and print contents |");
  110. Console.WriteLine("| |");
  111. Console.WriteLine("| 6: quit to menu |");
  112. Console.WriteLine("| |");
  113. Console.WriteLine("\\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/");
  114. Console.WriteLine();
  115. Console.Write("Please enter selection:");
  116.  
  117. input = Console.ReadLine();
  118.  
  119. while (!Int32.TryParse(input, out selection))
  120. {
  121. Console.WriteLine();
  122. Console.WriteLine("Invalid input, please try again");
  123. Console.Write("Please enter selection:");
  124. input = Console.ReadLine();
  125. }
  126.  
  127. Console.Clear();
  128.  
  129. switch (selection)
  130. {
  131. case 1:
  132. printQueue();
  133. Console.ReadKey();
  134. break;
  135.  
  136. case 2:
  137. Console.Write("please enter item to add:");
  138. input = Console.ReadLine();
  139. enqueue(input);
  140. Console.ReadKey();
  141. break;
  142.  
  143. case 3:
  144. dequeue();
  145. Console.ReadKey();
  146. break;
  147.  
  148. case 4:
  149. Console.Write("please enter item to check for:");
  150. input = Console.ReadLine();
  151. Contains(input);
  152. Console.ReadKey();
  153. break;
  154.  
  155. case 5:
  156. ToArray();
  157. Console.ReadKey();
  158. break;
  159.  
  160. case 6:
  161. exit = true;
  162. break;
  163.  
  164. default:
  165. Console.WriteLine("Invalid number, please try again");
  166. Console.ReadKey();
  167. break;
  168. }
  169. }
  170. }
  171.  
  172. static void printQueue()
  173. {
  174. Console.WriteLine("The queue currently contains:");
  175. Console.WriteLine();
  176. foreach (string x in Queue1)
  177. {
  178. Console.WriteLine($"- {x}");
  179. }
  180. }
  181. static void enqueue(string input)
  182. {
  183. Queue1.Enqueue(input);
  184. Console.WriteLine();
  185. Console.WriteLine($"{input} has been added to the queue");
  186. }
  187. static void dequeue()
  188. {
  189. if (Queue1.Count > 0)
  190. {
  191. check = Queue1.Dequeue();
  192. Console.WriteLine($"{check} has been dequeued from the queue");
  193. }
  194. else
  195. {
  196. Console.WriteLine("Queue is already empty");
  197. }
  198. }
  199. static void Contains(string item)
  200. {
  201. if (Queue1.Contains(item))
  202. {
  203. Console.WriteLine($"Queue contains {item}");
  204. }
  205. else
  206. {
  207. Console.WriteLine($"Queue does not contain {item}");
  208. }
  209. }
  210. static void ToArray()
  211. {
  212. if (Queue1.Count > 0)
  213. {
  214. Array1 = Queue1.ToArray();
  215. Console.WriteLine("Queue has been converted to an array");
  216. Console.WriteLine();
  217. Console.WriteLine("Array contents:");
  218. Console.WriteLine();
  219. foreach (var x in Array1)
  220. {
  221. Console.WriteLine($"- {x}");
  222. }
  223. }
  224. else
  225. {
  226. Console.WriteLine("No items in queue to convert to array");
  227. }
  228. }
  229. }
  230.  
  231. public static class Question2
  232. {
  233. static string input;
  234. static string check;
  235. static Stack<Char> palinStack = new Stack<Char>();
  236.  
  237. public static void checker()
  238. {
  239. palinStack.Clear();
  240. check = null;
  241.  
  242. Console.Write("Please enter word: ");
  243. input = Console.ReadLine().ToLower();
  244. input = input.Replace(" ", "");
  245. foreach (char c in input)
  246. {
  247. if (Char.IsPunctuation(c))
  248. {
  249. input = input.Replace(c.ToString(), "");
  250. }
  251. else
  252. {
  253. palinStack.Push(c);
  254. }
  255. }
  256. foreach (char c in palinStack)
  257. {
  258. check += c;
  259. }
  260. if (check == input)
  261. {
  262. Console.WriteLine($"{input} is a palindrome");
  263. }
  264. else
  265. {
  266. Console.WriteLine($"{input} is not a palindrome");
  267. }
  268.  
  269. }
  270. }
  271.  
  272. public static class Question3
  273. {
  274. static Stack<char> stack = new Stack<char>();
  275. static string input;
  276. static string postfix;
  277. static char ch;
  278. static int left;
  279. static int right;
  280. static int prio;
  281.  
  282. public static void converter()
  283. {
  284. stack.Clear();
  285. postfix = "";
  286. Console.Write("please enter infix equation to convert:");
  287. input = Console.ReadLine();
  288. if (checkParenthesis(input))
  289. {
  290. Console.WriteLine(convert(input));
  291. }
  292. else
  293. {
  294. Console.WriteLine("parenthesis unbalanced, equation invalid");
  295. }
  296. }
  297.  
  298. static string convert(string infix)
  299. {
  300. postfix = "";
  301. stack.Clear();
  302. stack.Push('(');
  303. infix += ')';
  304.  
  305. for (int i = 0; i < infix.Length; i++)
  306. {
  307. ch = infix[i];
  308. if (char.IsDigit(ch))
  309. {
  310. postfix += ch;
  311. }
  312. else if (ch == '(')
  313. {
  314. stack.Push(ch);
  315. }
  316. else if (isOperator(ch))
  317. {
  318. prio = precedence(ch);
  319. while(precedence(stack.Peek()) >= prio)
  320. {
  321. postfix += stack.Pop();
  322. }
  323. stack.Push(ch);
  324. }
  325. else if (ch == ')')
  326. {
  327. while(stack.Peek() != '(')
  328. {
  329. postfix += stack.Pop();
  330. }
  331. stack.Pop();
  332. }
  333. }
  334. return (postfix);
  335. }
  336.  
  337. static int precedence(char c)
  338. {
  339. if(c == '^')
  340. {
  341. return (3);
  342. }
  343. else if ("*/%".Contains(c))
  344. {
  345. return (2);
  346. }
  347. else if ("+-".Contains(c))
  348. {
  349. return (1);
  350. }
  351. else
  352. {
  353. return (0);
  354. }
  355. }
  356.  
  357. static bool checkParenthesis(string test)
  358. {
  359. right = 0;
  360. left = 0;
  361. foreach (char c in test)
  362. {
  363. if (c == '(')
  364. {
  365. left++;
  366. }
  367. else if (c == ')')
  368. {
  369. right++;
  370. }
  371. }
  372. if (left == right)
  373. {
  374. return (true);
  375. }
  376. else
  377. {
  378. return (false);
  379. }
  380. }
  381.  
  382. static bool isOperator(char c)
  383. {
  384. if ("^/%*+-".Contains(c))
  385. {
  386. return (true);
  387. }
  388. else
  389. {
  390. return (false);
  391. }
  392. }
  393. }
  394.  
  395. public static class Question4
  396. {
  397. static string input;
  398. static int answer;
  399. static Stack<string> stack = new Stack<string>();
  400. static int num1;
  401. static int num2;
  402. static bool invalid = false;
  403.  
  404. public static void start()
  405. {
  406. Console.Write("Please enter postfix equation:");
  407. input = Console.ReadLine();
  408. EvaluatePostfixExpression(input);
  409. }
  410.  
  411. public static void EvaluatePostfixExpression(string postfix)
  412. {
  413. invalid = false;
  414. stack.Clear();
  415. Console.WriteLine();
  416. foreach(char c in postfix)
  417. {
  418. if (char.IsNumber(c))
  419. {
  420. stack.Push(c.ToString());
  421. }
  422. else
  423. {
  424. if (stack.Count > 1)
  425. {
  426. //put into numbers (reverse order due to stack)
  427. num2 = Int32.Parse(stack.Pop());
  428. num1 = Int32.Parse(stack.Pop());
  429. stack.Push(calculate(num1, num2, c).ToString());
  430. }
  431. else
  432. {
  433. Console.WriteLine("Invalid postfix expression");
  434. invalid = true;
  435. }
  436. }
  437. if (invalid)
  438. {
  439. break;
  440. }
  441. }
  442.  
  443. if (stack.Count != 1)
  444. {
  445. invalid = true;
  446. Console.WriteLine("invalid postfix expression");
  447. }
  448.  
  449. if (!invalid)
  450. {
  451. Console.WriteLine($"{postfix} = {stack.Pop()}");
  452. }
  453. }
  454.  
  455. static int calculate(int a, int b, char op)
  456. {
  457. switch (op)
  458. {
  459. case '+':
  460. answer = a + b;
  461. break;
  462.  
  463. case '-':
  464. answer = a - b;
  465. break;
  466.  
  467. case '*':
  468. answer = a * b;
  469. break;
  470.  
  471. case '/':
  472. answer = a / b;
  473. break;
  474.  
  475. case '%':
  476. answer = a % b;
  477. break;
  478.  
  479. case '^':
  480. answer = (int)Math.Pow(a, b);
  481. break;
  482.  
  483. default:
  484. Console.WriteLine("Invalid postfix expression");
  485. invalid = true;
  486. break;
  487. }
  488.  
  489. return (answer);
  490. }
  491. }
  492. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement