Advertisement
Cinestra

Starting over Homework 2 Number 5

Mar 29th, 2023
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. // Homework 2 Problem 5.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include <iostream>
  5. #include <stack>
  6. #include <string>
  7. #include "Set.h"
  8.  
  9. using namespace std;
  10.  
  11. int priority(char infix_input)
  12. {
  13. if (infix_input == '!')
  14. return 1;
  15.  
  16. else if (infix_input == '&')
  17. return 2;
  18.  
  19. else if (infix_input == '|')
  20. return 3;
  21.  
  22. return 0;
  23. }
  24.  
  25. string infix_to_postfix(string infix, string& postfix)
  26. {
  27. int string_place = 0;
  28. stack<char> stack_for_nonletters;
  29.  
  30. // We must account for if the infix and postfix inputs are the same
  31. // So let's make a copy of the infix, use the copy of the infix whenever possible, and clear the postfix
  32. string infix_copy = infix;
  33. postfix = "";
  34.  
  35. while (string_place < infix_copy.size())
  36. {
  37. char char_in_string = infix_copy[string_place];
  38.  
  39. // If it's simply a letter, add to the postfix expression
  40. if (char_in_string >= 'a' && char_in_string <= 'z' || char_in_string >= 'A' && char_in_string <= 'Z')
  41. {
  42. postfix += char_in_string;
  43. string_place++;
  44. }
  45.  
  46. // If there is an open brack, push it on the stack
  47. else if (char_in_string == '(')
  48. {
  49. stack_for_nonletters.push(char_in_string);
  50. string_place++;
  51. }
  52.  
  53. // If it is a closed bracket, keep popping from the stack until a closing pair open bracket is found
  54. else if (char_in_string == ')')
  55. {
  56. while (stack_for_nonletters.top() != '(')
  57. {
  58. postfix += stack_for_nonletters.top();
  59. stack_for_nonletters.pop();
  60. }
  61.  
  62. // Now pop the open bracket
  63. stack_for_nonletters.pop();
  64. string_place++;
  65. }
  66.  
  67. // Let's now take into account the operatators
  68. else
  69. {
  70. // If the stack isn't empty and the current character in the string is of lower priority than one about to be added
  71. // We will pop the stack until an operand of lower priority
  72. while (!stack_for_nonletters.empty() && priority(char_in_string) <= priority(stack_for_nonletters.top()))
  73. {
  74. postfix += stack_for_nonletters.top();
  75. stack_for_nonletters.pop();
  76. }
  77.  
  78. stack_for_nonletters.push(char_in_string);
  79. string_place++;
  80. }
  81. }
  82.  
  83. // If we have finished going through the infix expression,
  84. while (!stack_for_nonletters.empty())
  85. {
  86. postfix += stack_for_nonletters.top();
  87. stack_for_nonletters.pop();
  88. }
  89.  
  90. return postfix;
  91. }
  92.  
  93. bool isValid(string infix)
  94. {
  95.  
  96. }
  97.  
  98. int evaluate(string infix, const Set& trueValues, const Set& falseValues, string& postfix, bool& result)
  99. {
  100. postfix = infix_to_postfix(infix, postfix);
  101.  
  102. if (validate(postfix) == false)
  103. exit(-1);
  104.  
  105.  
  106. }
  107.  
  108. // Evaluate a boolean expression
  109. // If infix is a syntactically valid infix boolean expression whose
  110. // only operands are single lower case letters (whether or not they
  111. // appear in the values sets), then postfix is set to the postfix
  112. // form of the expression. If not, postfix might or might not be
  113. // changed, result is unchanged, and the function returns 1.
  114. //
  115. // If infix is a syntactically valid infix boolean expression whose
  116. // only operands are single lower case letters:
  117. //
  118. // If every operand letter in the expression appears in either
  119. // trueValues or falseValues but not both, then result is set to the
  120. // result of evaluating the expression (using for each letter in the
  121. // expression the value true if that letter appears in trueValues or
  122. // false if that letter appears in false values) and the function
  123. // returns 0.
  124. //
  125. // Otherwise, result is unchanged and the value the function returns
  126. // depends on these two conditions:
  127. // at least one letter in the expression is in neither the
  128. // trueValues nor the falseValues sets; and
  129. // at least one letter in the expression is in both the
  130. // trueValues and the falseValues set.
  131. // If only the first condition holds, the function returns 2; if
  132. // only the second holds, the function returns 3. If both hold
  133. // the function returns either 2 or 3 (and the function is not
  134. // required to return the same one if called another time with the
  135. // same arguments).
  136.  
  137. int main()
  138. {
  139. string infix = "!(n|u) ";
  140. string postfix;
  141. infix_to_postfix(infix, postfix);
  142. cout << postfix;
  143. }
  144.  
  145. // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
  146. // Debug program: F5 or Debug > Start Debugging menu
  147.  
  148. // Tips for Getting Started:
  149. // 1. Use the Solution Explorer window to add/manage files
  150. // 2. Use the Team Explorer window to connect to source control
  151. // 3. Use the Output window to see build output and other messages
  152. // 4. Use the Error List window to view errors
  153. // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
  154. // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement