1. 程式人生 > >資料結構,多項式運算,C++,連結串列

資料結構,多項式運算,C++,連結串列

#include<iostream>
 #include<conio.h>
 #include<stdlib.h>  //標準庫標頭檔案包含函式exit()
 using namespace std;
 struct Node     //建立結構體
 {
     float coef;  //項式係數
     int exp;     //項式指數
 };
 typedef Node polynomial;//重新命名
 struct LNode
 {
     polynomial data;//連結串列型別
     LNode *next;
 };
 typedef LNode* Link;
 void Create(Link &L, int n);  //建立一個n項多項式函式
 void Print(Link L);          //輸出連結串列函式
 void Add(Link &pc, Link pa, Link pb); //多項式相加函式
 void Substract(Link &pc, Link pa, Link pb);//多項式相減函式
 void Copy(Link &pc, Link pa);//把一個連結串列的內容複製給另一個連結串列建立表函式
 int Judge(Link pa, Link e);//判斷指數是否與多項式中已存在的某項相同函式
 void Destroy(Link &L)//銷燬連結串列函式
 {
     Link p;
     p = L->next;
     while (p)
     {
         L->next = p->next;
         delete p;
         p = L->next;
     }
     delete L;
     L = NULL;
 }
 void Create(Link &L, int n)//建立含有n個連結串列型別結點的項,即建立一個n項多項式
 {
     if (L != NULL)
     {
         Destroy(L);
     }
     Link p, newp;//定義兩個連結串列
     L = new LNode;
     L->next = NULL;
     (L->data).exp = -1;//建立頭結點
     p = L;
     for (int i = 1; i <= n; i)
     {
         newp = new LNode;//申請新節點
         cout << "請輸入第" << i << "項的係數和指數:" << endl;
         cout << "係數:";
         cin >> (newp->data).coef;
         cout << "指數:";
         cin >> (newp->data).exp;
         if (newp->data.exp<0) //判斷指數是否為0,如果為0,銷燬連結串列,從新輸入
         {
             cout << "您輸入有誤,指數不允許為負值!" << endl;
             delete newp;
             i--;
             continue;
         }
         newp->next = NULL;
         p = L;
         if (newp->data.coef == 0)//判斷係數是否為0
         {
             cout << "係數為零,重新輸入!" << endl;
             delete newp;
             i--;
             continue;
         }
         while ((p->next != NULL) && ((p->next->data).exp<(newp->data).exp)) //利用while迴圈使p指向指數最小的項
         {
             p = p->next;
         }
         if (!Judge(L, newp))//判斷指數是否與多項式中已存在的某項相同
         {
             newp->next = p->next;
             p->next = newp;
         }
         else
         {
             cout << "輸入的該項指數與多項式中已存在的某項相同,請重新建立一個正確的多項式" << endl;
             delete newp;
             Destroy(L);
             Create(L, n); //建立多項式沒有成功,遞迴呼叫重新建立
             break;
         }
     }
 }
 int Judge(Link L, Link e)//判斷指數是否與多項式中已存在的某項相同
 {
     Link p;
     p = L->next;
     while (p != NULL && (e->data.exp != p->data.exp))
         p = p->next;
     if (p == NULL)return 0;
     else return 1;
 }
 void Print(Link L)//輸出連結串列
 {
     Link p;
     if (L == NULL || L->next == NULL)   //判斷多項式是否為空
         cout << "該一元多項式為空!" << endl;
     else
     {
         p = L->next;
         if ((p->data).coef>0)  //項的係數大於的種情況
         {
             if ((p->data).exp == 0)  //輸出指數為0項的係數
                 cout << (p->data).coef;
             else if ((p->data).coef == 1 && (p->data).exp == 1)
                 cout << "x";
             else if ((p->data).coef == 1 && (p->data).exp != 1)
                 cout << "x^" << (p->data).exp;
             else if ((p->data).exp == 1 && (p->data).coef != 1)
                 cout << (p->data).coef << "x";
             else cout << (p->data).coef << "x^" << (p->data).exp;
         }
         //項的係數小於的種情況
         if ((p->data).coef<0)
         {
             if ((p->data).exp == 0)
                 cout << (p->data).coef;
             else if (p->data.coef == -1 && p->data.exp == 1)
                 cout << "-x";
             else if (p->data.coef == -1 && p->data.exp != 1)
                 cout << "-x^" << p->data.exp;
             else if (p->data.exp == 1)
                 cout << p->data.coef << "x";
             else cout << (p->data).coef << "x^" << (p->data).exp;
         }
         p = p->next;
         while (p != NULL)
         {
             if ((p->data).coef>0)
             {
                 if ((p->data).exp == 0)
                     cout << "+ " << (p->data).coef;
                 else if ((p->data).exp == 1 && (p->data).coef != 1)
                     cout << "+ " << (p->data).coef << "x";
                 else if ((p->data).exp == 1 && (p->data).coef == 1)
                     cout << "+ " << "x";
                 else if ((p->data).coef == 1 && (p->data).exp != 1)
                     cout << "+ " << "x^" << (p->data).exp;
                 else cout << "+ " << (p->data).coef << "x^" << (p->data).exp;
             }
             if ((p->data).coef<0)
             {
                 if ((p->data).exp == 0)
                     cout << (p->data).coef;
                 else if (p->data.coef == -1 && p->data.exp == 1)
                     cout << "-x";
                 else if (p->data.coef == -1 && p->data.exp != 1)
                     cout << "-x^" << p->data.exp;
                 else if (p->data.exp == 1)
                     cout << p->data.coef << "x";
                 else cout << (p->data).coef << "x^" << (p->data).exp;
             }

             p = p->next;
         }
     }
     cout << endl;
 }
 void Copy(Link &pc, Link pa)//把一個連結串列的內容複製給另一個連結串列
 {
     Link p, q, r;
     pc = new LNode;
     pc->next = NULL;
     r = pc;
     p = pa;
     while (p->next != NULL)
     {
         q = new LNode;
         q->data.coef = p->next->data.coef;
         q->data.exp = p->next->data.exp;
         r->next = q;
         q->next = NULL;
         r = q;
         p = p->next;
     }
 }
 void Add(Link &pc, Link pa, Link pb)//兩個一元多項式相加
 {
     Link p1, p2, p, pd;
     Copy(p1, pa);
     Copy(p2, pb);
     pc = new LNode;
     pc->next = NULL;
     p = pc;
     p1 = p1->next;
     p2 = p2->next;
     while (p1 != NULL&&p2 != NULL)
     {
         if (p1->data.exp<p2->data.exp)//比較指數大小,把兩個一元多項式的項按指數遞增序列排列
         {
             p->next = p1;
             p = p->next;
             p1 = p1->next;
         }
         else if (p1->data.exp>p2->data.exp)
         {
             p->next = p2;
             p = p->next;
             p2 = p2->next;
         }
         else //當指數相同時兩係數相加
         {
             p1->data.coef = p1->data.coef+ p2->data.coef;
             if (p1->data.coef != 0)//找出係數為零的項並刪除
             {
                 p->next = p1;
                 p = p->next;
                 p1 = p1->next;
                 p2 = p2->next;
             }
             else
             {
                 pd = p1;
                 p1 = p1->next;
                 p2 = p2->next;
                 delete pd;
             }

         }
     }
     if (p1 != NULL)
     {
         p->next = p1;
     }
     if (p2 != NULL)
     {
         p->next = p2;
     }
 }
 void Substract(Link &pc, Link pa, Link pb)//兩個多項式相減
 {
     Link p, pt;
     Copy(pt, pb);//把pb複製在pt
     p = pt;
     while (p != NULL)
     {
         (p->data).coef = (-(p->data).coef);//將係數取反
         p = p->next;
     }
     Add(pc, pa, pt);//呼叫加法函式進行減法
     Destroy(pt);
 }
 void Multiply(Link &pc, Link pa, Link pb)//將兩個一元多項式相乘函式
 {
     Link p1, p2, p, pd, newp, t;
     pc = new LNode;
     pc->next = NULL;
     p1 = pa->next;
     p2 = pb->next;
     while (p1 != NULL)
     {
         pd = new LNode;
         pd->next = NULL;
         p = new LNode;
         p->next = NULL;
         t = p;
         while (p2)
         {
             newp = new LNode;
             newp->next = NULL;
             newp->data.coef = p1->data.coef*p2->data.coef;
             newp->data.exp = p1->data.exp *p2->data.exp;
             t->next = newp;
             t = t->next;
             p2 = p2->next;
         }
         Add(pd, pc, p);
         Copy(pc, pd);
         p1 = p1->next;
         p2 = pb->next;
         Destroy(p);
         Destroy(pd);
     }
 }
 void division(Link &pc, Link pa, Link pb)//將兩個一元多項式相除函式
 {
     Link p1, p2, p, pd, newp, t;
     pc = new LNode;
     pc->next = NULL;
     p1 = pa->next;
     p2 = pb->next;
     while (p1 != NULL)
     {
         pd = new LNode;
         pd->next = NULL;
         p = new LNode;
         p->next = NULL;
         t = p;
         while (p2)
         {
             newp = new LNode;
             newp->next = NULL;
             newp->data.coef = p1->data.coef / p2->data.coef;
             newp->data.exp = p1->data.exp - p2->data.exp;
             t->next = newp;
             t = t->next;
             p2 = p2->next;
         }
         Add(pd, pc, p);
         Copy(pc, pd);
         p1 = p1->next;
         p2 = pb->next;
         Destroy(p);
         Destroy(pd);
     }
 }
 void Menu()//選單函式
 {
     cout << "" << endl;
     cout << endl;
     cout << "\t\t\t\t\t一元多項式的加減乘除運算" << endl;
     cout << "\t\t\t\t\t\t\t\t   " << endl;
     cout << "\t\t  1 建立要運算的兩個一元多項式\t\t   " << endl;
     cout << "\t\t  2 將兩個一元多項式相加\t\t\t   " << endl;
     cout << "\t\t  3 將兩個一元多項式相減\t\t\t   " << endl;
     cout << "\t\t  4 將兩個一元多項式相乘\t\t\t   " << endl;
     cout << "\t\t  5 將兩個一元多項式相除\t\t\t   " << endl;
     cout << "\t\t  0 退出         " << endl;
     cout << "\t\t\t\t\t\t\t\t   " << endl;
     cout << endl;
     cout << "\t\t  請選擇:";
 }


 void main()
 {
     int n;
     Link L, La = NULL, Lb = NULL;//La,Lb分別為建立的兩個多項式
     int choose;
     while (1)
     {
         Menu(); //呼叫選單函式
         cin >> choose;
         switch (choose)
         {
         case 1:
             cout << "請輸入你要運算的第一個一元多項式的項數:" << endl;
             cin >> n;
             Create(La, n);
             cout << "請輸入你要運算的第二個一元多項式的項數:" << endl;
             cin >> n;
             Create(Lb, n);
             break;
         case 2:
             if (La == NULL || Lb == NULL)
             {
                 cout << "您的多項式建立有誤,請重新選擇……" << endl;
                 break;
             }
             Add(L, La, Lb);
             cout << "" << endl;

             cout << "待相加的兩個一元多項式為:" << endl;
             cout << "" << endl;
             cout << "A的多項式為:";
             Print(La);
             cout << "" << endl;
             cout << "B的多項式為:";
             Print(Lb);
             cout << "" << endl;
             cout << "相加後的結果為:";
             Print(L);
             cout << "" << endl;
             Destroy(L);
             break;
         case 3:
             Substract(L, La, Lb);
             cout << "相減的兩個一元多項式為:" << endl;
             cout << "" << endl;
             cout << "A的多項式為:";
             Print(La);
             cout << "" << endl;
             cout << "B的多項式為:";
             Print(Lb);
             cout << "" << endl;
             cout << "相減後的結果為:";
             Print(L);
             cout << "" << endl;
             Destroy(L);
             break;
         case 4:
             if (La == NULL || Lb == NULL)
             {
                 cout << "您的多項式建立有誤,請重新選擇……" << endl;
                 break;
             }
             Multiply(L, La, Lb);
             cout << "相乘的兩個一元多項式為:" << endl;
             cout << "" << endl;
             cout << "A的多項式為:";
             Print(La);
             cout << "" << endl;
             cout << "B的多項式為:";
             Print(Lb);
             cout << "" << endl;
             cout << "相乘後的結果為:";

             Print(L);
             Destroy(L);
             cout << "" << endl;
             break;
         case 5:
             if (La == NULL || Lb == NULL)
             {
                 cout << "您的多項式建立有誤,請重新選擇……" << endl;
                 break;
             }
             division(L, La, Lb);
             cout << "相除的兩個一元多項式為:" << endl;
             cout << "" << endl;
             cout << "A的多項式為:";
             Print(La);
             cout << "" << endl;
             cout << "B的多項式為:";
             Print(Lb);
             cout << "" << endl;
             cout << "相除後的結果為:";

             Print(L);
             Destroy(L);
             cout << "" << endl;
             break;
         case 0:exit(1);
             Print(L);
             cout << "" << endl;
             break;
         }
     }
 }