//简单实现C语言中printf函数
#include<iostream>
#include<typeinfo>
#include<string.h>
using namespace std;
void print(const char* str)//处理只有字符串的时候
{
cout << str << endl;
}
template <class T, class... Args>
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;
}
#include<iostream>
#include<typeinfo>
#include<string.h>
using namespace std;
void print(const char* str)//处理只有字符串的时候
{
cout << str << endl;
}
template <class T, class... Args>
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;
}