字符串比较问题
#include"string"
using namespace std;
string *str;
struct stringnode
{
string s1;
string s2;
}
stringnode *snode;
//
//
for(int i=0;i<total1;i++)
{
for(int j=0;j<total2;j++)
if (str[i]==snode[j].s1) 调试时这一步出错,执行不过去.
{
str[i]=snode[remain[j]].s2;
break;
}
}
str[i]==snode[j].s1,不能这么判断吗?
请教各位,谢谢
问题点数:80、回复次数:15Top
1 楼myhuman()回复于 2005-09-23 16:30:32 得分 0
还是把你的程序写全吧
你的snode有没有初始化?Top
2 楼healer_kx(甘草(楼主揭贴吧,我们这些上班灌水的也不容易))回复于 2005-09-23 16:31:22 得分 0
snode想必是没有初始化。既然都链接通过了。Top
3 楼surpass1026(步步高)回复于 2005-09-23 16:35:33 得分 0
void file_to_ArrayNode(ifstream &infile,stringnode *snode) //记录每个s1的相同的个数。
{
string s1,s2;
int i=0,j=0;
infile.open("dict.txt");
while(infile>>s1>>s2)
{
i++;
}
total2=i;
snode=new stringnode[total2];
remain=new int [total2];
for(int k=0;k<total2;k++)
remain[k]=k;
infile.close();
infile.clear();
infile.open("dict.txt");
while(infile>>s1>>s2)
{
snode[j].s1=s1;
snode[j].s2=s2;
j++;
}
for(int n1=0;n1<total2;n1++)
for(int n2=n1+1;n2<total2;n2++)
{
if (snode[n1].s1==snode[n2].s1)
remain[n1]=n2;
}
}
我已初始化了.Top
4 楼qhfu(改个名字)回复于 2005-09-23 16:36:07 得分 0
string *str;
stringnode *snode;
这两个的初始化看不到,不能做判断。Top
5 楼fiftymetre(50米深蓝)回复于 2005-09-23 16:37:22 得分 0
帖完整点看看,如果都初始化了是没有问题的Top
6 楼qhfu(改个名字)回复于 2005-09-23 16:54:01 得分 0
void file_to_ArrayNode(ifstream &infile,stringnode *snode)
to:
void file_to_ArrayNode(ifstream &infile,stringnode *&snode)Top
7 楼jay0518(jay)回复于 2005-09-23 16:54:55 得分 0
TO:
#include"string"
using namespace std;
string *str;//1.兄弟知道你定义的这个是什么吗?str是一个指针指向一个string字符串变量,==>>3.
struct stringnode
{
string s1;
string s2;
}
stringnode *snode;//2.注意这里你定义的是一个结构体指针
//这里要有语句为定义的指针申请内存空间如:stringnode *snode=new stringnode[total2];
//
for(int i=0;i<total1;i++)
{
for(int j=0;j<total2;j++)
if (str[i]==snode[j].s1)//3.接1处str[i]就表示一个字符;sn[j].s1表示的是一个string字符串变量,想一下,字符能和字符串比较大小吗?
{
str[i]=snode[remain[j]].s2;
break;
}
}
Top
8 楼surpass1026(步步高)回复于 2005-09-23 16:56:36 得分 0
#include<iostream>
#include<fstream>
#include <string>
using namespace std;
string *str;
int total1;
int total2;
struct stringnode
{
string s1;
string s2;
};
int *remain;
stringnode *snode;
void file_to_Array(ifstream &infile,string *str);
void file_to_ArrayNode(ifstream &infile,stringnode *snode);
void dicttofile(ofstream &outfile);
void main()
{
ifstream infile1,infile2;
ofstream outfile;
file_to_Array(infile1,str);
infile1.close();
file_to_ArrayNode(infile2,snode);//记录每个s1的相同的个数。
infile2.close();
outfile.open("tes.txt");
dicttofile(outfile);
outfile.close();
}
void file_to_Array(ifstream &infile,string *str)
{
string temp;
int i=0,j=0;
infile.open("test.txt");
while(infile>>temp)
{
i++;
}
total1=i;
str=new string[total1];
infile.close();
infile.clear();
infile.open("test.txt");
while(infile>>temp)
{
str[j]=temp;
j++;
}
}
void file_to_ArrayNode(ifstream &infile,stringnode *snode) //记录每个s1的相同的个数。
{
string s1,s2;
int i=0,j=0;
infile.open("dict.txt");
while(infile>>s1>>s2)
{
i++;
}
total2=i;
snode=new stringnode[total2];
remain=new int [total2];
for(int k=0;k<total2;k++)
remain[k]=k;
infile.close();
infile.clear();
infile.open("dict.txt");
while(infile>>s1>>s2)
{
snode[j].s1=s1;
snode[j].s2=s2;
j++;
}
for(int n1=0;n1<total2;n1++)
for(int n2=n1+1;n2<total2;n2++)
{
if (snode[n1].s1==snode[n2].s1)
remain[n1]=n2;
}
}
void dicttofile(ofstream &outfile)
{
for(int i=0;i<total1;i++)
{
for(int j=0;j<total2;j++)
if (str[i]==snode[j].s1)???????????????????????????
{
str[i]=snode[remain[j]].s2;
break;
}
}
for(int k=0;k<total1;k++)
outfile<<str[k];
}
if (str[i]==snode[j].s1)调试到这一句出现(unhandled exception)提示框.
谢谢各位看看.
Top
9 楼qhfu(改个名字)回复于 2005-09-23 17:07:42 得分 0
#include<iostream>
#include<fstream>
#include <string>
using namespace std;
string *str;
int total1;
int total2;
struct stringnode
{
string s1;
string s2;
};
int *remain;
stringnode *snode;
void file_to_Array(ifstream &infile,string *&str);
void file_to_ArrayNode(ifstream &infile,stringnode *&snode);
void dicttofile(ofstream &outfile);
void main()
{
ifstream infile1,infile2;
ofstream outfile;
file_to_Array(infile1,str);
infile1.close();
file_to_ArrayNode(infile2,snode);//记录每个s1的相同的个数。
infile2.close();
outfile.open("tes.txt");
dicttofile(outfile);
outfile.close();
}
void file_to_Array(ifstream &infile,string *&str)
{
string temp;
int i=0,j=0;
infile.open("test.txt");
while(infile>>temp)
{
i++;
}
total1=i;
str=new string[total1];
infile.close();
infile.clear();
infile.open("test.txt");
while(infile>>temp)
{
str[j]=temp;
j++;
}
}
void file_to_ArrayNode(ifstream &infile,stringnode *&snode) //记录每个s1的相同的个数。
{
string s1,s2;
int i=0,j=0;
infile.open("dict.txt");
while(infile>>s1>>s2)
{
i++;
}
total2=i;
snode=new stringnode[total2];
remain=new int [total2];
for(int k=0;k<total2;k++)
remain[k]=k;
infile.close();
infile.clear();
infile.open("dict.txt");
while(infile>>s1>>s2)
{
snode[j].s1=s1;
snode[j].s2=s2;
j++;
}
for(int n1=0;n1<total2;n1++)
for(int n2=n1+1;n2<total2;n2++)
{
if (snode[n1].s1==snode[n2].s1)
remain[n1]=n2;
}
}
void dicttofile(ofstream &outfile)
{
for(int i=0;i<total1;i++)
{
for(int j=0;j<total2;j++)
if (str[i]==snode[j].s1)
{
str[i]=snode[remain[j]].s2;
break;
}
}
for(int k=0;k<total1;k++)
outfile<<str[k];
}
Top
10 楼zhouhuahai(道号"虚无")回复于 2005-09-23 17:10:32 得分 0
void file_to_Array(ifstream &infile,string *str)
改成:
void file_to_Array(ifstream &infile,string *&str)
void file_to_ArrayNode(ifstream &infile,stringnode *snode)
改成:
void file_to_ArrayNode(ifstream &infile,stringnode *&snode)Top
11 楼zhouhuahai(道号"虚无")回复于 2005-09-23 17:11:03 得分 80
因为你的代码中指针本身是按值传递的Top
12 楼surpass1026(步步高)回复于 2005-09-23 17:15:56 得分 0
引用传递,谢谢你,zhouhuahai(道号"虚无")Top
13 楼qhfu(改个名字)回复于 2005-09-23 17:39:43 得分 0
楼主: 这样不对吧! 明显是我先帮你改正的, 一分都不给我是不是 有点说不过去。。Top
14 楼jixingzhong(瞌睡虫·星辰)回复于 2005-09-23 18:27:47 得分 0
在你贴的代码中, 没有找到 string *str 相关 ...Top
15 楼qhfu(改个名字)回复于 2005-09-23 19:18:52 得分 0
在你贴的代码中, 没有找到 string *str 相关 ...
========================
说我吗? 不会吧! 在 道号"虚无"回复的上一楼,我已经把他指出的两个 错误都在代码里改正了,
至于你说的string *str 相关是什么意思,有点不明白!Top




