华中科技大学计算机学院C++实验报告 实验二.docx
《华中科技大学计算机学院C++实验报告 实验二.docx》由会员分享,可在线阅读,更多相关《华中科技大学计算机学院C++实验报告 实验二.docx(10页珍藏版)》请在冰豆网上搜索。
![华中科技大学计算机学院C++实验报告 实验二.docx](https://file1.bdocx.com/fileroot1/2022-12/12/c568930e-e81f-4a6f-8ed9-89f328250384/c568930e-e81f-4a6f-8ed9-89f3282503841.gif)
华中科技大学计算机学院C++实验报告实验二
课程实验报告
课程名称:
面向对象程序设计
实验名称:
面向对象的整型栈编程
院系:
计算机科学与技术
专业班级:
CS1209班
学号:
姓名:
********
2014年1月22日
一、需求分析
1.题目要求
整型栈是一种先进后出的存储结构,对其进行的操作通常包括判断栈是否为空、向栈顶添加一个整型元素、出栈等。
整型栈类型及其操作函数采用面向对象的C++语言定义,请将完成上述操作的所有函数采用C++编程,然后写一个main函数对栈的所有操作函数进行测试。
classSTACK{
int*constelems;//申请内存用于存放栈的元素
constintmax;//栈能存放的最大元素个数
intpos;//栈实际已有元素个数,栈空时pos=0;
public:
STACK(intm);//初始化栈:
最多m个元素
STACK(constSTACK&s);//用栈s拷贝初始化栈
intsize()const;//返回栈的最大元素个数max
inthowMany()const;//返回栈的实际元素个数pos
intgetelem(intx)const;//取下标x处的栈元素
STACK&push(inte);//将e入栈,并返回栈
STACK&pop(int&e);//出栈到e,并返回栈
STACK&assign(constSTACK&s);//赋s给栈,并返回被赋值的栈
voidprint()const;//打印栈
~STACK();//销毁栈
};
2.需求分析
采用面向对象的C++语言定义整型栈,对其进行的操作通常包括判断栈是否为空、向栈顶添加一个整型元素、出栈等。
二、系统设计
概要设计
介绍设计思路、原理。
将一个复杂系统按功能进行模块划分、建立模块的层次结构及调用关系、确定模块间的接口及人机界面等。
三、软件开发
该实验代码我用CodeBlocks进行编写并调试。
四、软件测试
1)总体界面如图1所示:
图1
2)进行入栈操作,如图2所示:
图2
3)进行打印栈操作,如图3所示:
图3
4)进行出栈操作,如图4所示:
图4
5)用下标去元素,如图5所示:
图5
6)栈中实际元素的个数如图6所示:
图6
五、源码和说明
1.文件清单及其功能说明
main.c是源程序
2.exe是可执行文件
2.用户使用说明书
使用CodeBlocks进行编译,或者直接打开可执行文件。
3.源代码
#include
#include
#include
#include
usingnamespacestd;
classSTACK{
int*elems;//申请内存用于存放栈的元素
intmax;//栈能存放的最大元素个数
intpos;//栈实际已有元素个数,栈空时pos=0;
public:
STACK(intm);//初始化栈:
最多m个元素
STACK(constSTACK&s);//用栈s拷贝初始化栈
intsize()const;//返回栈的最大元素个数max
inthowMany()const;//返回栈的实际元素个数pos
intgetelem(intx)const;//取下标x处的栈元素
STACK&push(inte);//将e入栈,并返回栈
STACK&pop(int&e);//出栈到e,并返回栈
STACK&assign(constSTACK&s);//赋s给栈,并返回被赋值的栈
voidprint()const;//打印栈
~STACK();//销毁栈
};
STACK:
:
STACK(intm):
max(m),elems(newint[m])
{
pos=0;
}
STACK:
:
STACK(constSTACK&s):
max(s.max),elems(s.elems)
{
pos=0;
}
intSTACK:
:
size()const
{
returnmax;
}
intSTACK:
:
getelem(intx)const
{
if(pos==0)
{
cout<<"栈为空!
!
\n";
}
else
{
if(x>pos)
{
cout<<"当前的元素不存在:
\n";
}
else
returnelems[x];
}
}
intSTACK:
:
howMany()const
{
returnpos;
}
STACK&STACK:
:
push(inte)
{
if(pos>=max)
cout<<"空间不够!
!
\n";
else
{
*(elems+pos)=e;
pos++;
cout<<*(elems+pos-1);
}
return(*this);
}
STACK&STACK:
:
pop(int&e)
{
if(pos==0)
{
cout<<"栈为空";
}
else
e=*(elems+pos-1);
pos-=1;
return(*this);
}
STACK&STACK:
:
assign(constSTACK&s)
{
pos=s.pos;
return(*this);
}
voidSTACK:
:
print()const
{
cout<<"最大量:
"<cout<<"当前的元素个数:
"<cout<<"里面的元素为:
";
for(inti=pos-1;i>=0;i--)
{
cout<<*(elems+i)<<"\t";
}
}
STACK:
:
~STACK()
{
delete[]elems;
pos=0;
max=0;
}
voidmenu(void)
{
cout<<"\t********************************************************************\n";
cout<<"\t1.返回栈的最大元素个数max";
cout<<"\t\t2.返回栈的实际元素个数pos\n";
cout<<"\t3.取下标x处的栈元素";
cout<<"\t\t\t4.将e入栈,并返回栈\n";
cout<<"\t5.出栈到e,并返回栈";
cout<<"\t\t\t6.赋s给栈,并返回被赋值的栈\n";
cout<<"\t7.打印栈";
cout<<"\t\t\t\t8.销毁栈\n";
cout<<"\t\t\t0.退出程序\n";
cout<<"\t********************************************************************\n";
}
intmain()
{
intm;
intx1;
intop;
intt,x,x2,e,e3,e4;
cout<<"请输入栈的最大容量m:
\n";
cin>>m;
STACKst1(m);
STACKst2(st1);
cout<<"初始化栈:
最多"<Sleep(3000);
do
{
system("cls");
menu();
cout<<"请输入序号:
\n";
cin>>op;
switch(op)
{
case1:
t=st1.size();
cout<<"当前栈的大小为:
"<getchar();getchar();
break;
case2:
x=st1.howMany();
cout<<"实际元素为:
"<getchar();getchar();
break;
case3:
cout<<"请输入想得到的元素的下标:
"<<"\n";
cin>>x1;
x2=st1.getelem(x1);
cout<<"下表为"<"<getchar();getchar();
break;
case4:
cout<<"请输入想压栈的元素:
";
cin>>e;
st1=st1.push(e);
getchar();getchar();
break;
case5:
st1=st1.pop(e3);
cout<<"出栈元素为"<getchar();getchar();
break;
case6:
st2=st2.assign(st1);
getchar();getchar();
break;
case7:
cout<<"请输入想打印的栈1or2\n";
cin>>e4;
if(e4==1)
{
st1.print();
}
else
st2.print();
getchar();getchar();
break;
case8:
cout<<"请输入想销毁的栈1or2";
inte5;
cin>>e5;
if(e5==1)
st1.~STACK();
else
st2.~STACK();
getchar();getchar();
break;
default:
cout<<"输入有误请重新输入:
";
break;
}
}while(op);
cout<<"\t\t******谢谢使用******\n"<return0;
}