递归

  

#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: \n";
    string str;
    for(int i = 0; i<Maxsize;)
    {
        cin>>str;
        if(ALLisNum(str))
        {
            if(myatoi(str,Elements[i]))
                i++;
            else
                printf("myatoi error\n");
        }
        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!='\0')
    {
        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!='\0')
    {
        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<<"\n The list is:\n";
    ListNode *p;
    p=frist;
    while(p!=NULL)
    {
        cout<<p->data<<" ";
        p=p->link;
    }
    cout<<"\n";
}

//递归求最大值  不满足条件继续调用本身
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;
}


posted @ 2019-05-30 14:58  countryboy666  阅读(105)  评论(0编辑  收藏  举报