使用c字符串从多项式c ++提取指数 [英] extracting exponent from polynomial c++ using c string
问题描述
我试图从多项式中提取系数和指数的值。我已经成功地使用 strtok
提取系数。我应用相同的概念来找到指数,但我不知道如何使用 strtok
在分隔符之后提取字符串或跳过第一个字符, strtok
是我知道的唯一提取工具。
I'm trying to extract the value of the coeffecients and the exponent from a polynomial. I have already succeeded in extracting the coeffients using strtok
. I applied the same concept to find the exponent, but I don't know how to use strtok
to extract the string AFTER the delimiters or skip the first character, and strtok
is the only extracting tool I know.
这是主要函数
#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;
}
这是提取coeff(工作)的函数
This is the function to extract coeff (worked)
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;
}
这是提取指数(不工作)的函数
This is the function to extract exponents (not working)
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;
}
推荐答案
c $ c> strtok 是破坏性的。你部分似乎知道,当你做一个副本,以便能够在功能中使用它两次。但是 extractCoeff
返回到main后, string
指向的C字符串的内容已损坏,因此当您 $ extractExp
您传递一个严重截断的字符串的两个副本。
Your problem is that strtok
is destructive. You partly seem to know that, as you make a copy to be able to use it twice in the functions. But after extractCoeff
returns to main, the content of the C-string pointed to by string
is damaged, so when you call extractExp
you pass two copies of a badly truncated string.
> std :: string 用于处理字符串。使用 std :: string
,您可以使用成员函数 find
, find_first_of
和 find_first_not_of
找到子字符串,您正在寻找并使用 substr
In C++ you should use std::string
for handling strings. With std::string
you can use the member functions find
, find_first_of
and find_first_not_of
to locate the substrings, you are looking for and use substr
to extract them without destroying the original string.
您可以使用C函数在C字符串上做类似的事情,但这将是一个C问题。 (使用 cout
和C ++标题使得你的程序不能作为C程序有效,但其他一切都是纯C而不是惯用的C ++。)
You can do similar things on C strings, using C functions, but that would be a C question. (Using cout
and C++ headers makes your program not valid as a C program, but everything else is pure C rather than idiomatic C++.)
顺便说一句, strtok
不是你应该学习的方式来解析字符串。它是破坏性的,它不能可重用,在某些平台上不是线程安全的。除非你有非常好的理由需要破坏性的就地解析替代品,不要使用它或其稍微更好的亲(在POSIX) strtok_r
。
And, by the way: strtok
is not something you should learn as the way to parse strings. It is destructive, it can't be used reentrantly and on some platform is not thread-safe. Unless you have very good reasons to need destructive in-place parsing over alternatives, don't use it or its slightly better kin (in POSIX) strtok_r
.
这篇关于使用c字符串从多项式c ++提取指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!