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

📄 assem.cpp

📁 Assem一个集合类 实现集合对象的简单的操作
💻 CPP
字号:
// ASSEM.CPP  
//类Assem的定义
// 函数体的定义,声明在ASSEM.H中
#include <iostream.h>
#include <string.h>
#include <process.h>
#include "assem.h"

      //利用重载的运算符进行输出与输入
ostream &operator<<(ostream &output ,const Assem &a)  //output  重载运算符<<
{
    for(int i=0;i<a.Getlength();i++)             //FOR循环的输出
	{
		if(i%10==0)output<<endl;              //10个元素换一行
		output<<a.Getfirst()[i]<<"   ";
	}
	    
		output<<endl<<endl;

	return output; //返回输出流的对象可以循环调用即cout<<a<<b;
}
istream &operator>>(istream &input , Assem &a)    //input 重载运算符>>
{   
	if(a.Isempty())                       //若未指定集合元素的个数赋值,显示出错信息
	{cout<<"该集合未被授予元素个数"<<endl;
		return input;
	}
	cout<<"请输入"<<a.Getlength()<<"个字符:";
	char temp;                              //先存到一个临时的变量中,然后判断是否合理
	int i=0;                                	
	while(i<a.Getlength())                
	{
       input>>temp;
	   if(temp>='a'&&temp<='z')       //判断输入的元素的合法性
	   {
		a.Getfirst()[i]=temp;         //合理则存到集合中,否则输出错误信息继续输入
		i++;                          //同时指针加1
	   }
	   else
	   {
		   cout<<endl<<temp<<" is not a legal Parameter ,";    //输出出错信息
		   cout<<"please enter your Parameter between 'a' and 'z':\n";
		   continue;                               //可以不受影响继续输入,指针不加1
	   }
	 }
	    
	a.Getfirst()[a.Getlength()]='\0';      //指定结束符           
	cout<<endl;
	return input;               //循环调用  cin>>a>>b
}

Assem::Assem(const int s)          //默认构造函数
   
{
	size=(s>=0? s : 0);             //判断初始化的集合元素个数的合法性
	if(!Isempty())                      
	{
		if((first=new char[size+1])==NULL)
		{cout<<"内存不足"<<endl;
		 exit(1);
		}
		
	}	
	else
	{
		first=NULL;
	}

}


Assem::Assem(const char *s)
{

    size=strlen(s);
	if(!Isempty())
	{
	   if((first=new char[size+1])==NULL)
	   {
		   cout<<"内存不足"<<endl;
		   exit(1);
	   }
	
	   strcpy(first,s);
	}
	else
	{
		first=NULL;
	}
}


Assem::Assem(const Assem &a)
{
    size=a.Getlength();
	if(!Isempty())
	{
	   if((first=new char[size+1])==NULL)
	   {
		   cout<<"内存不足"<<endl;
		   exit(1);
	   }
	    strcpy(first,a.Getfirst());
	}
	else
	{
		first=NULL;
	}
}

void Assem::Setsize( int s1)            //void Setsize(int=10); 
{
    size=( s1>=0 ? s1 : size);
	if(!Isempty())
	  if((first=new char[size+1])==NULL)
	   {
		   cout<<"内存不足"<<endl;
		   exit(1);
	   }
}
  

int Assem::delete_element( int j )
{
	if(j<1&&j>size-1)return 0;
	first[j-1]='\0';
	strcat(first,first+j);
	size--;
	return 1;
}

int Assem::Isempty(void) const
{
	return (size==0);
}


char *Assem::Getfirst(void)const
{
     return first;
}


Assem &Assem::operator=(const Assem &s)
{   
	size=s.Getlength();
    if(Isempty())
	{
		first=NULL;
	    return *this;
	}
	else
	{
		if((first=new char[size+1])==NULL)
	   {
		   cout<<"内存不足"<<endl;
		   exit(1);
	   }
	
		strcpy(first,s.Getfirst());
		return *this;
	}

}

