面向对象课程设计报告格式Word格式.docx
《面向对象课程设计报告格式Word格式.docx》由会员分享,可在线阅读,更多相关《面向对象课程设计报告格式Word格式.docx(13页珍藏版)》请在冰豆网上搜索。
template<
classT>
classNode{
public:
Node(){this->
next=NULL;
}
Node(Tdata,Node<
T>
*next=NULL){this->
data=data;
this->
next=next;
Tdata;
Node<
*next;
明确设计为链表类型的存储结构,因而节点(Node)就必须包含一个需要存储类型数据成员,还应有一个指向下一个节点的指针,用构造函数初始化个数据成员。
将数据成员定义为public是为了下面的访问方便。
classLinkedStack{
LinkedStack();
~LinkedStack();
boolisEmpty();
voidpush(Tx);
Tpop();
TgetTop();
private:
*top;
};
根据需要,要用到多种类型的栈(stack),而栈的功能都大体相同,所以将类设计为模板类(template),这样在需要用的时候实例化就可以用了。
创建链栈,就要用到上面的节点(Node).栈的主要功能有初始化(LinkedStack()),入栈(voidpush(Tx)),出栈(Tpop()),得到栈顶元素(TgetTop()),还有可能判断栈是否为空栈(boolisEmpty()),然后分别将各个部分实现。
3、程序编码。
程序的流程图如下:
计算过程
成功
继续
失败
部分重点代码注释详见程序清单。
4、调试分析
分模块(类)进行,设计好测试数据,测试输出的结果,时间复杂度分析,和每个模块设计和调试时存在问题的思考(问题是哪些问题如何解决),算法的改进设想。
菜单界面。
选择读取方式...
从键盘读入表达式,并计算得出结果。
继续从键盘读入
选择从文件读入,并且文件打开成功
输入文件中的算术表达式
输出文件中的结果
调试时存在的问题:
刚开始时,链表的链接存在问题,老是出现内存访问错误,最后通过调试发现,原来多写了一个next。
然后就是在写tochange()函数时,switch语句中的条件考虑的不够全面。
再就是string类型的表达式不能够作为switch的条件。
最后就是定义了一个stringa,不能够通过a[i]="
2as"
这种方式给string类型的数据赋值
算法的改进设想:
在分配expression的时候,都是按的比较大的值分配的,有很多空间都没有用到,造成了很多内存的浪费。
改进:
运用链表,动态的分配,按需分配
程序清单:
#include<
iostream>
string>
fstream>
cstdlib>
usingnamespacestd;
//链表节点类
classNode
{
Node()//defaultconstructor
{
this->
}
*next=NULL)
//声明为public类型是便于top的访问
//next指向的对象是Node<
classLinkedStack
//defaultconstructor
//defaultdestructor
LinkedStack<
:
LinkedStack()
top=NULL;
~LinkedStack()
*p=top;
*q;
while(p!
=top)//将编表所分配的空间一个一个给释放掉
q=p;
p=p->
next;
deletep;
boolLinkedStack<
isEmpty()
returntop==NULL;
voidLinkedStack<
push(Tx)
top=newNode<
(x,top);
//将链表给链接起来,并且更新top指向的位置
TLinkedStack<
pop()
if(!
isEmpty())
Tx=top->
data;
Node<
top=top->
returnx;
throw"
空栈,不能执行出栈操作"
;
getTop()
returntop->
空栈,不能获得栈顶元素"
stringtochange(stringexpression)//非递归后序遍历,将输入的计算表式//按后续遍历的顺序放到postfix中去逆波兰式
LinkedStack<
char>
stack;
//模板用char实例化了
charpostfix[100];
inti=0,j=0;
charout;
while(expression[i]!
='
\0'
)//逆波兰式算法,将中序的算术表达式转化成为后续的式子
switch(expression[i])///将expression中的每个字符对号入座
{
case'
+'
-'
while(!
stack.isEmpty()&
&
stack.getTop()!
('
)
{
postfix[j++]=stack.pop();
}
stack.push(expression[i++]);
break;
*'
/'
(stack.getTop()=='
||stack.getTop()=='
))
stack.push(expression[i++]);
)'
out=stack.pop();
out!
postfix[j++]=out;
out=stack.pop();
i++;
default:
while(expression[i]>
0'
expression[i]<
9'
postfix[j++]=expression[i++];
}
postfix[j++]='
'
//输入空格是方便下面的计算
}
while(!
stack.isEmpty())
postfix[j++]=stack.pop();
postfix[j]='
//将postfix数组最后补上\0,表示结尾
returnpostfix;
intvalue(stringpostfix)//计算
int>
//模板用int实例化了,用于存放int型数据
inti=0;
intresult=0;
while(postfix[i]!
if(postfix[i]>
postfix[i]<
result=0;
while(postfix[i]!
){
result=result*10+postfix[i++]-'
}
i++;
stack.push(result);
else
if(postfix[i]!
{
inty=stack.pop();
intx=stack.pop();
switch(postfix[i])
case'
result=x+y;
break;
result=x-y;
result=x*y;
result=x/y;
stack.push(result);
returnstack.pop();
intmain()
cout<
<
"
******************计算器******************"
endl;
(ByLby)"
endl<
请选择您想要执行的操作:
"
1.从键盘输入2.从文件读取"
intt;
cin>
>
t;
switch(t)
case1:
intm=1;
while(m)
{cout<
请输入算式!
stringa;
cin>
a;
stringpostfix=tochange(a);
cout<
a<
="
value(postfix)<
继续?
1.是0.否"
m;
}
break;
case2:
ifstreamin;
ofstreamout;
in.open("
math.in"
);
out.open("
result.out"
if(!
in)
cerr<
Failtoopenthefile,pleasecheck!
!
exit(0);
inti=0;
intm=0;
stringexpression[100];
while(getline(in,expression[i++]));
m=i-1;
for(i=0;
i<
i++)
{
stringpostfix=tochange(expression[i]);
out<
;
in.close();
out.close();
return0;
}