fig18_02.cpp

来自「经典vc教程的例子程序」· C++ 代码 · 共 41 行

CPP
41
字号
// Fig. 18.2: fig18_02.cpp
// Using variable-length argument lists
#include <iostream.h>
#include <iomanip.h>
#include <stdarg.h>

double average( int, ... );

int main()
{
   double w = 37.5, x = 22.5, y = 1.7, z = 10.2;

   cout << setiosflags( ios::fixed | ios::showpoint ) 
        << setprecision( 1 ) << "w = " << w << "\nx = " << x 
        << "\ny = " << y << "\nz = " << z << endl;
   cout << setprecision( 3 ) << "\nThe average of w and x is "
        << average( 2, w, x ) 
        << "\nThe average of w, x, and y is " 
        << average( 3, w, x, y ) 
        << "\nThe average of w, x, y, and z is " 
        << average( 4, w, x, y, z ) << endl;
   return 0;
}

double average( int i, ... )
{
   double total = 0;
   va_list ap;

   va_start( ap, i );

   for ( int j = 1; j <= i; j++ )
      total += va_arg( ap, double );

   va_end( ap );

   return total / i;
}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?