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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
| #include "textsearchprogram.h"
// 重载>=运算符:比较当前运算符与另一个运算符的优先级 bool CompareOperation::operator >= (char rc) { // 修复:移除引用& return num >= mapoperation[rc]; }
// 优先级规则:~ > & > |(数字越大优先级越高) map<char, int> CompareOperation::mapoperation = { {'~', 3}, {'&', 2}, {'|', 1}, {'(', 0}, // 左括号优先级最低,仅用于控制范围 {')', 0} // 右括号优先级最低 };
void InsertVector (vector <string> & args, vector <char> & operation, char & c){ // 左括号直接入栈 if (c == '(') { operation.push_back(c); return; }
// 右括号:弹出到左括号(不含括号) if (c == ')') { while (!operation.empty() && operation.back() != '(') { args.emplace_back(1, operation.back()); operation.pop_back(); } if (!operation.empty()) operation.pop_back(); // 弹出左括号 return; }
// 处理普通运算符:按优先级入栈 CompareOperation cop(c); while (!operation.empty() && operation.back() != '(' && !(cop >= operation.back())) { args.emplace_back(1, operation.back()); operation.pop_back(); } operation.push_back(c); }
// 拆分命令行参数, 并且以后缀表达式方式存放 void ProgramDate::splitCommand(vector <string> & args) {
args.clear(); //0位置是全部的命令 string input; cout << "请输入命令:"; cin >> input;
args.emplace_back(input); vector <char> operation; string currentToken; // 用于累积当前单词
for (auto & c : input) { switch (c){ case '$': case '@': args.emplace_back(1, c); break; case '(': case ')': case '&': case '|': case '~': if (!currentToken.empty()) { // 避免连续空格导致空字符串 args.emplace_back(currentToken); currentToken.clear(); } InsertVector(args, operation, c); break; case ' ': if (!currentToken.empty()) { // 避免连续空格导致空字符串 args.emplace_back(currentToken); currentToken.clear(); } break; default: currentToken += c; break; } }
// 处理最后一个单词(如果输入不以空格结束) if (!currentToken.empty()) { args.push_back(currentToken); } // 处理剩余的运算符 while (!operation.empty()) { args.emplace_back(1, operation.back()); operation.pop_back(); } args.shrink_to_fit(); }
ProgramDate * ProgramDate::programdate = nullptr;
void ProgramDate::Init (){ _filecontent.clear(); _wordmap.clear(); args = stack<WordDate>(); }
ProgramDate * ProgramDate::getInstance() { if (programdate == nullptr) { programdate = new ProgramDate(); } atexit([](){ if(programdate != nullptr){ delete programdate; programdate = nullptr; } }); return programdate; }
//搜索单词 void ProgramDate::Search(const string & word){ cout << "放置结果" << word << endl; if(_wordmap.count(word)){ args.emplace(_wordmap[word]); } else { args.emplace(WordDate()); } }
// 打印 void ProgramDate::print(const string &word) { if(args.size() > 1){ cout << args.size() << endl; std::cerr << "命令错误" << endl; } else { cout << "Executing Query for: " << word << endl; cout << "occurs " << args.top().count << " times" << endl; for (auto & linenum : args.top().linenums) { cout << "(line" << linenum + 1 << ") " << _filecontent[linenum] << endl; } } args = stack <WordDate> (); }
void ProgramDate::VectorUpdate (const string & line){ _filecontent.emplace_back(line); }
void ProgramDate::MapUpdate (const string & word, const int & linenum) { programdate->_wordmap[word].Update(linenum); }
void ProgramDate::operator & (const string & op){ cout << "逻辑" << op << endl; if(args.size() < 2){ //std::cerr << "命令错误" << endl; //args = stack <WordDate> (); this->Search(op); return; } auto temporaryaid = args.top(); args.pop(); auto lbegin = args.top().linenums.begin(); auto rbegin = temporaryaid.linenums.begin(); while (lbegin != args.top().linenums.end() && rbegin != temporaryaid.linenums.end()) { if(*lbegin < *rbegin){ lbegin = args.top().linenums.erase(lbegin); } else if (*lbegin > *rbegin) { ++ rbegin; } else { ++ lbegin; ++ rbegin; } } if(lbegin != args.top().linenums.end()){ args.top().linenums.erase(lbegin, args.top().linenums.end()); } args.top().count = args.top().linenums.size(); }
void ProgramDate::operator | (const string & op){ cout << "逻辑" << op << endl; if(args.size() < 2){ this->Search(op); //std::cerr << "命令错误" << endl; //args = stack <WordDate> (); return; } auto temporaryaid = args.top(); args.pop(); if(temporaryaid.linenums != args.top().linenums){ for(auto & it : temporaryaid.linenums){ args.top().linenums.emplace(it); } args.top().count = args.top().linenums.size(); } }
void ProgramDate::operator ~ (){ cout << "逻辑~" << endl; if(args.size() < 1){ this->Search("~"); //std::cerr << "命令错误" << endl; //args = stack <WordDate> (); return; } // 取反运算:保留不在结果中的行号 set<int> newresult; if(args.top().linenums.size() != _filecontent.size()){ for (int i = 0; i < (int)_filecontent.size(); ++i) { if (args.top().linenums.find(i) == args.top().linenums.end()) { newresult.insert(i); } }
} args.top().linenums.swap(newresult); args.top().count = args.top().linenums.size(); }
bool ProgramDate::isempty(){ return _filecontent.size(); }
|