C ++中的数组字符串比较

时间:2014-02-28 19:58:33

标签: c++ string

基本上我的代码所做的是将keywords.txt文件中的单词与关键字数组中的单词进行比较,并根据这些单词之间的距离是1还是2以及这两个单词不相同时显示错误建议。 * 我无法弄清楚为什么它仍然显示相同的字词 * 的建议?有什么建议吗?

以下是我的代码

#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];
}

1 个答案:

答案 0 :(得分: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,也会输出。这不是你想要的。