69,382
社区成员
发帖
与我相关
我的任务
分享
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int transfer(char const* _ori,char* _dst)
{
char last_opt = ' ';
char last_last_opt = ' ';
char const* ori = _ori;
char * dst = _dst;
char buf[1024] = {0,};
int len = 0;
while (0 != *ori) {
if (isdigit(*ori)) {
*dst++ = *ori++;
if ('*' == last_opt || '/' == last_opt) {
*dst++ = last_opt;
last_opt = last_last_opt;
last_last_opt = ' ';
}
}
else if ('+' == *ori || '-' == *ori) {
if ('+' == last_opt || '-' == last_opt) {
*dst++ = last_opt;
}
last_opt = *ori++;
}
else if ('*' == *ori || '/' == *ori) {
last_last_opt = last_opt;
last_opt = *ori++;
}
else if ('(' == *ori) {
ori++;
len = transfer(ori,buf);
ori += len;
strcpy(dst,buf);
dst += strlen(buf);
}
else if (')' == *ori || 0 == *ori) {
if (' ' != last_opt)
*dst++ = last_opt;
ori++;
break;
}
}
*dst = 0;
return ori - _ori;
}
int main(int argc, char *argv[])
{
char dst[1024] = {0,};
char const* ori = "(5*(((9+8)*(4*6))+7))";
int len = 0;
len = transfer(ori,dst);
printf("%s\n",dst);
// printf("len: %d strlen: %d\n",len,strlen(ori));
return 0;
}