变长参数应用举例
2006-02-22 20:52
447 查看
先得声明一个变长参数的变量va_list list
在使用前要先用va_start(list, last_param)对list进行初始化,last_param为最右边的已知参数,表示list
从last_param的下一个参数开始
va_arg(list, 类型)
最后不要忘了用va_end(list)
eg1:
#include<iostream>
#include<iomanip>
#include<stdarg.h>
using namespace std;
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 << average(2, w, x) << endl;
cout << average(3, w, x, y) << endl;
cout << 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;
}
eg2:
#include<iostream.h>
#include <stdlib.h>
#include <stdarg.h>
void error(const char*format...);
void main()
{
int a;
char c='d';
char s[100];
error("Enter a string:"); //输入一个字符串
cin>>s;
error("Enter an integer:"); //输入一整数
cin>>a;
error("%s/n%d/n%c/n",s,a,c); //打印输出
}
void error(const char*format...) //实现像printf函数一样的打印输出功能
{
int i;
int j=0;
va_list ap;
va_start(ap,format);
for(i=0;*(format+i)!=0;)
{
int in;
char* pc;
char d;
if(*(format+i)=='%')
{
switch(*(format+i+1))
{
case'd':in=va_arg(ap,int);cout<<in;i=i+2;break;
case's':pc=va_arg(ap,char*);cout<<pc;i=i+2;break;
case'c':d=va_arg(ap,char);cout<<d;i=i+2;break;
default:cout<<'%';i=i+1;break;
}
}
else
{
cout<<*(format+i);
i++;
}
}
}
在使用前要先用va_start(list, last_param)对list进行初始化,last_param为最右边的已知参数,表示list
从last_param的下一个参数开始
va_arg(list, 类型)
最后不要忘了用va_end(list)
eg1:
#include<iostream>
#include<iomanip>
#include<stdarg.h>
using namespace std;
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 << average(2, w, x) << endl;
cout << average(3, w, x, y) << endl;
cout << 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;
}
eg2:
#include<iostream.h>
#include <stdlib.h>
#include <stdarg.h>
void error(const char*format...);
void main()
{
int a;
char c='d';
char s[100];
error("Enter a string:"); //输入一个字符串
cin>>s;
error("Enter an integer:"); //输入一整数
cin>>a;
error("%s/n%d/n%c/n",s,a,c); //打印输出
}
void error(const char*format...) //实现像printf函数一样的打印输出功能
{
int i;
int j=0;
va_list ap;
va_start(ap,format);
for(i=0;*(format+i)!=0;)
{
int in;
char* pc;
char d;
if(*(format+i)=='%')
{
switch(*(format+i+1))
{
case'd':in=va_arg(ap,int);cout<<in;i=i+2;break;
case's':pc=va_arg(ap,char*);cout<<pc;i=i+2;break;
case'c':d=va_arg(ap,char);cout<<d;i=i+2;break;
default:cout<<'%';i=i+1;break;
}
}
else
{
cout<<*(format+i);
i++;
}
}
}
相关文章推荐
- Jquery的ajax各个参数含义和应用举例
- Oracle自定义函数及应用举例(不同输入输出参数情况)
- va_start,va_arg,va_end,va_list应用举例--实现可变参数的函数
- LCD屏参数及应用举例
- grep常见参数及应用举例
- Linux内核网络参数的意义及应用
- 串的应用举例
- PagedDataSource属性及应用举例
- 《转》Ceilometer Alarm API 参数详解 及 举例说明
- 栈的应用举例之进制转换
- Redis实战应用举例
- 内存文件映射应用举例
- Redis实战应用举例
- Redis实战应用举例
- popup的简单应用举例(具体在增删改查组件中用到)以及补充的知识点
- Ruby方法参数默认值的一个小技巧在Rails中的应用
- Base64 算法的应用举例
- 《数据结构》(C语言版)——栈的应用举例-迷宫求解
- Linux RPM 命令参数使用详解[介绍和应用]
- HTML5 localStorage本地存储实际应用举例