簡體   English   中英

使用C字符串從多項式C ++提取指數

[英]extracting exponent from polynomial c++ using c string

我正在嘗試從多項式中提取系數和指數的值。 我已經成功使用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;
}

您的問題是strtok具有破壞性。 您似乎部分知道這一點,因為您制作一個副本可以在功能中使用兩次。 但經過extractCoeff返回到主力,C-字符串的內容指向string被損壞,所以當你打電話extractExp你傳遞一個嚴重截斷串的兩個副本。

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

您可以使用C函數在C字符串上執行類似的操作,但這將是一個C問題。 (使用cout和C ++標頭會使您的程序作為C程序無效,但其他所有內容都是純C而不是慣用的C ++。)

而且,順便說一句: strtok不是解析字符串的方法,您不應學習。 它具有破壞性,不能重新使用,並且在某些平台上也不是線程安全的。 除非您有充分的理由需要對替代方案進行破壞性的就地解析,否則請不要使用它或它的更好的strtok_r (在POSIX中) strtok_r

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM