zoukankan      html  css  js  c++  java
  • C++程序设计实践指导1.1删除序列中相同的数改写要求实现

    改写要求1:改写为以指针为数据结构

    #include <iostream>  
    #include <cstdlib>
    
    using namespace std;  
    
    class ARP  
    {  
        int m;  
        int* p;  
        int count[10];
     
    public:  
        ARP(int x[],int size)  
        {  
            m = size;  
            p = new int [m];  
            for (int i =0;i<m;i++)  
            {  
                p[i]=x[i];
            }  
              for (int i =0;i<m;i++)  
            {  
                count[i]=1;
            } 
        }  
        void delsame();  
        void show1();  
        void show2();
        ~ARP()  
        {  
            delete [] p;
        }  
    };  
    void ARP::show1()
    {
         for(int i=0;i<m;i++)  
        {  
            cout<<p[i]<<"	";  
             
        }  
        cout<<endl;     
    }
    void ARP::show2()  
    {  
        for(int i=0;i<m;i++)  
        {  
            cout<<p[i]<<"	";  
             
        }  
          for(int i=0;i<m;i++)  
        {  
            cout<<count[i]<<"	";  
             
        }  
        cout<<endl;  
    }  
    void ARP::delsame()  
    {  
        int i,j;  
        for(i=0;i<m-1;i++)  
        {  
            if(p[i]==p[i+1])  
            { 
                count[i]=count[i]+1; 
                for(j=i+1;j<m-1;j++)  
                {  
                    p[j]=p[j+1];  
                }  
                m --;  
                i --;  
            }         
        }  
    }  
    int main()  
    {  
    
        int b[16] = {1,2,2,3,4,4,5,6,6,7,8,8,8,9,10,10};  
        ARP temp(b,sizeof(b)/sizeof(b[0]));  
        temp.show1();      
        temp.delsame();  
        temp.show2();
        system("pause") ;     
        return 0;  
    }
    

      

  • 相关阅读:
    I2C总线之(三)---以C语言理解IIC
    /sys/class/gpio 文件接口操作IO端口(s3c2440)
    CSP-201503
    CSP-201409
    CSP-201412
    Linux命令行学习笔记
    sstream
    VECTOR
    CSP-201403
    CSP-201312
  • 原文地址:https://www.cnblogs.com/c5395348/p/4271906.html
Copyright © 2011-2022 走看看