重载运算符“+”、“*”、“-”实现集合的并集、交集、差集运算

 

Code:
  1. #include  
  2. #include  
  3. using namespace std;  
  4. class A  
  5. {  
  6. public:  
  7.         void getdata()//输入数组元素   
  8.         {   cout<<"请输入集合中的元素:";  
  9.             gets(data);        
  10.          }   
  11.         void print( )//输出数组元素  
  12.         {     
  13.             puts(data);  
  14.         }  
  15.          
  16.          A operator +(A s1)  //重载"+"实现字符串的并集运算  
  17.         {   
  18.                   
  19.             A temp;  
  20.             count=strlen(data);  
  21.             s1.count=strlen(s1.data);  
  22.             int i=0,j=0,k=0,x=0;   
  23.             for(i=0;i
  24.             {    
  25.                 temp.data[x]=data[i];  
  26.                 x++;  
  27.             }  
  28.             for(j=0;j
  29.             {    
  30.                 k=0;  
  31.                 for(i=0;i
  32.                 {    
  33.                     if(s1.data[j]==data[i])  
  34.                         break;  
  35.                     else if(s1.data[j]!=data[i])  
  36.                         k++;  
  37.                     if(k==count)  
  38.                       {  
  39.                         temp.data[x]=s1.data[j];  
  40.                         x++;  
  41.                       }  
  42.                      
  43.                 }  
  44.             }  
  45.              
  46.            temp.data[x]='/0';  
  47.            return temp;  
  48.         }  
  49.   
  50.         A operator *(A s1)  //重载“*”实现字符串的交集运算  
  51.         {  
  52.            A temp;  
  53.            count=strlen(data);  
  54.            s1.count=strlen(s1.data);  
  55.            int i=0,j=0,x=0;   
  56.            for(i=0;i
  57.            {  
  58.                for(j=0;j
  59.                {  
  60.                    if(data[i]==s1.data[j])  
  61.                    {  
  62.                        temp.data[x]=data[i];  
  63.                        x++;  
  64.                    }  
  65.                }  
  66.            }  
  67.            temp.data[x]='/0';  
  68.            return temp;  
  69.         }  
  70.   
  71.         A operator -(A s1)  //重载“-”实现字符串的差集运算  
  72.         {  
  73.            A temp;  
  74.            count=strlen(data);  
  75.            s1.count=strlen(s1.data);  
  76.            int i=0,j=0,k=0;  
  77.            static int x=0;   
  78.            for(i=0;i
  79.            {   k=0;  
  80.                for(j=0;j
  81.                {  
  82.                    if(data[i]==s1.data[j])  
  83.                        break;  
  84.                    else if(data[i]!=s1.data[j])  
  85.                        k++;  
  86.                        if(k==s1.count)  
  87.                        {  
  88.                           temp.data[x]=data[i];  
  89.                           x++;  
  90.                        }  
  91.                      
  92.                }  
  93.            }  
  94.            temp.data[x]='/0';  
  95.            return temp;  
  96.   
  97.         }  
  98.         
  99. private:  
  100.         char data[100];  
  101.         int count;  
  102. };  
  103.    
  104. void main()  
  105. {  
  106.         A s1,s2,s3,s4;  
  107.         s1.getdata();  
  108.         s2.getdata();  
  109.         s3.getdata();  
  110.           
  111.         cout<<"并集:";  
  112.         s4=s1+s2+s3;  
  113.         s4.print();  
  114.   
  115.         cout<<"交集:";  
  116.         s4=s1*s2*s3;  
  117.         s4.print();  
  118.   
  119.         cout<<"差集:";  
  120.         s4=s1-s2;  
  121.         s4.print();  
  122. }  

 

你可能感兴趣的:(重载运算符“+”、“*”、“-”实现集合的并集、交集、差集运算)