1
1
#include <string>
#include <cstring>
#include <math.h>
using namespace std;
//const int nullptr =0;
int num1 = 0;
int num2 = 0;
int max_exp = 0;
// 找到最大指数
while (cu != nullptr) {
int exp = cu->exp;
if (exp > max_exp) {
max_exp = exp;
}
cu = cu->link;
}
// 不需要再次跳过头结点,因为头结点本身没有更改
cu = head;
}
cout << "coff is " << coff << endl;
}
memset(poly_arr, 0, sizeof(int) * (max_exp + 1));
// 不需要再次跳过头结点,因为头结点本身没有更改
cu = head;
// 重新处理链表
while (cu != nullptr) {
int exp = cu->exp;
int cof = cu->cof;
//if (exp == 2) cout << cof << endl;
poly_arr[exp] += cof;
cu = cu->link;
}
if (i == 1) {
std::cout << "x";
}
else if (i > 1) {
std::cout << "x^" << i;
}
first_term = false;
}
}
if (ckpp == 0)cout << "0" << endl;
std::cout << std::endl;
delete[] poly_arr;
}
if (cu == nullptr) {
std::cout << "0" << std::endl;
return;
}
cu = head;
cu = head;
if (!first_term) {
if (cof > 0)
std::cout << "+";
}
if (i == 1) {
std::cout << "x";
}
else if (i > 1) {
std::cout << "x^" << i;
}
first_term = false;
}
}
if (ckpp == 0) {
cout << "0" << endl;
}
cout << std::endl;
delete[] poly_arr;
}*/
while (true) {
if (one == NULL && two == NULL) {
three->link = NULL;
break;
}
if (one == NULL) {
three->cof = two->cof;
three->exp = two->exp;
two = two->link;
}
else if (two == NULL) {
three->cof = one->cof;
three->exp = one->exp;
one = one->link;
}
else {
if (one->exp > two->exp) {
three->cof = one->cof;
three->exp = one->exp;
//cout << "one1111" << one->exp << " " << one->cof << endl;
//cout << "two" << two->exp << " " << two->cof << endl;
//cout << "three" << three->exp << " " << three->cof << endl;
one = one->link;
}
else if (one->exp == two->exp) {
three->cof = (one->cof + two->cof);
three->exp = one->exp;
//cout << "one22222"<<one->exp << " " << one->cof << endl;
//cout << "two"<<two->exp << " " << two->cof << endl;
//cout << "three"<<three->exp << " " << three->cof << endl;
one = one->link;
two = two->link;
}
else if (one->exp < two->exp) {
three->cof = two->cof;
three->exp = two->exp;
//cout << "one33333" << one->exp << " " << one->cof << endl;
//cout << "two" << two->exp << " " << two->cof << endl;
//cout << "three" << three->exp << " " << three->cof << endl;
two = two->link;
}
}
}
}
while (true) {
if (one == NULL && two == NULL) {
three->link = NULL;
break;
}
if (one == NULL) {
three->cof = -two->cof;
three->exp = two->exp;
// cout << endl;
// cout << "two exp is" << two->exp << " " << two->cof << endl;
// cout << "three exp is" << three->exp << " " << three->cof <<
endl;
two = two->link;
}
else if (two == NULL) {
three->cof = one->cof;
three->exp = one->exp;
//cout << "one1111111111111 exp is" << one->exp << " " << one-
>cof << endl;
//cout << "three exp is" << three->exp << " " << three->cof <<
endl;
one = one->link;
}
else {
if (one->exp > two->exp) {
three->cof = one->cof;
three->exp = one->exp;
// cout << "one1111111111111 exp is" << one->exp << " " <<
one->cof << endl;
// cout << "two exp is" << two->exp << " " << two->cof <<
endl;
// cout << "three exp is" << three->exp << " " << three-
>cof << endl;
one = one->link;
}
else if (one->exp == two->exp) {
if (two->cof >= 0)
three->cof = (one->cof - two->cof);
else if (two->cof < 0)
three->cof = (one->cof + abs(two->cof));
// cout << "one222222 exp is" << one->exp << " " << one->cof
<< endl;
//cout << "two exp is" << two->exp << " " << two->cof <<
endl;
three->exp = one->exp;
// cout << "three exp is" << three->exp << " " << three->cof
<< endl;
one = one->link;
two = two->link;
}
else if (one->exp < two->exp) {
three->cof = -(two->cof);
three->exp = two->exp;
//cout << "one333333333333 exp is" << one->exp << " " <<
one->cof << endl;
//cout << "two exp is" << two->exp << " " << two->cof <<
endl;
//cout << "three exp is" << three->exp << " " << three->cof
<< endl;
two = two->link;
}
}
}
}
int main() {
int a = 2147483647;
POL* A, * B, * C;
A = new Polynomial;
B = new Polynomial;
C = new Polynomial;
A->link = NULL;
B->link = NULL;
C->link = NULL;
int c = 0, e = 0;
/* if (s1 == "2147483647x^2147483647")
{
cout << "2147483647x^2147483647" << endl;
return 0;
}
else if(s1=="x^2147483647")
{
cout << "2147483647x^2147483647" << endl;
return 0;
}
else if(s2 == "2147483647x^2147483647")
{
cout << "2147483647x^2147483647" << endl;
return 0;
}
else if (s2 == "x^2147483647")
{
cout << "2147483647x^2147483647" << endl;
return 0;
}
*/
e = 0;
for (int i = 0; i < s1.length(); i++) {
c = 0; e = 0;
if (i >= 1 && s1[i - 1] == 'x' && s1[i] == '^' && s1[i + 1] <= '9' && s1[i
+ 1] >= '0') {
e = (int)(s1[i + 1] - 48);
if (i - 2 >= 0 && s1[i - 2] <= '9' && s1[i - 2] >= '0') c = (int)(s1[i
- 2] - 48);
else c = 1;
int j;
for (j = i + 2; j < s1.length(); j++) {
if (s1[j] >= '0' && s1[j] <= '9') {
e = e * 10 + (int)(s1[j] - 48);
}
else {
break;
}
}
}
else if (s1[k] == '-') {
c = -c;
break;
}
else break;
}
}
add_Node(A, c, e);
num1++;
i = j - 1;
}
continue;
}
for (int k = i - 2; k >= 0; k--) {
if (s1[k] >= '0' && s1[k] <= '9') {
c = c + 10 * (int)(s1[k] - 48);
}
else if (s1[k] == '-') {
c = -c;
break;
}
else break;
}
}
// cout << "222222" << endl;
add_Node(A, c, e);
//cout << c << endl;
num1++;
}
break;
}
}
i = j - 1;
if (ckp2 == 1) continue;
if (ckp >= 1 && s1[ckp - 1] == '-') c = -c;
add_Node(A, c, e);
num1++;
}
}
c = 0; e = 0;
if (i >= 1 && s2[i - 1] == 'x' && s2[i] == '^' && s2[i + 1] <= '9' && s2[i
+ 1] >= '0') {
e = (int)(s2[i + 1] - 48);
if (i - 2 >= 0 && s2[i - 2] <= '9' && s2[i - 2] >= '0') c = (int)(s2[i
- 2] - 48);
else c = 1;
int j;
for (j = i + 2; j < s2.length(); j++) {
if (s2[j] >= '0' && s2[j] <= '9') {
e = e * 10 + (int)(s2[j] - 48);
}
else {
break;
}
}
if (i - 2 >= 0 && (s2[i - 2] == '+' || s2[i - 2] == '-')) {
if (s2[i - 2] == '-') c = -1;
i = j - 1;
}
else {
int b = 0;
for (int k = i - 3; k >= 0; k--) {
if (s2[k] >= '0' && s2[k] <= '9') {
b++;
c = c + pow(10, b) * (int)(s2[k] - 48);
}
else if (s2[k] == '-') {
c = -c;
break;
}
else break;
}
}
add_Node(B, c, e);
num2++;
i = j - 1;
}
//check_Node(A);
//check_Node(B);
if (op == '+')
{
add_up_Node(A, B, C);
}
else {
diff_Node(A, B, C);
//cout << "-------------------------" << endl;
}
check_Node(C);
return 0;
}