從字符文件輸入兩個多項式的非零係數及對應的指數,創建多項式的鏈式存儲結構,計算這兩個多項式的乘積,輸出乘積多項式的所有非零係數及對應的指數到另外一字符文件中。node
兩個多項式的乘法,能夠藉助兩個多項式的加法的算法來實現。ios
設:算法
則:數組
採用帶附加頭結點單向鏈表;每一個結點包括雙精度類型的係數域、整型類型的指數域和一個指針域。數據結構
typedef struct polynode { double c; //係數 int e; //指數 struct polynode *next;//指針域,要求結點按指數e升序鏈接 }PNode,*Polyn; //PNode爲結點類型,Polyn爲結點指針類型
#include <iostream> #include <fstream> using namespace std; //定義結點及結點指針數據類型 typedef struct polynode { double c; //係數 int e; //指數 struct polynode *next; //要求結點按指數e升序鏈接 }PNode,*Polyn; //PNode爲結點類型,Polyn爲結點指針類型 Polyn h1, h2; //兩個多項式P(x),Q(x)的頭結點 double a[20]; //數組a保存係數 int b[20]; //數組b保存指數 void Read(char *s) //讀取數據,s代入不一樣的文件名 { double c; int i = 0, e; ifstream infile(s); if (!infile) //打開文件失敗輸出提示信息 { cout << "file open error!" << endl; exit(0); } while (1) //打開成功,將係數和指數保存在兩個數組中 { infile >> c >> e; if (infile.eof()) break; a[i] = c; b[i] = e; i++; } infile.close(); } void Write(Polyn h) //輸出函數,將結果輸出到文件中,h爲鏈表頭結點 { ofstream outfile("multiply.txt"); //輸出文件名爲multiply.txt Polyn p = h->next; //去掉附加頭結點 if (!outfile) //打開文件失敗輸出提示信息 { cout << "file open error!" << endl; exit(0); } if (!p) //第一個結點爲空,表示運算結果爲0 outfile << "0" << endl; while (p) //p不爲空,依次輸出係數和指數 { outfile << p->c << " " << p->e << endl; p = p->next; } outfile.close(); } Polyn Create(char *s) //先入先出創建鏈表,s代入不一樣的文件名 { int i = 0; //i用於記錄數組下標 Polyn h, last, p; h = new PNode; h->next = NULL; //h爲附加頭結點 last = h; Read(s); //從文件中讀取係數和指數 while (a[i]!=0) { p = new PNode; p->c = a[i]; p->e = b[i]; p->next = NULL; //建新結點 last->next = p; last = p; //新結點插入到鏈表尾 i++; } return h; } void PrintPoly(Polyn h) //按多項式的形式將結果打印到控制檯界面中 { Polyn p = h->next; if (!p) //所得結果爲空,則輸出0 cout << "0" << endl; while (p) { if (p->next) //p不是尾結點 { if (p->next->c < 0) //p的後繼結點的係數小於0,不輸出符號 + { if (p->c == -1) //p的後繼結點的係數等於-1 { cout << "-x^" << p->e; p = p->next; } else if (p->c == 1) //p的後繼結點的係數等於1 { cout << "x^" << p->e; p = p->next; } else { cout << p->c << "x^" << p->e; p = p->next; } } else if (p->next->c > 0) //p的後繼結點的係數大於0,需輸出符號+ { if (p->c == 1) //p的後繼結點的係數等於1 { cout << "x^" << p->e << "+"; p = p->next; } else if (p->c == -1) //p的後繼結點的係數等於-1 { cout << "-x^" << p->e << "+"; p = p->next; } else { cout << p->c << "x^" << p->e << "+"; p = p->next; } } } else //p是尾結點,需在結尾輸出換行 { if (p->c < 0) //p的指數小於0 { if (p->c == -1) { cout << "-x^" << p->e << endl; p = p->next; } else { cout << p->c << "x^" << p->e << endl; p = p->next; } } else if (p->c > 0) //p的指數大於0 { if (p->c == 1) { cout << "x^" << p->e << endl; p = p->next; } else { cout << p->c << "x^" << p->e << endl; p = p->next; } } } } } void CreateNode(Polyn &p) //建立新結點 { p = new PNode; } void DeleteNode(Polyn &p) //刪除結點 { delete p; } Polyn add(Polyn h1, Polyn h2) //實現兩個多項式的相加,返回和式頭指針 { Polyn p1, p2, p3, h, p; //h爲和式R(x)的附加頭結點指針 p1 = h1->next; p2 = h2->next; CreateNode(h); p3 = h; while (p1&&p2) { if (p1->e < p2->e) //p1的指數大於p2,先保存p1結點 { p = p1; p1 = p1->next; } else if (p2->e < p1->e) //p2的指數大於p1,先保存p2結點 { p = p2; p2 = p2->next; } else //p1與p2指數相等時 { p1->c += p2->c; //係數相加,結果保存在p1中 if (p1->c == 0) //係數之和爲0,則刪除該結點 { p = p1; p1 = p1->next; DeleteNode(p); //刪除結點 p = p2; p2 = p2->next; DeleteNode(p); continue; } p = p2; //係數之和不爲0時,先刪除p2結點 p2 = p2->next; DeleteNode(p); p = p1; //將p1鏈接到p中 p1 = p1->next; } p3->next = p; //插入p結點至和式末尾 p3 = p; } if (p1) //p1沒有結束,將p1後面全部的結點鏈接到和式 p3->next = p1; else if (p2) //p2沒有結束,將p2後面全部的結點鏈接到和式 p3->next = p2; else p3->next = NULL; h1->next = h2->next = NULL; //清空h1和h2鏈表 return h; } Polyn mul(Polyn hp, Polyn hq) //實現兩個多項式的相乘 { Polyn hr, ht, q, p, pt; CreateNode(hr); hr->next = NULL; //R(x) = 0 CreateNode(ht); ht->next = NULL; //T(x) = 0 q = hq->next; while (q) { pt = ht; p = hp->next; while (p) { CreateNode(pt->next); //建立新的尾結點 pt = pt->next; pt->c = p->c*q->c; //係數相乘 pt->e = p->e + q->e; //指數相加 p = p->next; } pt->next = NULL; q = q->next; p = add(hr, ht); //實現R(x) = R(x) + T(x) DeleteNode(hr); hr = p; } DeleteNode(ht); return hr; } int main() { Polyn h1, h2, h3; //定義單向鏈表附加頭結點指針 cout << "讀取文件,直到讀入0時中止,創建單向鏈表" << endl; h1 = Create("polynode1.txt"); h2 = Create("polynode2.txt"); cout << "P(x) = "; PrintPoly(h1); cout << "Q(x) = "; PrintPoly(h2); h3 = mul(h1, h2); cout << "R(x) = P(x) * Q(x) = "; PrintPoly(h3); Write(h3); //寫入文件 return 0; }
(1) P(x) 1 2 2 3 0函數
Q(x) 1 -2 2 -3 0spa
輸出結果爲:
(2)P(x) 1 1 2 2 3 3 0設計
Q(x) -1 1 -2 2 -3 3 0指針
輸出結果爲:
code