来源:互联网 发布:网络大电影是什么意思 编辑:程序博客网 时间:2024/04/30 10:43

本来打算是一天写一篇,一天一个程序的,结果好久没有写了。这几天都是卡在串这了,说是卡在这,也不准确,更多的是给自己找一个懒惰的理由罢了。

当时在写串的实现代码时,看到了关于运算符重载的内容。说实话,关于运算符重载,我真的是一片迷糊。书上讲的也不是很清楚,当时一气之下在图书馆里找了本最后的C++教程借了回来。我想最厚的,肯定是最详细的了。可是书道现在还没看。书上说,运算符重载,一般是放在类内或者是友元内的。我问了学C的同学,她说C里也有运算符重载。我当时就奇怪了,C里面不是没有类的概念吗?看来书还是要看的。

这次调程序小错误就不说了,对于跨度好几天的程序,里面出现大小写不匹配,缺胳膊少腿,个人感觉都正常,很好改的。就说说两个错误和一个警告吧:

这个错误刚开始很莫名其妙。我的文件结构是,在AString.h里定义了类的原型,在AString.cpp里实现原型,在MyMain.cpp写准程序。

AString.h:

#include<cstring>

#inlcude<iostream>

using namespace std;

class AString

{

……

}                                                            //L1

---------------------------------------------------------------------------------------------

AString.cpp:

#include<cstring>

#include<iostream>

using namespce std;

#include"AString.h"

AString::AString()

{}

……

----------------------------------------------------------------------------------------------

MyMain.cpp:

#include<cstring>

#include<iostream>

using namespce std;

#include"AString.h"

itn main()

{

……

}

----------------------------------------------------------------------------------------------

在调试的时候,老师提示:构造函数不能有返回值AString,main()函数的返回值是int不是AString。

当时我就郁闷了,我在构造函数前面没有加返回值啊,怎么会提示有返回值了,当时想破脑袋也没想出来。后来在床上的时候,突然想到书上的一句话:include是预编译命令,它会把包含的文件在编译时写到源文件中。我当时一下子明白了:虽然我在Mymain.cpp和AString.cpp前面没写AString,但是在L1行,最后我忘写了“;”,导致在用include包含后,编译系统把后面的mian和AString()当成了用类定义的变量了……在后面加了一个分号就解决问题了。

第二个错误是提示using namespace std;后面缺少分号。开始也是不得其解,但是试了一下,发现时写的位置不对。把include"AString.h”写到using那句后面就好了。

还有一个警告,还没搞定,就是提示strcpy函数使用不安全。

str(string.ch,str);

下面是源代码:

AString.h:

#if !defined(_AString_H)
#define _AString_H

#include<cstring>
using namespace std;

class AString
{
private:
char*ch;
int curLength;
int maxSize;
public:
AString();
AString(int sz);//构造函数,构造一个最大长度为sz,实际长度为零的字符串
AString(const char*init);//构造函数,构造一个最大长度为maxSize,由init初始化的新字符串对象
AString(const AString&ob);//复制构造函数,由一个已有的字符串对象ob构造一个新的字符串
~AString(){delete ch;}
/*与对象进行比较*/
int operator ==(AString&ob)const
{return strcmp(ch,ob.ch)==0;}//不等返回1,等返回0
int operator !=(AString&ob)const
{return strcmp(ch,ob.ch)!=0;}//不等返回1。等返回0
int operator <(AString &ob)const
{return strcmp(ch,ob.ch)<0;}
int operator >(AString &ob)const
{return strcmp(ch,ob.ch)>0;}
int operator <=(AString &ob)const
{return strcmp(ch,ob.ch)<=0;}
int operator >=(AString &ob)const
{return strcmp(ch,ob.ch)>=0;}
/*与字符串进行比较*/
int operator ==(char*str)const
{return strcmp(ch,str)==0;}//不等返回1,等返回0
int operator !=(char*str)const
{return strcmp(ch,str)!=0;}//不等返回1。等返回0
int operator <(char*str)const
{return strcmp(ch,str)<0;}
int operator >(char*str)const
{return strcmp(ch,str)>0;}
int operator <=(char*str)const
{return strcmp(ch,str)<=0;}
int operator >=(char*str)const
{return strcmp(ch,str)>=0;}
AString&operator=(AString&ob);
AString&operator=(char*str);
AString&operator+=(AString&ob);//若当前串的长度加上现在串的长度不大于maxSize,则把串接在现在串的后面
AString&operator+=(char*str);//若当前串的长度加上现在串的长度不大于maxSize,则把串接在现在串的后面
char&operator[](int i);
bool IsEmpty()const{return curLength==0;}
int Length()const{return curLength;}
//下面是常用函数的重载,方便各种调用方法
void SubString(AString&subs,int index,int length);//取子串
AString&Remove(int startIndex,int length);//删除一部分串
AString&Insert(int pos,const char*value);//插入
void Clear();
void Output();
void CheckMen();
};
#endif

