没见过的用法。记录一下
void append(char*& RPN, float opnd)
{
int n = strlen(RPN);
char buf[64];
if (opnd != (float)(int)(opnd))
{
sprintf(buf, "%.2f \0",opnd ); //不要忘了结尾的\0
}
else
{
sprintf(buf, "%d", opnd);
}
RPN = (char *)realloc(RPN, sizeof(char) * (n + 3));
strcat(RPN, buf);
}
//这里主要是三个函数。
//1.int sprintf( char *buffer, const char *format, [ argument] … );
//返回值是字符串的长度。
// 用法就是将一个字符按照相应的格式转换成字符串。
//2.
//realloc(void *mem_address, unsigned int newsize);
//重新分配内存,主要是怕字符串不够长考虑,可以考虑用之前的加倍处理法。
//3.
strcat(char , char)用于连接字符串。
void append(char*& RPN, char optr)
{
int n = strlen(RPN);
sprintf(RPN + n , "%c", optr); \\注意添加的位置
RPN[n + 2] = '\0';
}
//这里的RPN+n是第n+1个字符,sprintf用来将n+1的位置设置成给定的char,同时将n+2的字符赋值为'/0'