For the unary polynomial we are all familiar with, this is the use of data structure of the single-linked list implementation, the following paste my implementation code, welcome to criticize the guidance
Test.h#pragma once#include<iostream> #include <fstream>using namespace Std;class p_sum{public:typedef struct p_node{double Coef;int index; P_node* Next;} P_node; P_sum (): Head (Buynode ( -1,-1)) {}~p_sum () {clean (); Freenode (head);} void Clean () {p_node*p=head->next; P_node*q =null;while (P! = NULL) {q=p->next;delete p;p=q;} Head->next=null;} void Push_front (double coef=0.0, int index=0), void push_back (double coef=0.0,int inex=0), void add_poly_list (const p_sum &poly1,const p_sum&poly2); int max_index () const;void multiply_poly (const p_sum &poly1, const P_Sum& POLY2); void sub_poly_list (const p_sum &poly1,const p_sum&poly2); void sort (); void Load (); void DownLoad (); void Readin (); Friend istream& operator>> (istream& in,p_sum& Sum) {int coef=0;int index=0; P_node*p=sum.head->next;while (in>>coef>>index && Index! =-1) {sum.push_back (coef,index);} return in;} Friend ostream& operator<< (ostream& out,p_sum& Sum) { p_node* p=sum.head->next;if (p = = NULL) {out<< "not exist\n"; while (P! = NULL) {if (P->index = = 0) {Out<<p->coef;} else if (P->coef = = 1) {out<< "x^" <<P->INDEX;} else{out<<p->coef<< "x^" <<P->INDEX;} p = p->next;if (p!=null && p->coef>0) {out<< "+";}} out<< "\ n"; return out;} protected:p_node* Buynode (double coef=0.0, int index=0) {p_node* P = new P_node;p->coef = coef! = 0.0? coef:0.0;p-> ; index = Coef! = 0? Index:0;p->next =null;return p;} void Freenode (P_node *p) {Delete p;p = NULL;} void Insert (p_node*&ptr) {p_node*p = Head;while (p->next! = NULL && p->next->index > ptr-> Index) {p = P->next;} Ptr->next=p->next;p->next = Ptr;return;} Private:p_node*head;};
#include "test.h" void P_sum::p ush_front (double coef, int index) {p_node*p = Buynode (Coef, index);p->next = head-> Next;head->next = P;} void P_sum::p ush_back (double coef,int index) {p_node*s=buynode (coef,index); P_node *p = Head;while (P->next! = null) p = P->next;p->next = S;s->next = null;} int P_sum::max_index () const{p_node *p = head->next;int tmp = p->index; p_node* q = p->next;while (q! = NULL) {TMP = tmp > q->index? tmp:q->index;q = Q->next;} return tmp! = 0? TMP:-1;} void P_sum::add_poly_list (const p_sum &poly1,const p_sum&poly2) {p_node* P = poly1.head->next; p_node* q = poly2.head->next; p_node* ptmp = Null;while (P! = NULL && Q! = null) {if (P->index = = q->index) {Double tmp = P->coef + Q-&G T;coef;push_back (TMP, p->index);p = P->next;q = q->next;if (tmp <1e6) {continue;}} else if (P->index <q->index) {push_back (Q->coef, q->index); q = Q->next;} Else{push_back (P->coef, p->index);p = P->next;}} if (p = null) ptmp = Q;elseptmp = P;while (ptmp!= NULL) {push_back (Ptmp->coef, ptmp->index);p tmp = Ptmp->next;}} void P_sum::sub_poly_list (const p_sum &poly1,const p_sum&poly2) {p_node* P = poly1.head->next; p_node* q = poly2.head->next;if (q = = NULL | | p = = NULL) {cout<< "poly1 or Poly2 is empty\n"; return; p_node* ptmp = Null;while (P! = NULL && Q! = null) {if (P->index = = q->index) {Double tmp = P->COEF-Q-&G T;coef;push_back (TMP, p->index);p = P->next;q = q->next;if (tmp <1e6) {continue;}} else if (P->index >q->index) {push_back (P->coef, p->index);p = P->next;} Else{push_back (-(Q->COEF), q->index); q = Q->next;}} while (p!= NULL) {push_back (Ptmp->coef, ptmp->index);p = P->next;} while (Q!=null) {push_back (-(Q->COEF), q->index); q= Q->next;}} void P_sum::multiply_poly (const p_sum &poly1, const p_sum&poly2) {p_node*p = poly1.head->next; P_node*q = Poly2.head->next;int A = poly1. Max_index (); int b = Poly2. Max_index (); int tmp = 0;IF (A! =-1 && b! =-1) {tmp = a + B + 1;} double* DP = new Double[tmp];for (int i = 0; i< tmp; ++i) {dp[i] = 0.0;} while (P! = null) {while (P! = null) {int k = P->index + q->index;dp[k] + = P->coef * q->coef;p = p->next;} Q = Q->next;} for (int i = 0; I <= a+b; ++i) {if (dp[i]! = 0) Push_front (dp[i], i);} DELETE[]DP;} void P_sum::sort () {P_node *p = head->next; P_node *q = P->next;p->next = NULL; P_node *h = null;while (q! = NULL) {h = Q->next;insert (q); q = h;}} void P_sum::load () {cout<< "Please enter the name of the file to be deposited:"; char filename[100]={0};cin>>filename;ofstream outfile ( Filename,ios::out)///define file stream object, open Disk File "F1.dat" if (!outfile)//If Open fails, outfile return value {cerr<< ;" Open error! " <<endl; Exit (1); }cout<< "Number of entries:"; int n;cin>>n;double*a=new double[n];int*b=new int[n]; for (int i=0;i<n;i++) {cout<< "Please enter the section" <<i+1<< "Group data:"; Cin>>a[i]>>b[i];p ush_back (A[i],b[i]); outfile<<a[i]<< "" <<b[i]<< ""; }//To the disk file "F1.dat" output data Outfile.close ();} void P_sum::D ownload () {cout<< "Please enter the name of the file to be deposited:"; char filename[100]={0};cin>>filename;ofstream outfile ( Filename,ios::out)///define file stream object, open Disk File "F1.dat" if (!outfile)//If Open fails, outfile return value {cerr<< ;" Open error! " <<endl; Exit (1); } p_node* p=head->next;if (P = = NULL) {outfile<< "not exist\n"; return;} while (P! = NULL) {if (P->index = = 0) {Outfile<<p->coef;} else if (P->coef = = 1) {outfile<< "x^" <<P->INDEX;} else{outfile<<p->coef<< "x^" <<P->INDEX;} p = p->next;if (p!=null && p->coef>0) {outfile<< "+";}} outfile<< "\ n";} void P_sum:: Readin () {cout<< "Please enter the name of the file to be imported:"; char filename[100]={0};cin>>filename; Define the input file stream object to open the disk file as input f1.dat ifstream infile (filename,ios::in); if (!infile) {CErr<< "Open error!" <<endl; Exit (1); } char C; int n; infile>>n>>c; Double*a=new Double[n]; int *b = new Int[n]; for (int i=0;i<n;i++) {infile>>a[i]>>b[i]; Reads 10 integers from the disk file and stores them sequentially in the A array push_back (A[i],b[i]); Show 10 numbers sequentially on the display} infile.close ();}
#include "test.h" int main () {p_sum pa; P_sum PB; P_sum Pc;int select = 1;while (select) {printf ("*******************************************\n");p rintf ("* [1] to create the polynomial PA [ 2] Create a polynomial PB *\n ");p rintf (" * [3] Display polynomial PA [4] Display polynomial PB *\n ");p rintf (" * [5] polynomial addition [6] polynomial subtraction *\n ");p rintf ( "* [7] polynomial multiplication [8] pa Deposit file *\n");p rintf ("* [9] PB deposit file [10] operation result deposited file *\n");p rintf ("* [11] File import PA [12] File Import PB *\n ");p rintf (" * [13] emptying polynomial pa[14] emptying the polynomial PB *\n ");p rintf (" * [15] Sort PA [16] sort pa *\n "); printf ("* [0] Exit *\n");p rintf ("*******************************************\n");p rintf ("Please select: > "), scanf ("%d ", &select), if (select = = 0) break;switch (select) {case 1:cout<<" Please enter coefficients and exponents (-1-1 means end) \ n "; Cin >>pa;break;case 2:cout<< "Please input coefficients and indices (-1-1 for end) \ n"; cin>>pb;break;case 3:cout<<pa;break;case 4 : Cout<<pb;break;case 5:pc.add_poly_list (PA,PB);p c.sort (); Cout<<pc;//pc.clean (); Break;case 6:pc. Sub_poly_list (PA,PB);p C.sort (); Cout<<pc;//pc.clean (); Break;case 7:pc. Multiply_poly (PA,PB); Cout<<pc;//pc.clean (); Break;case 8:pa. Load (); Break;case 9:PB. Load (); Break;case 10:cout<<pc;pc. DownLoad (); break;case 11:pa.readin (); break;case 12:pb.readin (); break;case 13:pa.clean (); break;case 14:pb.clean (); Break;case 15:pa.sort (); break;case 16:pb.sort (); break;default:break;}} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Realization of arithmetic of unary polynomial with C + + including file import and export of data