0% found this document useful (0 votes)
17 views11 pages

1

This document defines functions for adding, subtracting, and printing polynomial expressions represented as linked lists. It defines a Polynomial struct with coefficient and exponent fields, and functions to add or subtract two polynomials by traversing the linked lists and creating a new list for the result. It also includes a function to find the maximum exponent and convert the lists to arrays for printing the polynomials.

Uploaded by

213 37顏妤珊
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views11 pages

1

This document defines functions for adding, subtracting, and printing polynomial expressions represented as linked lists. It defines a Polynomial struct with coefficient and exponent fields, and functions to add or subtract two polynomials by traversing the linked lists and creating a new list for the result. It also includes a function to find the maximum exponent and convert the lists to arrays for printing the polynomials.

Uploaded by

213 37顏妤珊
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

#include <iostream>

#include <string>
#include <cstring>
#include <math.h>
using namespace std;
//const int nullptr =0;

int num1 = 0;
int num2 = 0;

typedef struct Polynomial {


int cof;
int exp;
Polynomial* link;
} POL;

void add_Node(Polynomial* cu, int con, int pow) {


while (true) {
if (cu->link == NULL) {

cu->link = new Polynomial;


cu = cu->link;
cu->cof = con;
cu->exp = pow;
cu->link = NULL;
break;
}
else {
cu = cu->link;
}
}
}

