Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Homework 2 Problem 5.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include <iostream>
- #include <stack>
- #include <string>
- #include "Set.h"
- using namespace std;
- int priority(char infix_input)
- {
- if (infix_input == '!')
- return 1;
- else if (infix_input == '&')
- return 2;
- else if (infix_input == '|')
- return 3;
- return 0;
- }
- string infix_to_postfix(string infix, string& postfix)
- {
- int string_place = 0;
- stack<char> stack_for_nonletters;
- // We must account for if the infix and postfix inputs are the same
- // So let's make a copy of the infix, use the copy of the infix whenever possible, and clear the postfix
- string infix_copy = infix;
- postfix = "";
- while (string_place < infix_copy.size())
- {
- char char_in_string = infix_copy[string_place];
- // If it's simply a letter, add to the postfix expression
- if (char_in_string >= 'a' && char_in_string <= 'z' || char_in_string >= 'A' && char_in_string <= 'Z')
- {
- postfix += char_in_string;
- string_place++;
- }
- // If there is an open brack, push it on the stack
- else if (char_in_string == '(')
- {
- stack_for_nonletters.push(char_in_string);
- string_place++;
- }
- // If it is a closed bracket, keep popping from the stack until a closing pair open bracket is found
- else if (char_in_string == ')')
- {
- while (stack_for_nonletters.top() != '(')
- {
- postfix += stack_for_nonletters.top();
- stack_for_nonletters.pop();
- }
- // Now pop the open bracket
- stack_for_nonletters.pop();
- string_place++;
- }
- // Let's now take into account the operatators
- else
- {
- // If the stack isn't empty and the current character in the string is of lower priority than one about to be added
- // We will pop the stack until an operand of lower priority
- while (!stack_for_nonletters.empty() && priority(char_in_string) <= priority(stack_for_nonletters.top()))
- {
- postfix += stack_for_nonletters.top();
- stack_for_nonletters.pop();
- }
- stack_for_nonletters.push(char_in_string);
- string_place++;
- }
- }
- // If we have finished going through the infix expression,
- while (!stack_for_nonletters.empty())
- {
- postfix += stack_for_nonletters.top();
- stack_for_nonletters.pop();
- }
- return postfix;
- }
- bool isValid(string infix)
- {
- }
- int evaluate(string infix, const Set& trueValues, const Set& falseValues, string& postfix, bool& result)
- {
- postfix = infix_to_postfix(infix, postfix);
- if (validate(postfix) == false)
- exit(-1);
- }
- // Evaluate a boolean expression
- // If infix is a syntactically valid infix boolean expression whose
- // only operands are single lower case letters (whether or not they
- // appear in the values sets), then postfix is set to the postfix
- // form of the expression. If not, postfix might or might not be
- // changed, result is unchanged, and the function returns 1.
- //
- // If infix is a syntactically valid infix boolean expression whose
- // only operands are single lower case letters:
- //
- // If every operand letter in the expression appears in either
- // trueValues or falseValues but not both, then result is set to the
- // result of evaluating the expression (using for each letter in the
- // expression the value true if that letter appears in trueValues or
- // false if that letter appears in false values) and the function
- // returns 0.
- //
- // Otherwise, result is unchanged and the value the function returns
- // depends on these two conditions:
- // at least one letter in the expression is in neither the
- // trueValues nor the falseValues sets; and
- // at least one letter in the expression is in both the
- // trueValues and the falseValues set.
- // If only the first condition holds, the function returns 2; if
- // only the second holds, the function returns 3. If both hold
- // the function returns either 2 or 3 (and the function is not
- // required to return the same one if called another time with the
- // same arguments).
- int main()
- {
- string infix = "!(n|u) ";
- string postfix;
- infix_to_postfix(infix, postfix);
- cout << postfix;
- }
- // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
- // Debug program: F5 or Debug > Start Debugging menu
- // Tips for Getting Started:
- // 1. Use the Solution Explorer window to add/manage files
- // 2. Use the Team Explorer window to connect to source control
- // 3. Use the Output window to see build output and other messages
- // 4. Use the Error List window to view errors
- // 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
- // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement