未确定参数数目的函数编写

今天看<<the c++ programming language>>的时候看到了这个内容,就试着编写一个类似c下面的printf函数,虽然是实现了,但是效率好像不是很好,现在贴出我今晚的一个小成果。

 

  1. /*
  2. 题目: MyPrint
  3. 作者: sgx
  4. 日期: 2008-11-4
  5. IDE:  vc6.0
  6. */
  7. #include <iostream> 
  8. #include <cstdarg>//必须的头文件,编写不确定参数要用到里面的宏
  9. #include <assert.h>
  10. using namespace std; 
  11. void MyPrint(const char* ...);
  12. int str_copy2(const char* ,char*);
  13. int main() 
  14.     int i=12;
  15.     char c='c';
  16.     char str[]="love";
  17.     long il=35;
  18.     double id=35.001;
  19.     float ff=351.015f;
  20.     
  21.     MyPrint("cniksd/n");
  22.     MyPrint("my no is #n/n",&i,NULL);//最后要传递一个NULL控制结束
  23.     MyPrint("c=#c,str=#s,il=#l,id=#d,ff=#f/n",&c,str,&il,&id,&ff,NULL);
  24.     system("PAUSE");
  25.     return 0;
  26. }
  27. void MyPrint(const char* print_str  ...)
  28. {
  29.     bool no_arg = false;
  30.     const char* p_str=print_str;
  31.     char symbol[3];//用于接收特别的字符,以确定输出类型
  32.     /*
  33.     #n 整形,
  34.     #l 长整形,
  35.     #c 字符型
  36.     #s 字符串
  37.     #d double
  38.     #f float*/
  39.     va_list print_list;  //创建一个表把未知参数载入
  40.     va_start(print_list,print_str);
  41.     
  42.     while(*p_str!='/0')
  43.     {
  44.         if(*p_str=='#')
  45.         {
  46.             int i = str_copy2(p_str,symbol);
  47.             if(!no_arg && i == 2)
  48.             {
  49.                 switch(symbol[1])
  50.                 {
  51.                 case 'n':
  52.                     {
  53.                         int* pn = va_arg(print_list,int*);//指定参数类型
  54.                         if(pn != NULL)
  55.                         {
  56.                             cout<<*pn;
  57.                         }
  58.                         else
  59.                             no_arg=true;
  60.                     }
  61.                     break;
  62.                 case 'l':
  63.                     {
  64.                         long* pl = va_arg(print_list,long*);//指定参数类型
  65.                         if(pl != NULL)
  66.                         {
  67.                             cout<<*pl;
  68.                         }
  69.                         else
  70.                             no_arg=true;
  71.                     }
  72.                     break;
  73.                 case 'c':
  74.                     {
  75.                         char* pc = va_arg(print_list,char*);//指定参数类型
  76.                         if(pc != NULL)
  77.                         {
  78.                             cout<<pc[0];
  79.                         }
  80.                         else
  81.                             no_arg=true;
  82.                     }
  83.                     break;
  84.                 case 's':
  85.                     {
  86.                         char* ps = va_arg(print_list,char*);//指定参数类型
  87.                         if(ps != NULL)
  88.                         {
  89.                             cout<<*ps;
  90.                         }
  91.                         else
  92.                             no_arg=true;
  93.                     }
  94.                     break;
  95.                 case 'd':
  96.                     {
  97.                         double* pd = va_arg(print_list,double*);//指定参数类型
  98.                         if(pd != NULL)
  99.                         {
  100.                             cout<<*pd;
  101.                         }
  102.                         else
  103.                             no_arg=true;
  104.                     }
  105.                     break;
  106.                 case 'f':
  107.                     {
  108.                         float* pf = va_arg(print_list,float*);//指定参数类型
  109.                         if(pf != NULL)
  110.                         {
  111.                             cout<<*pf;
  112.                         }
  113.                         else
  114.                             no_arg=true;
  115.                     }
  116.                     break;
  117.                 default:
  118.                     cout<<symbol[1];
  119.                 }
  120.             }
  121.             p_str+=i;
  122.         }
  123.         else
  124.         {
  125.             cout<<p_str[0];
  126.             p_str++;
  127.         }
  128.     }
  129.     va_end(print_list);//对参数清理
  130. int str_copy2(const char* sour,char* dest)//把两个字符复制到dest中
  131. {
  132.     int i=0;
  133.     assert(sour!=NULL && dest!=NULL);
  134.     
  135.     while(*sour != ' ' && *sour!='/0')
  136.     {
  137.         *dest++ = *sour++;
  138.         i++;
  139.         if(i==2)break;//只复制两个字符
  140.     }
  141.     *dest='/0';
  142.     
  143.     return i;
  144. }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值