[数据结构]Polynomial

//Term.h
#pragma once
class Term
{
public:
	Term();
	int exp;
	double coe;
	Term(int exp, double coe);
};

//Term.cpp
#include"Term.h"
#include
using namespace std;

Term::Term() {
	exp = 0;
	coe = 0.0;
}

Term::Term(int exp, double coe){
	this->exp = exp;
	this->coe = coe;
}

//Extended_queue.h
#pragma once
enum Error_code { success, overflow, underflow };
template
class Extend_Queue
{
public:
	Extend_Queue();
	Extend_Queue(const Extend_Queue &original);
	~Extend_Queue();
	void operator=(const Extend_Queue & original);
	Error_code append(const Queue_entry &item);
	Error_code serve();
	Error_code retriver(Queue_entry &item)const;
	Error_code serve_and_retrieve(Queue_entry &item);
	bool empty()const;
	void clear();
	int size();
protected:
	struct Node
	{
		Node();
		Node(Queue_entry item, Node *add_on = NULL);
		Node *next;
		Queue_entry entry;
	};
	int count;

	Node *front;
	Node  *rear;
};

template
inline Extend_Queue::Node::Node()
{
	next = NULL;
}

template
inline Extend_Queue::Node::Node(Queue_entry item, Node * add_on)
{
	entry = item;
	next = add_on;
}

//Extended_queue.cpp
#include
#include"Extend_Queue.h"
using namespace std;

template
Extend_Queue::Extend_Queue() {
	count = 0;
	Node *front = NULL;
	Node *rear = NULL;
}

template
Extend_Queue::Extend_Queue(const Extend_Queue & original)
{
	Node *temp = original.front;
	if (front == original.front)return;
	while (temp) {
		append(temp->entry);
		temp = temp->next;
	}
}

template
Extend_Queue::~Extend_Queue()
{
	clear();
}

template
void Extend_Queue::operator=(const Extend_Queue & original){
	Node *temp = original.front;
	if (front == original.front)return;
	while (temp) serve();
	while (temp) {
		append(temp->entry);
		temp = temp->next;
	}
}

template
Error_code Extend_Queue::append(const Queue_entry & item)
{
	if (count == 0) {
		front = new Node(item);
		rear = front;
		count++;
		return success;
	}
	else {
		rear->next = new Node(item);
		rear = rear->next;
		count++;
		return success;
	}
}

template
Error_code Extend_Queue::serve()
{
	if (count == 0)return underflow;
	if (count == 1) {
		rear = NULL;
		delete front;
		front = NULL;
		count--;
		return success;
	}
	Node *temp = front;
	front = front->next;
	delete temp;
	count--;
	return success;
}

template
Error_code Extend_Queue::retriver(Queue_entry & item) const
{
	if (empty())return underflow;
	item = front->entry;
	return success;
}

template
Error_code Extend_Queue::serve_and_retrieve(Queue_entry & item)
{
	if (count == 0)return underflow;
	retriver(item);
	serve();
	return success;
}

template
bool Extend_Queue::empty() const
{
	if (count == 0)return true;
	else return false;
}

template
void Extend_Queue::clear()
{
	while (count) serve();
}

template
int Extend_Queue::size()
{
	return count;
}


//Polynomial.h
#pragma once
#include"Extend_Queue.cpp"
#include"Term.h"
class Polynomial:private Extend_Queue
{
public:
	Polynomial();
	Polynomial::Polynomial(const Polynomial &original);
	void operator=(const Polynomial &original);
	void read();
	void print()const;
	void equals_sum(Polynomial p, Polynomial q);
	void equals_Difference(Polynomial p, Polynomial q);
	void equals_product(Polynomial p, Polynomial q);
	Error_code equals_quotient(Polynomial p, Polynomial q);
	int degree()const;

private:
	void mult_term(Polynomial p, Term t);
};

//Polynomial.cpp
#include "Polynomial.h"
#include
#include"Term.h"
using namespace std;

Polynomial::Polynomial()
{
	front = NULL;
	rear = NULL;
}

Polynomial::Polynomial(const Polynomial & original)
{
	Node *temp = original.front;
	if (front == original.front)return;
	while (temp) {
		append(temp->entry);
		temp = temp->next;
	}
}

void Polynomial::operator=(const Polynomial & original)
{
	if (front == original.front)return;
	while (!empty()) serve();
	Node *temp = original.front;
	while (temp) {
		append(temp->entry);
		temp = temp->next;
	}
}

void Polynomial::read()
{
	double coe;
	bool first_term = 1;
	int last_exp, exp;
	cout << "请根据提示分别输入系数和指数;" << endl;
	cout << "请化简后按照指数由高到低输入,如果出现后一个指数>=前一个指数,自动停止输入。" << endl;
	cout << "输入系数为0或者指数为0时同样停止输入" << endl;
	do
	{
		cout << "请输入系数:";
		cin >> coe;
		if (coe != 0) {
			cout << "请输入指数:";
			cin >> exp;
			if (!first_term&&exp >= last_exp || exp < 0) {
				exp = 0;
				cout << "读取结束,最后一个数据丢失。" << endl;
			}
			else {
				Term new_term(exp,coe);
				append(new_term);
				first_term = false;
				cout << "输入系数:" << coe << " 指数:" << exp << "成功!" << endl;
			}
			last_exp = exp;
		}
	} while (coe!=0.0&&exp!=0);
	cout << "输入结束。" << endl;
}

void Polynomial::print() const
{
	Node *current_node = front;
	bool first_term = true;
	while (current_node){
		Term current_term = current_node->entry;
		if (first_term) {									//打印符号
			first_term = false;
			if (current_term.coe < 0)cout << '-';
		}
		else if (current_term.coe >= 0)cout << '+';
		else  cout << '-';

		double r = (current_term.coe >= 0) ? current_term.coe : -current_term.coe;
		if (current_term.coe != 0) {
			if (r != -1 && r != 1)cout << r;
			if (current_term.exp == 1)cout << "X";
			if (current_term.exp > 1)cout << "X^" << current_term.exp;
			if ((r == 1 || r == -1) && current_term.exp == 0)cout << r;
		}
		else cout << 0;
		current_node = current_node->next;
	}
	if (first_term)cout << 0;
	cout << endl;
}

void Polynomial::equals_sum(Polynomial p, Polynomial q)
{
	clear();
	while (!p.empty()||!q.empty()){
		Term p_term, q_term;
		if (p.degree() > q.degree()) {
			p.serve_and_retrieve(p_term);
			append(p_term);
		}
		else if (q.degree() > p.degree()) {
			q.serve_and_retrieve(q_term);
			append(q_term);
		}
		else {
			p.serve_and_retrieve(p_term);
			q.serve_and_retrieve(q_term);
			Term result(p_term.exp, p_term.coe + q_term.coe);
			if (result.coe != 0)append(result);
		}
	}
}

void Polynomial::equals_Difference(Polynomial p, Polynomial q)
{
	clear();
	while (!p.empty() || !q.empty()) {
		Term p_term, q_term;
		if (p.degree() > q.degree()) {
			p.serve_and_retrieve(p_term);
			append(p_term);
		}
		else if (q.degree() > p.degree()) {
			q.serve_and_retrieve(q_term);
			Term new_term(q_term.exp, -q_term.coe);
			append(new_term);
		}
		else {
			p.serve_and_retrieve(p_term);
			q.serve_and_retrieve(q_term);
			Term new_term(q_term.exp, p_term.coe-q_term.coe);
			append(new_term);
		}
	}
}

void Polynomial::equals_product(Polynomial p, Polynomial q)
{
	clear();
	Polynomial current_result,previous_result,finate_result;
	while (!q.empty()) {
		Term q_term;
		q.serve_and_retrieve(q_term);
		previous_result.mult_term(p, q_term);
		finate_result.equals_sum(previous_result, current_result);
		current_result = finate_result;
	}
	while (!finate_result.empty()) {			//存储运算结果
		Term result_term;
		finate_result.serve_and_retrieve(result_term);
		append(result_term);
	}
}

Error_code Polynomial::equals_quotient(Polynomial p, Polynomial q)
{
	clear();
	bool indivisible_flag = false;		//标明是否除尽
	if (q.empty())return overflow;
	if (q.degree() > p.degree()) return underflow;
	Polynomial mult_temp,differ_temp;
	Term p_term, q_term, divides_term;
	differ_temp = p;
	do {
		differ_temp.retriver(p_term);
		q.retriver(q_term);
		divides_term.coe = p_term.coe / q_term.coe;//确定当前商的系数和指数。
		divides_term.exp = p_term.exp - q_term.exp;
		if (divides_term.exp < 0)indivisible_flag = true;
		mult_temp.mult_term(q, divides_term);//将当前商与除数相乘
		differ_temp.equals_Difference(differ_temp, mult_temp);//做差
		if(divides_term.coe>=0) append(divides_term);//append当前商
		Term temp;
		differ_temp.retriver(temp);
		while (temp.coe == 0 || temp.exp < 0) {//消除非法项,防止print出错
			differ_temp.serve();
			if (differ_temp.retriver(temp) == underflow) break;
		}
		} while (!differ_temp.empty() && p_term.exp >= q_term.exp);
	if (indivisible_flag)
		cout << "It's indivisible.The remainder is abandoned" << endl;
	return success;
}

int Polynomial::degree() const
{
	if (empty())return -1;
	Term temp;
	retriver(temp);
	return temp.exp;
}

void Polynomial::mult_term(Polynomial p, Term t)
{
	clear();
	if (t.coe == 0) {
		clear();
		return;
	}
	while (!p.empty()) {
		Term current_term;
		p.serve_and_retrieve(current_term);
		Term result(current_term.exp + t.exp, current_term.coe*t.coe);
		append(result);
	}
}

//Linked_stack.h
#pragma once
enum Error_Code { Success, Underflow, Overflow };
template
class Linked_Stack
{
public:
	Linked_Stack();
	bool empty()const;
	Error_Code pop();
	Error_Code top(Stack_entry &item)const;
	Error_Code push(const Stack_entry &item);
	~Linked_Stack();
	void operator=(const Linked_Stack &original);
	Linked_Stack(const Stack_entry &item);
	void print();
private:
	struct Node
	{
		Node();
		Node(const Stack_entry &item, Node *add_on = NULL);
		Node *next;
		Stack_entry entry;
	};
	Node *head;
};

template
inline Linked_Stack::Node::Node()
{
	next = NULL;
}

template
inline Linked_Stack::Node::Node(const Stack_entry & item, Node * add_on)
{
	next = add_on;
	entry = item;
}


//Linked_stack.cpp
#include
#include"Linked_Stack.h"
using namespace std;

template
Linked_Stack::Linked_Stack() {
	head = NULL;
}

template
bool Linked_Stack::empty() const
{
	if (!head)return true;
	else return false;
}

template
Error_Code Linked_Stack::pop()
{
	if (!head)return Underflow;
	Node *temp = head;
	head = head->next;
	delete temp;
	return Success;
}

template
Error_Code Linked_Stack::top(Stack_entry & item) const
{
	if (!head)return Underflow;
	item = head->entry;
	return Success;
}

template
Error_Code Linked_Stack::push(const Stack_entry & item)
{
	head = new Node(item, head);
	return Success;
}

template
Linked_Stack::~Linked_Stack()
{
	while (head) {
		Node *temp = head;
		head = head->next;
		delete temp;
	}
}

template
void Linked_Stack::operator=(const Linked_Stack & original)
{
	Node *temp = original.head;
	Linked_Stack  middle;
	if (head == original.head)return;
	while (temp) {
		middle.push(temp->entry);
		temp = temp->next;
	}
	while (head) pop();
	temp = middle.head;
	while (temp) {
		push(temp->entry);
		temp = temp->next;
	}
}
template
Linked_Stack::Linked_Stack(const Stack_entry & original)
{
	Node *new_head, *new_temp, *original_temp = original.head;
	if (original == NULL) { new_head == NULL; return; }
	new_temp = new_head = new Node(original_temp->entry);
	while (original_temp) {
		original_temp = original_temp->next;
		new_temp->next = new Node(original_temp->entry);
		new_temp = new_temp->next;
	}
}

template
void Linked_Stack::print()
{
	Node *temp = head;
	while (temp) {
		cout << temp->entry << ' ';
		temp = temp->next;
	}
	cout << endl;
}

