数据结构课设:用链表实现多项式相加(c++) 500-800行左右

能附加一些别的操作更好,易懂,最好有解释跟在每行后面。急!!!
谢谢……

第1个回答  2010-06-09
//C++
//Polynomial objects in single variable

//class_poly_def.h

class Polynomial{
private:
typedef struct POLY_ELEMENT{
DATA_TYPE coefficient;
unsinged int power_of_x;
POLY_ELEMENT *next;
}*POLY_PTR;

POLY_PTR head_ptr;
void inline poly_obj_init(){ head_ptr==NULL;};
POLY_PTR allocate_poly_element(POLY_ELEMENT new_element);
POLY_PTR link_poly_element(POLY_PTR head_ptr,POLY_ELEMENT new_element,enum type_def type);
POLY_PTR read_poly_obj(int poly_number);
void key_to_continue(void);
public:
Polynomial(){ poly_obj_init();}
~Polynomial();

void build_poly_obj(int poly_num){ head_ptr=read_poly_obj(poly_num);}
void print_poly_obj(POLY_PTR head_ptr);
POLY_PTR get_poly_head(){ return head_ptr;};

friend Polynomial operator+(Polynomial poly_obj1,Polynomial poly_obj2);
friend Polynomial operator-(Polynomial poly_obj1,Polynomial poly_obj2);
friend Polynomial operator*(Polynomial poly_obj1,Polynomial poly_obj2);
friend Polynomial operator/(Polynomial poly_obj1,Polynomial poly_obj2);
friend void display_poly(Polynomial poly_obj1,Polynomial poly_obj2,Polynomial poly_obj_sum);

float evaluate_poly_obj(float x_value);
};

//polynoml.cpp

#include<stdio.h>
#include<stdlib.h>
#ifdef MSDOS
#include<conio.h>
#define CLRSCR() (clrscr())
#else
#define CLRSCR() (system("clear"))
#endif

typedef double DATA_TYPE;
const int FOREVER=1;

enum type_def{REPLACE,ADD}; //"type_def" is used by link_poly_element().

#include "class_poly_def.h"

Polynomial::~Polynomial()
{
POLY_PTR current_ptr=head_ptr,next_elt_ptr;

while(current_ptr!=NULL){
next_elt_ptr=current_ptr->next;
delete current_ptr;
current_ptr=next_elt_ptr;
}
}

Polynomial::POLY_PTR
Polynomial::allocate_poly_element(POLY_ELEMENT new_element)
{
POLY_PTR new_poly_elt_ptr=new POLY_ELEMENT;
if(new_poly_elt_ptr==NULL){
printf("\n ERROR:system memory is not available\n");
exit(-1);
}

new_poly_elt_ptr->coefficient=new_element.coefficient;
new_poly_elt_ptr->power_of_x=new_element.power_of_x;
new_poly_elt_ptr->next=NULL;
return (new_poly_elt_ptr);
}

Polynomial::POLY_PTR
Polynomial::read_poly_obj(int number)
{
POLY_ELEMENT new_element;
char buffer[25];

if(head_ptr!=NULL)
return head_ptr;

while(FOREVER){
CLRSCR(); //in conio.h

printf("OOP for single variable polynomial ");

print_poly_obj(head_ptr);

printf("\n coefficient:"
gets(buffer); //get coefficient of x
if(buffer[0]=='\0')
return (head_ptr);
new_element.coefficient=atof(buffer);
printf("power of x:");
gets(buffer); //get power of x
if(buffer[0]=='\0')
return (head_ptr);
new_element.power_of_x=atof(buffer);
head_ptr=link_poly_element(head_ptr, new_element,REPLACE);
}
}

void Polynomial::print_poly_obj(POLY_PTR head_ptr)
{
if(head_ptr==NULL)
return;
if(head_ptr->coefficient!=0)
printf("%.2f",head_ptr->coefficient);
if(head_ptr->power_of_x>0){
printf("x");
if(head_ptr->power_of_x>1)
printf("^%d",head_ptr->power_of_x);
}
}
if(head_ptr->next==NULL)
return;
else {
if(head_ptr->next->coefficient!=0)
printf("+");
print_poly_obj(head_ptr->next);
}
}

