回 帖 发 新 帖 刷新版面

主题:[原创]高手进来!!特别是重载高手进来!!我的+重栽有点问题?

#include<string.h>         //包含头文件
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<windows.h>
#define NULL 0
const int max=100;
class zdystring              //定义字符串类
{
private:
    char *str;           //字符串的私有成员
    int size;            //字符串长度
public:
    zdystring();            //无实参的构造函数
    zdystring(char *zdystring);//有字符串指针的构造函数
    void set(char *zdystring);//设置字符串
    void operator =(zdystring &obj);//等号重载
    zdystring operator +(zdystring &obj);//加号重载
    void substring(zdystring &obj,int count,int length); //求子串
    char *getstr(){return str;}//取串植
    int getsize(){return size;}//取串长
    void display();//输出函数
    ~zdystring();//析构函数
};
zdystring::zdystring()
{
    size=1;
    str=new char[size];
    if(str==NULL)
    {
        cout<<"分配错误."<<endl;
        exit(1);
    }
    str=NULL;
}
zdystring::zdystring(char *zdystring)
{
    size=strlen(zdystring)+1;
    str=new char[size];
    if(str==NULL)
    {
        cout<<"分配错误."<<endl;
        exit(1);
    }
    strcpy(str,zdystring);
}
void zdystring::set(char *zdystring)
{
    size=strlen(zdystring)+1;
    str=new char[size];
    if(str==NULL)
    {
        cout<<"分配错误."<<endl;
        exit(1);
    }
    strcpy(str,zdystring);
    size=strlen(str);
}
void zdystring:: substring(zdystring &obj,int count,int length)
{
    int i;
    str=new char[length];
    for(i=0;i<length;i++,count++)
        str[i]=obj.str[count-1];
    size=length;
        
}
void zdystring::operator =(zdystring &obj)
{
    if(!strcmp(str,obj.str))
        ;
    else
    {
        delete str;
        size=strlen(obj.str);
        str=new char[size+1];
        if(str==NULL)
        {
        cout<<"分配错误."<<endl;
        exit(1);
        }
        else
        {
            strcpy(str,obj.str);
            
        }
    }

    
}

zdystring zdystring::operator +(zdystring &obj)
{
    zdystring temp;
    int len;
    delete temp.str;
    len=strlen(obj.str)+strlen(str);
    temp.size=len;
    temp.str=new char[len+1];
    if(temp.str==NULL)
    {
        cout<<"分配错误."<<endl;
        exit(1);
    }
    strcpy(temp.str,str);
    strcat(temp.str,obj.str);
    temp.display();
    return temp;
}

