| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 802 人关注过本帖
标题:strcat 乱码问题
只看楼主 加入收藏
haoyasen
Rank: 2
等 级:论坛游民
帖 子:90
专家分:20
注 册:2013-3-30
结帖率:91.67%
收藏
已结贴  问题点数:20 回复次数:3 
strcat 乱码问题
#include<iostream>
#include<assert.h>
//#include<string>
using namespace std;
class MyString {
public:
    MyString(char *pstr);
    MyString(const MyString& str);
    ~MyString();
    void ShowString();    //显示字符串
    void Add(const MyString& str);    //与另外一个相加
    void Copy(const MyString& str);    //从另外一个拷贝
    int GetLength();
private:
    char *m_buf;   
};
MyString ::MyString (char *pstr)
{
    if(pstr ==NULL)
        m_buf =new char('\0');
    else{
    m_buf =new char[strlen(pstr)+1];
    strcpy(m_buf,pstr);
    }
}
MyString ::MyString (const MyString& str)
{
    assert(str.m_buf !=NULL);
    m_buf=new char[strlen(str.m_buf)+1];
    strcpy(m_buf ,str.m_buf);
}
MyString::~MyString()
{
    delete []m_buf ;
}
void MyString ::ShowString ()
{
    cout<<"show string:    "<<m_buf<<endl;
}
void MyString::Add(const MyString& str)
{
    assert(str.m_buf !=NULL);
    int len=strlen(m_buf)+strlen(str.m_buf)+1;
    char *p=new char[len];   
    strcpy(p,m_buf);
    strcat(p,str.m_buf);
   // delete []m_buf ;
    m_buf=new char[len];
    strcat(m_buf ,p);
    delete []p;
}
void MyString::Copy(const MyString& str)
{
    assert(str.m_buf !=NULL);
    m_buf=new char[strlen(str.m_buf)+1];
    strcpy(m_buf ,str.m_buf );
}
 int MyString::GetLength()
 {
    if(m_buf ==NULL)
        return 1;   
    return strlen(m_buf)+1;
 }
 void main()
 {
    MyString mystring("haoyasen"),mystring2("shuaizhenhao");
    //mystring(mystring2);
    mystring.ShowString();
    cout<<"len: "<<mystring .GetLength ()<<endl;
    mystring.Add(mystring2);mystring.ShowString ();//不能正确显示
    mystring.Copy(mystring2);mystring.ShowString();
 }
搜索更多相关主题的帖子: public private void namespace include 
2013-06-02 11:12
haoyasen
Rank: 2
等 级:论坛游民
帖 子:90
专家分:20
注 册:2013-3-30
收藏
得分:0 
我知道 啦

void MyString::Add(const MyString& str)
 {
     assert(str.m_buf !=NULL);
     int len=strlen(m_buf)+strlen(str.m_buf)+1;
     char *p=new char[len];   
     strcpy(p,m_buf);
     strcat(p,str.m_buf);
    // delete []m_buf ;
     m_buf=new char[len];
     strcat(m_buf ,p);//改为strcpy(m_buf,p);
     delete []p;
 }
2013-06-02 11:26
justlxy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:28
专家分:158
注 册:2013-5-14
收藏
得分:10 
2013-06-02 11:36
q215236213
Rank: 4
等 级:业余侠客
威 望:1
帖 子:148
专家分:276
注 册:2012-7-24
收藏
得分:10 
接分咯!楼主自己都搞定了!~
2013-06-02 19:05
快速回复:strcat 乱码问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014098 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved