把指针指向的值付给二维数组无法成功(我在CSDN上的第一个问题,帮帮我!)

Rischaos 2016-12-27 05:33:29
在做一个关于编辑距离的课设时遇到了一个问题,在已知的TXT文档中,有单个长度在7000以内,总长度在400000以内的字符串,需要求任意两个字符串的编辑距离和计算时间。我的想法是1.将整个文档先读入字符串中,2将字符串转为数组(在网上找到将数组分割的代码),3分割数组(给的文档中,每个字符串是如下形式:>1gctctgccgtccgcac.....>2ggcccctatgtg.......),4将分割好的字符串放到一个二维数组中x【56】【7000】,5使用编辑距离函数计算,打印时间。为了简化,我先用了一个小的TXT文档,数据如下:
1>
qwertyuiop
2>
qwegftyuiy
3>
qwrtrytret
4>
rewrttqerq
5>
ewqrwretrt
6>
qertryyrtu
7>
qweretreyu
8>
qeretryuyt
9>
wewrtwere

然后在进行第4步时遇到了问题,无法解决。泪~~~~

帮帮我!如果可以的话,提出一些关于程序的改进意见,我会非常感激的!


#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

int main()
{

/*读取文档为字符串,计算文档长度,将字符串转为数组*/
fstream fileintput;
fileintput.open("D://rdy.txt",ios::in);
char a;
string s = "";
int i=1;
while(1){
fileintput>>a;
if(fileintput.eof())
break;
s.push_back(a);
}
fileintput.close();
cout << s.length() << endl;
char ch[400000];
strcpy(ch,s.c_str());
for (i=0;i<400000;i++)
{
if(ch[i]=='\0'){
cout << i << endl;
break;
}
}
/***********************************************/


char x[9][11];

/************分割数组并赋给二维数组*************/
const char * split = ">1234567890";
char * p;
p = strtok (ch,split);
int k=0;

while(p!=NULL)
{
cout<<k<<endl;
printf ("%s\n",p);
p = strtok(NULL,split);


x[k][0]= *p;

k++;
}
/*********************************************/

/*****打印二维数组(测试数组赋值成功)******/
cout<<1111111<<endl;
int l,h;
for(l=0 ; l<10 ; l++)
{
for(h=0 ; h<12 ; h++)
printf("%d",x[l][h]);
cout<<l<<endl;

}
cout<<2222222<<endl;
/********************************************/

return 0;
}

/*****************编辑距离函数********************/
/*

int edit(string str1, string str2)
{
int max1 = str1.size();
int max2 = str2.size();

int **ptr = new int*[max1 + 1];
for(int i = 0; i < max1 + 1 ;i++)
{
ptr[i] = new int[max2 + 1];
}

for(int i = 0 ;i < max1 + 1 ;i++)
{
ptr[i][0] = i;
}

for(int i = 0 ;i < max2 + 1;i++)
{
ptr[0][i] = i;
}

for(int i = 1 ;i < max1 + 1 ;i++)
{
for(int j = 1 ;j< max2 + 1; j++)
{
int d;
int temp = min(ptr[i-1][j] + 1, ptr[i][j-1] + 1);
if(str1[i-1] == str2[j-1])
{
d = 0 ;
}
else
{
d = 1 ;
}
ptr[i][j] = min(temp, ptr[i-1][j-1] + d);
}
}

int dis = ptr[max1][max2];

for(int i = 0; i < max1 + 1; i++)
{
delete[] ptr[i];
ptr[i] = NULL;
}

delete[] ptr;
ptr = NULL;
/****************输出结果(a b 结果 时间)****************/
// cout<< << << << ;
/*********************************************************/
// return dis;
//}
/*********************************************************/







谢谢。
...全文
106 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Rischaos 2016-12-28
  • 打赏
  • 举报
回复
引用 1 楼 qq423399099 的回复:
字符串赋值不是这样赋的x[k][0] = *p; 1.首先字符串复制不是用=,而是用strcpy 2.x[k][0]表示的是某一行的第一个字符,应该是strcpy(x[k], p);
发现使用时还会报错,不知道怎么回事,苦恼
ri_aje 2016-12-28
  • 打赏
  • 举报
回复
二维字符串数组用 vector<vector<string>>。
小灸舞 2016-12-27
  • 打赏
  • 举报
回复
字符串赋值不是这样赋的x[k][0] = *p;
1.首先字符串复制不是用=,而是用strcpy
2.x[k][0]表示的是某一行的第一个字符,应该是strcpy(x[k], p);

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