Assem &Assem::operator+(const Assem &s2)const
{   
    
	if(size+s2.Getlength())
	{
		
		char *s;
		if((s=new char[size+s2.Getlength()+1])==NULL)
		{
			cout<<"内存不足"<<endl;
		    exit(1);
		}
		strcpy(s,first);
		strcat(s,s2.Getfirst());
		Assem* p;
			if((p=new Assem (s))==NULL)
			{
				cout<<"内存不足"<<endl;
				exit(1);
			}
		p->asend();
		p->element_exclude();
		return (*p);
	}
	else 
	{
		Assem* p;
		if((p=new Assem(0))==NULL)
		{
			cout<<"内存不足"<<endl;
			exit(1);
		}
	    return (*p);
	}
}


Assem &Assem::operator-(const Assem &s2)const
{  
	Assem* p;
    if((p=new Assem((*this)))==NULL)
	{
		cout<<"内存不足"<<endl;
		exit(1);
	}
	for(int i=0;i<s2.Getlength();i++)
	{ 
		int j=0;
		while(j<size)
		
			if(p->Getfirst()[j]==s2.Getfirst()[i])
				 p->delete_element(j+1);
			else j++;
	}
    p->asend();
	p->element_exclude();
	return (*p);
}
		
	
Assem &Assem::operator*(const Assem &s2)const
{  
  
   char *s;
   if((s=new char[size+s2.Getlength()+1])==NULL)
   {
	   cout<<"内存不足"<<endl;
	   exit(1);
   }
   int k=0;
   for(int i=0;i<size;i++)
       for(int j=0;j<s2.Getlength();j++)
		   if(first[i]==s2.Getfirst()[j])
			   s[k++]=first[i];
   s[k]='\0';
   Assem* p;
   if((p=new Assem(s))==NULL)
   {
	   cout<<"内存不足"<<endl;
	   exit(1);
   }
   p->asend();
   p->element_exclude();
   return (*p);
}

  
void Assem::asend(void)
{  
	int flag=0;             //flag为1则表示交换,为0则没交换
	int pass=0;
	while(!flag)
	{   flag=1;
		pass++;
		for(int i=0;i<size-pass;i++)
		
			if(first[i]>first[i+1])
			{
				char temp;
				temp=first[i+1];
				first[i+1]=first[i];
				first[i]=temp;
				flag=0;
			}
	}
}
		

void Assem::element_exclude(void)          //用于将排好序的集合中的多余元素去除
{   
	int i=0 ;
	while(i<size)
	
		if(first[i]==first[i+1])
		{
			delete_element(i+2);
		}
		else i++;
			
}



Assem::~Assem()
{
	delete [] first;
}
 


//主函数,测试函数
main(void)
{   
	char choice;
	cout<<"请选择何种方式创建集合:\n  '1'为以输入集合个数形式创建;";
	cout<<"\n  '2'为输入字符串形式创建.\n"<<endl;
	cout<<"请输入选择:";
	cin>>choice;
    switch(choice)
	   {
	    case '1':
			{
	   				int size=0;
					cout<<"请输入集合一的元素个数:";
					cin>>size;
					cout<<endl;
					Assem a;
					a.Setsize(size);
					cin>>a;
					cout<<a;

					cout<<"请输入集合二的元素个数:";
					cin>>size;
					cout<<endl;
					Assem b;
					b.Setsize(size);
					cin>>b;
					cout<<b;
						cout<<"集合一和二并集为:"<<endl<<a+b;

						cout<<"集合一和二差集为:"<<endl<<a-b;

						cout<<"集合一和二交集为:"<<endl<<a*b;
						break;
			}
	
		case '2':
			{   
				cout<<"请输入集合一的字符串(字符串以'#'结尾):";
				char* s=new char[50];
				int i=0;
				cin>>s[i];
				while(s[i]!='#')
				{
					cin>>s[++i];
				}
				s[i]='\0';
				
				Assem a(s);
                cout<<endl<<"集合一为:"<<endl<<a;

				cout<<"请输入集合二的字符串:";
			    i=0;
				cin>>s[i];
				while(s[i]!='#')
				{
					cin>>s[++i];
				}
				s[i]='\0';
				
				Assem b(s);
				delete [] s;
                cout<<endl<<"集合一为:"<<endl<<b;

				cout<<"集合一和二并集为:"<<endl<<a+b;

						cout<<"集合一和二差集为:"<<endl<<a-b;

						cout<<"集合一和二交集为:"<<endl<<a*b;
						break;
			}
		
		
		default:
			{
				cout<<"选择只能为'1'或'2'"<<endl;
			    break;
			}
	   }
	
    return 0;

}

⌨️ 快捷键说明

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