Construction and addition and subtraction of polynomials

Source: Internet
Author: User

Scenario description: polynomial construction is implemented here, for example, 2x (2) + 4x (4). Because of the limited level, brackets represent the exponent. The most interesting thing is to insert a polynomial into a complete polynomial with the same index and insertion. Classification:

1) add a node directly without any node

2) there is only one node, which can be merged or inserted in the front or added subsequently.

3) when two nodes exist, use a combination of two nodes to find a suitable location.

Typedef struct spolympus nomial {float coef; int expn; spolympus nomial * signature;} spolympus nomial; class CPolyn {private: spolympus nomial * signature; public: CPolyn () {m_pspnoolympus nomialheader = NULL ;}~ CPolyn () {if (NULL = m_pspolympus nomialheader) return; spolympus nomial * pTmp = m_pspolympus nomialheader; while (NULL! = PTmp) {free (pTmp); m_pspolympus nomialheader = pTmp-> pnextspolympus nomial ;}} int AddPolyn (float coef, int expn) {// 2. create insert node spolympus nomial * p = new spolympus nomial; // spolympus nomial * p = (spolympus nomial *) malloc (sizeof (spolympus nomial); p-> coef = coef; p-> expn = expn; p-> pnextspolympus nomial = NULL; // 3.if polynheader is null, add and return if (NULL = m_pspolympus nomialheader) {m_pspolympus nomialheader = p; return 0 ;} // 4.if polynhea Der has only one node if (m_pspolympus nomialheader-> expn> p-> expn) {p-> pnextspolympus nomial = m_pspolympus nomialheader; m_pspolympus nomialheader = p; return 0 ;} if (m_pspolympus nomialheader-> expn = p-> expn) {m_pspolympus nomialheader-> coef + = p-> coef; delete p; return 0;} // 1. assing polynheader to tmp spolympus nomial * ptmpspolympus nomial = m_pspolympus nomialheader; // 4.if polynheader only have two node, if a1.expn <p-> expn <a2.expn, betinsert we En if (NULL! = M_pspolympus nomialheader-> pnextspolympus nomial) {if (m_pspolympus nomialheader-> expn <p-> expn) & (p-> expn <m_pspolympus nomialheader-> accept-> expn )) {p-> pnextspolympus nomial = m_pspolympus nomialheader-> pnextspolympus nomial; m_pspolympus nomialheader-> signature = p; return 0 ;}// 5 transever all node while (NULL! = Ptmpspolympus nomial-> signature) {if (ptmpspolympus nomial-> expn = p-> expn) {ptmpspolympus nomial-> coef = ptmpspolympus nomial-> coef + p-> coef; delete p; return 0;} if (ptmpspolympus nomial-> pnextspolympus nomial-> expn = p-> expn) {ptmpspolympus nomial-> pnextspolympus nomial-> coef + = p-> coef; delete p; return 0;} if (ptmpspolympus nomial-> expn <p-> expn) & (ptmpspolympus nomial-> pnextspolympus nomial-> expn <p-> expn )) {ptmpspolympus nomial = ptmpspolympus nomial-> p Nextspolympus nomial;} else if (ptmpspolympus nomial-> expn <p-> expn) & (ptmpspolympus nomial-> pnextspolympus nomial-> expn> p-> expn )) {p-> signature = ptmpspolympus nomial-> signature; ptmpspolympus nomial-> pnextspolympus nomial = p; return 0 ;}} ptmpspolympus nomial-> signature = p; return 0;} void PrintPolyn () {spolympus nomial * pTmp = m_pspolympus nomialheader; while (NULL! = PTmp) {cout <"coef:" <pTmp-> coef <"<" expn: "<pTmp-> expn <endl; pTmp = pTmp-> pnextspolympus nomial;} int PolyLength () {spolympus nomial * pTmp = m_pspolympus nomialheader; int nLen = 0; while (NULL! = PTmp) {nLen ++; pTmp = pTmp-> pnextspolympus nomial;} return nLen;} int AddOnePolynToPoly (CPolyn * p) {// of course we can use addPolyn function to finish it but not valid tive // first step we shoshould sketch one spolympus nomial coef and expn and then use // AddPolyn (coef, expn) finish it, however every time we shoshould traserval every link // list float coef = 0; int expn = 0; while (-1 )! = P-> PopPolyn (coef, expn) {AddPolyn (coef, expn) ;}return 0 ;}int PopPolyn (float & coef, int & expn) {if (NULL = signature) return-1; coef = m_pspolympus nomialheader-> coef; expn = signature-> expn; spolympus nomial * pTmp = m_pspolympus nomialheader; m_pspolympus nomialheader = pTmp-> signature; delete pTmp; return 0 ;}}; int main (int argc, char * argv []) {QCoreApplication a (argc, argv); {CPolyn B; B. addPolyn (1, 2); B. addPolyn (3, 3); B. addPolyn (2, 4); B. addPolyn (4, 4); B. addPolyn (5, 6); B. addPolyn (6, 5); B. addPolyn (6, 5); B. addPolyn (7,7); int size = B. polyLength (); cout <"size =" <size <endl; B. printPolyn (); cout <"Finish it" <endl;} return a.exe c ();}

Construction and addition and subtraction of polynomials

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.