在 C 中标记字符串?
我正在为用 C 编写的计算器开发终端解析器。 我无法弄清楚如何连接运算符之间的所有数字以将它们放入数组中。
例如,如果输入(命令行参数)为“4+342
”, 理想的情况是 input[] = {"4", "+", "342"}
。
到目前为止,这是我的代码。我包括
、
和
。
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;
}
如果使用 ./calc 4+5-546
运行,它将输出:
4
+
5
-
546
那么将其每一行放入其自己的数组槽中的最简单方法是什么?
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.
For example, if the input (command line argument) was "4+342
",
it would ideally be input[] = {"4", "+", "342"}
.
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;
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试这个大小...
给定程序称为 tokenizer 和命令:
它给了我输出:
Try this for size...
Given the program is called
tokenizer
and the command:It gives me the output:
最简单的解决方案是使用像 flex 这样的工具来生成词法分析器并让它完成将输入分解为标记的工作(尽管 flex 期望其输入来自文件流,而不是字符数组)。
strtok() 不是一个好的解决方案,原因如下:
通常的解决方案是编写一个状态机(这基本上就是 flex 为您所做的)。这是一个非常快速肮脏(强调肮脏)的示例:
为什么使用
(*source)++
而不是*source++
或 <代码>源++?我不想更新source
,我想更新source
指向的内容,所以我必须先取消引用指针 应用了++
。表达式*(*source)++
基本上翻译为“给我表达式*source
所指向的字符的值,然后更新的值>*来源
”。The easiest solution is to use a tool like flex to generate your lexer and let it do the work of breaking the input into tokens (although flex expects its input to come from a file stream, not a character array).
strtok()
isn't a good solution for several reasons:The usual solution is to write a state machine (which is basically what flex does for you). Here's a very quick-n-dirty (emphasis on the dirty) example:
Why
(*source)++
instead of*source++
orsource++
? I don't want to updatesource
, I want to update whatsource
points to, so I have to dereference the pointer before the++
is applied. The expression*(*source)++
basically translates to "give me the value of the character that the expression*source
is pointing to, then update the value of*source
".--> MAN STRCAT
--> MAN STRCAT
strsep 在这里是一个不错的选择 - 获取令牌,然后决定你想用它做什么...
char *string = "(3+(5+6)/8)"
char token;
while ((token = strsep(&string, "(+/) "))) {
// 存储令牌...如果它不是 ( 或 ) 或空格
此处
- 令牌的处理方式与 Java/C# 中的 Split() 类似。这确实会在处理字符串时破坏它 - 但是,使用正确的分隔符 - 事情会很好:)
strsep is a good choice here - grab the token and then decide what you want to do with it...
char *string = "(3+(5+6)/8)"
char token;
while ((token = strsep(&string, "(+/) "))) {
// Store token... if it's not a ( or ) or space
}
Here - token will be processed similar to a Split() in Java/C#. This does mutilate the string while processing it - however, with the correct delimiters - things will be good :)
听起来您想查看标准
strtok
函数。Sounds like you want to look at the standard
strtok
function.这会给您一个想法:
这只是一个示例代码,用于演示如何仅使用加法情况来完成它。
了解此代码的主要思想并将其应用到您的代码中。
示例输出:
同样,我仅演示“+”号。您可能想要检查 p 直到它为 NULL,然后继续进行下一个操作,例如减法,然后乘法,然后除法。
this will give you an idea:
This is just a sample code to demonstrate how it is done using addition case only.
Get the main idea of this code and apply it in your code.
Example output for this:
Again, I am only demonstrating the "+" sign. You may want to check for p until it is NULL, then proceed with the next operation, say subtraction, then multiplication, then division.