----------------------------------------------------------------------------------------------

AString.cpp:

#include<iostream>
#include<cstring>
const int defaultsize=100;
using namespace std;

#include"AString.h"

AString::AString()
{
maxSize=defaultsize;
ch=new char[maxSize];
ch[0]='/0';
curLength=0;
}
AString::AString(int sz)
{
maxSize=sz;
ch=new char[maxSize];
ch[0]='/0';
curLength=0;
}
AString::AString(const char *init)
{
int initLength=sizeof(init);
maxSize=initLength>defaultsize?initLength:defaultsize;
ch=new char[maxSize+1];
curLength=maxSize;
strcpy(ch,init);
}
AString::AString(const AString &ob)
{
maxSize=ob.maxSize;
ch=new char[maxSize+1];
strcpy(ch,ob.ch);
curLength=ob.curLength;
}
AString& AString::operator =(AString&ob)
{
if(&ob!=this)
{
   Clear();
   curLength=ob.curLength;
   strcpy(ch,ob.ch);
}
else
   cout<<"字符串自身赋值,出错!/n";
return *this;
}
AString& AString::operator =(char *str)
{
int strLength=sizeof(str);
maxSize=strLength>maxSize?strLength:maxSize;
Clear();
strcpy(ch,str);
curLength=strLength;
return *this;
}
AString& AString::operator +=(AString &ob)
{
char*TempCH=ch,*TempOB=ob.ch;
int addLength=curLength+sizeof(TempOB);
maxSize=maxSize>addLength?maxSize:addLength;
char*temp=new char[maxSize+1];
char*tempT=temp;
while(*TempCH!='/0')
   *tempT++=*TempCH++;
while(*TempOB!='/0')
   *tempT++=*TempOB++;
*tempT++='/0';
Clear();
strcpy(ch,temp);
curLength=sizeof(ch);
delete temp;
return *this;
}
AString& AString::operator +=(char *str)
{
char*TempCH=ch;
int addLength=curLength+sizeof(str);
maxSize=maxSize>addLength?maxSize:addLength;
char*temp=new char[maxSize+1];
char*tempT=temp;
while(TempCH!='/0')
   *tempT++=*TempCH++;
while(*str='/0')
   *tempT++=*str++;
*tempT++='/0';
Clear();
strcpy(ch,temp);
curLength=sizeof(ch);
delete temp;
return *this;
}
char& AString::operator [](int i)
{
if(i<0||i>curLength)
{
   cout<<"字符下标越界!!/n";
   exit(1);
}
return ch[i];
}
void AString::SubString(AString &subs, int index, int length)
{
if(index<1||index+length>maxSize||length<0)
{
   cerr<<"索引或者长度越界!/n";
   exit(1);
}
if(IsEmpty())
{
   cout<<"串为空,无法执行操作!!/n";
   exit(1);
}
char*temp=new char[length+1];
if(temp==NULL)
{
   cout<<"动态内存申请失败!/n";
   exit(1);
}
for(int i=0, j=index-1;i<length;i++,j++)
   temp[i]=ch[j];
temp[length]='/0';
subs=temp;
}
AString& AString::Remove(int startIndex, int length)
{
if(startIndex<0||startIndex>curLength||startIndex+length>maxSize||length<0)
{
   cout<<"索引或者长度出界!/n";
   exit(1);
}
if(IsEmpty())
{
   cout<<"串为空,无法执行操作!!/n";
   exit(1);
}
for(int i=0;i<curLength-startIndex-length;i++)
   ch[i+startIndex]=ch[startIndex+length+i];
ch[curLength-length]='/0';
curLength=sizeof(ch);
return *this;
}
AString& AString::Insert(int pos, const char *value)
{
if(pos<0||pos>curLength)
{
   cout<<"试图插入的位置出错!/n";
   exit(1);
}
int addLength=strlen(value)+curLength;
maxSize=addLength>maxSize?addLength:maxSize;
char*temp=new char[maxSize+1];
char*tempCH=ch;
char*tempH=temp;
for(int i=0;i<pos;i++)
   *temp++=*tempCH++;
while(*value!='/0')
   *temp++=*value++;
while(*tempCH!='/0')
   *temp++=*tempCH++;
*temp='/0';
Clear();
strcpy(ch,tempH);
curLength=sizeof(ch);
return *this;
}
void AString::Clear()
{
delete []ch;
ch=new char[maxSize+1];
CheckMen();
ch[0]='/0';
curLength=0;
}
void AString::Output()
{
if(IsEmpty())
   cout<<"字符对象为空!/n";
else
   cout<<ch<<endl;
}
void AString::CheckMen()
{
if(ch==NULL)
{
cerr<<"内存分配失败!/n";
exit(1);
}
}

