用C++实现一元多项式的四则运算包括数据的文件导入与导出

来源:互联网 发布:淘宝店铺最迟多久发货 编辑:程序博客网 时间:2024/05/19 12:26

对于一元多项式我们都再熟悉不过了,这是使用数据结构的单链表进行实现的,下面贴一下我的实现代码,欢迎批评指导

//test.h#pragma once#include<iostream>#include<fstream>using namespace std;class P_Sum{public: typedef struct P_node{double coef;int index;P_node* next;}P_node;P_Sum():head(BuyNode(-1,-1)){}~P_Sum(){clean();Freenode(head);}void clean(){P_node*p=head->next;P_node*q =NULL;while(p != NULL){ q=p->next;delete p;p=q;}head->next=NULL;}void push_front(double coef=0.0, int index=0);void push_back(double coef=0.0,int inex=0);void add_Poly_list(const P_Sum &poly1,const P_Sum&poly2);int Max_index()const;void Multiply_Poly(const P_Sum &poly1, const P_Sum&poly2);void Sub_Poly_list(const P_Sum &poly1,const P_Sum&poly2);void sort();void Load();void DownLoad();void readin();friend istream& operator>>(istream& in,P_Sum& sum){int coef=0;int index=0;P_node*p=sum.head->next;while(in>>coef>>index && index != -1){    sum.push_back(coef,index);}return in;}friend ostream& operator<<(ostream& out,P_Sum& sum){ P_node* p=sum.head->next;if(p == NULL){out<<"not exist\n";return out;}while(p != NULL){if(p->index == 0){out<<p->coef;}else if(p->coef == 1){out<<"X^"<<p->index;}else{  out<<p->coef<<"X^"<<p->index;}p = p->next;if(p!=NULL && p->coef>0){out<<"+";}}out<<"\n";return out;}protected:P_node* BuyNode(double coef=0.0, int index=0){P_node* p = new P_node;p->coef = coef != 0.0 ? coef : 0.0;p->index = coef != 0 ? index : 0;p->next =NULL;return p;}void Freenode(P_node *p){delete p;p = NULL;}void insert(P_node*&ptr){P_node*p = head;while (p->next != NULL && p->next->index > ptr->index){p = p->next;}ptr->next=p->next;p->next = ptr;return;}private: P_node*head;};

#include"test.h"void P_Sum::push_front(double coef, int index){P_node*p = BuyNode(coef, index);p->next = head->next;head->next = p;}void P_Sum::push_back(double coef,int index){P_node*s=BuyNode(coef,index);P_node *p = head;while(p->next != NULL)p = p->next;p->next = s;s->next = NULL;}int P_Sum::Max_index()const{P_node *p = head->next;int tmp = p->index;P_node* q = p->next;while (q != NULL){tmp = tmp > q->index ? tmp : q->index;q = q->next;}return tmp != 0 ? tmp : -1;}void P_Sum::add_Poly_list(const P_Sum &poly1,const P_Sum&poly2){P_node* p = poly1.head->next;P_node* q = poly2.head->next;P_node* ptmp = NULL;while (p != NULL && q != NULL){if (p->index == q->index){double  tmp = p->coef + q->coef;push_back(tmp, p->index);p = p->next;q = q->next;if(tmp <1e6) {continue; }}else if (p->index <q->index){push_back(q->coef, q->index);q = q->next;}else{push_back(p->coef, p->index);p = p->next;}}if (p != NULL)ptmp = q;elseptmp = p;while (ptmp!= NULL){push_back(ptmp->coef, ptmp->index);ptmp = ptmp->next;}}void P_Sum::Sub_Poly_list(const P_Sum &poly1,const P_Sum&poly2){P_node* p = poly1.head->next;P_node* q = poly2.head->next;if(q == NULL || p == NULL){cout<<"poly1 or poly2 is empty\n";return ;}P_node* ptmp = NULL;while (p != NULL && q != NULL){if (p->index == q->index){double  tmp = p->coef - q->coef;push_back(tmp, p->index);p = p->next;q = q->next;if(tmp <1e6) {continue; }}else if (p->index >q->index){push_back(p->coef, p->index);p = p->next;}else{push_back(-(q->coef), q->index);q = q->next;}}while (p!= NULL){push_back(ptmp->coef, ptmp->index);p= p->next;}while(q!=NULL){push_back(-(q->coef), q->index);q= q->next;}} void P_Sum::Multiply_Poly(const P_Sum &poly1, const P_Sum&poly2){P_node*p = poly1.head->next;P_node*q = poly2.head->next;int a = poly1.Max_index();int b = poly2.Max_index();int tmp = 0;if (a != -1 && b != -1){tmp = a + b + 1;}double* dp = new double[tmp];for (int i = 0; i< tmp; ++i){dp[i] = 0.0;}while (p != NULL){while (p != NULL){int k = p->index + q->index;dp[k] += p->coef * q->coef;p = p->next;}q = q->next;}for (int i = 0; i <= a+b; ++i){if (dp[i] != 0)push_front(dp[i], i);}delete[]dp;}void P_Sum::sort(){P_node *p = head->next;P_node *q = p->next;p->next = NULL;P_node *h = NULL;while (q != NULL){h = q->next;insert(q);q = h;}}void P_Sum::Load(){cout<<"请输入要存入的文件名称: ";char filename[100]={0};cin>>filename;ofstream outfile(filename,ios::out);//定义文件流对象,打开磁盘文件"f1.dat"   if(!outfile)                        //如果打开失败,outfile返回值   {      cerr<<"open error!"<<endl;      exit(1);   }cout<<"输入项数: ";int n;cin>>n;double*a=new double[n];int*b=new int[n];   for(int i=0;i<n;i++)   {   cout<<"请输入第"<<i+1<<"组数据: ";        cin>>a[i]>>b[i];push_back(a[i],b[i]);        outfile<<a[i]<<" "<<b[i]<<" ";   }            //向磁盘文件"f1.dat"输出数据   outfile.close();}void P_Sum::DownLoad(){cout<<"请输入要存入的文件名称: ";char filename[100]={0};cin>>filename;ofstream outfile(filename,ios::out);//定义文件流对象,打开磁盘文件"f1.dat"   if(!outfile)                        //如果打开失败,outfile返回值   {      cerr<<"open error!"<<endl;      exit(1);   }    P_node* p=head->next;if(p == NULL){outfile<<"not exist\n";return ;}while(p != NULL){if(p->index == 0){outfile<<p->coef;}else if(p->coef == 1){outfile<<"X^"<<p->index;}else{  outfile<<p->coef<<"X^"<<p->index;}p = p->next;if(p!=NULL && p->coef>0){outfile<<"+";}}outfile<<"\n";}void P_Sum:: readin(){cout<<"请输入要导入的文件名称: ";char filename[100]={0};cin>>filename;              //定义输入文件流对象,以输入方式打开磁盘文件f1.dat   ifstream infile(filename,ios::in);   if(!infile)   {      cerr<<"open error!"<<endl;      exit(1);   }   char c;   int n;   infile>>n>>c;   double*a=new double[n];   int *b = new int[n];   for(int i=0;i<n;i++)   {      infile>>a[i]>>b[i];  //从磁盘文件读入10个整数,顺序存放在a数组中      push_back(a[i],b[i]); //在显示器上顺序显示10个数   }            infile.close();}

#include "test.h"int main(){P_Sum pa;P_Sum pb;P_Sum pc;int select = 1;while(select){printf("*******************************************\n");printf("* [1] 创建多项式pa [2] 创建多项式pb       *\n");printf("* [3] 显示多项式pa [4] 显示多项式pb       *\n");printf("* [5] 多项式相加   [6] 多项式相减         *\n");printf("* [7] 多项式相乘   [8] pa存入文件         *\n");printf("* [9] pb存入文件   [10] 操作的结果存入文件*\n");printf("* [11] 文件导入pa  [12] 文件导入pb        *\n");printf("* [13] 清空多项式pa[14] 清空多项式pb      *\n");printf("* [15] 排序pa      [16]  排序pa           *\n");        printf("* [0] 退出                                *\n");printf("*******************************************\n");printf("请选择:>");scanf("%d",&select);if(select == 0)break;switch(select){case 1:cout<<"请输入系数和指数(-1 -1 表示结束)\n";cin>>pa;break;case 2:cout<<"请输入系数和指数(-1 -1 表示结束)\n";cin>>pb;break;case 3:cout<<pa;break;case 4:cout<<pb;break;case 5:pc.add_Poly_list(pa,pb);pc.sort();cout<<pc;//pc.clean();break;case 6:pc.Sub_Poly_list(pa,pb);pc.sort();cout<<pc;//pc.clean();break;case 7:pc.Multiply_Poly(pa,pb);cout<<pc;//pc.clean();break;case 8:pa.Load();break;case 9:pb.Load();break;case 10:cout<<pc;pc.DownLoad();break;case 11:pa.readin();break;case 12:pb.readin();break;case 13:pa.clean();break;case 14:pb.clean();break;case 15:pa.sort();break;case 16:pb.sort();break;default:break;}}return 0;}


0 0
原创粉丝点击