I must really love you...
This probably won't work, but I needed to kill some time before hockey It should give you an idea of how my algorithm would work.
PHP Code:
#include <string>
#include <cstdlib>
#include <fstream>
#include <iostream>
struct rhead
{
int index;
int entries;
};
// ...
GetAStringFromTheUser(pszInput);
// ...
srand(time(NULL));
std::ifstream fResponses("responses.dat", ios::in);
int indicies[256]; // Holds the instances of each index number for which a matching string/response entered
memset(reinterpret_cast<void *>(indicies), 0, sizeof(indicies)); // Set it to 0
rhead response; // Holds information about the response strings
char *pToken = std::strtok(pszInput, " ");
while (pToken != NULL)
{
char pszBuffer[128];
while (fResponses.eof() == 0)
{
fResponses.getline(pszBuffer, 127, ';'); // Read up to a semicolon
if (strchr(pszBuffer, '%')) // If this isn't a response
{
response.index = atoi(pszBuffer[1]); // Current index eg %2%3;
response.entries = atoi(pszBuffer[3]); // Entries, see above
} else {
if (std::strncmp(pInputToken, pszBuffer) == 0) // if the current word matches the one in the file
{
indicies[response.index]++; // Increment our instance count for the current index
}
}
}
pToken = std::strtok(pszInput, NULL); // Get a new word
fResponses.seekg(0, std::ios::beg); // Back to the start
}
int largestIndexVal = 0;
for (int i = 0; i < 256; i++) // Find the largest instance count for our indicies
{
if (indicies[i] > largestIndexVal)
{
response.index = i;
largestIndexVal = indicies[i];
}
};
fResponses.seekg(0, std::ios::beg); // Back to the start
while (fResponses.eof() == 0)
{
fResponses.getline(pszBuffer, 127, ';');
if (strchr(pszBuffer, '%')) // If this is a response header
{
if (atoi(pszBuffer[1]) == response.index) // if the best fit index is equal to the one we just extracted
{
response.entries = atoi(pszBuffer[3]); // Same as above
break;
}
}
}
if (fResponses.eof()) // Ahh! We couldn't find the index!
{
SomethingHasGoneTerriblyWrong();
EndTheProgramOrAlertTheUser();
} else {
int randomResponse = (1 + (rand() % response.entries)); // Generate a random number
while (randomResponse--)
{
fResponses.getline(pszBuffer, 127, ';'); // read randomReponse times
}
DisplayTheStringOrDoSomethingElseWithIt(pszBuffer); // Uhh, yeah the php tags messed the end up a little bit, we should have a closing bracket here...
};