0

多項式から係数と指数の値を抽出しようとしています。を使用して係数を抽出することにすでに成功していstrtokます。strtok同じ概念を適用して指数を見つけましたが、区切り文字の後に文字列を抽出する方法や最初の文字をスキップする方法がわかりません。これはstrtok、私が知っている唯一の抽出ツールです。

これが主な機能です

#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;
}
4

1 に答える 1

1

あなたの問題はそれstrtokが破壊的であるということです。関数で2回使用できるようにコピーを作成すると、そのことを部分的に知っているようです。ただし、extractCoeffmainに戻った後、が指すC文字列の内容stringが破損しているため、呼び出すときextractExpに、正しく切り捨てられた文字列の2つのコピーを渡します。

C ++ではstd::string、文字列の処理に使用する必要があります。を使用するとstd::string、メンバー関数を使用findでき、サブストリングを見つけるために、元のストリングを破棄せにサブストリングを探して抽出するために使用できます。find_first_offind_first_not_ofsubstr

C関数を使用して、C文字列に対して同様のことを行うことができますが、それはCの質問になります。(coutおよびC ++ヘッダーを使用すると、プログラムはCプログラムとして無効になりますが、それ以外はすべて、慣用的なC ++ではなく純粋なCです。)

そして、strtokちなみに、文字列を解析する方法として学ぶべきものではありません。これは破壊的であり、再入可能に使用することはできず、一部のプラットフォームではスレッドセーフではありません。代替案に対して破壊的なインプレース解析を必要とする非常に正当な理由がない限り、それまたはそのわずかに優れた親族(POSIX)を使用しないでくださいstrtok_r

于 2013-03-01T11:40:01.503 に答える