----------------------------------------------------------------------------------------------
MyMain.cpp:

#include<iostream>
#include<cstring>
#include"stdio.h"
const int defaultsize=128;
using namespace std;

#include "AString.h"

int main()
{

int len;
cout<<"请输入大小:";
cin>>len;
char*str=new char[len];
cout<<"请输入初始化字符串:";
cin>>str;
AString string(len);
string=str;
int tag=0;
while(tag!=9)
{
   cout<<"1、删除操作/n"
    <<"2、插入操作/n"
    <<"3、连接操作/n"
    <<"4、截取字符串/n"
    <<"5、字符比较/n"
    <<"6、输出字符串对象/n"
    <<"7、清空字符对象(清空后可选择8来重新赋值)/n"
    <<"8、字符对象赋值/n"
    <<"9、退出程序/n";
   cout<<"/n请输入要操作的方法序号:";
   cin>>tag;
   switch(tag)
   {
   case 1:
      {
       int index,length;
       cout<<"请输入要删除的位置:";
       cin>>index;
       cout<<"请输入要删除的长度:";
       cin>>length;
       string.Remove(index,length);
       cout<<"删除后的字符为:"<<endl;
       string.Output();
       break;
      }
   case 2:
    {
    int pos;
    char*input=new char[defaultsize];
    cout<<"请输入要插入的字符:";
    cin>>input;
    cout<<"请输入要插入的位置:";
    cin>>pos;
    string.Insert(pos,input);
    cout<<"插入后的字符串为:/n";
    string.Output();
    delete []input;
    break;
    }
   case 3:
    {
    char *value=new char[defaultsize+1];
    cout<<"请输入要连接的字符:";
    cin>>value;
    AString astr=value;//这里可不可以不用建立一个新对象?
    string+=astr;
    cout<<"连接后的字符串为:/n";
    string.Output();
    delete []value;
    break;
    }
   case 4:
    {
    int len,i;
    AString subs;
    cout<<"请输入要截取的字符串长度:";
    cin>>len;
    cout<<"请输入开始截取的位置:";
    cin>>i;
    string.SubString(subs,i,len);
    cout<<"/n下面为截取的字符串:/n";
    subs.Output();
    break;
    }
   case 5:
    {
    char*value=new char[defaultsize];
    cout<<"/n请输入要比较的字符串:";
    cin>>value;
    cout<<"/n字符串比较结构如下:/n";
    string.Output();
    if(string>value) cout<<"/b>";
    if(string<value) cout<<"/b<";
    if(string==value) cout<<"/b=";
    cout<<value<<endl;
    break;
    }
   case 6:
    cout<<"/n当前的字符串对象如下:";
    string.Output();
    break;
   case 7:
    string.Clear();
    cout<<"字符串已清空!/n";
    break;
   case 8:
    {
    char*str=new char[defaultsize];
    cout<<"请输入要赋值的字符串(不能有空格):";
    cin>>str;
    string=str;
    cout<<"赋值后的字符串为:/n";
    string.Output();
    break;
    }
   case 9:
    cout<<"程序运行结束!/n";
   
    break;
   default:
    cout<<"输入错误,请重新输入!!/n";
   
   }

}
return 0;
}

原创粉丝点击