【文件属性】:
文件名称:模拟拔河游戏,自己做的,共享出来
文件大小:1.57MB
文件格式:BMP
更新时间:2012-01-12 06:37:31
模拟拔河游戏 课设
#include
#include
#include
#include
#define STACK_INIT_SIZE 100 //存储空间初始分配量
#define STACKINCREMENT 10 //存储空间分配增量
#define SElemType char
//多项式结构体
struct PolyType
{
float coef; //系数
int expn; //指数
struct PolyType *next; //指向下个元素
};
typedef PolyType Polynomial; //用带头结点的有序链表表示多项式
void InitList(Polynomial *&L); //初始化链表
void CompositorList(Polynomial *&L); //按指数降序重排列
void Output(Polynomial *L); //输出链表内容
void CreatePolyn(Polynomial *&p, int m); //创建一元多项式链表
void OperPolyn(Polynomial *&Pa, Polynomial *&Pb, //多项式运算
Polynomial *&Pc, char oper);
int Compare(Polynomial *Pa, Polynomial *Pb); //比较指数项大小
void CopyList(Polynomial *&Pc, Polynomial *p); //拷贝节点
void CreateComboPolyn(Polynomial *&Pc, int n); //建立合并空链表
int CountRepeat(Polynomial *Pa, Polynomial *Pb); //计算pa和pb中指数项重复的个数
////////////////////////////////////
//Polynomial.cpp
#include "Polynomial.h"
//初始化链表
void InitList(Polynomial *&L)
{
L->coef = 0.0;
L->expn = 0;
L->next = NULL;
}
//按指数降序重排列
void CompositorList(Polynomial *&L)
{
cout<<"链表按其指数降序重排列..."<next;
}
//按降序重排列
p = L;
int i = 0;
int expn = 0; //临时变量
float coef = 0.0;
while(i<=num)
{
while(p->next)
{
if(p->expn < p->next->expn)
{
expn = p->expn;
coef = p->coef;
p->expn = p->next->expn;
p->coef = p->next->coef;
p->next->expn = expn;
p->next->coef = coef;
}
p = p->next;
}
p = L;
i++;
}
Output(L);
}
//输出链表内容
void Output(Polynomial *L)
{
while(L)
{
cout<coef<<", "<expn<next;
}
}
//创建一元多项式链表,节点数为m个,包含头结点
void CreatePolyn(Polynomial *&p, int m)
{
p->next = NULL;
cout<<"请输入第1个节点的系数和指数:"<>p->coef>>p->expn;
for(int n = 2; n <= m; n++)
{
Polynomial *Polyn = (Polynomial*)malloc(sizeof(Polynomial));
cout<<"请输入第"<>Polyn->coef>>Polyn->expn;
Polyn->next = p->next;
p->next = Polyn;
}
}
//多项式运算
void OperPolyn(Polynomial *&Pa, Polynomial *&Pb, Polynomial *&Pc, char oper)
{
Polynomial *qa, *qb, *p;
qa = (Polynomial*)malloc(sizeof(Polynomial));
qb = (Polynomial*)malloc(sizeof(Polynomial));
p = (Polynomial*)malloc(sizeof(Polynomial));
qa = Pa;
qb = Pb;
p = Pc;
while(qa && qb)
{
switch(Compare(qa, qb))
{
case 1:
CopyList(p, qa);
qa = qa->next;
break;
case -1:
CopyList(p, qb);
qb = qb->next;
break;
case 0:
if(oper == '+') //多项式加法
{
qa->coef = qa->coef + qb->coef;
}
else if(oper == '-') //多项式减法
{
qa->coef = qa->coef - qb->coef;
}
else
{
cout<<"多项式输入有误:"<next;
qb = qb->next;
break;
default:
return;
}
}
while(qa)
{
CopyList(p, qa);
qa = qa->next;
}
while(qb)
{
CopyList(p, qb);
qb = qb->next;
}
}
//比较指数项大小
int Compare(Polynomial *Pa, Polynomial *Pb)
{
if(Pa->expn == Pb->expn)
return 0;
return (Pa->expn - Pb->expn) > 0?1:-1;
}
//拷贝节点
void CopyList(Polynomial *&Pc, Polynomial *p)
{
Pc->coef = p->coef;
Pc->expn = p->expn;
Pc = Pc->next;
}
//建立合并空链表
void CreateComboPolyn(Polynomial *&Pc, int n)
{
Pc->next = NULL;
for(int i = 1; i != n; i++)
{
Polynomial *p = (Polynomial*)malloc(sizeof(Polynomial));
p->next = Pc->next;
Pc->next = p;
}
}
//计算pa和pb中指数项重复的个数
int CountRepeat(Polynomial *Pa, Polynomial *Pb)
{
Polynomial *pa = (Polynomial*)malloc(sizeof(Polynomial));
Polynomial *pb = (Polynomial*)malloc(sizeof(Polynomial));
pa = Pa;
pb = Pb;
int n = 0;
while(pa && pb)
{
switch(Compare(pa, pb))
{
case 1:
pa = pa->next;
break;
case -1:
pb = pb->next;
break;
case 0:
++n;
pa = pa->next;
pb = pb->next;
break;
default:
return 0;
}
}
return n;
}
//////////////////////////////////
//main.cpp
#include "Polynomial.h"
//主函数
void main()
{
Polynomial *Pa;
Pa = (Polynomial*)malloc(sizeof(Polynomial));
InitList(Pa);
Polynomial *Pb;
Pb = (Polynomial*)malloc(sizeof(Polynomial));
InitList(Pb);
Polynomial *Pc;
Pc = (Polynomial*)malloc(sizeof(Polynomial));
int m = 0;
int n = 0;
char oper;
cout<<"请输入a多项式的项数:"<>m;
CreatePolyn(Pa, m);
CompositorList(Pa);
cout<<"请输入b多项式的项数:"<>n;
CreatePolyn(Pb, n);
CompositorList(Pb);
cout<<"请输入多项式要做的运算(只有'+''-'):"<>oper;
int repeat = 0;
repeat = CountRepeat(Pa, Pb); //计算pa,pb中指数重复的元素个数
CreateComboPolyn(Pc, m + n - repeat); //建立pc合并链表
OperPolyn(Pa, Pb, Pc, oper);
cout<<"合并多项式后共有"<