Polynomial::POLY_PTR
Polynomial::link_poly_element(POLY_PTR head_ptr,POLY_ELEMENT new_element,enum type_def type)
{
POLY_PTR new_poly_elt_ptr,previous_ptr,
current_ptr=head_ptr;

new_poly_elt_ptr=allocate_poly_element(new_element);

if(head_ptr==NULL){
head_ptr=new_poly_elt_ptr;
return head_ptr;
}

while((new_poly_elt_ptr->power_of_x<current_ptr->power_of_x)&&(current_ptr->next!=NULL){
previous_ptr=current_ptr;
current_ptr=current_ptr->next;
}

if(new_poly_elt_ptrf_x > current_ptr->power_of_x){
if(current_ptr==head_ptr){
new_poly_elt_ptr->next=current_ptr;
return (new_poly_elt_ptr);
}

else {
previous_ptr->next=new_poly_elt_ptr;
new_poly_elt_ptr->next=current_ptr;
return head_ptr;
}
}
else {
if(type==REPLACE)
current_ptr->coefficient=new_poly_elt_ptr->coefficient;
else //type==ADD,and new value to coefficient
current_ptr->coefficient+=new_poly_elt_ptr->coefficient;
delete new_poly_elt_ptr;
return head_ptr;
}
}

Polynomial operator+(Polynomial poly_obj1,Polynomial poly_obj2)
{
Polynomial poly_obj_sum;
Polynomial::POLY_PTR head_ptr_1=poly_obj1.head_ptr,
head_ptr_2=poly_obj2.head_ptr,
head_ptr_sum=NULL,
current_ptr;

current_ptr=head_ptr_1;

while(current_ptr!=NULL){
head_ptr_sum=poly_obj1.link_poly_element(head_ptr_sum,*current_ptr,ADD);
current_ptr=current_ptr->next;
}

current_ptr=head_ptr_2;
while(current_ptr!=NULL){
head_ptr_sum=poly_obj2.link_poly_element(head_ptr_sum,*current_ptr,ADD);
current_ptr=current_ptr->next;
}

poly_obj_sum.head_ptr=head_ptr_sum;
return (head_ptr_sum);
}

Polynomial operator+(Polynomial poly_obj1,Polynomial poly_obj2)
{ //ex
Polynomial sub;
return sub;
}

Polynomial operator*(Polynomial poly_obj1,Polynomial poly_obj2)
{ //ex
Polynomial mult;
return mult;
}

Polynomial operator/(Polynomial poly_obj1,Polynomial poly_obj2)
{ //ex
Polynomial divide;
return divide;
}

float Polynomial::evaluate_poly_obj(float x_value)
{ //ex
float result;
return result;
}

void Polynomial::key_to_continue(void)
{
printf("\n press any key to continue!");
#ifdef MSDOS
if(getch()==0) // read extended ASCII code
getch();
#else
getchar();
#endif
}

void display_poly(Polynomial poly_obj1,Polynomial poly_obj2,Polynomial poly_obj_sum)
{
CLRSCR();
printf("\n OOP for single variable polynomials");
printf("\n.................................\n");
printf("\npolynomial object 1:\n\n");
poly_obj1.print_poly_obj(poly_obj1.head_ptr);

printf("\npolynomial object 2:\n\n");
poly_obj2.print_poly_obj(poly_obj2.head_ptr);

prinf("\n\n sum of polynomial object 1 %s:\n\n","and polynomial object 2");
poly_obj_sum.print_poly_obj(poly_obj_sum.head_ptr);
printf("\n");
poly_obj1.key_to_continue();
}

//程序测试,自己弄
第2个回答  2010-06-09
////////////////////////////////////////////
///作者:陈辰
///日期:2009/04/20
///项目:一元稀疏多项式化简
///////////////////////////////////////////
#include <cstdlib>
#include <iostream>

using namespace std;
struct xiang
{
float xs,zs,x; //分别表示系数、指数、以及X
xiang *next;
};
class duoxiangshi
{
private:
xiang *first,*p;
public:
duoxiangshi();
~duoxiangshi();
void add(float xs,float zs); //n代表多项式的项数个数
xiang * operator+(const duoxiangshi & b); //重载多项式的相加
xiang * operator-(const duoxiangshi & b);
void print(xiang *athead); //输出多项式相加结果
void printself(); //看一下
};
duoxiangshi::duoxiangshi()
{
first=new xiang;
first->next=NULL;
p=first;
}
duoxiangshi::~duoxiangshi()
{
while(p=first)
{
first=first->next;
delete p;
}
}
void duoxiangshi::add(float xs,float zs) //增加多项式的项数,在插入的同时给它由指数的大小进行排序
{
xiang *q=new xiang;
xiang *tmp; //临时指针 。
q->xs=xs;
q->zs=zs;
p=first->next;
tmp = first; //链表的第一项不使用,所以防止p就是空的在这里记前一个,后面那个一一样。
while(p) //历遍p,找找有没有指数相同的
{
if(q->zs==p->zs) //假如指数相同那么只需要系数相加即可
{
p->xs+=q->xs;
delete q;
break;
}
if(p->zs > q->zs)
{
q->next=p;
tmp->next=q;
break;
}
tmp = p;
p=p->next;
}
if(p==NULL)//没有该项,所以挂到后面去。
{
tmp->next=q;//接上
q->next=NULL;//它为尾巴
}
}
xiang * duoxiangshi::operator+(const duoxiangshi & b)
{
xiang *q,*p;
q=b.first->next;
p=b.first;
while(q)
{
p->next=q->next;
add(q->xs,q->zs);
q=b.first->next;
p=b.first;
}
return first;//这样很不好。
}
xiang * duoxiangshi::operator -(const duoxiangshi & b)
{
xiang *q,*p;
q=b.first->next;
p=b.first;
while(q)
{
p->next=q->next;
add((-1*q->xs),q->zs);
q=b.first->next;
p=b.first;
}
return first;//这样很不好。
}
void duoxiangshi::print(xiang *athead)
{
cout<<endl;
bool firsttime;
xiang *p=athead->next;
firsttime=true;
while(p)
{
if(p->xs!=0)
{
if(firsttime)
{
cout<<p->xs<<"X"<<p->zs;
firsttime = false;
}
else
{
if(p->xs>0)cout<<"+"<<p->xs<<"X"<<p->zs;
else cout<<p->xs<<"X"<<p->zs;
}
}
p=p->next;
}
cout<<endl;
}
void duoxiangshi::printself()
{
print(first);
}

int main(int argc, char *argv[])
{
duoxiangshi a,b,c,d;
int i,j,zs,xs,input;
cout<<"*********************加法测试**********************"<<endl;
cout<<"输入多项式A的项数:"<<endl;
cin>>input;
for(i=0;i<input;i++)
{
cout<<"请输入A的第"<<i+1<<"项的系数";
cin>>xs;
cout<<"请输入A的第"<<i+1<<"项的指数";
cin>>zs;
a.add(xs,zs);
c.add(xs,zs);
}
cout<<"输入多项式B的项数:"<<endl;
cin>>input;
for(i=0;i<input;i++)
{
cout<<"请输入B的第"<<i+1<<"项的系数";
cin>>xs;
cout<<"请输入B的第"<<i+1<<"项的指数";
cin>>zs;
b.add(xs,zs);
d.add(xs,zs);
}
cout<<"多项式A为:";
a.printself();

cout<<"多项式B为:";
b.printself();

cout<<"多项式A+B为:";
a.print(a+b);
cout<<"/n******************减法测试********************"<<endl;
cout<<"多项式A-B为:";
c.print(c-d);

system("PAUSE");
return EXIT_SUCCESS;
}

数据结构课设:用链表实现多项式相加(c++) 500-800行左右
void add(float xs,float zs); \/\/n代表多项式的项数个数 xiang * operator+(const duoxiangshi & b); \/\/重载多项式的相加 xiang * operator-(const duoxiangshi & b);void print(xiang *athead); \/\/输出多项式相加结果 void printself(); \/\/看一下 };duoxiangshi::duoxiangshi...

大家用的行车记录仪都是啥型号的,我想买500-800左右的?
在选购行车记录仪时,首要考虑的是分辨率和使用便捷性。目前,大部分产品分辨率在720p到1080p之间,甚至有部分提供4K选项,但个人认为4K并非必需,720p到1080p已经能满足日常需求。对于广角,120°到149°之间就足够了,过宽的视角可能导致画面失真,反而会增加不必要的麻烦。在方便性方面,有三种主要安装...

求一篇采用象征手法的作文,500字左右,高中初中的都行
用象征手法写小草 象征的意思 象征手法的作文800字 用象征的手法写梅花 用象征的手法写一段话 用象征手法写仙人掌 写作手法 用象征手法写一首小诗 其他类似问题2014-03-16 以象征手法写一篇作文,500字 160 2015-04-26 以春天为题,用象征的手法写一篇作文,初中水平,500字左右,... 17 2014-05-...

相似回答
大家正在搜