您的位置:首页 > 其它

一元稀疏多项式计算器

2005-10-31 18:39 507 查看

一元稀疏多项式计算器

班级:02计2 姓名:刘晓明 学号:200201020219完成日期:2004年9月30日
一,需求分析:
1, 输入并建立多项式2, 输出多项式3, 相同指数的同类项合并4, 对于没有指数的项,把指数设为15, 对于常数项指数设为06, 没有系数的项目,系数设为17, 输入输出按照字符串处理二,概要设计
主程序按照以下流程执行:1, 输出提示信息2, 读入字符串数组3, 分析字符串4, 对输出的项目按照指数排序5, 输出多项式6, 释放链表占用的内存空间7, 完成退出
三,测试数据
一元稀疏多项式计算器Copyright@1999-2004, Gashero Liu.作者:刘晓明
请输入一个1024个字符以内的稀疏多项式:2x^4+5x^5输出表达式:+5x^5+2x^4请按任意键继续. . .
一元稀疏多项式计算器Copyright@1999-2004, Gashero Liu.作者:刘晓明
请输入一个1024个字符以内的稀疏多项式:x输出表达式:+x请按任意键继续. . .一元稀疏多项式计算器Copyright@1999-2004, Gashero Liu.作者:刘晓明
请输入一个1024个字符以内的稀疏多项式:22x^63-32x^22+42x^63-2x^63输出表达式:+64x^63-2x^63-32x^22请按任意键继续. . .
此结果出现了错误,对于三个同类项没有正确合并!!!
一元稀疏多项式计算器Copyright@1999-2004, Gashero Liu.作者:刘晓明
请输入一个1024个字符以内的稀疏多项式:-4x^4+5x^4输出表达式:+x^4请按任意键继续. . .
一元稀疏多项式计算器Copyright@1999-2004, Gashero Liu.作者:刘晓明
请输入一个1024个字符以内的稀疏多项式:-4输出表达式:-4x请按任意键继续. . .
在此出现错误,常数项指数设定错误
四,详细设计
1, 文件main.cpp,主逻辑的实现文件,实现了主函数和字符串分析函数
#include <iostream>
#include <stdlib.h>
#include <ctype.h>
#include "ListOper.h"

using namespace std;

LinkList AnayString(char aString[], int aLength);

int main(int argc, char *argv[]) //-------------------------------
{
LinkList L;
char InStr[1024];
int len;
cout << "一元稀疏多项式计算器" << endl;
cout << "Copyright@1999-2004, Gashero Liu." << endl;
cout << "作者:刘晓明" << endl << endl;
cout << "请输入一个1024个字符以内的稀疏多项式:";
cin >> InStr;
len=strlen(InStr);
L=AnayString(InStr,len);
SortList(L);
OutPutList(L);
FreeList(L);
system("PAUSE");
return 0;
}

