Skip to content

Instantly share code, notes, and snippets.

@splitline
Created October 10, 2017 14:58
Show Gist options
  • Save splitline/1a12e1753e53826add6f0652cf6aeb38 to your computer and use it in GitHub Desktop.
Save splitline/1a12e1753e53826add6f0652cf6aeb38 to your computer and use it in GitHub Desktop.
#ifndef LIST_H
#define LIST_H
template <class T>
class List {
public:
List() {
this->size = 0;
this->capacity = 10;
this->array = new T[0]();
};
~List() {
delete [] array;
};
int len() {
return size;
}
void push(T val) {
(*this)[size] = val;
}
T & operator[] (int index) {
if (index >= size) {
this->capacity = size * 2;
T* newArr = new T[this->capacity]();
std::copy(this->array, this->array + size, newArr);
delete[] this->array;
this->array = newArr;
size = index + 1;
}
return array[index];
}
const T operator[] (int index) const {
if (index >= size) {
this->capacity = size * 2;
T* newArr = new T[this->capacity]();
std::copy(this->array, this->array + size, newArr);
delete[] this->array;
this->array = newArr;
size = index + 1;
}
return array[index];
}
List<T> merge(List<T> &rl) {
List<T> &ll = *this;
int lenL = ll.len(), lenR = rl.len();
T lval = ll[0], rval = rl[0];
List<T> retList;
for (int idxL = 0, idxR = 0; idxL < lenL || idxR < lenR;) {
if ((lval < rval && idxL < lenL) || idxR >= lenR) {
retList.push(lval);
lval = ll[++idxL];
} else if ((lval >= rval && idxR < lenR) || idxL >= lenL) {
retList.push(rvalk);
rval = rl[++idxR];
}
}
return retList;
}
private:
T *array;
int size;
int capacity;
};
#endif
#include <iostream>
#include "list.h"
#include "poly.h"
#include "spmatrix.h"
using namespace std;
int main() {
cout << "=====================List=====================" << endl;
List<int> a, b;
a[0] = 1; a[1] = 2; a[2] = 3; a[3] = 7; a[4] = 9; a[5] = 33; a[6] = 2000;
b.push(2);
b.push(6);
b.push(20);
b.push(300);
b.push(2001);
b.push(2002);
b.push(2004);
List<int> list = a.merge(b);
for (int i = 0; i < a.len(); i++) cout << a[i] << ", ";
cout << endl;
for (int i = 0; i < b.len(); i++) cout << b[i] << ", ";
cout << endl;
for (int i = 0; i < list.len(); i++) cout << list[i] << ", ";
cout << endl;
cout << endl;
cout << "==================Polynomial==================" << endl;
// Term x(2, 3), y(3, 4);
// x+y;
// Error Test
Poly p;
p.appendTerm(2, 1000);
p.appendTerm(8, 3);
p.appendTerm(1, 0);
cout << p;
cout << endl;
Poly q;
q.appendTerm(1,4);
q.appendTerm(10,3);
q.appendTerm(3,2);
q.appendTerm(1,0);
cout << q;
cout << endl;
Poly r = p.add(q);
cout << r;
cout << endl;
}
#ifndef POLY_H
#define POLY_H
#include "list.h"
class Poly;
class Term {
friend class Poly;
public:
Term() {
this -> coef = 0;
this -> exp = 0;
}
Term(double c, int e) {
this -> coef = c;
this -> exp = e;
}
Term operator+(Term y){
if(this -> exp == y.exp){
Term ret(this -> coef + y.coef, this -> exp);
return ret;
} else{
throw std::invalid_argument( "Can't add two terms with different exponent." );
}
};
friend std::ostream& operator<<(std::ostream& out, const Term& t){
out << t.coef << "*x^" << t.exp;
return out;
}
private:
double coef; // coefficient
int exp; // exponent
};
class Poly: public List<Term> {
public:
Poly() {};
~Poly() {};
void appendTerm(double c, int e){
Term t(c,e);
push(t);
}
Poly add(Poly &rPoly) {
Poly &lPoly = *this;
int lenL = lPoly.len(), lenR = rPoly.len();
Term lval = lPoly[0], rval = rPoly[0];
Poly retPoly;
for (int idxL = 0, idxR = 0; idxL < lenL || idxR < lenR;) {
if ((lval.exp < rval.exp && idxL < lenL) || idxR >= lenR) {
retPoly.push(rPoly[idxR]);
rval = rPoly[++idxR];
} else if ((lval.exp > rval.exp && idxR < lenR) || idxL >= lenL) {
retPoly.push(lPoly[idxL]);
lval = lPoly[++idxL];
} else if(lval.exp == rval.exp && idxL < lenL && idxR < lenR){
retPoly.push(lval + rval);
lval = lPoly[++idxL];
rval = rPoly[++idxR];
}
}
return retPoly;
}
friend std::ostream& operator<<(std::ostream& out, Poly& p){
for (int i = 0; i < p.len(); i++)
out << p[i] << (i == p.len()-1 ? "" :" + ");
return out;
}
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment