我的项目让我使用带括号的单词表达式并检查输入文件的位置。如 (蓝色和黑色) 文件1 (黑暗和(天空和(花或玫瑰))) file2的
等
我的程序适用于更简单的表达式,如(蓝色或黑色) 但是当我执行更复杂的操作时,我会遇到分段错误。
这是包含错误的.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);
}
答案 0 :(得分: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
结果用于其中。