Practical training c ++ language design-polynomial operation

Source: Internet
Author: User

Platform: VC ++ 2005 passed the test!
. Vcproj
This is the main project file of the VC ++ project generated by using the Application Wizard.
It contains the Visual C ++ version information that generates the file, as well as information about the platform, configuration, and project features selected by the Application Wizard.
Stdafx. H, stdafx. cpp
These files are used to generate a pre-compiled header (PCH) file named twod. PCH and a pre-compiled file named stdafx. obj.
These are all VC ++ files generated using the Application Wizard, so they are not listed
I will only list the main parts of the program!

/* Implement vector and lists in the standard library
Sparse polynomial of one element */
# Include <iostream>
# Include <vector>
# Include <list>
# Include <string>
# Include <algorithm>
Using namespace STD;

Typedef struct {
Float _ coef;
Int _ EXPN;
} Lnode; // defines the 'node' type of the polynomial linked list.

Typedef list <lnode> polynomial;

/* Definition of an interface function of the polynomial type,
This part is public to users of the polynomial type */
Void creatpolyn (Polynomial & P, vector <double> & coef, vector <int> & expon );
Void printpolyn (const Polynomial & P );
Bool comp (const lnode & A, const lnode & B );
Void addpolyn (const Polynomial & PA, const Polynomial & Pb, Polynomial & psum );

 

/* Implementation of interface functions of Polynomial Type,
This part is not made public to polynomial users */
Void creatpolyn (Polynomial & P, vector <double> & coef, vector <int> & expon)
{
For (INT I = 0; I <expon. Size (); ++ I ){
Lnode tempnode;
Tempnode. _ coef = coef [I]; tempnode. _ EXPN = expon [I];
/* Constantly Insert new nodes from the head of the linked list */
P. push_back (tempnode );
}
/* Sort the newly generated list by Index */
P. Sort (COMP); // comp is the node sorting standard.
}

// Lnode sorting standard. This function will be used as the sort function's
// Use parameters
Bool comp (const lnode & A, const lnode & B ){
If (A. _ EXPN <B. _ EXPN) return true;
Else return false;
}

// Output the content of the polynomial linked list
Void printpolyn (const Polynomial & P ){
If (P. Empty ())
Cout <"The linked list is an empty linked list! ";
Else {
/* Define the iterator Of The traversal table */
List <lnode >:: const_iterator iter = P. Begin ();
For (INT I = 0; I <p. Size (); I ++ ){
Cout <(* ITER). _ coef <"x ^" <(* ITER). _ EXPN;
If (I! = (P. Size ()-1) cout <"+ ";
ITER ++;
}
Cout <Endl;
}
}

Void addpolyn (const Polynomial & PA, const Polynomial & Pb, Polynomial & psum)
{
/* Define the iterator Of The traversal table */
List <lnode >:: const_iterator iter_a = pa. Begin ();
List <lnode >:: const_iterator iter_ B = Pb. Begin ();

While (iter_a! = Pa. End () & (iter_ B! = Pb. End ())){
If (* iter_a). _ EXPN> (* iter_ B). _ EXPN) {// if 1
Lnode newnode;
Newnode. _ coef = (* iter_ B). _ coef;
Newnode. _ EXPN = (* iter_ B). _ EXPN;
Psum. push_back (newnode );
Iter_ B ++;
} // Endif 1
Else if (* iter_a). _ EXPN <(* iter_ B). _ EXPN) {// If 2
Lnode newnode;
Newnode. _ coef = (* iter_a). _ coef;
Newnode. _ EXPN = (* iter_a). _ EXPN;
Psum. push_back (newnode );
Iter_a ++;
} // End if 2
Else {
Lnode newnode;
Newnode. _ coef = (* iter_a). _ coef + (* iter_a). _ coef;
Newnode. _ EXPN = (* iter_a). _ EXPN;
Psum. push_back (newnode );
Iter_a ++; iter_ B ++;
}

}
// There are other nodes in the PA linked list
While (iter_a! = Pa. End ()){
Lnode newnode;
Newnode. _ coef = (* iter_a). _ coef;
Newnode. _ EXPN = (* iter_a). _ EXPN;
Psum. push_back (newnode );
Iter_a ++;
}
// The PB linked list contains the remaining nodes.
While (iter_ B! = Pb. End ()){
Lnode newnode;
Newnode. _ coef = (* iter_ B). _ coef;
Newnode. _ EXPN = (* iter_ B). _ EXPN;
Psum. push_back (newnode );
Iter_ B ++;
}

}

 

 

// Polynomial_sl.cpp: defines the entry point of the console application.
//

# Include "stdafx. H"
# Include "polynomial_sl.h"

Int _ tmain (INT argc, _ tchar * argv [])
{
String S;
Do {
Double dval = 0.0;
Int ival = 0;
Int a_num_items; // number of items contained in a polynomial, which is specified by the user in the program
Int B _num_items; // number of items contained in the polynomial B, which is specified by the user in the program
Vector <double> a_coefficients; // a polynomial coefficient Array
Vector <int> a_exponents; // a polynomial Index Array
Vector <double> B _coefficients; // array of polynomial coefficients of B
Vector <int> B _exponents; // B polynomial exponent Array
 
Cout <"Enter the number of items in polynomial A (x :";
Cin> a_num_items;
Cout <"Enter the coefficient of Polynomial A (x :";
For (INT I = 0; I <a_num_items; I ++ ){
Cin> dval;
A_coefficients.push_back (dval );
}
Cout <"Enter the exponent of Polynomial A (x :";
For (INT I = 0; I <a_num_items; I ++ ){
Cin> ival;
A_exponents.push_back (ival );
}

Cout <"Enter the number of items in polynomial B (X :";
Cin> B _num_items;
Cout <"Enter the coefficient of polynomial B (X :";
For (INT I = 0; I <B _num_items; I ++ ){
Cin> dval;
B _coefficients.push_back (dval );
}
Cout <"Enter the exponent of polynomial B (X :";
For (INT I = 0; I <B _num_items; I ++ ){
Cin> ival;
B _exponents.push_back (ival );
}

Polynomial Pa, Pb, psum;

Creatpolyn (Pa, a_coefficients, a_exponents );
Creatpolyn (Pb, B _coefficients, B _exponents );
Cout <"A (x) = ";
Printpolyn (PA );
Cout <"B (X) = ";
Printpolyn (PB );

Cout <"A (x) + B (X) = ";
Addpolyn (Pa, Pb, psum );
Printpolyn (psum );

} While (! (S = "exit "));

Return 0;
}

 

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.