43 lines
1.4 KiB
C++
43 lines
1.4 KiB
C++
//Derek McQuay 647465151 CS 235 Fall 2012 midterm 1
|
|
#include "node.h"
|
|
|
|
node::node(const int exponent, const int coefficient, const char variable, node* next) :
|
|
exponent(exponent), coefficient(coefficient), variable(variable), next(next) {}
|
|
|
|
node::node(const int exponent, const char variable, node* next) : //construtor for when coefficient is assumed 1
|
|
exponent(exponent), coefficient(1), variable(variable), next(next) {}
|
|
|
|
ostream & operator<<(ostream & os, node n) { //used to correctly print out each node
|
|
if(n.coefficient == 0) {
|
|
os << "";
|
|
}
|
|
else if(n.coefficient == 1 && n.exponent == 0) {
|
|
os << n.coefficient;
|
|
}
|
|
else if(n.coefficient == -1 && n.exponent == 0) {
|
|
os << n.coefficient;
|
|
}
|
|
else if(n.exponent == 0) {
|
|
os << n.coefficient;
|
|
}
|
|
else if(n.coefficient == -1) {
|
|
os << "-" << n.variable << " ^ " << n.exponent;
|
|
}
|
|
else if(n.coefficient < 0 && n.exponent == 1) {
|
|
os << n.coefficient << n.variable;
|
|
}
|
|
else if(n.coefficient == 1 && n.exponent == 1) {
|
|
os << n.variable;
|
|
}
|
|
else if(n.exponent == 1) {
|
|
os << n.coefficient << " " << n.variable;
|
|
}
|
|
else if(n.coefficient == 1) {
|
|
os << n.variable << " ^ " << n.exponent;
|
|
}
|
|
else {
|
|
os << n.coefficient << " " << n.variable << " ^ " << n.exponent;
|
|
}
|
|
return os;
|
|
}
|