图1。3
5.特点与不足
5、1技术特点
完成了实验得所有要求,没有错误得地方。
5。2不足与改进得建议
没有做人机交互界面,无法自由选择入栈得数据;同时注释较少,对于程序不了解得人可能需要花费更多时间去了解。
6、过程与体会
6。1遇到得主要问题与解决方法
输出结果数字与预计不同,检查后发现原因就是变量初始值未设置。
6、2课程设计得体会
本次实验主要还就是通过回顾C语言中栈得知识完成在C++上得编程,所
以总体过程没有出现太大得问题;同时也对const变量有了进一步得认识。
7。源码与说明
7。1文件清单及其功能说明
experiment1.cpp源码
experiment1、exe可执行文件。
7.2用户使用说明书
experiment1。cpp就是程序得源码,可通过修改其中main函数中得变量来测试各个函数、
7、3源代码
#include
int *elems; //申请内存用于存放栈得元素 int max; //栈能存放得最大元素个数
int pos;? //栈实际已有元素个数,栈空时pos=0; };
void initSTACK(STACK *const p, int m);?//初始化p指向得栈:最多m个元素
void initSTACK(STACK *const p, const STACK&s); //用栈s初始化p指向得栈
int size (const STACK *const p); ?//返回p指向得栈得最大元素个数max
int howMany (const STACK *const p);?//返回p指向得栈得实际元素个数pos
int getelem (const STACK *const p, int x);?//取下标x处得栈元素
STACK *const push(STACK *const p, int e); ?//将e入栈,并返回p
STACK *const pop(STACK *const p, int &e); ?//出栈到e,并
返回p
STACK *const assign(STACK*const p, const STACK&s); //赋s给p指得栈,并返回p
void print(const STACK*const p); ??//打印p指向得栈 void destroySTACK(STACK*const p); ?//销毁p指向得栈 int main(int argc, char* argv[]) {
STACK *s1 = (STACK *)malloc(sizeof(STACK)); STACK *s2 = (STACK *)malloc(sizeof(STACK)); initSTACK(s1,10); push(s1,1); push(s1,2); push(push(s1,3),4); initSTACK(s2,*s1); print(s2); printf(\栈s1:\\n"); print(s1); //assign(s2,*s1); printf("栈s2:\\n"); print(s2); int a,b,c; a = size(s1);
printf(\栈得最大元素个数就是 %d\n\a); b = howMany(s1);
printf(\栈得实际元素个数就是 %d\\n”,b); c = getelem(s1,3);
printf(”3处栈元素就是就是%d\n\ int x,y,z; pop(s2,x);
pop(pop(s2,y),z);
printf(\x= %d, y= %d, z= %d \n",x,y,z); destroySTACK(s2); destroySTACK(s1); getchar(); return 0;
}
void initSTACK(STACK *const p, int m) //初始化p指向得栈:最多m个元素
{
p->elems = (int*)malloc(m*sizeof(int*)); if(!p->elems) return; p->pos = 0; p->max = m; int i;
for(i=0;i<(p->max);i++) p—>elems[i] = 0; }
void initSTACK(STACK *const p, const STACK&s) //用栈s初始化p指向得栈
{
p—>elems = (int*)malloc((s、max)*sizeof(int)); p—>pos = s、pos; p—>max = s、max; int i;
for(i=0;i<(s、pos);i++) {
p->elems[i]=s、elems[i]; printf(\\n”,p—〉elems[i]); } }
int size (const STACK *const p)??//返回p指向得栈得最大元素个数max
{
return p->max; }
int howMany (const STACK *const p) //返回p指向得栈得实际元素个数pos
{
return p—>pos;
}
int getelem (const STACK *const p, int x)?//取下标x处得栈元素
{
if(p==NULL) return NULL; else {
if(x〉(p->pos))
printf(”不存在元素\n\ else
return p-〉elems[x]; } }
STACK *const push(STACK *const p, int e) ?//将e入栈,并返回p
{
if(p==NULL) return NULL; else {
if((p-〉pos)<=(p->max)) {
p-〉elems[p—>pos]=e; p—〉pos++; return p; } else
printf(”栈满\\n"); } }
STACK *const pop(STACK *const p, int &e) ?//出栈到e,并返回p
{
if(p==NULL)
面向对象程序设计课程实验报告



