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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#define SLength 10
#define SAmount 10
using namespace std;
void FindResponse(int KeyCodes[15]);
int comparison(int codes[15], int responses[15]);
void CompareCodes(ifstream &ResponseFile, int Codes[15]);
void ChooseBestMatch(struct BestResponses Best[15]);
struct BestResponses{
int Matches;
int TotalMatches;
int MatchPercentage;
string Reply;
};
int main()
{
char ChatIn[SAmount][SLength];
string line;
string keyword;
string UserIn;
int code;
int count;
char Key[10];
int Codes[15];
ifstream KeyWords("Keywords.txt");
if(KeyWords.is_open())
{
while(ChatIn[0]!="Bye")
{
for(int i=0;i<15;i++)
{
Codes[i]=-1;
}
count=0;
getline(cin, UserIn);
stringstream Input(UserIn);
Input>>ChatIn[0]>>ChatIn[1]>>ChatIn[2]>>ChatIn[3]>>ChatIn[4]>>ChatIn[5]>>ChatIn[6]>>ChatIn[7]>>ChatIn[8]>>ChatIn[9];
while(KeyWords.good())
{
getline(KeyWords,line);
cout<<line<<endl;
stringstream ToCode(line);
ToCode>>Key>>code;
for(int i=0;i<10;i++)
{
if(strcmpi(Key,ChatIn[i])==0&&KeyWords.good())
{
cout<<"Match, yay!"<<endl;
Codes[count]=code;
count++;
}
}
}
KeyWords.clear();
KeyWords.seekg(0,ios::beg);
FindResponse(Codes);
}
KeyWords.close();
}
else
{
cout<<"unable to open file";
}
return 0;
}
void FindResponse(int KeyCodes[15])
{
ifstream ResponseFile("Responses.txt");
if(ResponseFile.good())
{
CompareCodes(ResponseFile, KeyCodes);
}
else
{
return;
}
}
void CompareCodes(ifstream &ResponseFile, int Codes[15])
{
string FileLine;
string LineChunk;
BestResponses Best[15];
int ChunkNumber;
int NumMatches;
float MatchPercent;
int MatchNumber=0;
//Below are sections
int ID;
int ResponseCodes[15];
int NumCodes;
string Reply;
//End of sections
while(getline(ResponseFile, FileLine))
{
for(int i=0;i<15;i++)
{
ResponseCodes[i]=0;
}
ChunkNumber=0;
istringstream ResponseLine(FileLine);
while(getline(ResponseLine, LineChunk,'|'))
{
istringstream Chunk(LineChunk);
ChunkNumber++;
switch(ChunkNumber)
{
case 1: Chunk>>ID;
break;
case 2: Chunk>>ResponseCodes[0]>>ResponseCodes[1]>>ResponseCodes[2]>>ResponseCodes[3]>>ResponseCodes[4]>>ResponseCodes[5]>>ResponseCodes[6]>>ResponseCodes[7]>>ResponseCodes[8]>>ResponseCodes[9]>>ResponseCodes[10]>>ResponseCodes[11]>>ResponseCodes[12]>>ResponseCodes[13]>>ResponseCodes[14];
break;
case 3: Chunk>>NumCodes;
break;
case 4: Chunk>>Reply;
break;
}
}//while line loop end
NumMatches=comparison(Codes, ResponseCodes);
MatchPercent=(NumMatches/NumCodes);
MatchPercent=MatchPercent*100;
if(MatchPercent>=25)
{
Best[MatchNumber].Matches=NumMatches;
Best[MatchNumber].TotalMatches=NumCodes;
Best[MatchNumber].MatchPercentage=MatchPercent;
Best[MatchNumber].Reply=Reply;
}
MatchNumber++;
}//while end get file line
ChooseBestMatch(Best);
//reset stuff for next run
ResponseFile.clear();
ResponseFile.seekg(0,ios::beg);
}
void ChooseBestMatch(struct BestResponses Best[15])
{
int BestMatch;
BestMatch=0;
for(int i=0;i<14;i++)
{
if(Best[i].Matches<Best[i+1].Matches)
{
BestMatch=i+1;
}
}
cout<<Best[BestMatch].Reply<<endl;
}
int comparison(int Codes[15], int responseCodes[15])
{
int matches=0;
for(int i=0;i<15;i++)
{
for(int l=0;l<15;l++)
{
if(Codes[i]==responseCodes[l])
{
matches++;
break;
}
}
}
return matches;
}
|