LinkList AnayString(char aString[], int aLength) //---------------
//TODO: 字符串分析函数
{
LinkList L=NULL;
Node *pos=NULL;
Node *last;
Node *head;
CreateList(L);
head=L;
last=head;
int c=0;
int e=0;
char temp[1];
char tp;
bool plus=true;
char status='n'; //状态指示符,我省略了系数为负的情况
/*
n: 非运算状态
c: 正在计算系数
e: 正在计算指数
p: 指数为0
f: 完成了一个项目的输入
*/
if(aLength==1)
{
if(isdigit(tp))
{
c=atoi(temp);
e=1;
status='f';
}
if(tp=='x')
{
c=1;
e=1;
status='f';
}
}
for(int i=0;i<aLength;i++)
{
temp[0]=aString[i];
tp=temp[0];
switch(status)
{
case 'n':
{
c=0;e=0;
status='c';
if(tp=='-')
{
plus=false;
continue;
}
if(isdigit(tp))
{
c=atoi(temp);
continue;
}
if(tp=='x')//多项式以x开头
{
c=1;
e=0;
status='e';
continue;
}
}
case 'c':
{
if(isdigit(aString[i]))
{
if(plus)
{
c=c*10+atoi(temp);
}
else
{
c=c*10-atoi(temp);
}
continue;
}
if(tp=='x')
{
if(c==0)
{
c=1;
e=0;
}
status='e';
e=0;
continue;
}
//此处考虑了常数项出现在其他位置的可能
if(tp=='+')
{
plus=true;
status='p';
continue;
}
if(tp=='-')
{
plus=false;
status='p';
continue;
}
/*if(temp[0]=='^')
{
status='e';
e=0;
continue;
}*/ //此种情况不可能出现
continue;
} //正在解析系数
case 'e':
{
if(tp=='^')
{
continue;
}
if(isdigit(tp))
{
e=e*10+atoi(temp);
continue;
}
if(tp=='+')
{
plus=true;
if(e==0)
{
e=1;
}
status='f';
continue;
}
if(tp=='-')
{
plus=false;
if(e==0)
{
e=1;
}
status='f';
continue;
}
} //正在解析系数
case 'p':
{
e=0;
status='f';
continue;
}
case 'f':
{
pos=CreateNode(e,c);
last->next=pos;
last=pos;
c=0;e=0;
status='c';
i--;
continue;
}
}
}
pos=CreateNode(e,c);
last->next=pos;
return L;
}
2, 文件datastruct.h,定义数据结构
typedef struct list{ int c; //多项式的项数 int e; //多项式的指数 struct list *next; //下一结点};
typedef struct list *LinkList;typedef struct list Node;
3, 文件ListOper.h,对链表操作函数的声明
//File: ListOper.h

#ifndef DATASTRUCT#define DATASTRUCT#include "datastruct.h"#endif
//some functions declaretioin
bool CreateList(LinkList &L);Node *CreateNode(int e, int c);void FreeList(LinkList &L);void SortList(LinkList &L);void DeleteNextNode(Node *d);void SweepNextNode(Node *s);void OutPutList(LinkList &L);
4, 文件ListOper.cpp,链表操作函数的定义
//File: ListOper.cpp
#include <stdlib.h>#include <iostream>#ifndef DATASTRUCT#define DATASTRUCT#include "datastruct.h"#endif#include "ListOper.h"
using namespace std;
bool CreateList(LinkList &L){ //TODO: 创建线性链表 Node *head; head=(Node *)malloc(sizeof(Node)); if(head==NULL) { cout << "内存分配错误" << endl; return false; } head->next=NULL; head->c=0; head->e=0; L=head; return true;}
Node *CreateNode(int e, int c){ //TODO: 创建结点 Node * pos; pos=(Node *)malloc(sizeof(Node)); if(pos==NULL) { cout << "内存分配错误" << endl; exit(1); } pos->e=e; pos->c=c; pos->next=NULL; return pos;} void FreeList(LinkList &L){ //TODO: 释放整个线性表所占用的内存空间 Node *pos; Node *next; pos=L; while(pos!=NULL) { next=pos->next; free(pos); pos=next; }}
void SortList(LinkList &L){ bool flag=true; //是否需要排序标志 Node *head=L->next; Node *pos; Node *last; Node *temp; if(head->next==NULL) { return; } while(flag) { flag=true; last=head; pos=last->next; if(last==NULL||last->next==NULL) { break; } while(last!=NULL && last->next!=NULL) { flag=false; pos=last->next; if(last->e<pos->e) //哈哈哈哈哈,HTML代码 { SweepNextNode(last); flag=true; } if(last->e==pos->e) { last->c+=pos->c; DeleteNextNode(last); flag=true; /*last=last->next; pos=last->next;*/ } last=last->next; } }}
void DeleteNextNode(Node *d){ Node *temp; temp=d->next; d->next=temp->next; free(temp);}
void SweepNextNode(Node *s)//一点偷懒的办法,只交换值,不修改指针{ int c,e; c=s->c;e=s->e; s->c=s->next->c;s->e=s->next->e; s->next->c=c;s->next->e=e;}
void OutPutList(LinkList &L){ Node *pos; pos=L->next; cout << "输出表达式:"; while(pos!=NULL) { if(pos->c>0) { cout << "+"; } if(pos->c!=1) { cout << pos->c; } if(pos->e!=0) { cout << "x^"; cout << pos->e; } else { cout << "x"; } pos=pos->next; } cout << endl;}
五,调试
C/C++的字符串处理功能完全依靠字符串数组来实现,很多在其他高级语言中实现的字符串比较等操作,在这里完全依靠函数来实现,因此调试中字符串处理函数的调试很多。
对于涉及的循环的操作开始和结束条件设置很关键,因此多次出现死循环。
直到我写完了以后才听说这些问题可以在编译原理里面找到解答,白费了好多力气。
六,用户手册
输入一元稀疏多项式如例:2x^5+22x^4-3x^6+4-x输出按照指数排序,合并同类项后的结果输出。
七,附录
程序各源文件:datastruct.hmain.cppListOper.hListOper.cpp可执行文件qst.exe
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: