#include<cstring>
#include<iostream>
#include<fstream>
#include<cctype>
using namespace std;
//This
program prompts the user for a query, and
//returns
true if the page on disk answers the query
//and
false otherwise
bool searchpage(ifstream &,char[][80],int);
void divideintosearchterms(char[],char[][80],int &);
int main()
{
ifstream
in;
char query[80]; //holds the user query
char searchterms[5][80];
//holds up to 5 search terms
int
number_terms=0;
//holds the number of search terms
bool
match;
in.open("page.txt");
if (!in)
{
cout<<"error opening
file...";
exit(1);
//exit with an error code;
}
cin.getline(query,80,'\n'); //newline is the
default value for
//for the last parameter
cout<<query[79]<<endl<<"is it null?";
divideintosearchterms(query,searchterms,number_terms);
match = searchpage(in,searchterms,number_terms);
for (int i=0; i
< number_terms; i++)
cout<<searchterms[i];
if (match)
cout<<"Page is
returned!";
else
cout<<"Page is not
returned...";
return 0;
}
void divideintosearchterms(char q[],char
terms[][80],int & n)
{
//q holds the query, terms the searchterms and n the
//number of search terms
//This function fills the terms array and
sets n to the
//number of search terms
int
row = 0; //which searchterm?
int
column = 0; //position within searchterm
int
position = 0; //position in query
int
len = strlen(q);
cout<<len<<endl;
while (row < 5
&& position <= len) //up to 5 search terms
//up to length of query
{
cout<<endl<<"q
position is :"<<q[position]<<endl;
if
(q[position] == ' ' || q[position] == '\0')
{
//make sure the term has an
ending null
terms[row][column]
= '\0';
//increment to next term
row++;
//set column of new term to 0
column
= 0;
cout<<"current search term
is"<<terms[n];
//increment the number of searchterms
n++;
}
else
{
terms[row][column]
= q[position];
column++;
}
//increment the position in q
position++;
}
}
bool searchpage(ifstream & in,char
terms[][80],int n)
{
bool
matched[5]={0};
int
i,len,count = 0;
char word[80];
while (!in.eof() && count < n)
{
//run this loop for each word of the file
until all search terms
//are found
in>>word;
len = strlen(word);
//make sure all characters are lower case
//this assumes query is in lower case
for (i = 0; i < len;
i++)
word[i] = static_cast<char>(tolower(word[i]));
cout<<word[len-1]<<" is wordlen -1
"<<endl;
//remove punctuation from the end of the
word
if
(word[len-1] == '.' || word[len-1] == '?')
word[len-1]
= '\0';
for (i = 0; i < n; i++)
{
if (strcmp(word,terms[i])==0)
if
(!matched[i])
{
matched[i] == 1;
count++;
}
}
}
if (count==n)
return true;
return false;
}