1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#include <iostream>
#include <string>
using namespace std;
const static enum { TUTORIAL, BEGINNER };
class CppForum {
private:
static string cppForumText;
public:
CppForum() {
if (cppForumText.empty()) {
//this should come from the CppForum database
cppForumText.assign("Hello World: a sample cpp program/");
cppForumText.append("Looping structures: do while, for/");
}
}
string searchCppForum(string searchString) {
int position;
string retrievedText;
position = cppForumText.find(searchString.c_str());
if (position<0) return "";
retrievedText = cppForumText.substr(position);
position = retrievedText.find("/");
retrievedText = retrievedText.substr(0, position);
return retrievedText;
}
};
class MsdnForum {
private:
static char msnTutorial[];
static char msnBeginners[];
static bool forumsLoaded;
public:
MsdnForum() {
//this should come from the MsdnForum database
if (!forumsLoaded) {
//forum items limited to 20 characters x 2 posts per section
strcpy(msnTutorial, "arrays: xxx.........");
strcpy(msnBeginners, "hello world: xxx...");
forumsLoaded = true;
}
}
char *searchForum(char* searchString, int iSection) {
char *buffer = NULL;
char *foundString = new char[21];
if (iSection==TUTORIAL) {
if((buffer=strstr(msnTutorial, searchString))!=NULL)
strcpy(foundString, buffer);
}
else if (iSection==BEGINNER) {
if((buffer=strstr(msnBeginners, searchString))!=NULL)
strcpy(foundString, buffer);
}
if (buffer == NULL) strcpy(foundString, "");
return foundString;
}
};
class searchForumFacade {
CppForum cppForum;
MsdnForum msForum;
string cppSearchString;
public:
void searchAnyForum(char *searchStr) {
cppSearchString.assign(searchStr);
printf("\nYour search returned: \n");
printf("Cpp Forum: %s \n", cppForum.searchCppForum(cppSearchString).c_str());
printf("Msdn tutorial: %s \n", msForum.searchForum(searchStr, TUTORIAL));
printf("Msdn beginners: %s \n", msForum.searchForum(searchStr, BEGINNER));
}
};
//define and/or initialize statics
string CppForum::cppForumText;
char MsdnForum::msnTutorial[100];
char MsdnForum::msnBeginners[100];
bool MsdnForum::forumsLoaded = false;
int main() {
searchForumFacade p2ff;
char searchStr[100] = "nothing";
while (1) {
printf("Enter search text for forums (q=quit): ");
cin >> searchStr;
if (!strcmp(searchStr,"q")) break;
p2ff.searchAnyForum(searchStr); //shield user from search details
}
return 0;
}
|