利用链表实现多项式求值

闲来无事,写了一个多项式之间的加减乘除

// 多项式.cpp : 定义控制台应用程序的入口点。
//

#include "iostream"
#include <stdlib.h>
#include "iostream"
#include "string"
#include "stdafx.h"
using namespace std;

class X{
public :
	X();
	X* build();
	X* auto_build(int n);
	X* multiplication(X *x1, X *x2);
	X* division(X *x1, X *x2);
	X* addition(X *x1, X *x2);
	X* subtraction(X *x1, X *x2);
	void show();
	friend	istream & operator >> (istream &, X *);
	friend  ostream & operator << (ostream &, X *);
	friend	istream & operator >> (istream &, X *x);
	friend  ostream & operator << (ostream &output, X *x);
private:	
	X* next;
	X* other;
	int exponent;
	double coefficient;
	int weight;
};

ostream & operator << (ostream &output, X *x){
	if (x != NULL){
		if (x->coefficient == 0){
			return output;
		}else if (x->coefficient == 1){
			if (x->exponent != 0){
				cout << "+";
			}else{
				cout <<"+"<< 1;
			}		
		}else if (x->coefficient > 1){
			cout << "+" << x->coefficient;
		}else if (x->coefficient == -1){
			cout << "-" ;
		}else{
			cout << x->coefficient;
		}
		if (x->exponent == 0){
		}else if (x->exponent == 1){
			cout << "x";
		}else{
			cout << "x^" << x->exponent;
		}
	}
	return output;
}

X::X(){
	next = NULL;
	other = NULL;
	exponent = 0;
	coefficient = 0;
	weight = 0;
}

X* X::build(){ //输入多项式
	X *head, *p1,*p2;
	p2 = NULL;
	head = new X;
	head->other = NULL;
	head->next = new X;
	cout << "请输入该多项式的最高次数" << endl;
	cin >> head->weight;
	p1 = head->next;
	for (int i = 0; i <= head->weight; i++){
		p1->exponent = (head->weight) - i;
		p1->weight = p1->exponent;
		cout << "请输入x^" << (head->weight)-i << "的系数:" ;
		cin >> p1->coefficient;
		cout << endl;
		p1->next = new X;                     
		p2 = p1;
		p1 = p1->next;
	}
	p2->next = NULL;
	head->show();
	return head;
}

X* X::auto_build(int n){ //自动创建n个结点的空链表
	X *head, *p1, *p2;
	int i;
	p2 = NULL;
	head = new X;
	head->other = NULL;
	head->next = new X;
	head->weight = n;
	p1 = head->next;
	for ( i = 0; i <= head->weight; i++){
		p1->exponent = (head->weight) - i;
		p1->weight = p1->exponent;
		p1->next = new X;
		p2 = p1;
		p1 = p1->next;
	}
	if (i == 0){
		head->next = NULL;
		return head;
	}else{
		p2->next = NULL;
		return head;
	}
}

X* X::multiplication(X *x1, X *x2){ // 乘法
	X *p1,*p2,*p3,*result;
	int n = x1->weight + x2->weight;
	result = auto_build(n);
	p1 = x1->next;
	p2 = x2->next;
	p3 = result->next;
	while (p2 != NULL){
		while (p1 != NULL){
			while (p3->exponent != p1->exponent + p2->exponent)
				p3 = p3->next;
			p3->coefficient = p3->coefficient + p1->coefficient * p2->coefficient;
			p1 = p1->next;
		}
		p1 = x1->next;
		p2 = p2->next;
		p3 = result->next;
	}
	cout << "积为" << endl;
	result->show();
	return result;
}

X* X::division(X *x1, X *x2){ //除法
	X *p1, *p2, *p3,*p4,*result;
	int i = 0;
	int n = x1->weight - x2->weight;
	if (n <= 0){
		cout << "这是个真分式,咱不能除" << endl;
		system("pause");
		return NULL;
	}
	result = auto_build(n);
	p1 = x1;
	p2 = x2;
	p3 = result;
	do{
		while (p1->next->exponent - p2->next->exponent != n-i)	p1 = p1->next;
		cout << p3->next->coefficient << "=" << p1->next->coefficient << "/" << p2->next->coefficient << endl;
		p3->next->coefficient = p1->next->coefficient / p2->next->coefficient;
		p1->show();
		cout << "-" << endl;
		p3->show();
		cout << "*" << endl;
		p2->show();
		p1 = subtraction(p1, multiplication(p3, p2));
		p3 = p3->next;
		i++;
	} while (p3->next != NULL);
	result->other = p1;
	result->show();
	return result;
}

