设为首页 加入收藏

TOP

C++实现C语言printf函数
2015-11-21 01:03:56 来源: 作者: 【 】 浏览:1
Tags:实现 语言 printf 函数
//简单实现C语言中printf函数
#include
#include
#include
using namespace std;




void print(const char* str)//处理只有字符串的时候
{
cout << str << endl;
}


template
void print(const char* str, T t, Args... args)
{
if((*str) == '\0' || str == NULL)//退出递归条件
{
return;
}


if(*str == '%')
{
switch(*(++str))//这里只实现几个代表一下
{
case 'd':
if(strcmp(typeid(t).name(), "i"))
{
cout << "参数类型不匹配!" << endl;
return;
};
break;
case 'c':
if(strcmp(typeid(t).name(), "c"))
{
cout << "参数类型不匹配!" << endl;
return;
};
break;
default:;break;
}
cout << t;
print(++str, args...);
}
else if(*str == ' ')
{
cout << ' ';
print(++str, t, args...);
}
else
{
cout << *str;
print(++str, t, args...);
}


}




int main()
{





return 0;
}
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇并查集 poj1308 hd1272 下一篇LeetCode (23) Jump Game (动态..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: