如何将'='附加到字符串 [英] How to append '=' to a string
本文介绍了如何将'='附加到字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在数组末尾添加 =
,然后通过添加0结束它
I am trying to add =
at the end of my array and then end it by appending a 0
这就是我分配空间的方式
This is how I allocate my space
char* postExpr = malloc(sizeof(char)*MAX_LEN);
我尝试了很多方法,但是我仍然无法在字符串的末尾添加字符'=',其他所有字符都可以正常工作.
I have tried many methods but I am still failing to append character '=' at the end of my string, every other character works just fine.
我尝试了什么
postExpr[postLen++] = 61;
postExpr[postLen++] = '=';
postExpr[postLen++] = infExpr[i];
在infExpr [i]中
存储值'='
in infExpr[i] is stored the value '='
char* infix2postfix (const char* infExpr) {
char* postExpr = malloc (sizeof(char)*MAX_LEN);
if(postExpr == NULL)
return NULL;
tStack* s = (tStack*) malloc(sizeof(tStack));
if(s == NULL)
{
free(postExpr);
return NULL;
}
unsigned postLen = 0;
for(int i = 0; i< MAX_LEN; i++)
{
switch(infExpr[i])
{
case '*':
case '/':
case '+':
case '-': doOperation(s,infExpr[i],postExpr,&postLen); break;
case '(': stackPush(s,infExpr[i]); break;
case ')': untilLeftPar(s,postExpr,&postLen); break;
case '=':
while(!stackEmpty(s))
{
stackTop(s,&postExpr[postLen++]);
stackPop(s);
}
postExpr[postLen++] = '='; //NO APPEND HERE!!!
postExpr[postLen++] = 0;
postLen = MAX_LEN;
break;
case 0:
postExpr[postLen++] = '=';
postExpr[postLen++] = 0;
postLen = MAX_LEN;
break;
default: postExpr[postLen++] = infExpr[i]; break;
}
}
return postExpr;
}
推荐答案
问题出在我的堆栈上,我忘了初始化它,因此它将存储值0,该值附加在'='之前,从而使char不可读
The problem was with my stack, I forgot to initializate it, so it would store value 0 which was appended before '=' and thus made the char unreadable
这篇关于如何将'='附加到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文