X* X::addition(X *x1, X *x2){ //加法
	X *p[3];
	int n = x1->weight > x2->weight ? x1->weight : x2->weight;
	X *result = auto_build(n);
	p[2] = result->next;
	int j = x1->weight > x2->weight ? 0 : 1;//j为大
	int k = x1->weight > x2->weight ? 1 : 0;//k为小
	p[0] = x1->next;
	p[1] = x2->next;	
	for (int i = 0; i <= n; i++){
		if (p[j]->exponent == p[k]->exponent){
			p[2]->coefficient = p[j]->coefficient + p[k]->coefficient;
			p[j] = p[j]->next;
			p[k] = p[k]->next;
			p[2] = p[2]->next;
		}else{
			p[2]->coefficient = p[j]->coefficient;
			p[2] = p[2]->next;
			p[j] = p[j]->next;
		}
	}
	system("cls");
	cout << "和为" << endl;
	result->show();
	return result;
}

X* X::subtraction(X *x1, X *x2){ //减法
	int n = x1->weight > x2->weight ? x1->weight : x2->weight;
	X *result = auto_build(n);
	X *p1 = x1->next;
	X *p2 = x2->next;
	X *p3 = result->next;
	for (int i = 0; i <= n; i++){
		if (p1->exponent == p2->exponent){
			p3->coefficient = p1->coefficient - p2->coefficient;
			cout << p3 << endl;
			p1 = p1->next;
			p2 = p2->next;
			p3 = p3->next;
		}else{
			if (p1->exponent > p2->exponent){
				p3->coefficient = p1->coefficient;
				cout << p3 << endl;
				p3 = p3->next;
				p1 = p1->next;
			}else{
				p3->coefficient = 0 - p2->coefficient;
				p3 = p3->next;
				p2 = p2->next;
			}	
		}
	}
	cout << 差为" << endl;
	result->show();
	return result;
}

void X::show(){ //展示多项式
	X *p = next;
	while (p->coefficient == 0){
		p = p->next;
		if (p == NULL)
			break;
	}
	if (p==NULL){
		cout << "0" << endl;
	}else{
		if (p->coefficient == 1){
		}else if (p->coefficient > 1){
			cout << p->coefficient;
		}else if (p->coefficient == -1){
			cout << "-" ;
		}else{
			cout << p->coefficient;
		}
		if (p->exponent == 0){
		}else if (p->exponent == 1){
			cout << "x";
		}else{
			cout << "x^" << p->exponent;
		}
		p = p->next;
		while (p != NULL){
			cout << p;
			p = p->next;
		}
	}
	p = other;
	if (p != NULL&&p->coefficient!=0){
		cout << "+++++++++++++++++++++++";
		while (p != NULL){
			cout << p;
			p = p->next;
		}
	}
	cout << endl;
}

void menu(){
	cout << "****************************************************************" << endl;
	cout << "*                        1,加法                                *" << endl;
	cout << "*                        2,减法                                *" << endl;
	cout << "*                        3,乘法                                *" << endl;
	cout << "*                        4,除法                                *" << endl;
	cout << "*                        5,退出                                *" << endl;
	cout << "****************************************************************" << endl;
}

int main(){
	int panduan, switch_on;
	X *xx = new X;
	X *x1, *x2, *X;
	X = NULL;
	panduan = 1;
	cout << "请输入第一个多项式" << endl;
	x1 = xx->build();
	cout << endl;
	cout << "请输入第二个多项式" << endl;
	x2 = xx->build();
	while (panduan){
		system("cls");
		cout << endl;
		cout << "第一个多项式为:";
		x1->show();
		cout << endl;
		cout << "第二个多项式为:";
		x2->show();
		cout << endl;
		if (X != NULL){
			cout << "结果为" << endl;
			X->show();
			cout << endl;
		}		
		menu();
		cin >> switch_on;
		switch (switch_on){
			case 1:X = xx->addition(x1, x2);		break;//加法
			case 2:X = xx->subtraction(x1, x2);		break;//减法
			case 3:X = xx->multiplication(x1, x2);	break;//乘法
			case 4:X = xx->division(x1, x2);		break;//除法
			default:panduan = 0;					break;
		}
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值