2013-11-20 22 views
-1

請,我怎樣才能轉換此:分離串到多個整數

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

如何數的從該字符串中分離(爲整數,如123 354 87 156,沒有爲1 2 3 3 5 4 .. 。)和char(字符+ * /)。

+2

這就是所謂的「詞法」或「符號化」,是計算機科學的充分研究的問題。你想如何格式化數據?你有什麼嘗試? –

+0

看看strtok:http://man7.org/linux/man-pages/man3/strtok.3.html – Paul92

+0

我把它保存爲令牌(結構 - 整型和字符),但仍然有問題轉換int( strtol),然後越來越char – user3014511

回答

0

我想你需要建立一個簡單的計算器...如果你打算從頭做到這一點,你會需要從Compiling Theory一些背景和使用的概念,如有限狀態機,解析等

但有很多工具可以使這項任務變得更加簡單:lex/yacc(C),flex/bison(C++)或COCO/R(許多語言)。

這是在C簡單的例子,分割在數字串(狀態= NUM​​)和符號(狀態= 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; 
} 
+0

謝謝你,是的,我正在尋找這個......我終於用我的代碼類似這個 – user3014511

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 
0

當您使用C++和每個運營商有一個字符的長度和字符串的形式爲「數字操作員編號操作。 ...號碼操作員「(表示以數字開頭,以操作員結束並在兩者之間始終切換),則使用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 
} 
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 
=