gpt4 book ai didi

c++ - 使用c字符串从多项式c++中提取指数

转载 作者:行者123 更新时间:2023-11-28 07:44:31 24 4
gpt4 key购买 nike

我正在尝试从多项式中提取系数和指数的值。我已经使用 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返回main之后,string指向的C字符串的内容已经损坏,所以调用extractExp时传递了两个严重截断的字符串的拷贝。

在 C++ 中,您应该使用 std::string 来处理字符串。使用 std::string,您可以使用成员函数 findfind_first_offind_first_not_of 来定位子字符串,您正在寻找并使用 substr 提取它们而不破坏原始字符串。

您可以使用 C 函数在 C 字符串上做类似的事情,但那将是一个 C 问题。 (使用 cout 和 C++ header 会使您的程序作为 C 程序无效,但其他一切都是纯 C 而不是惯用的 C++。)

顺便说一句:strtok 不是您应该学习的解析字符串的方法。它具有破坏性,不能以可重入方式使用,并且在某些平台上不是线程安全的。除非您有充分的理由需要对替代方案进行破坏性就地解析,否则不要使用它或它稍微好一点的同类(在 POSIX 中)strtok_r

关于c++ - 使用c字符串从多项式c++中提取指数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15156565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com