数据结构课程设计报告(共19页).docx
精选优质文档-倾情为你奉上安徽工业大学数据结构课程设计指导老师:陈学进姓名:陆俊贤学号:计算机学院目录课题一:进制转换1·数据结构:栈和队列;2·算法:将需要转换的数据分为两部分,整数部分和小数部分,将整数部分利用辗转相除法取得的余数存入栈中,将小数部分乘以进制取整后存入队列中,然后顺序出栈和出队列,输出转换后的数值,3·程序源代码:#include<iostream>using namespace std;typedef int datatype;class Seqstackprivate:datatype *base;datatype *top;int size;public:Seqstack(int stacksize=100)base =new datatype stacksize;top=base;size=stacksize;int Empty_stack();int Push_stack(datatype e);int Pop_stack(datatype &e);class SeqQueueprivate:datatype *base;int front,rear;int size;public:SeqQueue(int Queuesize=100)base =new datatype Queuesize;front=rear=0;size=Queuesize;int Empty_Queue();int In_Queue(datatype e);int Out_Queue(datatype &e);int Seqstack:Empty_stack()return top<=base;int Seqstack:Push_stack(datatype e)if(top-base<size)*top=e;top+;return 1;elsereturn 0;int Seqstack:Pop_stack(datatype&e)if(top>base)top-;e=*top;return 1;elsereturn 0;int SeqQueue:Empty_Queue()return front=rear;int SeqQueue:In_Queue(datatype e)if(rear+1)%size!=front)rear=(rear+1)%size;baserear=e;return 1;elsereturn 0;int SeqQueue:Out_Queue(datatype &e)if(rear!=front)front=(front+1)%size;e=basefront;return 1;elsereturn 0;void Calculate1(int Num,int r);void Calculate2(double Num,int r);int main()int r;double Num;cout<<"请输入一个任意十进制数(输入0结束):"<<endl;while(1)cin>>Num;if(Num=0)return 0;cout<<"请输入转换进制:"<<endl;cin>>r;cout<<"转换后的数为:"<<endl;if(Num<0)Num=-Num;cout<<"-"Calculate1(int)Num,r);if(Num-(int)Num)Calculate2(Num-(int)Num,r);elsecout<<endl;cout<<"请输入一个任意十进制数(输入0结束):"<<endl;return 0;void Calculate1(int Num,int r)Seqstack s;int e;char ch;while(Num)e=Num%r;Num=Num/r;s.Push_stack(e);while(!s.Empty_stack()s.Pop_stack(e);if(e>=0&&e<10)cout<<e;elsech=e+55;cout<<ch;void Calculate2(double Num,int r)if(Num!=0)cout<<"."int e;char ch;SeqQueue q;while(Num>0.001)Num=Num*r;e=(int)Num;Num-=e;q.In_Queue(e);while(!q.Empty_Queue()q.Out_Queue(e);if(e<10&&e>-1)cout<<e;elsech=e+55;cout<<ch;cout<<endl;4·测试数据与调试请输入一个任意十进制数(输入0结束):20.3请输入转换进制:2转换后的数为:10100.1101请输入一个任意十进制数(输入0结束):98.5请输入转换进制:16转换后的数为:62.8请输入一个任意十进制数(输入0结束):65.3请输入转换进制:16转换后的数为:41.4CCCCCCCCCCC课题二·一元多项式的计算1·数据结构顺序表2·算法将从设备中接收的字符串按照一元多项式的书写规则写入顺序表中 ,然后将两个顺序表按照用户要求的运算规则(加、减、乘)将运算结果存入顺序表中并输出。3·程序源代码#include<iostream>#include<cstring>using namespace std;#define maxsize 100typedef int datatype;class apublic:int xishu;int cishu;char ch;char sign;class Seqlistpublic:a Itemmaxsize;int len;Seqlist()len=0;void Initiate();void Output();void Sort();friendvoid Calculate1(Seqlist &l1,Seqlist &l2);friend void Calculate2(Seqlist &l1,Seqlist &l2);friend void Calculate3(Seqlist &l1,Seqlist &l2 ,Seqlist &l3);void Add(Seqlist &l3, a &l_flag);int main()Seqlist l1,l2,l3;char ch;cout<<"请输入第一个一元多项式:"<<endl;l1.Initiate();cout<<endl<<"请输入运算符:"<<endl;cin>>ch;cout<<endl<<"请输入第二个一元多项式:"<<endl;l2.Initiate();cout<<endl<<"计算结果:"<<endl;switch(ch)case '+':Calculate1(l1,l2);break;case '-':Calculate2(l1,l2);break;case '*':Calculate3(l1,l2,l3);break;cout<<endl;return 0;void Seqlist:Initiate()char a100;int i=0,j=0,str_len;cin>>a;str_len=strlen(a);while(i<str_len)Itemj.xishu=0;if(i=0)if(ai='-')Itemj.sign='-'i+;if(ai>='a'&&ai<='z')Itemj.ch=ai;Itemj.xishu=1;i+;if(ai>'0'&&ai<='9')while(1)if(ai>'0'&&ai<='9')Itemj.xishu=ai-'0'+Itemj.xishu*10;i+;elsebreak;Itemj.ch=ai;i+;if(ai!='')i-;Itemj.cishu=1;elsei+;Itemj.cishu=ai-'0'i+;j+;elseItemj.xishu=0;Itemj.sign=ai;i+;if(ai>='a'&&ai<='z')Itemj.ch=ai;Itemj.xishu=1;i+;else if(ai>'0'&&ai<='9')while(1)if(ai>'0'&&ai<='9')Itemj.xishu=ai-'0'+Itemj.xishu*10;i+;elsebreak;Itemj.ch=ai;i+;if(ai!='')i-;Itemj.cishu=1;elsei+;Itemj.cishu=ai-'0'i+;j+;len=j;for(i=0;i<len;i+)if(Itemi.sign='-')Itemi.xishu=-Itemi.xishu;void Seqlist:Output()int i=0;for(i=0;i<len;i+)if(i>0)if(Itemi.xishu<0&&Itemi.xishu!=-1)cout<<Itemi.xishu;if(Itemi.xishu>1)cout<<'+'<<Itemi.xishu;if(Itemi.xishu=-1)cout<<'-'if(Itemi.xishu=1)cout<<'+'cout<<Itemi.ch;if(Itemi.cishu!=1)cout<<''<<Itemi.cishu;elseif(Itemi.xishu=-1)cout<<'-'if(Itemi.xishu!=1&&Itemi.xishu!=-1)cout<<Itemi.xishu;cout<<Itemi.ch;if(Itemi.cishu>1)cout<<''<<Itemi.cishu;void Calculate1(Seqlist &l1,Seqlist &l2)cout<<'('l1.Output();cout<<") + "cout<<'('l1.Output();cout<<") "cout<<"= "int i,j,flag=0;for(j=0;j<l2.len;j+)flag=0;for(i=0;i<l1.len;i+)if(l2.Itemj.cishu=l1.Itemi.cishu)l1.Itemi.xishu+=l2.Itemj.xishu;flag=1;break;if(flag=0)l1.Iteml1.len=l2.Itemj;l1.len+;l1.Sort();l1.Output();void Calculate2(Seqlist &l1,Seqlist &l2)cout<<'('l1.Output();cout<<") - "cout<<'('l1.Output();cout<<") "cout<<"= "int i,j,flag=0;for(j=0;j<l2.len;j+)flag=0;for(i=0;i<l1.len;i+)if(l2.Itemj.cishu=l1.Itemi.cishu)l1.Itemi.xishu-=l2.Itemj.xishu;flag=1;break;if(flag=0)l1.Iteml1.len=l2.Itemj;l1.len+;l1.Sort();l1.Output();void Seqlist:Sort()int i,j;a l_e;for(i=0;i<len;i+)for(j=0;j<len-i;j+)if(Itemj.cishu<Itemj+1.cishu)l_e=Itemj;Itemj=Itemj+1;Itemj+1=l_e;void Calculate3(Seqlist &l1,Seqlist &l2,Seqlist &l3)cout<<'('l1.Output();cout<<") * "cout<<'('l1.Output();cout<<") "cout<<"= "a l_flag;int i,j;for(i=0;i<l1.len;i+)for(j=0;j<l2.len;j+)l_flag.xishu=l1.Itemi.xishu*l2.Itemj.xishu;l_flag.cishu=l1.Itemi.cishu+l2.Itemj.cishu;l_flag.ch=l1.Itemi.ch;Add(l3,l_flag);l3.Sort();l3.Output();void Add(Seqlist &l3, a &l_flag)int i;int flag=0;for(i=0;i<l3.len;i+)if(l_flag.cishu=l3.Itemi.cishu)l3.Itemi.xishu+=l_flag.xishu;flag=1;if(flag=0)l3.Iteml3.len=l_flag;l3.len+;4·测试数据与调试输入:23x2+6x3+x6+-21x2+6x3-81x5输出:x6-81x5+12x3+2x2输入:3x2+x3*x4+6x5输出:6x8+19x7+3x6课题三·大数相乘1·数据结构线性表2·算法将从外接设备中接收的字符串存入整形线性表中,并且倒序存入,每个数组元素存一个,然后错位相乘,对应相加。3·源程序代码#include<iostream>#include<cstring>#include<cmath>using namespace std;typedef int datatype;#define maxsize 9999class Seqlistprivate:datatype datamaxsize;int len;public:Seqlist()len=0;for(int i=0;i<maxsize;i+)datai=0;void Initiate();void Output();void Reinitiate();friend void Calculate(Seqlist &l1,Seqlist &l2, Seqlist &l3);int main()Seqlist l1,l2,l3;l1.Initiate();l2.Initiate();Calculate(l1,l2,l3);l1.Output();cout<<'*'l2.Output();cout<<'='l3.Output();cout<<endl;return 0;void Seqlist:Initiate()char amaxsize;int str_len,i,j;cin>>a;str_len=strlen(a);for(i=str_len-1,j=0;i>=0;i-,j+)dataj=ai-'0'len+;void Seqlist:Output()int i;for(i=maxsize-1;i>=0;i-)if(datai!=0)cout<<datai;void Calculate(Seqlist &l1,Seqlist &l2, Seqlist &l3)int i,j,k;int flag;for(i=0;i<l1.len;i+)for(j=0;j<l2.len;j+)flag=l1.datai*l2.dataj;l3.datai+j+=flag;l3.len=i+j;l3.Reinitiate();void Seqlist:Reinitiate()for(int i=0;i<len;i+)if(datai>10)datai+1+=datai/10;datai=datai%10;4·测试数据与调试输入:36523659输出:3652*3659=专心-专注-专业