zdystring::~zdystring()
{
    delete[]str;
    size=0;
}
void zdystring::display()
{
    cout<<"字符串为"<<this->str<<' ';
    cout<<"字符串长度为"<<this->size<<endl;
    cout<<endl; 
}
void printmeau(void)
{
    int i;
    char *a[13]=
    {
        "◆◆◆◆◆◆欢迎使用串编译器◆◆◆",
        "●1 →字符串初始化",
        "●2 →串拷贝                                      ",
        "●3 →求子串                                      ",
        "●4 →连接字符串                                  ",
        "●5 →求串长度                                    ",
        "●6 →插入串                                      ",
        "●7 →删除串                                      ",
        "●8 →串长度比较                                  ",
        "●9 →显示所有的字符串                            ",
        "●0 →清屏                                        ",
        "●q →退出系统                                    ",
        "◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆",
    };
    for(i=0;i<13;i++)
    {
        
        cout<<a[i]<<endl;
    }
}
int strsize(zdystring obj1,zdystring obj2)
{
    if(obj1.getsize()==obj2.getsize())
        return 0;
    else if(obj1.getsize()>obj2.getsize())
        return 1;
    else 
        return -1;
}
void face(int n){
    char * string[]={"",
                   "\t┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓",
                   "\t┃         ┏┯┓┏┯┓┏┯┓┏┯┓┏┯┓                       ┃",
                   "\t┃         ┠C+┨┠课┨┠设┨┠设┨┠计┨                       ┃ ",
                   "\t┃         ┗┷┛┗┷┛┗┷┛┗┷┛┗┷┛                       ┃   ",
                   "\t┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫",
                   "\t┃     字符串功能实现                                           ┃",
                   "\t┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫",
                   "\t┃     用户使用手册(Using Book)                                 ┃",
                   "\t┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫",
                   "\t┃        在其他功能演示之前,请先初始化                        ┃",
                   "\t┃请你按照菜单的每次要求,输入所要求的数据,否则容易出错        ┃",
                   "\t┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫",
                   "\t┃        ¤ ╭⌒╮ ╭⌒╮                                     ┃",
                   "\t┃       ╱◥██◣╭╭ ⌒╮                                  ┃",
                   "\t┃      ︱田︱田田|╰----------------------------------        ┃",
                   "\t┃      ╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬      ┃",
                   "\t┃                                                              ┃",
                   "\t┃        程序设计:T523-5-18陈琪    指导老师:王晓东老师        ┃   ",
                   "\t┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛"}; 
  if(n>=0)
  {
  for(int i=20;i>0;i--)
  {
      system("cls");
      for(int j=i;j<20;j++)
          cout<<string[j]<<endl;
      Sleep(n);
  }
  }
  else
  {
      system("cls");
      for(int i=0;i<20;i++)
      {
          cout<<string[i]<<endl;
      }
  }
  
}

回复列表 (共3个回复)

沙发

