⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 string.cpp

📁 关于数据结构中串的实现
💻 CPP
字号:
# include <iostream.h>
# include <malloc.h>
# include <string.h>
 # define NULL 0
struct elemstring
{  
  char *array;
	  int length;
}string;

class String 
{
public:
     String()
	 {string.array =NULL; 
       string.length=0;
	   newstring.array =NULL;
	   newstring.length =0;
	   substring.length=0;
	   substring.array=NULL;
	  cout<<"构造函数被调用。"<<endl;
	  cout<<"初始化后,string.  newstring.   substring  都为空"<<endl;
	  cout<<endl;
	 }
  /*	 ~String()
	 {    
		 cout<<endl<<"析构函数被调用。"<<endl;
		 cout<<"析构前的串为:"<<string.array <<endl;
   		 cout<<"析构前合并的新串为:"<<newstring.array<<endl;
  	     cout<<"析构前的子串为:"<<substring.array<<endl;
 	     delete string.array	 ;
		 free(substring.array);
		 free(newstring.array);	 
	 }*/
	  void StrAssign(char *ap);
	  void StrConcat(char *s1 );
	  int StrLength();
	  int StrCompare(char  *ap);
	  void ClearString();
	  int SubString(int i,int b);
	  void showString();
protected:                       
    elemstring  string;
    elemstring  newstring;
	elemstring  substring;
};
   

void String::StrAssign(char *ap)//构造对象
{   
	cout<<"串为:"<<ap<<endl;  
       if(ap==NULL)
	   {   
		      string.array=NULL;
	          string.length=0;
			  cout<<"赋值串为空不用输出字符串." <<endl;
			  cout<<"它的长度为0."<<string.length<<endl;

	   }
		      else { 
				  int c=0;
                  for(c;ap[c] ;c++)
 				 string.array=new char[strlen(ap)+1];                                                      
				  if (string.array==NULL)
		             cout<<"串是空的."<<endl;
				  else 
				  {      strcpy(string.array ,ap);   
 							 string.length =c ;
				  }
 				       	      cout<<"创建成功!"<<endl;
					  	      cout<<"串的内容为:"<<string.array<<endl;
					     	  cout<<"串长为 :"<<string.length<<endl;
					  }  
			
			  
	}
  





int String::StrLength()//返回串的元素个数
{  
	cout<<"输出串的长度:" ;
	for(int i=0;string.array[i];i++){}
		cout<<i<<endl;
	return  1;
}


void String::ClearString()//串的清空
{
	if( string.array==NULL)
	{cout<<"串是空的:"<<string.array<<endl;
	cout<<"串长为:"<<string.length<<endl;}
	 else
	 {  cout<<"清空前string:"<<string.array<<endl
	        <<"清空前string的长度为:"<<string.length<<endl;
		 free(string.array);
    	  string.array=NULL; 
		  string.length=0;
	 cout<<"现在string的为为空。" <<endl;
	 cout<<"现在string的长度为:"<<string.length<<endl;
	 }
}


int String::StrCompare(char*ap) //两串进行比较
{   
     cout<<"进行串比较。"<<endl;
	 cout<<"串一:"<<string.array <<endl
		 <<"串二:"<<ap<<endl;
      
	 cout<<"串比较结:"<<endl;
			if(strcmp(string.array,ap)==0)
					cout<<"两串相等。"<<endl;
			  else  
			  {if (strcmp(string.array ,ap)>0)
					  cout<<"串一大于串二。"<<endl; 
				       else 
						   cout<<"串一小于串二。"<<endl; }
 		  return 1;
}
    
 
 

void String::StrConcat (char *s1)//合并串函数
{      
	  
	   if(sizeof(s1)==NULL)
	   {
		  cout<<"串二为空。"<<endl;
	   }  
 	      else
		  {	      
	             cout<<"被合并的串为:"<<s1<<endl;
                 int c=0;
				 for(c;s1[c] ;c++){}
				 newstring.length =c;
                 newstring.array=new char[strlen(string.array)+1];                                                      
				  if (string.array==NULL)
		             cout<<"串是空的."<<endl;
				 strcpy(newstring.array ,string.array );
				 strcat(newstring.array,s1);
  			     newstring.length =c+string.length  ;
				 cout<<"newstring is(新串为) :"<<newstring.array <<endl;
 	             cout<<"newstring(新串的)长度为:"<< newstring.length  <<endl;
		  }
}		
     


int String::SubString(int pos,int len)//求子串函数
{  
	 
 	if(pos<=0||pos>string.length||len<0||len>string.length-pos+1)
	{
		cout<<"输入的长度和位置是错的。"<<endl;
		return 0;
	 
	}
   /* { 
     loop:
		{cout<<"再次输入:"<<endl;
	    cout<<"位置:";
		cin>>pos;
		cout<<"长度:";
	    cin>>len;
        }
		if(pos<=0||pos>string.length||len<0||len>string.length-pos+1)
    	goto loop;
	}*/
 
    
         if (!substring.array)
		//free(substring.array);
		     if(!substring.length )
			 {
			    substring.array =NULL;
			    substring.length =0;
			 }
 		    
			 substring.array =(char*)malloc(len*sizeof(char));
				     for(int i=0;i<=len-1 ;i++)
					 { 
						 substring.array [i]=string.array[pos-1+i] ;
						 substring.length =len;
					 }
			 cout<<"输出子串:"<<substring.array <<endl
							 <<"输出子串长度:"<<substring.length <<endl;
	   return 1;
 
}
		 
	  
void String::showString ()//打印函数
{
	cout<<"本串string为:"<<string.array <<endl;
	cout<<"本串string的长度为:"<<string.length <<endl;
	cout<<"newstring的内容为:"<<newstring.array<<endl
		<<"newstring的长度为:"<<newstring.length<<endl
		<<"substring的内容为:"<<substring.array<<endl
		<<"substring的长度为:"<<substring.length<<endl;
}


void main ()
{   
 	 String AString;
	 cout<<endl<<"定义了一个对象,开始初始化。"<<endl;
  	 AString.StrAssign("每天都快乐");
     cout<<endl<<"长度函数调用,输出该string的长度."<<endl;
     AString.StrLength ();
	 cout<<endl<<"比较函数开始调用."<<endl;
     AString.StrCompare("每天都快了啊呀啊呀");
     cout<<endl<<"合并函数调用.合并开始."<<endl;
     AString.StrConcat ("大家来跳舞");
     cout<<endl<<"求子串函数调用.求子串开始."<<endl;
	 AString.SubString (1,2);
	 cout<<endl<<"打印函数调用."<<endl;
     AString.showString ();
	 cout<<endl<<"清除函数调用."<<endl;
	 AString.ClearString ();
}






 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -