school/cs235/exam1/polyman.cpp

108 lines
2.6 KiB
C++

//Derek McQuay 647465151 CS 235 Fall 2012 midterm 1
#include "polyman.h"
PolynomialListInterface * polyman::addLists() {
node * node_ptr1 = l1.head;
node * node_ptr2 = l2.head;
polylist l3;
stringstream exp;
while(node_ptr1 != NULL) {
if(node_ptr1->coefficient != 0) {
exp << node_ptr1->coefficient;
exp << " ";
}
exp << node_ptr1->variable;
if(node_ptr1->exponent != 0) {
exp << " ^ ";
exp << node_ptr1->exponent;
}
l3.insert(exp.str());
exp.str("");
node_ptr1 = node_ptr1->next;
}
while(node_ptr2 != NULL) {
if(node_ptr2->coefficient != 0) {
exp << node_ptr2->coefficient;
exp << " ";
}
exp << node_ptr2->variable;
if(node_ptr2->exponent != 0) {
exp << " ^ ";
exp << node_ptr2->exponent;
}
l3.insert(exp.str());
exp.str("");
node_ptr2 = node_ptr2->next;
}
polylist * p = NULL;
p = &l3;
return p;
}
PolynomialListInterface * polyman::subtractLists() {
node * node_ptr1 = l1.head;
node * node_ptr2 = l2.head;
polylist l3;
stringstream exp;
while(node_ptr1 != NULL) {
if(node_ptr1->coefficient != 0) {
exp << node_ptr1->coefficient;
exp << " ";
}
exp << node_ptr1->variable;
if(node_ptr1->exponent != 0) {
exp << " ^ ";
exp << node_ptr1->exponent;
}
l3.insert(exp.str());
exp.str("");
node_ptr1 = node_ptr1->next;
}
while(node_ptr2 != NULL) {
if(node_ptr2->coefficient != 0) {
node_ptr2->coefficient = node_ptr2->coefficient * -1; // will cause it to be subtracting
exp << node_ptr2->coefficient;
exp << " ";
}
exp << node_ptr2->variable;
if(node_ptr2->exponent != 0) {
exp << " ^ ";
exp << node_ptr2->exponent;
}
l3.insert(exp.str());
exp.str("");
node_ptr2 = node_ptr2->next;
}
polylist * p = NULL;
p = &l3;
return p;
}
void polyman::fillListOne(string term) {
l1.insert(term);
}
void polyman::fillListTwo(string term) {
l2.insert(term);
}
void polyman::clearListOne() {
l1.clear();
}
void polyman::clearListTwo() {
l2.clear();
}
PolynomialListInterface * polyman::getListOne() {
polylist * p = NULL;
p = &l1;
return p;
}
PolynomialListInterface * polyman::getListTwo() {
polylist * p = NULL;
p = &l2;
return p;
}