#include "stdafx.h"
#include <iostream.h>

/************************************************************************
函数功能: 实现跟printf()函数一样,不确定参数个数,
         
参数累加功能
   
参数: 参数可以是N,但第后一个必须为-1来作为结束条件
************************************************************************/

int OneAddToN(int nFirst, ...)
{
    int nRet = 0;
    //
得到第一个参数的地址
    int *pFirst = &nFirst;
   
    //
参数值不为-1就循环累加
    while (-1 != *pFirst)
    {
        nRet += *pFirst;
        //
移动指针,使其指向第2, 3, ..., N个参数
        pFirst++;
    }
    return nRet;
}


int main(int argc, char* argv[])
{
    cout << OneAddToN(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -1) << endl;
    return 0;
}

多参

posted on 2010-01-20 20:22  o无尘o  阅读(2754)  评论(0编辑  收藏  举报