多项式【数据结构课设一枚,671原创】

帮别人写的。。先是在网上找了一个。。但是发现错误百出。。然后又找不到更好的。。于是就改了下。。。将就着看。。。完全c语言,。。。木有任何c++~~~~不过倒是很别扭。。。学了java之后。。。感觉自己被面向过程残害匪浅。。。。面向对象和多线程的思路完全屡不清头绪。。。。哎。。。ACM害我一生么难道?

主要实现了以下功能

            1. 多项式创建    
           2.
多项式相加    
           3.
多项式相减    
           4.
多项式相乘    
           5.
多项式求导    
           6.
代数求值      
           7.
积分计算      
           8.
打印多项式    
           9.
清空多项式     


以下是代码:

#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>
#include <math.h>
#define NULL 0
#define MAXSIZE 20				//定义线性表最大容量
//定义多项式项数据类型
typedef struct
{
	float coef;				//系数
	int expn;				//指数 
}term,elemType;

typedef struct
{
	term terms[MAXSIZE];			//线性表中数组元素
	int last;						//指向线性表中最后一个元素位置 
}SeqList;

typedef SeqList polynomial;
void printPloyn(polynomial*p);

int PloynStatus(polynomial*p)			//判断多项式的状态 
{
	if(p==NULL)
	{
		return -1;
	}
	else if(p->last==-1)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

polynomial* Init_Polynomial()				//初始化空的多项式
{
	polynomial*P;
	P=new polynomial;
	if(P!=NULL)
	{
		P->last=-1;
		return P;
	}
	else
	{
		return NULL;
	}
}

void Reset_Polynomial(polynomial*p)
{
	if(PloynStatus(p)==1)
	{
		p->last=-1;
	}
}

int Location_Element(polynomial*p,term x)			//在多项式p中查找与x项指数相同的项是否存在
{
	int i=0;
	if(PloynStatus(p) == -1)
		return 0;
	while(i<=p->last && p->terms[i].expn!=x.expn)
	{
		i++;
	}
	if(i > p->last)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

int Insert_ElementByOrder(polynomial*p,term x)			//在多项式p中插入一个指数项x
{
	int j;
	if(PloynStatus(p)==-1)
		return 0;
	if(p->last==MAXSIZE-1)
	{
		printf("The polym is full!\n");
		return 0;
	}
	j=p->last;
	while(p->terms[j].expn<x.expn && j>=0)
	{
		p->terms[j+1]=p->terms[j];
		j--;
	}
	p->terms[j+1]=x;
	p->last++;
	return 1;
}

int CreatePolyn(polynomial *P, int m)			//输入m项系数和指数,建立表示一元多项式的有序表p
{
	float coef;
	int expn;
	term x;
	if(PloynStatus(P) == -1)
		return 0;
	if(m > MAXSIZE)
	{
		printf("顺序表溢出\n");
		return 0;
	}
	else
	{
		printf("请依次输入%d对系数和指数...\n",m);
		for(int i=0;i<m;i++)
		{
			scanf("%f%d",&coef,&expn);
			x.coef = coef;
			x.expn = expn;
			if(!Location_Element(P,x))
			{
				Insert_ElementByOrder(P,x);
			}
		}
	}
	return 1;
}

char compare(term term1,term term2)			//比较指数项term1和指数项term2
{
	if(term1.expn>term2.expn)
	{
		return'>';
	}
	else if(term1.expn<term2.expn)
	{
		return'<';
	}
	else
	{
		return'=';
	}
}

polynomial*addPloyn(polynomial*p1,polynomial*p2)		//将多项式p1和多项式p2相加,生成一个新的多项式
{
	int i=0, j=0, k=0;
	if((PloynStatus(p1)==-1)||(PloynStatus(p2)==-1))
	{
		return NULL;
	}
	polynomial*p3 = Init_Polynomial();
	while(i <= p1->last && j <= p2->last)
	{
		switch(compare(p1->terms[i], p2->terms[j]))
		{
		case'>':
			p3->terms[k++] = p1->terms[i++];
			p3->last++;
			break;
		case'<':
			p3->terms[k++] = p2->terms[j++];
			p3->last++;
			break;
		case'=':
			if(p1->terms[i].coef + p2->terms[j].coef != 0)
			{
				p3->terms[k].coef = p1->terms[i].coef + p2->terms[j].coef;
				p3->terms[k].expn = p1->terms[i].expn;
				k++;
				p3->last++;
			}
			i++;
			j++;
		}
	}
	while(i<=p1->last)
	{
		p3->terms[k++] = p1->terms[i++];
		p3->last++;
	}
	while(j<=p2->last)
	{
		p3->terms[k++] = p2->terms[j++];
		p3->last++;
	}
	return p3;
}

polynomial*subStractPloyn(polynomial*p1,polynomial*p2)		//多项式p1和多项式p2相减,生成一个新的多项式
{
	int i=0;
	if((PloynStatus(p1)!=1)||(PloynStatus(p2)!=1))
	{
		return NULL;
	}
	polynomial*p3=Init_Polynomial();
	p3->last=p2->last;
	for(i=0;i<=p2->last;i++)
	{
		p3->terms[i].coef=-p2->terms[i].coef;
		p3->terms[i].expn=p2->terms[i].expn;
	}
	p3=addPloyn(p1,p3);
	return p3;
}

polynomial* mulitPloyn(polynomial*p1,polynomial*p2)		//多项式p1和多项式p2相乘,生成一个新的多项式
{
	int i=0, j, k;
	if((PloynStatus(p1)!=1)||(PloynStatus(p2)!=1))
	{
		return NULL;
	}
	polynomial*p3=Init_Polynomial();
	polynomial**p=new polynomial*[p2->last+1];
	for(k=0; k<=p2->last; k++)
	{
		p[k]=Init_Polynomial();
		p[k]->last=p1->last;
		for(j=0; j<=p1->last; j++)
		{
			p[k]->terms[j].coef = p1->terms[j].coef * p2->terms[k].coef;
			p[k]->terms[j].expn = p1->terms[j].expn + p2->terms[k].expn;
		}
		p3=addPloyn(p[k], p3);
	}
	return p3;
}

void printPloyn(polynomial*p)		//输出在顺序存储结构的多项式p
{
	int i;
	for(i=0;i<=p->last;i++)
	{
		if(p->terms[i].coef>0 && i>0)
			printf("+%.2f", p->terms[i].coef);
		else
			printf("%.2f", p->terms[i].coef);
		printf("x^%d", p->terms[i].expn);
	}
	puts("");
}

void menu()
{
	printf("\n\t\t*******数据结构综合性实验*********\n");
	printf("\t\t***一、多项式的加、减、乘法运算***\n");
	printf("\t\t*******  1.多项式创建    *********\n");
	printf("\t\t*******  2.多项式相加    *********\n");
	printf("\t\t*******  3.多项式相减    *********\n");
	printf("\t\t*******  4.多项式相乘    *********\n");
	printf("\t\t*******  5.多项式求导    *********\n");
	printf("\t\t*******  6.代数求值      *********\n");
	printf("\t\t*******  7.积分计算      *********\n");
	printf("\t\t*******  8.打印多项式    *********\n");
	printf("\t\t*******  9.清空多项式    *********\n");
	printf("\t\t*******  0.退出系统      *********\n");
	printf("\t\t******   请选择(0-9)      ********\n");
	printf("\t\t**********************************\n");
}

polynomial* derivative(polynomial* p1)		//多项式求导
{
	int i=0, k;
	if((PloynStatus(p1)!=1))
	{
		return NULL;
	}
	polynomial*p3=Init_Polynomial();
	polynomial*tmp;
	for(k=0; k<=p1->last; k++)
	{
		tmp=Init_Polynomial();
		tmp->last = 0;
		if (p1->terms[k].expn != 0)
		{
			tmp->terms[0].coef = p1->terms[k].expn * p1->terms[k].coef;
			tmp->terms[0].expn = p1->terms[k].expn - 1;
			p3=addPloyn(tmp, p3);
		}
	}
	return p3;
}

polynomial* Rederivative(polynomial* p1)		//多项式原函数(积分要用)。。。。。太麻烦了 *.*||
{
	int i=0, k;
	if((PloynStatus(p1)!=1))
	{
		return NULL;
	}
	polynomial*p3=Init_Polynomial();
	polynomial*tmp;
	for(k=0; k<=p1->last; k++)
	{
		tmp=Init_Polynomial();
		tmp->last = 0;
		tmp->terms[0].coef = p1->terms[k].coef / (float)(p1->terms[k].expn + 1);
		tmp->terms[0].expn = p1->terms[k].expn + 1;
		p3=addPloyn(tmp, p3);
	}
	return p3;
}

double getanswer(polynomial* p1, double x)		//求值
{
	double ans = 0.0;;
	int i=0, k;
	if((PloynStatus(p1)!=1))
	{
		return NULL;
	}
	for(k=0; k<=p1->last; k++)
	{
		ans += pow(x, p1->terms[k].expn) * p1->terms[k].coef;
	}
	return ans;
}

int choose()			//选择。。方便省代码。。
{
	int tmp;
	printf("\n请输入要操作的多项式编号(1/2):");
	scanf("%d", &tmp);
	if (tmp != 1 && tmp != 2)
	{
		puts("输入有误,结束操作。\n");
		return 0;
	}
	return tmp;
}

void main()
{
	int sel, tmp;
	double ans;
	polynomial*p1=NULL;
	polynomial*p2=NULL;
	polynomial*p3=NULL;
	while(1)
	{
		menu();
		printf("\t\t*请选择(0-9):");
		scanf("%d", &sel);
		switch(sel)
		{
		case 1:
			p1=Init_Polynomial();
			p2=Init_Polynomial();
			int m;
			printf("请输入第一个多项式的项数:\n");
			scanf("%d",&m);
			CreatePolyn(p1,m);
			printf("第一个多项式的表达式为p1=");
			printPloyn(p1);
			printf("请输入第二个多项式的项数:\n");
			scanf("%d",&m);
			CreatePolyn(p2,m);
			printf("第二个多项式的表达式为p2=");
			printPloyn(p2);
			break;
		case 2:
			printf("p1+p2=");
			if((p3 = addPloyn(p1,p2)) != NULL)
				printPloyn(p3);
			break;
		case 3:
			printf("\np1-p2=");
			if((p3 = subStractPloyn(p1,p2)) != NULL)
				printPloyn(p3);
			break;
		case 4:
			printf("\np1*p2=");
			if((p3 = mulitPloyn(p1,p2)) != NULL)
				printPloyn(p3);
			break;
		case 5:
			if (!(tmp = choose()))
				break;
			printf("p%d导数为:", tmp);
			switch(tmp)
			{
			case 1:
				if ((p3=derivative(p1)) != NULL)
					printPloyn(p3);
				break;
			case 2:
				if ((p3=derivative(p2)) != NULL)
					printPloyn(p3);
				break;
			}
			break;
		case 6:
			if (!(tmp = choose()))
				break;
			printf("请输入x的值:\n");
			double tmp2;
			scanf("%lf", &tmp2);
			switch(tmp)
			{
			case 1:
				ans = getanswer(p1, tmp2);
				printf("当x=%.2lf时,p1的值为:%.2lf\n", tmp2, ans);
				break;
			case 2:
				ans = getanswer(p2, tmp2);
				printf("当x=%.2lf时,p2的值为:%.2lf\n", tmp2, ans);
				break;
			}
			break;
		case 7:
			if (!(tmp = choose()))
				break;
			double a, b;
			printf("请输入下限与上限:");
			scanf("%lf %lf", &a, &b);
			if (a > b)
			{
				a = a + b;
				b = a - b;
				a = a - b;
			}
			if (tmp == 1)
				ans = getanswer(Rederivative(p1), b) - getanswer(Rederivative(p1), a);
			if (tmp == 2)
				ans = getanswer(Rederivative(p2), b) - getanswer(Rederivative(p2), a);
			printf("结果为:%.2lf", ans);
			break;
		case 8:
			if (!(tmp = choose()))
				break;
			printf("p%d=", tmp);
			if (tmp == 1)
				printPloyn(p1);
			if (tmp == 2)
				printPloyn(p2);
			break;
		case 9:
			Reset_Polynomial(p1);
			Reset_Polynomial(p2);
			Reset_Polynomial(p3);
			puts("\n清空成功!\n");
			getchar();
			printf("是否清空屏幕?y/n\t");
			char tmpa;
			scanf("%c", &tmpa);
			if (tmpa == 'y')
			{
				system("cls");
			}
			break;
		case 0:
			return;
		default:
			puts("输入错误,请重新输入:");
		}
	}
	return;
}


你可能感兴趣的:(多项式【数据结构课设一枚,671原创】)