使用scanf函数解析式的字符串在C [英] Parsing strings of equations in c using scanf

查看:136
本文介绍了使用scanf函数解析式的字符串在C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从可以是任何数量的变型的用户解析输入:
1 + 1 4(3-0)=×1 *(3)-8

I need to parse input from a user that could be any number of variations: 1+1 4( 3-0 ) =x 1*(3)-8

我如何做到这一点使用scanf函数来获取的raw_input,然后分割出各种不同的价值观,并告诉要么如果它是一个字符串,即X = - ()或INT?

How do I do this using scanf to get the raw_input, then split out all of the different values and tell either if it is a string ie x = - () or a int?

这是我在想什么。

    char * raw_input;
    scanf("%s",raw_input);

这需要字符的数组,然后我只需要分割和转换成一个单一的元素。什么是做输入(分割和转换)的最佳方式。

It takes a array of char and then I just need to split and convert into a single elements. What is the best way of doing the input and (splitting and converting)

感谢

推荐答案

如果你想编写自己的code,最好的办法就是在语法形式来定义你的前pression。为了便于分析语法,最好让它简单。

If you want to write your own code, the best way is to define your expression in form of a grammar. To easily parse the grammar, it's best to make it simple.

例如,要分析前pressions在这样的形式(1+(3 * 4 + X)* Y)+1,你可以写这样的语法:

For example, to parse expressions in the form like this (1+(3*4+x)*y)+1, you could write such a grammar:

Expression -> Addition | null
Addition -> Multiplication RestOfAddition
RestOfAddition -> null | + Addition
Multiplication -> Element RestOfMultiplication
RestOfMultiplication -> null | * Element
Element -> number | variable | ( Expression )

然后在你的程序中,在这个语法每个非终端(在左边的那些 - >),你写一个函数,就像这样:

Then in your program, for every non-terminal in this grammar (the ones on the left of ->), you write one function, like this:

ExpTree *Expression(char *exp, int *position)
{
    if (exp[*position])
    {
        ExpTree *node = malloc(sizeof(*node));
        node->type = LAMBDA;
        node->value = 0;
        return node;
    }
    else
        return Addition(exp, position);
}

ExpTree *Addition(char *exp, int *position)
{
    ExpTree *node = malloc(sizeof(*node));
    node->type = ADDITION;
    node->left = Multiplication(exp, position);
    node->right = RestOfAddition(exp, position);
    return node;
}

ExpTree *RestOfAddition(char *exp, int *position)
{
    ExpTree *node;
    if (exp[*position] == '+')
    {
        ++*position;
        return Addition(exp, position);
    }
    else
    {
        ExpTree *node = malloc(sizeof(*node));
        node->type = LAMBDA;
        node->value = 0;
        return node;
    }
}

同样, RestOfMultiplication 将写作功能。

ExpTree *Element(char *exp, int *position)
{
    if (exp[*position] == '(')
    {
        ExpTree *node;
        ++*position;
        node = Expression(exp, position);
        if (!exp[*position] != ')')
             printf("Expected ) at position %d\n", *position);
        else
             ++*position;
        return node;
    }
    else if (exp[*position] == ')')
    {
        printf("Unexpected ) at position %d\n", *position);
        return NULL;
    }
    else if (exp[*position] >= '0' && exp[*position] <= '9')
    {
        ExpTree *node = malloc(sizeof(*node));
        node->type = INTEGER;
        node->value = extract_int(exp, position);
        return node;
    }
    else if ((exp[*position] >= 'a' && exp[*position] <= 'z') ||
             (exp[*position] >= 'A' && exp[*position] <= 'Z') ||
             exp[*position] == '_')
    {
        ExpTree *node = malloc(sizeof(*node));
        node->type = VARIABLE;
        node->value = extract_variable(exp, position);
        return node;
    }
    else
    {
        printf("Warning: unexpected character %c in location %d\n", exp[*position], *position);
        return NULL;
    }
}

其中, extract_int extract_variable 是采取前pression和位置上有两个功能,走在前面,他们看到来自前pression串号(或在 extract_variable 功能的盘符),他们建号(变量),并将其返回,设置位置在结束的地方。

Where extract_int and extract_variable are two functions that take the expression and the position on it, go ahead while they are seeing a number (or a letter in the extract_variable function) they build the number (variable) from the expression string and return it, setting position to after where they finished.

请注意:这不是code的复制粘贴。它是不完整的,缺乏足够的错误检查。一些细节已被省略,并提出作为一个解决方案来教解析如何简单的是做,而不是简单的解决方案。

Note: This is not code for copy paste. It is not complete and lacks sufficient error checking. Some details have been omitted and is offered as a solution to teach how simple parsing is done rather than easiest solution.

这篇关于使用scanf函数解析式的字符串在C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