请问C已将做字符串添加任何工具? [英] Does C have any tools for doing string addition?
问题描述
我在做一个函数,返回重新psented为一棵树$ P $像
I'm making a function that returns the derivative of a function that is represented as a tree like
/ + \
* ^
/ \ / \
x 5 3.14 x
与形式的节点
with nodes of the form
typedef struct node
{
char * fx; // function
struct node * gx; // left-hand side
char * op; // operator
struct node * hx; // right-hand side
} node;
如果一个节点没有孩子,如 X
, 5
, 3.14
在上面的例子中,然后其运
, GX
和 HX
是 NULL
,否则它的 FX
是 NULL
。
If a node has no children, e.g. x
, 5
, 3.14
in the above example, then its op
, gx
and hx
are NULL
, and otherwise its fx
is NULL
.
我的函数计算衍生的样子
My function for calculating the derivative looks like
char * deriveFromTree ( node * rt )
{
char * buff = malloc(100*sizeof(char));
int curBuffIdx = 0;
if (rt->op) // if rt is of the form rt = gx op hx
{
char * dgdx = deriveFromTree(rt->gx); // g'(x)
char * dhdx = deriveFromTree(rt->hx); // h'(x)
char thisop = *rt->op;
if (thisop == '+' || thisop == '-')
{
// ... want to do equivalent of
// buff = dgdx + thisop + dhdx
}
else if (thisop == '*')
{
// ...
}
else if (thisop == '/')
{
// ...
}
else if (thisop == '^')
{
// ...
}
}
else // rt is a base expression -- x or a constant
{
buff[curBuffIdx] = strcmp(rt->fx, 'x') ? '1': '0';
}
buff[++curBuffIdx] = '\0';
return buff;
}
但我越来越绊倒上的所有字符串添加。我可以从头开始创建一个字符串加法器,如果有已经在做的一种简洁的方式
but I'm getting tripped up on all the string addition. I could create from scratch a string adder, if there's already a compact way of doing
// ... want to do equivalent of
// buff = dgdx + thisop + dhdx
那么我想使用该工具。
then I'd like to use that tool.
推荐答案
如果您的C标准库是GNU或者* BSD,那么你可能有 asprintf
可用。您可能需要启用一个功能测试宏使用它,但。如果你没有 asprintf
可用,它可以很容易地在C标准定义的 vsnprintf
功能。
If your C standard library is GNU or *BSD, then you probably have asprintf
available. You may need to enable a feature test macro to use it, though. If you don't have asprintf
available, it can easily be defined in terms of the C standard vsnprintf
function.
asprintf
返回格式作为一个新分配的字符串(这是你的责任,免费
的结果所以你可以写,例如:
asprintf
returns the result of the format as a newly-allocated string (which it is your responsibility to free
. So you could write, for example:
char* buff;
int n = asprintf(&buff, "%s%c%s", dgdx, thisop, dhdx);
我通常使用一个包装函数,返回的字符串,而不是长度,所以你可以写:
I usually use a wrapper function, which returns the string rather than the length, so you can write:
char* buff = concatf("%s%c%s", dgdx, thisop, dhdx);
下面是三个简单的实现;第一个将工作系统 vasprintf
;在系统的第二个符合POSIX vsnprintf
;第三个针对Windows,这显然实现了一个不同的的snprintf
接口。
Here are three simple implementations; the first will work on systems with vasprintf
; the second on systems with Posix vsnprintf
; and the third for Windows, which apparently implements a different snprintf
interface.
// Version 1, systems which have vasprintf:
char* concatf(const char* fmt, ...) {
va_list args;
char* buf = NULL;
va_start(args, fmt);
int n = vasprintf(&buf, fmt, args);
va_end(args);
if (n < 0) { free(buf); buf = NULL; }
return buf;
}
// Version 2: Systems without vasprintf but with vsnprintf
char* concatf(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
char* buf = NULL;
int n = vsnprintf(NULL, 0, fmt, args);
va_end(args);
if (n >= 0) {
va_start(args, fmt);
buf = malloc(n+1);
if (buf) vsnprintf(buf, n+1, fmt, args);
va_end(args);
}
return buf;
}
// Version 3: Windows
// Apparently, the implementation of vsnprintf on Windows returns -1
// if not enough space has been provided. So here is the above code
// rewritten according to the documentation I found in
// https://msdn.microsoft.com/en-us/library/w05tbk72%28VS.71%29.aspx
// and
// https://msdn.microsoft.com/en-us/library/1kt27hek%28v=vs.71%29.aspx
// but totally untested. (If you try it, let me know)
char* concatf(const char* fmt, ...) {
char* buf = NULL;
va_list args;
va_start(args, fmt);
int n = _vscprintf(fmt, args);
va_end(args);
if (n >= 0) {
va_start(args, fmt);
buf = malloc(n+1);
if (buf) _vsnprintf(buf, n+1, fmt, args);
va_end(args);
}
return buf;
}
这是最简洁的等效我所知道的字符串连接运营商在其他语言。 (这并不一定是最高效的执行时间,但它可能是程序员的时间。)
That's the most concise equivalent I know of to string concatenation operators in other languages. (It's not necessarily the most efficient in execution time, but it probably is in programmer time.)
这篇关于请问C已将做字符串添加任何工具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!