0

我的项目让我使用带括号的单词表达式并检查输入文件的出现位置。如 (Blue AND Black) file1 (Dark AND (Sky AND (Flower OR Roses))) file2

ETC

当涉及到更简单的表达式时,我的程序可以工作,比如(蓝色或黑色),但是当我执行更复杂的操作时,我会遇到分段错误。

这是包含错误的 .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);
}
4

1 回答 1

0

由于您的代码非常复杂且令人费解,因此可能还有其他错误,但突出的是您从未为结果数组分配内存。
doMultipleQuery

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);   // <-- temp1 is uninitialized here       
doMultipleQuery(operand2, temp2, size2);  // <-- temp2 is uninitialized here

它会更好地使用std::vector并将push_back结果放在它上面。

于 2012-06-05T07:43:51.140 回答