C algorithm and data structure-linear table application, polynomial summation --- ShinePans

Source: Internet
Author: User

/* --- Computer job, binary addition --- * // * --- By Pan Shang --- * // * --- Date. --- * // * --- question: --- * // assume there are two sparse polynomials A and B. design the algorithm to complete the following tasks: // 1. input and establish polynomials A and B; // 2. calculate the sum of two polynomials and polynomial C; // 3. returns the product polynomial D of two polynomials. // four polynomials A, B, C, D are output. # include
 
  
# Include
  
   
# Include
   
    
Typedef struct Node {float A; // coefficient int m; // index struct Node * next;} LNode, * LinkList; // linked list type, how to use a nested struct to access void Initialize (LNode * plist), LinkList PolyAdd (LNode * plist1, LNode * plist2), int Cmp (int, int B); void Append (LNode * pa, LNode * pb); int ListIsEmpty (const LinkList * plist); void EmptyTheList (LNode * plist ); void PrintList (LNode * plist); void PolySort (LNode * plist); int GetLength (LNode * plist); voi D PolyMerge (LNode * plist); float HornorEvaluate (LNode * plist, float x); LinkList PolyMultiply (LNode * plist1, LNode * plist2); int GetMaxExpn (LNode * plist ); /*** main function */int main (void) {LNode * p1 = (LNode *) malloc (sizeof (LNode); LNode * p2 = (LNode *) malloc (sizeof (LNode); LNode * result1 = (LNode *) malloc (sizeof (LNode); LNode * result2 = (LNode *) malloc (sizeof (LNode )); initialize (p1); Initialize (p2); PolySort (p1); // multiple Sort items by PolySort (p2); printf ("\ np1 ="); PrintList (p1); printf ("\ np1 (value) = % f", HornorEvaluate (p1, 2); printf ("\ np2 ="); PrintList (p2); printf ("\ np2 (value) = % f", HornorEvaluate (p2, 2 )); result1 = PolyAdd (p1, p2); result2 = PolyMultiply (p1, p2); // printf ("\ n % d", GetLength (p1 )); // PolyMerge (p1); // merge same class items // UnitePoly (p1); // merge same class items // EmptyTheList (p1); printf ("\ np1 + p2 = "); printList (result1); printf ("\ np1 * p2 = "); PrintList (result2); getchar (); return 0;}/*** Operation: initialize a polynomial * Precondition: * Postcondition: */void Initialize (LNode * plist) {int I, n; float c; int e; LNode * prev = NULL, * current = NULL; plist-> next = NULL; prev = plist; printf ("\ nPlease input the length of the polynomial:"); scanf_s ("% d", & n); plist-> A = 0; plist-> m = n; // the length of the linked list printf ("Please input the coefficient and the exponent of each term: \ N "); for (I = 1; I <= n; I ++) {current = (LNode *) malloc (sizeof (LNode )); scanf_s ("% 2f % d", & c, & e); current-> A = c; current-> m = e; prev-> next = current; prev = current;/* plist-> next = current; plist = current; */} current-> next = NULL; PolySort (plist);}/*** Operation: add two polynomials * Precondition: * Postcondition: * Notes: You can first merge the two polynomials (spliced) and then merge the same class items to add them */LinkList PolyAdd (LNode * plist1, LNode * plist2) {LNode * pa, * pb; LNode * Result = (LNode *) malloc (sizeof (LNode); int a, B; result-> next = NULL; LNode * r = result; pa = plist1-> next; pb = plist2-> next; while (pa & pb) {LNode * current = (LNode *) malloc (sizeof (LNode); a = pa->; B = pb-> m; switch (Cmp (a, B) {case-1: current-> A = pa-> m; current-> A = pa-> m; pa = pa-> next; break; case 0: current-> A = pa-> A + pb->; current-> m = pa-> m; pa = pa-> next; pb = pb-> next; break; case 1: current-> A = pb -> A; current-> m = pb-> m; pb = pb-> next; break;} r-> next = current; r = r-> next; // result the polynomial pointer is moved back to current-> next = NULL;} if (! Pa |! Pb) {if (! Pa) Append (r, pb); if (! Pb) Append (r, pa);} // free (current); return result;}/*** compare two integers */int Cmp (int a, int B) {if (a> B) return 1; if (
    
     
A = p-> A; current-> m = p-> m; p = p-> next; r-> next = current; // SEGIVEcurrent-> next = NULL;}/*** Operation: determines whether the polynomial is NULL */int ListIsEmpty (const LinkList * plist) {if (* plist = NULL) return 1; elsereturn 0;}/*** Operation: clears a polynomial linked list * Precondition: plist points to a polynomial list * Postcondition: this list is cleared and released */void EmptyTheList (LNode * plist) {LNode * psave; while (plist! = NULL) {psave = plist-> next; free (plist); plist = psave ;}}/*** Operation: output A Polynomial linked list */void PrintList (LNode * plist) {LNode * p = plist; p = p-> next; // skip the header pointer while (p! = NULL) {if (p-> next! = NULL) printf ("% 0.1f * x ^ % d +", p-> A, p-> m); elseprintf ("% 0.1f * x ^ % d ;", p-> A, p-> m); p = p-> next;} printf ("\ n");}/*** Operation: sort the input unordered polynomial linked list * Precondition: unordered polynomial linked list * Postcondition: progressively ordered polynomial linked list */void PolySort (LNode * plist) {LNode * pa = plist-> next, * pb = pa-> next; LNode * temp = (LNode *) malloc (sizeof (LNode); int length = GetLength (plist); int I; for (I = 0; I
     
      
M> pb-> m) {temp-> A = pa-> A; temp-> m = pa-> m; pa-> A = pb->; pa-> m = pb-> m; pb-> A = temp-> A; pb-> m = temp-> m;} pa = pa-> next; pb = pa-> next;} pa = plist-> next; pb = pa-> next; // these two sentences are used to set pa, pb points to the first node again}/*** Operation: merges the same exponent items in the input polynomial (merges the same class items) * Precondition: ordered polynomial linked list * Postcondition: ordered polynomial linked list without similar items */void PolyMerge (LNode * plist) {LNode * prev, * current; int l = GetLength (plist); int I; prev = plist-> next; current = prev-> next; for (I = 0; I
      
        M = current-> m) {prev-> A + = current-> A; // why "prev-> coef + = current-> coef" is wrong? Prev-> next = current-> next; free (current); current = prev-> next; continue ;//! Without this sentence, the function will be wrong and report an problem} prev = prev-> next; current = prev-> next;} if (current) {prev = plist-> next; current = prev-> next ;}}/ * // merge the same class void UnitePoly (LNode * h) // merge the same class items {LNode * p1, * p2, * q1, * q2, * temp; q1 = h; p1 = q1-> next; while (p1! = NULL) {p2 = p1-> next; q2 = p1; while (p2! = NULL) {if (p1-> expn = p2-> expn) {p1-> coef = p1-> coef + p2-> coef; if (p1-> coef = 0) {temp = p2; q2-> next = p2-> next; free (temp); temp = p1; q1-> next = p1-> next; p1 = q1; free (temp); break;} else {temp = p2; q2-> next = p2-> next; p2 = p2-> next; free (temp) ;}} else {q2 = p2; p2 = p2-> next ;}} q1 = p1; p1 = p1-> next;} * // *** calculate the length of the linked list */int GetLength (LNode * plist) {LNode * p = plist; int lenght = 0; p = p-> next; // skip the node while (p) {lenght ++; p = p-> next;} return lenght ;// Return lenght-1; // if there is no overhead node, return value minus one}/*** Operation: returns the product of two polynomials * Precondition: * Postcondition: */LinkList PolyMultiply (LNode * plist1, LNode * plist2) {LNode * pa, * pb; LNode * result = (LNode *) malloc (sizeof (LNode); LNode * r = result; // cannot be less! Otherwise, the result does not indicate the header pointer pa = plist1-> next; pb = plist2-> next; while (pa) {while (pb) {LNode * current = (LNode *) malloc (sizeof (LNode); current-> A = pa-> m * pb->; // multiply the coefficients by current-> m = pa-> m + pb-> m; // exponentially add r-> next = current; current-> next = NULL; r = r-> next; pb = pb-> next;} pa = pa-> next; pb = plist2-> next; // pb points to the header node again} PolyMerge (result); return result;}/*** Operation: Computing the value of the polynomia * Precondition: Ordered Polynomial Linklist * Postcondition: the value of the polynomial */float HornorEvaluate (LNode * plist, float x) {// int max = GetMaxExpn (plist) + 10; int n = 0; int I; float result = 0; // float Poly [max]; // VC6 sidn't support VLAfloat Poly [20]; LNode * p = plist-> next; memset (Poly, 0, sizeof (Poly); while (p) {if (p-> m = n) {Poly [n ++] = p->; p = p-> next;} elsePoly [n ++] = 0;} // Transform linklist to array to store the polynomial/* for (I = 0; I
       
         0; I --) // The number of loops and the subscript relationship cannot be wrong! {Result = result * x + Poly [I-1]; // printf ("[% d % 0.2f]", I, result ); // output intermediate variables for debugging} return result;}/*** Operation: * Precondition: * Postcondition: */int GetMaxExpn (LNode * plist) {LNode * p = plist-> next; int max = p-> m; while (p) {if (p-> m> max) max = p-> m; p = p-> next;} return max ;}
       
      
     
    
   
  
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.