最近在数据结构学习中,学到了多项式方面的应用,因此,特意记录下,以后可以用
#include<iostream> #include<cstdlib> #include<cmath> #include<stdio.h> #include<algorithm> #include<fstream> using namespace std; #define P 0.0000001 typedef struct { double coef; int exp; }polynomial; bool comp(polynomial p1, polynomial p2) { return p1.exp > p2.exp; } void Zero(int n, polynomial poly[]) { int i; for (i = 0; i < n; i++) { poly[i].coef = 0; poly[i].exp = 0; } } bool IsZero(polynomial poly[], int n) { int i; for (i = 0; i < n; i++) if (fabs(poly[i].coef - 0)>P) return false; return true; } double Coef(polynomial poly[], int exp, int n) { int i; for (i = 0; i < n; i++) { if (poly[i].exp == exp &&fabs(poly[i].coef - 0)>P) return poly[i].coef; } return 0; } int Lead_Exp(polynomial poly[], int n) { int max = 0; for (int i = 0; i < n; i++) if (poly[i].exp >= max) max = poly[i].exp; return max; } void Attach(polynomial poly[], double coef, int n, int exp) { for (int i = 0; i < n; i++) { if (poly[i].exp == exp) return; } poly[n].coef = coef; poly[n].exp = exp; } void Remove(polynomial poly[], int exp, int n) { for (int i = 0; i<n; i++) { if (poly[i].exp == exp) { poly[i].exp = 0; poly[i].coef = 0; } } } void SingleMult(polynomial poly[], double coef, int exp, int n) { for (int i = 0; i<n; i++) { if (poly[i].coef != 0) { poly[i].coef *= coef; poly[i].exp += exp; } } } int Add(polynomial poly1[], int n1, polynomial poly2[], int n2) { int i, k = 0, j; for (i = 0; i<n2; i++) { bool flag = false; for (j = 0; j<n1; j++) { if (poly1[j].exp == poly2[i].exp) { flag = true; poly1[j].coef += poly2[i].coef; break; } } if (flag == false) { poly1[n1 + k] = poly2[i]; k++; } } n1 += k; return n1; } int Mult(polynomial poly1[], int n1, polynomial poly2[], int n2, polynomial poly3[]) { int k = 0; for (int i = 0; i<n1; i++) { for (int j = 0; j<n2; j++) { poly3[k].exp = poly1[i].exp + poly2[j].exp; poly3[k].coef = poly1[i].coef*poly2[j].coef; k++; } } for (int i = 0; i < k; i++) { for (int j = i + 1; j < k; j++) { if (poly3[i].exp == poly3[j].exp) { poly3[i].coef += poly3[j].coef; poly3[j].exp = poly3[j].coef = 0; } } } return k; } void Print(polynomial poly[], int n) { if (n == 1) { if (fabs(poly[0].coef - 1) < P) { if (poly[0].coef < 0) cout << "-"; else cout << " "; } else cout << poly[0].coef; if (poly[0].exp == 0) cout << " "; else if (poly[0].exp == 1) cout << "x"; else cout << "x^" << poly[0].exp; } else for (int i = 0; i<n; i++) { if (i == 0) { if (fabs(poly[i].coef - 1) < P) { if (poly[i].coef < 0) cout << "-"; else cout << " "; } else cout << poly[i].coef; if (poly[i].exp == 1) cout << "x"; else cout << "x^" << poly[i].exp; } else if (i == n - 1){ if (poly[i].exp == 0) { if (poly[i].coef > 0) cout << "+"; cout << poly[i].coef; } else { if (fabs(poly[i].coef - 1) < P) { if (poly[i].coef < 0) cout << "-"; else cout << "+"; } else { if (poly[i].coef>0) cout << "+"; cout << poly[i].coef; } if (poly[i].exp == 1) cout << "x"; else cout << "x^" << poly[i].exp; } } else { if (fabs(poly[i].coef - 1) < P) { if (poly[i].coef < 0) cout << "-"; else cout << "+"; } else { if (poly[i].coef>0) cout << "+"; cout << poly[i].coef; } cout << "x^" << poly[i].exp; } } } int main() { ios::sync_with_stdio(false); cin.tie(false); freopen("D:/a.txt", "r",stdin); freopen("D:/b.txt", "w", stdout); polynomial poly1[100], poly2[100], poly3[100], poly4[100], poly5[100]; int n1, n2, i, j, k; cin >> n1; Zero(100, poly5); for (i = 0; i < n1; i++) { cin >> poly1[i].coef >> poly1[i].exp; poly3[i] = poly1[i]; } cin >> n2; for (i = 0; i < n2; i++) { cin >> poly2[i].coef >> poly2[i].exp; poly4[i] = poly2[i]; } int n3 = Add(poly1, n1, poly2, n2); sort(poly1, poly1 + n3, comp); Print(poly1, n3); cout << endl; int n4 = Mult(poly3, n1, poly4, n2, poly5); sort(poly5, poly5 + n4, comp); Print(poly5, n4); return 0; }具体可以看这个
http://blog.sina.com.cn/s/blog_827ed2e00100tsff.html