[HTML]http://scidiv.bcc.ctc.edu/mg/211/programs/program5.htm[/HTML]
I've gotten to a point where it works for most cases but definitely has problems. My version allows for more than one total WFF, which isn't allowed. For instance, "pp" should not qualify, but it does in my version. Also, something like "ANp" isn't a wff, but would identify as so in my program. I've really gotten to the point where I'm just stuck in my own head at this point, unable to figure out how to do this. Well, here's my code. This was written using Visual C++ 2005, although that probably doesn't matter much. Also, we are allowed to use more arguments to the recursive function than just the string input. I'd really appreciate any suggestions.
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- bool WFF(string);
- int main()
- {
- string input;
- ofstream outData;
- outData.open("output.dat");
- cout << "Enter a string to test: ";
- getline(cin, input);
- while (! input.empty())
- {
- if (WFF(input))
- {
- cout << input << " is a WFF.\n";
- outData << input << " is a WFF.\n";
- }
- else
- {
- cout << input << " is not a WFF.\n";
- outData << input << " is not a WFF.\n";
- }
- cout << "Enter a string to test: ";
- getline(cin, input);
- }
- return 0;
- }
- bool WFF(string input)
- {
- if ((input[0]=='p'||input[0]=='q'||input[0]=='r'||input[0]=='s')&&
- input.length()==1)
- return true;
- if ((input[0]=='p'||input[0]=='q'||input[0]=='r'||input[0]=='s')&&
- input.length()>1)
- return WFF(input.substr(1,input.length()-1));
- if (input[0]=='N'&&input.length()>1)
- return WFF(input.substr(1,input.length()-1));
- if ((input[0]=='C'||input[0]=='A'||input[0]=='K'||input[0]=='E')&&
- input.length()>2)
- return WFF(input.substr(1,input.length()-1));
- else
- return false;
- }