2014-02-28 111 views
0

我的代碼基本上是將keywords.txt文件中的單詞與關鍵字數組中的單詞進行比較,並根據這些單詞之間的距離是1還是2以及這兩個單詞的時間來顯示錯誤建議不一樣。 * 我無法弄清楚它爲什麼仍然顯示相同的文字 *?有什麼建議麼?C++中的數組字符串比較

下面是我的代碼

#include <iostream> 
#include <fstream> 
#include <string> 
#include<vector> 
using namespace std; 

int EditDistance(string word1, string word2); 

int main() 
{ 
    //keywords provided. 
    string keywords[24] ={"auto","break","case","char","const","continue","default","do","double","else","enum","extern","float","for","goto","if","int","long","return","short","struct","switch","void","while"}; 
    int loop=0; //short for loop for input 
    string line; //this will contain the data read from the file 
    int numLines = 0; 
    string unused; 
    int result; 
    ifstream myfile ("keywords.txt"); //opening the file. 

    string arr[200]; 
    if (myfile.is_open()) //if the file is open 
    { 
     while (! myfile.eof()) //while the end of file is NOT reached 
     { 
      getline (myfile,line); //get one line from the file 

      arr[loop] = line; 
      // cout << arr[loop] << endl; //and output it 
      loop++; 
     } 
     myfile.close(); //closing the file 
    } 
    else cout << "Unable to open file"; //if the file is not open output 
    /* 
    for(int i=0;i<24;i++) 
    { 
     for(int j=0;j<loop;j++) 
     { 
     if(arr[j]==keywords[i]) 
      cout<<arr[j]<<" and "<<keywords[i]<<" match. "<<endl; 

     } 
    }*/ 

    cout<<endl<<endl; 
    cout<<"################## ERROR SUGGESTIONS ################"<<endl; 
    cout<<"#             "<<endl; 
    cout<<"#             "<<endl; 

    for(int i=0;i<24;i++) 
    { 
      for(int j=0;j<loop;j++) 
     { 

      result=EditDistance(arr[j],keywords[i]); 
      if (result==1 || result==2 && (arr[j]!=keywords[i]))  
      cout<<"# Use "<<keywords[i]<<" instead of " <<arr[j]<<" ? "<<endl; 


     } 
    } 
    cout<<"#             "<<endl; 

    cout<<"#"<<endl; 
    cout<<"#####################################################"<<endl; 
    system("pause"); 
    return 0; 
} 

int EditDistance(string word1, string word2) //function to find the distance between two words. 
{ 
    int i, j, l1, l2, m; 
    l1 = word1.length(); 
    l2 = word2.length(); 
    vector< vector<int> > t(l1 + 1, vector<int>(l2 + 1)); 

    for (i = 0; i <= l1; i++) 
     t[i][0] = i; 
    for (i = 1; i <= l2; i++) 
     t[0][i] = i; 

    for (i = 1; i <= l1; i++) 
    { 
     for (j = 1; j <= l2; j++) 
     { 
      m = min(t[i-1][j], t[i][j-1]) + 1; 
      t[i][j] = min(m, t[i-1][j-1] + (word1[i-1] == word2[j-1] ? 0 : 1)); 
     } 
    } 
    return t[l1][l2]; 
} 
+0

您是否驗證過字符串是否真的相同?最後沒有'\ r'',在那裏? – Useless

+0

我的建議是你在調試器中逐步執行你的代碼 - 答案不僅很明顯,而且你不會學習如何使用C++。 – kfmfe04

回答

1

上線

if (result==1 || result==2 && (arr[j]!=keywords[i]))  

你大概的意思

if((result==1 || result==2) && arr[j]!=keywords[i])  

第一個版本是一樣的

if (result==1 || (result==2 && arr[j]!=keywords[i])) 

因此,即使單詞正確,如果結果爲1,也會輸出。這不是你想要的。

+1

關鍵是在邏輯或之前邏輯與得到評估。 http://en.cppreference.com/w/cpp/language/operator_precedence – AndyG

+0

@AndyG謝謝你補充一點,我在寫完之前意外地提交了,我也打算包含它。 – asbumste

+0

我試着通過做「if((result == 1 || result == 2)&& arr [j]!= keywords [i])」以及我試過if((result == 1 || result = = 2)&&(arr [j]!= keywords [i])),但仍顯示 「用於代替for?」的輸出行之一。 –