2013-03-01 24 views
0

我試圖從多項式中提取coeffecients的值和指數。我已經成功地使用strtok來提取係數。我應用相同的概念來找到指數,但我不知道如何使用strtok來提取字符串後的分隔符或跳過第一個字符,而strtok是我知道的唯一的提取工具。使用c字符串從多項式C++中提取指數

這是主要的功能

#include <iostream> 
#include <cctype> 
#include <cstring> 
#include <cstdlib> 
#include <string> 
using namespace std; 

void extractCoeff (char *str, char *copy); 
void extractExp (char *str, char *copy); 
int main() 
{ 
    const int SIZE = 150; // size for string input 

    char *string; 
    string = new char[SIZE]; 
    cout << "Enter the polynomial\n"<<"minus sign must not have a blank with a coeff"; 
    cin.ignore(); 
    cin.getline(string, SIZE); // input string example: -4x^0 + x^1 + 4x^3 -3x^4 

    char *copy1; 
    copy1 = new char[SIZE]; 
    strcpy(copy1, string); 
    extractCoeff(string, copy1); 

    cout << endl << endl; 
    char *copy2; 
    copy2 = new char[SIZE]; 
    strcpy(copy2, string); 
    extractExp(string, copy2); 


    return 0; 
} 

這是函數提取係數_(工作)

void extractCoeff (char *str, char *copy) 
{ 
    char *p = strtok(str, " +"); // extract the first time 
    char *search; 
    int counter = 0; 
    while (p) 
    { 
     search = strstr(p, "x^"); 
     cout << "Token: " << p << endl; 
     cout << "Search " << search << endl; 
     p = strtok(NULL, " +"); 
     counter++; 
    } 

    cout << copy << endl; 

    // find coeff 
    int *coefficient; 
    coefficient = new int[counter]; 

    p = strtok(copy, " +"); // extract the second time to find coeff 
    int a = 0; 
    while (p) 
    { 
     cout << "p: " << p << endl; 
     long coeff; 
     if (*p == 'x') 
     { 
      coeff = 1; 
     } 
     else if (*p == NULL) 
     { 
      coeff = 0; 
     } 
     else 
     { 
      char *endptr; 
      coeff = strtol(p, &endptr, 10); 
     } 
     coefficient[a] = coeff; 
     p = strtok(NULL, " +"); 
     a++; 
    } 

    for (int i = 0; i < counter; i++) 
     cout << coefficient[i] << endl; 
} 

這是提取指數(不工作)

void extractCoeff (char *str, char *copy) 
{ 
    char *p = strtok(str, " +"); // extract the first time 
    char *search; 
    int counter = 0; 
    while (p) 
    { 
     search = strstr(p, "x^"); 
     cout << "Token: " << p << endl; 
     cout << "Search " << search << endl; 
     p = strtok(NULL, " +"); 
     counter++; 
    } 

    cout << copy << endl; 

    // find coeff 
    int *coefficient; 
    coefficient = new int[counter]; 

    p = strtok(copy, " +"); // extract the second time to find coeff 
    int a = 0; 
    while (p) 
    { 
     cout << "p: " << p << endl; 
     long coeff; 
     if (*p == 'x') 
     { 
      coeff = 1; 
     } 
     else if (*p == NULL) 
     { 
      coeff = 0; 
     } 
     else 
     { 
      char *endptr; 
      coeff = strtol(p, &endptr, 10); 
     } 
     coefficient[a] = coeff; 
     p = strtok(NULL, " +"); 
     a++; 
    } 

    for (int i = 0; i < counter; i++) 
     cout << coefficient[i] << endl; 
} 

void extractExp (char *str, char *copy) 
{ 
    char *p = strtok(str, " x^"); // extract the first time 
    //char *search; 
    int counter = 0; 
    while (p) 
    { 
     //search = strstr(p, "x^"); 
     //cout << "Token: " << p << endl; 
     //cout << "Search " << search << endl; 
     p = strtok(NULL, " x^"); 
     counter++; 
    } 

    cout << copy << endl; 

    // find coeff 
    int *exp; 
    exp = new int[counter]; 

    p = strtok(copy, " x^"); // extract the third time 
    int b = 0; 
    while (p) 
    { 
     cout << "p2: " << p << endl; 
     int expVal; 
     if (*p == NULL) 
     { 
      expVal = 0; 
     } 
     else 
     { 
      char *endptr; 
      expVal = strtol(p, &endptr, 10); 
     } 
     exp[b] = expVal; 
     p = strtok(NULL, " x^"); 
     b++; 
    } 

    for (int i = 0; i < counter; i++) 
     cout << exp[i] << endl; 
} 
+1

你應該告訴我們它「不起作用」。你期望看到什麼,你會得到什麼? – JoergB 2013-03-01 11:35:48

+0

也許更好的主意是使用正則表達式解析完整的輸入。您可以使用例如['boost :: regex'](http://www.boost.org/doc/libs/1_53_0/libs/regex/doc/html/index.html),或者新的C++中的'std :: regex', +11標準。 – 2013-03-01 11:46:47

回答

1
功能

您的問題是strtok是破壞性的。你部分似乎知道這一點,因爲你做了一個副本,可以在函數中使用它兩次。但extractCoeff返回到main後,由string指向的C字符串的內容已損壞,因此當您調用extractExp時,會傳遞兩個嚴重截斷字符串的副本。

在C++中,您應該使用std::string來處理字符串。使用std::string可以使用成員函數find,find_first_offind_first_not_of來查找子字符串,您正在查找並使用substr來提取它們,沒有破壞原始字符串。

你可以使用C函數在C字符串上做類似的事情,但那會是一個C問題。 (使用cout和C++頭使你的程序無效,作爲一個C程序,但一切是純C,而不是慣用的C++。)

而且,順便說一句:strtok是不是你應該學會以解析方式字符串。它具有破壞性,不能重複使用,並且在某些平臺上不是線程安全的。除非你有很好的理由需要破壞性的就地解析,否則不要使用它或者稍微好一點的親屬(在POSIX中)strtok_r

+0

和正則表達式? http://en.cppreference.com/w/cpp/regex – qPCR4vir 2013-03-01 11:50:57

+0

@Lam Nguyen決定:C或C++。 – qPCR4vir 2013-03-01 11:52:03

+0

@ qPCR4vir正則表達式可能是在生產代碼中完成工作的更好工具。但不要學習使用字符序列。 – JoergB 2013-03-01 11:56:27