void main()
{
    zdystring str[max],stri;//定义字符串数组
    char *zdystring[max];//定义字符串指针数组
    int m,n;//拷贝时的数组第几个
    int i,j=0,k;
    char d;
    int p,q;
    int r,s,t;//子串的参数
    int u,v;//连接时数组第几个
    int w;//求数组第几个长读
    int h,c;
    int x;
    int y,z;
    int l;
    char order;
    face(10);
    cout<<endl;
    do{
    cout<<"在操作之前请阅读上面的用户使用手册"<<endl;
    cout<<"━━━━━━━━━━━━━━━━━━━━━━━"<<endl;
    cout<<"是否已经阅读? Y/N  ";
    cin>>d;
    cout<<"━━━━━━━━━━━━━━━━━━━━━━━"<<endl;
    }while(d!='y'&&d!='Y');
    cout<<endl<<endl;
    system("cls");
    printmeau();
    cout<<"输入指令:";
    cin>>order;

    while(order!='q'&&order!='Q')
    {
        
        switch(order)
        {
        case '1':
            if(!j)
            
            {
                cout<<"你要输入多少个字符串:"<<endl;
                cin>>m;
                if(m>max||m<=0)
                    cout<<"要求输入的超出规定大小或者不符合规矩:"<<endl;
                else
                {
                    for(i=0;i<m;i++)
                    {
                    cout<<"请你输入第"<<i+1<<"个字符串"<<endl;
                    zdystring[i]=new char;
                    cin>>zdystring[i];
                    str[j].set(zdystring[i]);
                    j++;
                    }
                    cout<<"初始化成功!!"<<endl;
                }
            }
            else
                cout<<"已经初始化!不需要再次初始化!! "<<endl;
            break;
        case '2':
             cout<<"请问拷贝的串和被拷贝串分别为第几个:"<<endl;
             cin>>m;
             cin>>n;
             if(m>j||n>j||m<=0||n<=0)
                 cout<<"你要求所操作的串或者其中之一不存在。"<<endl;
             else
             {
                 cout<<"拷贝前两个字符串分别是:"<<endl;
                 str[m-1].display();
                 str[n-1].display();
                 str[m-1]=str[n-1];
                 cout<<"拷贝后两个字符串分别是:"<<endl;
                 str[m-1].display();
                 str[n-1].display();
             }
             break;
       case '3':
            cout<<"请问你需要求第几个字符串的子串:"<<endl;
            cin>>r;
            if(r>j||r<=0)
                 cout<<"你要求所操作的串不存在。"<<endl;
             else
             {
                 cout<<"这个串是"<<endl;
                 str[r-1].display();
                 cout<<"请选择第几个字符开始:"<<endl;
                 cin>>s;
                 cout<<"请选择子串的长度:"<<endl;
                 cin>>t;
                 if(s<0||t<0||s>str[r-1].getsize()||t>str[r-1].getsize()-s+1)
                     cout<<"子串长度或者开始位置有问题,请重新输入:"<<endl;
                 else
                 {
                     str[j].substring(str[r-1],s,t);
                     cout<<"子串,存入数组位置是第"<<j+1<<"个"<<endl;
                     str[j].display();
                     j++;
                 }
             }
            break;
       case '4':
           cout<<"请问要第几个和哪一个连接起来:"<<endl;
           cin>>u;
           cin>>v;
           if(u>j||v>j||u<=0||v<=0)
               cout<<"你要求所操作的串不存在。"<<endl;
           else
           {
               str[j].set(zdystring[0]);
               str[j].display();
               str[j]=str[u-1]+str[v-1];
               str[j].display();
               cout<<"连接后组成新的串,存入数组位置是第"<<j+1<<"个"<<endl;
               str[j].display();
               j++;
           }
           break;
       case '5':
           cout<<"请问你要第几个字符串的长度:"<<endl;
           cin>>w;
           if(w>j||w<=0)
                cout<<"你要求所操作的串不存在。"<<endl;
           else
                cout<<"该串长为:"<<str[w-1].getsize()<<endl;
           break;
       case '6':
           cout<<"请问你要插入第几个位置后面 :"<<endl;
           cin>>h;
           if(h>j||h<=0)
               cout<<"你要求所操作的位置不能插入或者不存在。"<<endl;
           else
           {
               cout<<"请你第"<<i+1<<"次输入的字符串"<<endl;
               zdystring[i]=new char;
               cin>>zdystring[i];
               stri.set(zdystring[i]);
               str[j].set(zdystring[i]);
               stri.display();
               for(c=j;c>=h-1;c--)
               {
                   str[c]=str[c-1];
               }
               str[h+1]=stri;
               j++;
               cout<<"插入成功!"<<endl;
           }
           break;

       case '7':
           cout<<"请问你要删除第几个串:"<<endl;
           cin>>x;
           if(x>j||x<=0)
               cout<<"你要求所操作的串不存在。"<<endl;
           else
           {
               
               for(x;x<j;x++)
                   str[x-1]=str[x];
               j--;
               cout<<"删除成功!"<<endl;
           }
           break;
       case '8':
           cout<<"哪两个比较:"<<endl;
           cin>>y;
           cin>>z;
           if(y>j||z>j||y<=0||z<=0)
               cout<<"你要求所操作的串或者其中之一不存在。"<<endl;
           else 
           {
               l=strsize(str[y-1],str[z-1]);
               if(l==0)
                   cout<<"两个字符串长度相等:"<<endl;
               else if(l==1)
                   cout<<"前面的字符串长度比后面的大"<<endl;
               else 
                   cout<<"后面的字符串长度比前面的大"<<endl;
           }
           break;
       case'9':
           if(!j)
               cout<<"不存在字符串,无法输出。"<<endl;
           else
           for(k=0;k<j;k++)
           {
               cout<<"第"<<k+1<<"个字符串:";
               str[k].display();
           }
           break;
        case '0':
            system("cls");
            printmeau();
            break;
        default:
            cout<<"你的命令有误。请再次";
            break;
        }
        cout<<"输入指令:";
        cin>>order;
    }
}

板凳

我的这个程序主要原因是+重载时的返回植有问题,请高手帮我看看,不是高手不要乱扯淡,

3 楼

[em1]

我来回复

您尚未登录,请登录后再回复。点此登录或注册