zoukankan      html  css  js  c++  java
  • 递归

      

    #include <iostream>
    using namespace  std;
    bool ALLisNum(string str);
    bool myatoi(string str,int &num);
    class RecurveArray
    {
    public:
        int Maxsize;
        int *Elements;//数组类声明
        int CurrentSize;
        RecurveArray(int Size = 10): Maxsize(Size),Elements(new int[Size]){}
        ~RecurveArray()
        {delete [] Elements;}
        void InputArray();//输入数组内容
        int MaxKey(int n);//最大值
        int Sum(int n);//
        float Average(int n);
    };
    
    
    void RecurveArray::InputArray()
    {
        cout<<"input the num of array: 
    ";
        string str;
        for(int i = 0; i<Maxsize;)
        {
            cin>>str;
            if(ALLisNum(str))
            {
                if(myatoi(str,Elements[i]))
                    i++;
                else
                    printf("myatoi error
    ");
            }
            else
                fprintf(stdout,"error");
            
        }
    }
    int RecurveArray::MaxKey(int n)
    {
        if(n==1)
            return Elements[0];
        
        int temp = MaxKey(n-1);
        if(Elements[n-1]>temp)
            return Elements[n-1];
        else
            return temp;
        
    }
    int RecurveArray::Sum(int n)
    {
        if(n==1)
            return Elements[0];
        
        return Sum(n-1)+Elements[n-1];
        
    }
    
    
    float RecurveArray::Average(int n)
    {
        if(n ==1)
            return Elements[0];
        return ((float)Elements[n-1]+Average(n-1)*(n-1))/n;
    }
    
    
    int main()
    {
        int size =-1;
        char str[5];
        
        while(true)
        {
            std::cout<<"No.of the elements:";
            cin>>str;
            if(ALLisNum(str))
            {
                if(myatoi(str,size))
                    break;
            }
            
        }
        RecurveArray re(size);
        re.InputArray();
        std::cout<<"the max is:"<<re.MaxKey(re.Maxsize)<<endl;
        std::cout<<"The sum is"<<re.Sum(re.Maxsize)<<endl;
        std::cout<<"The Average is"<<re.Average(re.Maxsize)<<endl;
        return 0;
        
    }
    
    
    bool ALLisNum(string str)
    {
        const char *p = str.c_str();
        
        while (*p!='')
        {
            if(*p<'0' || *p>'9')
                return false;
            p++;
        }
        
        return true;
        
    }
    bool myatoi(string str,int &num)
    {
        const char* p = str.c_str();
        int temp;
        while(*p!='')
        {
            temp=temp*10+(*p-'0');
            p++;
            
        }
        num = temp;
        return true;
    }

    似懂非懂
    #include <iostream>
    using namespace std;
    class List;  //声明
    class ListNode  //节点
    {
        friend class List;
    private:
        int data;
        ListNode* link;
        ListNode(const int item):data(item),link(NULL){}
    };
    class List  //链表
    {
    private:
        //连个指指针方便尾插
        ListNode* frist,*current;
        //私有方法
        int Max(ListNode* f);
        int Num(ListNode* f);
        float Avg(ListNode* f,int &n);
    
    
    public:
        //共有接口
        List():frist(NULL),current(NULL){}
        ~List(){}
        ListNode* NewNode(const int item);
        void NewList(const int retvalue);
        void PrintList();
        int GetMax(){return Max(frist);}
        int GetNum()
        {
            return Num(frist);
        }
        float GetAvg(){
            int n=0;
            return Avg(frist,n);}
    
    
    };
    //产生新节点
    ListNode* List::NewNode(const int item)
    {
        ListNode* newnode =  new ListNode(item);
        return newnode;
    }
    void List::NewList(const int retvalue)
    {
        frist =NULL;
        int value;
        ListNode *q;
        cout<<"input new num:"<<endl;
        cin>>value;
        while(value != retvalue)
        {
            q = NewNode(value);
    
    
            if(frist ==NULL)
            {
                frist=current=q;
            }
            else
            {
                current->link =q;
                current = q;
            }
            cin>>value;
        }
        current->link = NULL;
    
    
    }
    void List::PrintList()
    {
        cout<<"
     The list is:
    ";
        ListNode *p;
        p=frist;
        while(p!=NULL)
        {
            cout<<p->data<<" ";
            p=p->link;
        }
        cout<<"
    ";
    }
    
    
    //递归求最大值  不满足条件继续调用本身
    int List::Max(ListNode *f)
    {
        if(f->link==NULL) return f->data;
        int temp = f->data;
        return temp>Max(f->link)?temp:Max(f->link);
    
    
    }
    
    
    //
    int List::Num(ListNode* f)
    {
        if(f == NULL) return 0;
        return 1+Num(f->link);
    }
    //不为null 返回前面(n-1)的总和加上
    float List::Avg(ListNode* f,int &n)
    {
        //条件为一个结点时返回 第一个节点的值
        
        if(f->link== NULL)
        {
            n=1;
            return (float)(f->data);
        }
        else
        {  //这个地方不太明白
            float sum = Avg(f->link,n)*(n);
            n++;
    
    
            return (f->data+sum)/n;
        }
    }
    
    
    int main()
    {
        List ll;
        ll.NewList(88);
        ll.PrintList();
    
    
        int num = ll.GetNum();
        cout<<num<<" "<<endl;;
        int maxNum =ll.GetMax();
        cout<<maxNum<<endl;
    
    
        int ave = ll.GetAvg();
        cout<<ave<<endl;
    }


  • 相关阅读:
    Java设计模式之代理模式
    Java设计模式之单例模式
    Android 退出多Activity的application的方式
    自己动手系列——实现一个简单的LinkedList
    自己动手系列——实现一个简单的ArrayList
    Java多线程基础——Lock类
    制作一个功能丰富的Android天气App
    IdentityServer4 学习二
    IdentityServer4 学习一
    SC创建服务编写bat脚本
  • 原文地址:https://www.cnblogs.com/countryboy666/p/10949539.html
Copyright © 2011-2022 走看看