将字符串分隔为多个整数

时间:2013-11-20 18:49:29

标签: c

拜托,我怎么能转换这个:

char infix[] = "123+354*87/156=" (can be variable)

如何将数字与此字符串分开(如整数,如123 354 87 156,no至1 2 3 3 5 4 ...)和char(字符+ * /)。

4 个答案:

答案 0 :(得分:0)

你可以这样做

#include<stdio.h>

int main()
{
    char infix[] = "123+354*87/156=";
    char *p = infix;
    while(1)
    {
        if(*p == '\0')
            break;
        if(*p == '+' || *p == '*' ||*p == '/' ||*p == '=' || *p == '-')
        {
            printf(" ");
        }
        else
            printf("%c", *p);
        p++;

    }
    printf("\n");
    return 0;
}  

输出:

123 354 87 156

答案 1 :(得分:0)

当你使用C ++并且每个运算符的长度为一个char时,字符串的格式为“number operator number operator ... number operator”(表示以number开头,以operator结尾,并且两者之间始终都是开关)然后使用istringstream:

#include <sstream>

using namespace std;

void main()
{
    char infix[] = "123+345*87/156=";
    istringstream is(infix);

    double nums[999]; // maybe you need more than 999
    char chars[999];
    int nums_pos = 0;
    int chars_pos = 0;
    bool number = true; // begin with number

    while (!is.eof())
    {
        if (number)
        {
            is >> nums[nums_pos];
            nums_pos++;
            number = false;
        }
        else
        {
            is >> chars[chars_pos];
            chars_pos++;
            number = true;
        }
    }

    // you got nums_pos-1 numbers and chars_pos chars
}

答案 2 :(得分:0)

我想你需要构建一个简单的计算器...如果你打算从头开始这样做,你需要Compiling Theory的一些背景,并使用有限状态机,解析等概念。

但是有很多工具可以让这项工作变得更容易:lex / yacc(C),flex / bison(C ++)或COCO / R(多种语言)。

这是C中的一个简单示例,它将数字(state = NUM​​)和符号(state = SYM)分割为字符串:

#include <string.h>
#include <ctype.h>

#define NONE    0
#define NUM 1
#define SYM 2

int _tmain(int argc, char* argv[])
{
    char infix[] = "123+354*87/156=";
    char buffer[10];
    int i, j;
    int state = NONE;
    char c;
    i = 0;
    j = 0;
    while(i < strlen(infix)) {
        c = infix[i];
        switch(state) {
            case NUM:
                if ( isdigit(c) ) {
                    buffer[j++] = c;
                    buffer[j] = 0;
                    i++;
                }
                else {
                    printf("%s\n", buffer);
                    j = 0;
                    state = NONE;
                }
                break;
            case SYM:
                i++;
                printf("%c\n", c);
                state = NONE;
                break;
            case NONE:
                if ( isdigit(c) ) state = NUM;
                else state = SYM;
                break;
        }
    }
    getchar();
    return 0;
}

答案 3 :(得分:0)

这是另一种可能的方式,但在C中,你问...

#include <stdio.h>

main()
{
    char infix[] = "123+354*87/156=";
    int curVal = 0;

    // For each character in infix
    for(char *p = infix; *p != '\0'; ++p)
    {
        // If it is not a ascii numeral
        if(*p > '9' || *p < '0')
        {
            // Output value
            printf("%d\n", curVal/10);

            // Output char
            printf("%c\n", *p);

            curVal = 0;
        }
        else
        {
            // Accumulate the individual digits 
            curVal += (*p) - '0';
            curVal *= 10;
        }
    }
}

将输出:

123
+
354
*
87
/
156
=
相关问题