將字符串追加用的函數

沒見過的用法。記錄一下

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'

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章