我试图绘制LCS表。我的代码的这部分在fill_the_table()
函数的内部while循环的第一次迭代之后停止。我无法在代码中找到错误。顺便说一下,我是C ++的新手......
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
#define U 1
#define L 2
#define UL 3
void fill_the_table(ifstream& file1, ifstream& file2, int **table, int **p){
table[0][0]=0;
p[0][0]=UL;
string line1, line2;
int i=1, j=1;
file1.clear();
file1.seekg(0, ios::beg);
file2.clear();
file2.seekg(0, ios::beg);
while(getline(file1, line1)){
table[0][j]=0;
p[0][j]=L;
while(getline(file2, line2)){
table[i][0]=0;
p[i][0]=L;
if(line1==line2){
table[i][j]=table[i-1][j-1]+1;
p[i][j]=UL;
i++;
}
else{
if(table[i-1][j]>table[i][j-1]){
table[i][j]=table[i-1][j];
p[i][j]=U;
}
else{
table[i][j]=table[i][j-1];
p[i][j]=L;
}
i++;
}
j++;
}
}
cout<<table[i][j];
return;
}
void table(ifstream& file1, ifstream& file2){
int i=0, j=0;
string line1, line2;
while(getline(file1, line1)){
i++;
}
while(getline(file2, line2)){
j++;
}
int** table = new int* [i];
for(int a=0; a<i; a++){
table[a] = new int [j];
}
int** p = new int* [i];
for(int a=0; a<i; a++){
p[a] = new int [j];
}
fill_the_table(file1, file2, table, p);
for(int a=0; a<i; a++){
delete[] table[j];
}
delete table;
for(int a=0; a<i; a++){
delete[] p[j];
}
delete p;
return;
}
int main(int argc, char* argv[]){
ifstream file1(argv[1]);
ifstream file2(argv[2]);
table(file1, file2);
return 0;
}
int** table
和int** p
的方式从table()
传递给fill_the_table()
吗?vector<string>
而不是自己阅读文件,哪一个更好?