//main.cpp
#include
#include"Polynomial.h"
#include"Linked_Stack.cpp"
void UI() {
	cout << "**********************************Polynomial Calc************************************" << endl;
	cout << "MENU:" << endl;
	cout << "[?].Enter a Polynomial." << endl;
	cout << "[+].Pop two Polynomial and add them." << endl;
	cout << "[-].Pop two Polynomial and minium them." << endl;
	cout << "[/].Pop two Polynomial and divide them." << endl;
	cout << "[*].Pop two Polynomial and multiply them," << endl;
	cout << "[=].Show current result." << endl;
	cout << "[q].To quit the programm." << endl;
}
char get_command() {
	cout << "Please enter the command." << endl;
	char command;
	cin >> command;
	while (command != '?'&&command != '+'&&command != '-'&&command != '*'
		&&command != 'q'&&command != '='&&command != '/') {
		cout << "Invalid enter.please confirm and enter again." << endl;
		cin >> command;
	}
	return command;
}
bool do_command(char command, Linked_Stack  &Store_Polynomials) {
	switch (command)
	{
	case '?': {
		Polynomial current;
		current.read();
 		Store_Polynomials.push(current);
		cout << "Operate [?] Executed successfully" << endl;
		Store_Polynomials.top(current);
		current.print();
		system("pause");

		system("cls");
		UI();
		break;
	}
	case'+': {
		if (Store_Polynomials.empty())cout << "Stack empty.Command Terminate." << endl;
		else {
			Polynomial p, q, r;
			Store_Polynomials.top(p);
			Store_Polynomials.pop();
			if (Store_Polynomials.empty()) {
				cout << "Stack has just one Polynomial.Command Terminate." << endl;
				Store_Polynomials.push(p);
			}
			else {
				Store_Polynomials.top(q);
				Store_Polynomials.pop();
				r.equals_sum(p, q);
				Store_Polynomials.push(r);
				cout << "Operate [+] Executed successfully" << endl;
			}
		}
		system("pause");
		system("cls");
		UI();
		break;
	}
	case'=': {
		if (Store_Polynomials.empty())cout << "Stack Empty. Command Terminal." << endl;
		else {
			Polynomial out;
			Store_Polynomials.top(out);
			out.print();
		}
		system("pause");
		system("cls");
		UI();
		break;
	}
	case'-': {
		if (Store_Polynomials.empty())cout << "Stack empty.Command Terminate." << endl;
		else {
			Polynomial p, q, r;
			Store_Polynomials.top(p);
			Store_Polynomials.pop();
			if (Store_Polynomials.empty()) {
				cout << "Stack has just one Polynomial.Command Terminate." << endl;
				Store_Polynomials.push(p);
			}
			else {
				Store_Polynomials.top(q);
				Store_Polynomials.pop();
				r.equals_Difference(p, q);
				Store_Polynomials.push(r);
				cout << "Operate [-] Executed successfully" << endl;
			}
		}
		system("pause");
		system("cls");
		UI();
		break;
	}
	case'q': {
		cout << "You'll quit the programm." << endl;
		system("pause");
		return false;
	}
	case'/': {
		if (Store_Polynomials.empty())cout << "Stack empty.Command Terminate." << endl;
		else {
			Polynomial p, q, r;
			Store_Polynomials.top(q);
			Store_Polynomials.pop();
			if (Store_Polynomials.empty()) {
				cout << "Stack has just one Polynomial.Command Terminate." << endl;
				Store_Polynomials.push(q);
			}
			else {
				Store_Polynomials.top(p);
				Store_Polynomials.pop();
				r.equals_quotient(p, q);
				Store_Polynomials.push(r);
				cout << "Operate [/] Executed successfully" << endl;
			}
		}
		system("pause");
		system("cls");
		UI();
		break;
	}
	case'*': {
		if (Store_Polynomials.empty())cout << "Stack empty.Command Terminate." << endl;
		else {
			Polynomial p, q, r;
			Store_Polynomials.top(p);
			Store_Polynomials.pop();
			if (Store_Polynomials.empty()) {
				cout << "Stack has just one Polynomial.Command Terminate." << endl;
				Store_Polynomials.push(p);
			}
			else {
				Store_Polynomials.top(q);
				Store_Polynomials.pop();
				r.equals_product(p, q);
				Store_Polynomials.push(r);
				cout << "Operate [*] Executed successfully" << endl;
			}
		}
		system("pause");
		system("cls");
		UI();
		break;
	}
	}
	return true;
}
void main() {
	Linked_Stack Store_Polynomials;
	UI();
	while (do_command(get_command(), Store_Polynomials));

}

你可能感兴趣的:([数据结构]Polynomial)