您好,欢迎光临本网站![请登录][注册会员]  
文件名称: 模拟拔河游戏,自己做的,共享出来
  所属分类: iOS
  开发工具:
  文件大小: 1mb
  下载次数: 0
  上传时间: 2008-12-22
  提 供 者: ghai****
 详细说明: #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<<"合并多项式后共有"<
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: 模拟拔河游戏 课设
 输入关键字,在本站1000多万海量源码库中尽情搜索: