/** * Get two polynomials from the user and output the summation * * @Author Mario Lopez * @Version 1.0 * @Date Feb 23 2007 */ #include #include #include using namespace std; // Class to hold data for each element // e.g. +4.9x^7 // where + is held in "sign" // 4.9 is held in "coefficient" // 7 is held in "exponent" class Component{ char sign; float coefficient; float exponent; public: Component(char s, float c, float e){sign = s; coefficient = c; exponent = e;}; void printComponent(); float getExponent(); void getComponentArgs(char &s, float &c, float &e); }; // Method to print out a component (e.g. "+4.9x^7") void Component::printComponent() { cout << sign << coefficient << "x^" << exponent; }; // Method to return the exponent of a certain component float Component::getExponent() { return exponent; }; // Copy component variables void Component::getComponentArgs(char &s, float &c, float &e) { s = sign; c = coefficient; e = exponent; }; // To get input and parse it into a component int readComponent(char &s, float &c, float &e){ char l; l = cin.get(); if( l == '+' || l == '-') { s = l; // Get sign cin >> c; // Get coefficient cin.get();cin.get(); // Ignore x and ^ chars cin >> e; // Get exponent } else if (l == '\n') return 0; return 1; } int main() { list L1, L2, summedList; float c,e, c2; char s, s2; cout << "Please input the first polynomial:" << endl; while( readComponent(s,c,e) ) { Component component(s,c,e); L1.push_back(component); } cout << "Please input the second polynomial:" << endl; while( readComponent(s,c,e) ) { Component component(s,c,e); L2.push_back(component); } list::iterator element1, element2; element1 = L1.begin(); element2 = L2.begin(); // Start adding the two polynomials while( element1 != L1.end() && element2 != L2.end() ) { // If List2 has a component with an exponent List1 doesn't have if( (*element1).getExponent() < (*element2).getExponent() ) { (*element2).getComponentArgs(s,c,e); Component component(s,c,e); summedList.push_back(component); element2++; } // If List1 has a component with an exponent List2 doesn't have else if( (*element1).getExponent() > (*element2).getExponent() ) { (*element1).getComponentArgs(s,c,e); Component component(s,c,e); summedList.push_back(component); element1++; } // If List1 and List2 both have a component with the same exponent else { (*element1).getComponentArgs(s,c,e); (*element2).getComponentArgs(s2, c2, e); // Deal with the signs if( s == s2 ) { // Same sign, either + or - c += c2; } else { // Different sign if( c > c2 ) c -= c2; else { c = c2 - c; s = s2; } } Component component(s,c,e); summedList.push_back(component); element2++; element1++; } } // Finish up L1 if need be while( element1 != L1.end() ) { (*element1).getComponentArgs(s,c,e); Component component(s,c,e); summedList.push_back(component); element1++; } // Finish up L2 if need be while( element2 != L2.end() ) { (*element2).getComponentArgs(s,c,e); Component component(s,c,e); summedList.push_back(component); element2++; } cout << "The summation of these two polynomials are:" << endl; // Reusing the variable "element1" // Going through the "summedList" to print the output for( element1 = summedList.begin(); element1 != summedList.end(); element1++ ) (*element1).printComponent(); cout << endl; return 0; }