Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 22nd, 2012  |  syntax: C++  |  size: 1.85 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #ifndef POLYNOMIAL_H
  2. #define POLYNOMIAL_H
  3.  
  4. #include <string>
  5. #include <list>
  6. #include <utility>
  7.  
  8. class Polynomial;
  9. class Iterator;
  10.  
  11. class Polynomial {
  12. public:
  13.  
  14.         /** Constructs an empty Polynomial object list */
  15.         Polynomial();
  16.         /** Constructs a Polynomial object from two integers, forming one term*/
  17.         Polynomial(pair<double c, int i> term);
  18.         /** Adds pair of values to Polynomial object list */
  19.         void add(const Polynomial& p);
  20.         /** Returns Polynomial Object which is the product of this Polynomial and another supplied */
  21.         Polynomial multiply(Polynomial input);
  22.         /** Returns a Polynomial object as a string in the format of an in-line expression */
  23.         void print(Polynomial input);
  24.  
  25. private:
  26.         list<pair<double, int>> terms;
  27.  
  28. };
  29.  
  30. #endif
  31.  
  32. #include "polynomial.h"
  33. #include <iostream>
  34.  
  35. using namespace std;
  36.  
  37. Polynomial::Polynomial() {
  38.  
  39. }
  40.  
  41. Polynomial::Polynomial(pair<double c, int i> term) {
  42.         terms.push_back(term);
  43. }
  44.  
  45. void Polynomial::add(const Polynomial& p) {
  46.         terms.merge(p.terms);
  47. }
  48.  
  49. Polynomial Polynomial::multiply(Polynomial input) {
  50.         Polynomial product;
  51.  
  52.         for (iterator ithis = this->terms.begin(); ithis <= this->terms.end();
  53.                         ithis++) {
  54.                 for (iterator iinput = input.terms.begin(); iinput <= input.terms.end();
  55.                                 iinput++) {
  56.                         pair<double, int> combined;
  57.                         combined.first() = ithis.first() * iinput.first();
  58.                         combined.second() = ithis.second() + iinput.second();
  59.  
  60.                         product->terms->push_back(combined);
  61.                 }
  62.         }
  63.  
  64.         return product;
  65. }
  66.  
  67. void Polynomial::print() {
  68.         string output;
  69.        
  70.         iterator next =input.terms.begin() + 1;
  71.        
  72.         for (iterator i = input.terms.begin(); i <= input.terms.end(); i++) {
  73.                 if (i < input.terms.end() && i.first() > 0) {
  74.                         output + i.first() + "x^" + i.second() + " ";
  75.                 }
  76.                 if (i.first() > 0 && i != input.terms.end){
  77.                         output + "+";
  78.                 }
  79.                
  80.                 next++;
  81.         }
  82.  
  83.         cout << output;
  84. }