#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include "polynomial.h"
term polynomial::termArray[200];
int polynomial::free =0;
polynomial::polynomial(char name){
	start = free;
	setpoly = name;
}
void polynomial::polyinput(){
	
	int sel;
	float c;
	int e;
	int size;
	cout << "Now start polynomial input" << endl;
	while(1){
		cout << "1. Input   " << "2. Done" << endl;
		cout << "Select number : ";
		cin >> sel;
		if(sel==1){
			cout << "How many?? : ";
			cin >> size;
			cout << "Start input " << setpoly << "(x)" << endl;
			for(int i=0; i<size; i++){
				cout << "Input coef : ";
				cin >> c;
				cout << "Input expo : ";
				cin >> e;
				newterm(c, e);
			}
			finish = free - 1;
			break;
		}
		else if(cin.fail()){
			cin.clear();
			cin.ignore(512, '\n');
			cout << "Wrong select" << endl;
		}
		else if(sel==2){
			finish = free - 1;
			break;
		}
		else{
			cout << "Wring selsct" << endl;
		}
	}
	cout << start << endl;
}
void polynomial::newterm(float c, int e){
	if(free >= 200){
		cout << "Too many terms in polynomials" << endl;
		return;
	}
	termArray[free].coef = c;
	termArray[free].exp = e;
	free++;
}
void polynomial::printpoly(){
	cout << setpoly << "(x) : ";
	for(int i=start; i<=finish; i++){
		cout << termArray[i].coef << "x^" << termArray[i].exp;
		if(i != finish){
			cout << " + ";
		}
		else{
			cout << endl;
		}
	}
}
polynomial polynomial::addpoly(polynomial B){
	polynomial C('C');
	int Ast = start; int Bst = B.start; float c;
	while((Ast<=finish) && (Bst <= B.finish)){
		switch(compare(termArray[Ast].exp, termArray[Bst].exp)){
		case '=':
			c = termArray[Ast].coef + termArray[Bst].coef;
			if(c){
				newterm(c, termArray[Ast].exp);
			}
			Ast++;
			Bst++;
			cout << Ast << "   " << Bst << endl;
			break;
		case '<':
			newterm(termArray[Bst].coef, termArray[Bst].exp);
			Bst++;
			cout << Bst << endl;
			break;
		case '>':
			newterm(termArray[Ast].coef, termArray[Ast].exp);
			Ast++;
			cout << Ast << endl;
		}
	}
	for(; Ast<=finish; Ast++){
		newterm(termArray[Ast].coef, termArray[Ast].exp);
	}
	for(; Bst<=B.finish; Bst++){
		newterm(termArray[Bst].coef, termArray[Bst].exp);
	}
	C.finish = free - 1;
	return C;
}
char polynomial::compare(int cmp1, int cmp2){
	if(cmp1 == cmp2)
		return '=';
	else if(cmp1 > cmp2)
		return '>';
	else
		return '<';
}
polynomial::~polynomial(){}
// 항공대 09년도 자료구조 수강인원은 Copy 가 나올수 있으니 주의바람 04오세혁(http://tigernet.tistory.com)