标记化在C字符串? [英] Tokenizing a string in C?
问题描述
我正在为C语言编写的一个计算器终端解析器
我无法弄清楚如何连接一切都在运营商之间的数字来把它们放进一个数组。
I'm working on a terminal parser for a calculator written in C. I cannot figure out how to concatenate all of the numbers that are in between operators to put them into an array.
例如,如果输入(命令行参数) 4 + 342
,
这将理想的输入[] = {4,+,342}
。
For example, if the input (command line argument) was "4+342
",
it would ideally be input[] = {"4", "+", "342"}
.
下面是我的code为止。我包括< stdio.h中>
,<文件stdlib.h>
和<文件ctype.h方式>
Here's my code so far. I'm including <stdio.h>
, <stdlib.h>
, and <ctype.h>
.
typedef char * string;
int main(int argc, char *argv[])
{
string inputS = argv[1];
string input[10];
string temp;
printf("%s\n", inputS);
int i;
int len = strlen(inputS);
printf("parsed:\n");
for(i = 0; i < len; inputS++, i++)
{
if(isdigit(*inputS))
{
printf("%c",*inputS);
}
else
{
printf("\n%c\n",*inputS);
}
}
printf("\n");
return 0;
}
如果它运行与 ./钙4 + 5-546
,它将输出:
If it is run with ./calc 4+5-546
, it will output:
4
+
5
-
546
那么,让这每一行成自己的数组插槽最简单的方法?
So what's the easiest way to get each line of this into its own array slot?
推荐答案
看看这个大小...
#include <stdio.h>
#include <ctype.h>
typedef char * string;
int main(int argc, char *argv[])
{
string inputS = argv[1];
string input[50]; /* Up to 50 tokens */
char buffer[200];
int i;
int strnum = 0;
char *next = buffer;
char c;
if (argc != 2)
{
fprintf(stderr, "Usage: %s expression\n", argv[0]);
return 1;
}
printf("input: <<%s>>\n", inputS);
printf("parsing:\n");
while ((c = *inputS++) != '\0')
{
input[strnum++] = next;
if (isdigit(c))
{
printf("Digit: %c\n", c);
*next++ = c;
while (isdigit(*inputS))
{
c = *inputS++;
printf("Digit: %c\n", c);
*next++ = c;
}
*next++ = '\0';
}
else
{
printf("Non-digit: %c\n", c);
*next++ = c;
*next++ = '\0';
}
}
printf("parsed:\n");
for (i = 0; i < strnum; i++)
{
printf("%d: <<%s>>\n", i, input[i]);
}
return 0;
}
由于计划被称为标记生成器
和命令:
tokenizer '(3+2)*564/((3+4)*2)'
它给我的输出:
input: <<(3+2)*564/((3+4)*2)>>
parsing:
Non-digit: (
Digit: 3
Non-digit: +
Digit: 2
Non-digit: )
Non-digit: *
Digit: 5
Digit: 6
Digit: 4
Non-digit: /
Non-digit: (
Non-digit: (
Digit: 3
Non-digit: +
Digit: 4
Non-digit: )
Non-digit: *
Digit: 2
Non-digit: )
parsed:
0: <<(>>
1: <<3>>
2: <<+>>
3: <<2>>
4: <<)>>
5: <<*>>
6: <<564>>
7: <</>>
8: <<(>>
9: <<(>>
10: <<3>>
11: <<+>>
12: <<4>>
13: <<)>>
14: <<*>>
15: <<2>>
16: <<)>>
这篇关于标记化在C字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!