0
我的項目讓我帶括號括起來的單詞表達式並檢查輸入文件的發生位置。如 (藍色和黑色) 文件1 (黑暗和(天空和(花或玫瑰))) 文件2C++遞歸導致的段錯誤(與詞和與查詢)
等
,當涉及到多個簡單表達式,如(藍色或我的項目工程黑) 但我執行更復雜的操作時運行在分段錯誤。
這是.cpp文件包含錯誤 我確信必須發生在任何doQuery或doMultipleQuery 錯誤(我沒有包括包括的和不重要的功能)
void WordSearch::doQuery(string query,string *result,int &size){
int temp = 0, i = 0;
MultiQuery thisQuery;
thisQuery.parse_string(query);
string word1 = thisQuery.getOperand1();
string word2 = thisQuery.getOperand2();
string op = thisQuery.getOperator();
if (thisQuery.getSize(query) < 5) {
List *list1;
int index = 0, list1_size = 0;
string temp[MAX_SIZE];
list1 = wordlist->search(word1);
if (list1 != NULL){
list1->all(temp, list1_size);
while(index < list1_size){
result[size] = temp[index];
size++;
index++;
}
if(size == 0){
result[size]="No Such File";
size++;
}
sort(result,result+size);
}
else
cout<<"Wrong query";
return;
}
if (op=="AND") {
size=0;
and_operation(wordlist,word1,word2,size,result);
sort(result,result+size);
}
else if(op=="OR") {
size=0;
or_operation(wordlist,word1,word2,size,result);
sort(result,result+size);
}
return;
}
void WordSearch::doMultipleQuery(string query,string *result,int &size){
MultiQuery thisQuery;
thisQuery.parse_string(query);
string operand1 = thisQuery.getOperand1();
string operand2 = thisQuery.getOperand2();
string oper = thisQuery.getOperator();
int op1_size = thisQuery.getSize(operand1);
int op2_size = thisQuery.getSize(operand2);
string *temp1, *temp2;
int index1 = 0, index2 = 0, size1 = 0, size2 = 0;
if (thisQuery.getSize(query) <= 5) // (Red AND Blue)
doQuery(query, result, size);
if (oper == "AND"){ // Files need to include both
if (op1_size < 5 && op2_size >= 5){ // (Pink OR (Blue AND Black))
string op1Temp = "(" + operand1 + ")";
doQuery(op1Temp, temp1, size1);
doMultipleQuery(operand2, temp2, size2);
while (index2 < size2) {
while (index1 < size1){
if (temp2[index2] == temp1[index1]){
string tempString = temp2[index2];
result[size] = tempString;
size++;
}
index1++;
}
index2++;
}
}
if (op1_size >= 5 && op2_size < 5){ // ((Blue AND Black) OR Pink)
string op2Temp = "(" + operand2 + ")";
doQuery(op2Temp, temp2, size2);
doMultipleQuery(operand1, temp1, size1);
while (index1 < size1) {
while (index2 < size2){
if (temp1[index1] == temp2[index2]){
string tempString = temp1[index1];
result[size] = tempString;
size++;
}
index2++;
}
index1++;
}
}
if (op1_size >= 5 && op2_size >= 5){ // ((Flower AND Red) OR (Pink AND Blue))
doMultipleQuery(operand1, temp1, size1);
doMultipleQuery(operand2, temp2, size2);
while (index1 < size1) {
while (index2 < size2){
if (temp1[index1] == temp2[index2]){
string tempString = temp1[index1];
result[size] = tempString;
size++;
}
index2++;
}
index1++;
}
}
}
if (oper == "OR") { // Files only need to include one
if (op1_size < 5 && op2_size >= 5){ // (Pink OR (Blue AND Black))
string op1Temp = "(" + operand1 + ")";
doQuery(op1Temp, temp1, size1);
doMultipleQuery(operand2, temp2, size1);
while (index2 < size2){
result[size] = temp2[index2];
index2++;
size++;
}
while (index1 < size1){
result[size] = temp1[index1];
index1++;
size++;
}
}
if (op1_size >= 5 && op2_size < 5){ // ((Blue AND Black) OR Pink)
string op2Temp = "(" + operand2 + ")";
doQuery(op2Temp, temp2, size2);
doMultipleQuery(operand1, temp1, size1);
while (index2 < size2){
result[size] = temp2[index2];
index2++;
size++;
}
while (index1 < size1){
result[size] = temp1[index1];
index1++;
size++;
}
}
if (op1_size >= 5 && op2_size >= 5){ // ((Flower AND Red) OR (Pink AND Blue))
doMultipleQuery(operand1, temp1, size1);
doMultipleQuery(operand2, temp2, size2);
while (index2 < size2){
result[size] = temp2[index2];
index2++;
size++;
}
while (index1 < size1){
result[size] = temp1[index1];
index1++;
size++;
}
}
}
sort(result,result+size);
}
嘗試正確縮進您的代碼之前發佈在這裏,很難閱讀不是的代碼。 – Dave
你可能想看看[Shunting-yard算法](http://en.wikipedia.org/wiki/Shunting-yard_algorithm)。此外,分段錯誤意味着你正在執行內存管理錯誤,這似乎不在上面的代碼中。例如,如何調用'doQuery'? (什麼作爲結果傳遞?) – Cameron
請訪問http://sscce.org/。將代碼複製粘貼到空白文本文件中,並且由於缺少'#include'和類定義(即'MultiQuery'和'WordSearch'),編譯將失敗。 – moshbear