主题:[讨论]帮我看看着个程序,的错误,
#include<string.h>
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<windows.h>
#define NULL 0
class string
{
private:
char *str;
int size;
public:
string();
string(char *string);
void set(char *string);
string operator =(string obj);
string operator +(string obj);
string substring();
//void strconcat(string string1,string string2);
//string strsub(string substring,string string,int count1,int len);
//string strcopy(string string1,string string2);
char *getstr(){return str;}
int getsize(){return size;}
void display();
~string();
};
string::string()
{
size=1;
str=new char[size];
if(str==NULL)
{
cout<<"分配错误."<<endl;
exit(1);
}
str=NULL;
}
string::string(char *string)
{
size=strlen(string)+1;
str=new char[size];
if(str==NULL)
{
cout<<"分配错误."<<endl;
exit(1);
}
strcpy(str,string);
}
void string::set(char *string)
{
size=strlen(string)+1;
str=new char[size];
if(str==NULL)
{
cout<<"分配错误."<<endl;
exit(1);
}
strcpy(str,string);
size=strlen(str);
}
string string::operator =(string obj)
{
string temp;
int len;
len=strlen(obj.str)+1;
temp.str=new char[len];
temp.size=len;
if(temp.str==NULL)
{
cout<<"分配错误."<<endl;
exit(1);
}
strcpy(temp.str,obj.str);
return temp;
}
string string::operator +(string obj)
{
string temp;
int len;
len=strlen(obj.str)+strlen(str)+1;
temp.str=new char;
if(temp.str==NULL)
{
cout<<"分配错误."<<endl;
exit(1);
}
strcpy(temp.str,str);
strcpy(temp.str,obj.str);
return temp;
}
string::~string()
{
delete[]str;
}
void string ::display()
{
cout<<"字符串长度为"<<size-1<<endl;
cout<<"字符串为"<<str<<endl;
}
void printmeau(void)
{
int i;
char *a[9]=
{
"◆◆◆◆◆◆欢迎使用串编译器◆◆◆",
"●1 →串拷贝 ",
"●2 →求子串 ",
"●3 →连接字符串 ",
"●4 →求串长度 ",
"●5 →串比较 ",
"●6 →清屏 ",
"●q →退出系统 ",
"◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆",
};
for(i=0;i<9;i++)
{
cout<<a[i]<<endl;
}
}
void main()
{
string str1,str2,str;
char *string1,*string2;
int d;
char order;
printmeau();
while(order!='q'&&order!='Q')
{
cout<<"输入指令:";
cin>>order;
switch(order)
{
case '1':
cout<<"请你输入字符串1"<<endl;
string1=new char;
cin>>string1;
str1.set(string1);
cout<<"请你输入字符串2"<<endl;
string2=new char;
cin>>string2;
str2.set(string2);
cout<<"请问你要将第几个拷贝给你另外一个(1或者2):"<<endl;
cin>>d;
if(d==1)
str2=str1;
else if(d==2)
str1=str2;
else
cout<<"输入有误。"<<endl;
break;
case '3':
cout<<"请你输入字符串1"<<endl;
string1=new char;
cin>>string1;
str1.set(string1);
cout<<"请你输入字符串2"<<endl;
string2=new char;
cin>>string2;
str2.set(string2);
str=str1+str2;
str.display();
case '4':
cout<<"请你输入字符串"<<endl;
string1=new char;
cin>>string1;
str1.set(string1);
cout<<"该串长为:"<<str1.getsize()<<endl;
break;
case '6':
system("cls");
printmeau();
break;
default:
cout<<"你的命令有误。请再次";
break;
}
}
}
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<windows.h>
#define NULL 0
class string
{
private:
char *str;
int size;
public:
string();
string(char *string);
void set(char *string);
string operator =(string obj);
string operator +(string obj);
string substring();
//void strconcat(string string1,string string2);
//string strsub(string substring,string string,int count1,int len);
//string strcopy(string string1,string string2);
char *getstr(){return str;}
int getsize(){return size;}
void display();
~string();
};
string::string()
{
size=1;
str=new char[size];
if(str==NULL)
{
cout<<"分配错误."<<endl;
exit(1);
}
str=NULL;
}
string::string(char *string)
{
size=strlen(string)+1;
str=new char[size];
if(str==NULL)
{
cout<<"分配错误."<<endl;
exit(1);
}
strcpy(str,string);
}
void string::set(char *string)
{
size=strlen(string)+1;
str=new char[size];
if(str==NULL)
{
cout<<"分配错误."<<endl;
exit(1);
}
strcpy(str,string);
size=strlen(str);
}
string string::operator =(string obj)
{
string temp;
int len;
len=strlen(obj.str)+1;
temp.str=new char[len];
temp.size=len;
if(temp.str==NULL)
{
cout<<"分配错误."<<endl;
exit(1);
}
strcpy(temp.str,obj.str);
return temp;
}
string string::operator +(string obj)
{
string temp;
int len;
len=strlen(obj.str)+strlen(str)+1;
temp.str=new char;
if(temp.str==NULL)
{
cout<<"分配错误."<<endl;
exit(1);
}
strcpy(temp.str,str);
strcpy(temp.str,obj.str);
return temp;
}
string::~string()
{
delete[]str;
}
void string ::display()
{
cout<<"字符串长度为"<<size-1<<endl;
cout<<"字符串为"<<str<<endl;
}
void printmeau(void)
{
int i;
char *a[9]=
{
"◆◆◆◆◆◆欢迎使用串编译器◆◆◆",
"●1 →串拷贝 ",
"●2 →求子串 ",
"●3 →连接字符串 ",
"●4 →求串长度 ",
"●5 →串比较 ",
"●6 →清屏 ",
"●q →退出系统 ",
"◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆",
};
for(i=0;i<9;i++)
{
cout<<a[i]<<endl;
}
}
void main()
{
string str1,str2,str;
char *string1,*string2;
int d;
char order;
printmeau();
while(order!='q'&&order!='Q')
{
cout<<"输入指令:";
cin>>order;
switch(order)
{
case '1':
cout<<"请你输入字符串1"<<endl;
string1=new char;
cin>>string1;
str1.set(string1);
cout<<"请你输入字符串2"<<endl;
string2=new char;
cin>>string2;
str2.set(string2);
cout<<"请问你要将第几个拷贝给你另外一个(1或者2):"<<endl;
cin>>d;
if(d==1)
str2=str1;
else if(d==2)
str1=str2;
else
cout<<"输入有误。"<<endl;
break;
case '3':
cout<<"请你输入字符串1"<<endl;
string1=new char;
cin>>string1;
str1.set(string1);
cout<<"请你输入字符串2"<<endl;
string2=new char;
cin>>string2;
str2.set(string2);
str=str1+str2;
str.display();
case '4':
cout<<"请你输入字符串"<<endl;
string1=new char;
cin>>string1;
str1.set(string1);
cout<<"该串长为:"<<str1.getsize()<<endl;
break;
case '6':
system("cls");
printmeau();
break;
default:
cout<<"你的命令有误。请再次";
break;
}
}
}