void check_Node(Polynomial* cu) {

Polynomial* head = cu->link; // 保留链表头部的引用


cu = cu->link; // 跳过头结点
if (cu == nullptr) {
std::cout << "0" << std::endl;
return;
}

int max_exp = 0;

// 找到最大指数
while (cu != nullptr) {
int exp = cu->exp;
if (exp > max_exp) {
max_exp = exp;
}
cu = cu->link;
}

// 不需要再次跳过头结点,因为头结点本身没有更改
cu = head;

int* poly_arr = nullptr;


if (max_exp != 2147483647) poly_arr = new int[max_exp + 1] {};
else if (max_exp == 2147483647) {
cout << "here" << endl;
poly_arr = new int[max_exp];
cout << "here again" << endl;
cu = head;
int coff = 0;
// 重新处理链表
while (cu != nullptr) {
int exp = cu->exp;
int cof = cu->cof;
if (exp == 2147483647)
{
coff += cof;
}

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

bool first_term = true;


int ckpp = 0;
for (int i = max_exp; i >= 0; i--) {
int cof = poly_arr[i];
//if (i == 2) cout << "here"<<cof << endl;
// cout << "222222222222222222222222222" << endl;
if (cof != 0) {
ckpp = 1;
if (!first_term) {
if (cof > 0)
std::cout << "+";
}
if (i == 0 && (cof == 1 || cof == -1)) {
cout << cof;
//
// cout << "hahaha" << endl;
// 如果指数为 0,直接输出数字
continue;
}
if (cof != 1 && cof != -1) {
std::cout << cof;
}
else if (cof == -1) cout << "-";

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

/*void check_Node(Polynomial* cu) {


Polynomial* head = cu->link;
cu = cu->link;

if (cu == nullptr) {
std::cout << "0" << std::endl;
return;
}

long long max_exp = 0;

while (cu != nullptr) {


long long exp = cu->exp;
if (exp > max_exp) {
max_exp = exp;
}
cu = cu->link;
}

cu = head;

long long* poly_arr = new long long[max_exp + 1] {};


memset(poly_arr, 0, sizeof(long long) * (max_exp + 1));

cu = head;

while (cu != nullptr) {


long long exp = cu->exp;
long long cof = cu->cof;
poly_arr[exp] += cof;
cu = cu->link;
}

bool first_term = true;


int ckpp = 0;

for (long long i = max_exp; i >= 0; i--) {


long long cof = poly_arr[i];
if (cof != 0) {
ckpp = 1;

if (!first_term) {
if (cof > 0)
std::cout << "+";
}

if (i == 0 && (cof == 1 || cof == -1)) {


cout << cof;
continue;
}

if (cof != 1 && cof != -1) {


std::cout << cof;
}
else if (cof == -1) {
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;
}*/

void add_up_Node(Polynomial* one, Polynomial* two, Polynomial* three) {


one = one->link;
two = two->link;

while (true) {
if (one == NULL && two == NULL) {
three->link = NULL;
break;
}

three->link = new Polynomial;


three = three->link;

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

void diff_Node(Polynomial* one, Polynomial* two, Polynomial* three) {


one = one->link;
two = two->link;

while (true) {
if (one == NULL && two == NULL) {
three->link = NULL;
break;
}

three->link = new Polynomial;


three = three->link;

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;

string s1, s2;


char op;
cin >> s1;
cin >> s2;
cin >> op;

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

if (i - 2 >= 0 && (s1[i - 2] == '+' || s1[i - 2] == '-')) {


if (s1[i - 2] == '-')
c = -1;
i = j - 1;
}
else {
int b = 0;
for (int k = i - 3; k >= 0; k--) {
if (s1[k] >= '0' && s1[k] <= '9') {
b++;
c = c + pow(10, b) * (int)(s1[k] - 48);

}
else if (s1[k] == '-') {
c = -c;
break;
}
else break;
}
}
add_Node(A, c, e);
num1++;
i = j - 1;
}

else if (s1[i] == 'x') {


//cout << "hahahahh " << i << endl;
if (i + 1 < s1.length() && s1[i + 1] == '^') continue;
e = 1;
if (i - 1 >= 0 && s1[i - 1] <= '9' && s1[i - 1] >= '0') c = (int)(s1[i
- 1] - 48);
else {
c = 1;
}
if (i - 1 >= 0 && (s1[i - 1] == '+' || s1[i - 1] == '-')) {
if (s1[i - 1] == '-') c = -1;
//cout << "here" << endl;
}
else {
if (i + 1 < s1.length() && s1[i + 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++;
}

else if (s1[i] <= '9' && s1[i] >= '0') {


//cout << "1111111111111 " << i << endl;
e = 0;
c = (int)(s1[i] - 48);
int ckp = i;
int ckp2 = 0;
int j = i + 1;
for (j; j < s1.length(); j++) {
if (s1[j] == 'x') {
i = ckp;
ckp2 = 1;
break;
}
else if (s1[j] >= '0' && s1[j] <= '9') {
c = c * 10 + (int)(s1[j] - 48);
}
else {
i = j - 1;

break;
}
}
i = j - 1;
if (ckp2 == 1) continue;
if (ckp >= 1 && s1[ckp - 1] == '-') c = -c;
add_Node(A, c, e);
num1++;
}
}

for (int i = 0; i < s2.length(); i++) {

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

else if (s2[i] == 'x') {


if (i + 1 < s2.length() && s2[i + 1] == '^') continue;
e = 1;
if (i - 1 >= 0 && s2[i - 1] <= '9' && s2[i - 1] >= '0') c = (int)(s2[i
- 1] - 48);
else {
c = 1;
}
if (i - 1 >= 0 && (s2[i - 1] == '+' || s2[i - 1] == '-')) {
if (s2[i - 1] == '-') c = -1;
}
else {
if (i + 1 < s2.length() && s2[i + 1] == '^') {
continue;
}
for (int k = i - 2; k >= 0; k--) {
if (s2[k] >= '0' && s2[k] <= '9') {
c = c + 10 * (int)(s2[k] - 48);
}
else if (s2[k] == '-') {
c = -c;
break;
}
else break;
}
}
add_Node(B, c, e);
num2++;
}

else if (s2[i] <= '9' && s2[i] >= '0') {


//cout << "hahaha" << i << endl;
e = 0;
c = (int)(s2[i] - 48);
int ckp = i;
int ckp2 = 0;
int j = i + 1;
for (j; j < s2.length(); j++) {
if (s2[j] == 'x') {
i = ckp;
ckp2 = 1;
break;
}
else if (s2[j] >= '0' && s2[j] <= '9') {
c = c * 10 + (int)(s2[j] - 48);
// cout << c << endl;
}
else {
i = j - 1;
break;
}
}
i = j - 1;
if (ckp2 == 1) continue;
if (ckp >= 1 && s2[ckp - 1] == '-') c = -c;
add_Node(B, c, e);
// cout << "here"<<c << endl;
num2++;
}
}

//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;
}

You might also like