如何在此计划中输入单词?

时间:2013-01-17 04:01:53

标签: codeblocks

他告诉我在这个程序中添加任何单词,我可以看到另一个转换,但我不知道在哪里插入我想要的单词。我下载了CodeBlocks,因为它看起来像谷歌最好的C ++跑步者,但当我点击运行它只是说我需要两个字符串。猜猜那意味着话语。

任何人都可以看看这个并告诉我如何插入2个单词吗?

该计划:

#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>

using namespace std;

#define MIN(X,Y) ((X <= Y) ? X:Y)
#define MIN3(A,B,C) ((A <= MIN(B,C)) ? A : MIN(B,C))

class EDIST {
private:
  string _s1, _s2;
  int _edit_distance;

  enum backtrace_pointer { L, D, U };
  // L==left, D==diagonal, U==up.

  vector<vector<int> > dp_table;
  vector<vector<int> > backtrace;

public:
  EDIST(const char *a, const char *b) {
    _s1 = a;
    _s2 = b;
    _edit_distance = 0;
  }

  void run();

private:
  int edit_distance() const { return _edit_distance; }
  void dp_edit_distance();
  void print_dp_tables();
  void init_dp_tables();
  void print_solution();
};

void EDIST :: print_dp_tables()
{
  cout << "\nPrinting dynamic programming table\n";

  cout << "\t_";
  for ( int i=0; i < _s2.size(); i++ )
    cout << "\t" << _s2[i];
  cout << endl;

  for ( int i=0; i <= _s1.size(); i++ ) {
    if ( i ) cout << _s1[i-1];
    else cout << "_";

    for ( int j=0; j <= _s2.size(); j++ )
      cout << "\t" << dp_table[i][j];
    cout << endl;
  }

  cout << "\nPrinting backtrace table\n";

  cout << "\t_";
  for ( int i=0; i < _s2.size(); i++ )
    cout << "\t" << _s2[i];
  cout << endl;

  for ( int i=0; i <= _s1.size(); i++ ) {
    if ( i ) cout << _s1[i-1];
    else cout << "_";

    for ( int j=0; j <= _s2.size(); j++ ) {
      cout << "\t";
      if ( backtrace[i][j] == L ) cout << "L";
      else if ( backtrace[i][j] == D ) cout << "D";
      else cout << "U";
    }
    cout << endl;
  }
}

void EDIST :: init_dp_tables()
{
  for ( int i=0; i <= _s1.size(); i++ ) {
    vector <int> v;
    vector<int> b;

    for ( int j=0; j <= _s2.size(); j++ ) {
      int n=0, op=0;;

      if ( !i ) { n=j; op=L; }
      else if ( !j ) { n=i; op=U; }
      else { n=0; op=D; }

      v.push_back(n);
      b.push_back(op);
    }

    dp_table.push_back(v);
    backtrace.push_back(b);
  }

  for ( int i=0; i <= _s1.size(); i++ )
    dp_table[i][0] = i;

  for ( int j=0; j <= _s2.size(); j++ )
    dp_table[0][j] = j;
}

void EDIST :: dp_edit_distance()
{
  for ( int j=1; j <= _s2.size(); j++ ) {

    for ( int i=1; i <= _s1.size(); i++ ) {

      int a = dp_table[i-1][j]+1;
      int b = dp_table[i-1][j-1];
      int c = dp_table[i][j-1]+1;

      if ( _s1[i-1] != _s2[j-1] )
    b++;

      dp_table[i][j] = MIN3(a,b,c);

      if ( a == dp_table[i][j] ) backtrace[i][j] = U;
      else if ( b == dp_table[i][j] ) backtrace[i][j] = D;
      else backtrace[i][j] = L;
    }
  }
}

void EDIST :: print_solution()
{
  vector<string> string_sequence;
  string_sequence.push_back(_s2);

  int i = _s1.size();
  int j = _s2.size();

  while ( i || j ) {

    string s = string_sequence[string_sequence.size()-1];

    bool add_string=true;
    int new_i=i, new_j=j;

    if ( backtrace[i][j] == L ) {//LEFT :: insert
      new_j--;
      s.erase(j-1,1);
    }
    else if ( backtrace[i][j] == U ) {//UP : delete
      new_i--;
      string sub1 = (j >= 1 ? s.substr(0,j) : "");
      string sub2 = (j < s.size() ? s.substr(j) : "");
      s = sub1 + _s1[i-1] + sub2;
    }
    else {//DIAGONAL : replace OR no-operation
      new_i--;
      new_j--;
      if ( i && j && dp_table[i][j] != dp_table[new_i][new_j] )
    s.replace(j-1,1,_s1.substr(i-1,1));
      else
    add_string = false;
    }

    if ( add_string ) {
      string_sequence.push_back(s);
      _edit_distance++;
    }

    i = new_i;
    j = new_j;
  }

  cout << "\nEdit distance : " << edit_distance() << endl;

  if ( string_sequence.size() )
    cout << "\nPrinting mutations : \n";

  for ( int i=string_sequence.size()-1; i >= 0; i-- )
    cout << "\t" << string_sequence[i] << endl;

}

void EDIST :: run()
{
  cout << "\nFinding edit-distance between strings `";
  cout << _s1 << "' and `" << _s2 << "'" << endl;

  init_dp_tables();
  dp_edit_distance();
  print_dp_tables();
  print_solution();
}

int main(int argc, char *argv[])
{
  if ( argc != 3 ) {
    cerr << "Need 2 strings as input!\n" << endl;
    exit(1);
  }

  EDIST e(argv[1], argv[2]);
  e.run();

  return 0;
}

2 个答案:

答案 0 :(得分:1)

当你下载程序时,你可能得到了一个.exe文件,你需要用两个命令行参数来执行这个程序,如下所示:

programname.exe word1 word2

如果你的朋友没有给你一个可执行文件,你需要将源代码编译成可执行文件。 CodeBlocks提供此功能并自动运行编译结果。不幸的是,它没有传递任何参数到结果,这就是为什么程序告诉你它需要两个单词。 (CodeBlocks正在执行programname.exe

此问题的一个解决方案是配置代码块以提供参数。 As danben pointed out您可以使用“项目”菜单中的“设置程序参数”选项将其配置为提供参数。如果您将程序参数设置为word1 word2,那么CodeBlocks将执行您想要的programname.exe word1 word2

如果你想在其他地方运行这个程序,你需要编译它。幸运的是,无论何时单击CodeBlocks中的“run”,它实际上都会在某处编译可执行文件,通常位于bin\debugbin\release下的项目文件夹中,您将找到一个可执行文件。您可以使用上面概述的此文件。

答案 1 :(得分:1)

这是一个关于CodeBlocks的问题,而不是关于C ++的问题,但快速谷歌搜索显示您需要选择“项目”菜单下的“设置程序参数”选项。

相关问题