面向对象程序设计课程实验报告材料

上传人:cl****1 文档编号:477121956 上传时间:2023-04-06 格式:DOC 页数:45 大小:275KB
返回 下载 相关 举报
面向对象程序设计课程实验报告材料_第1页
第1页 / 共45页
面向对象程序设计课程实验报告材料_第2页
第2页 / 共45页
面向对象程序设计课程实验报告材料_第3页
第3页 / 共45页
面向对象程序设计课程实验报告材料_第4页
第4页 / 共45页
面向对象程序设计课程实验报告材料_第5页
第5页 / 共45页
点击查看更多>>
资源描述

《面向对象程序设计课程实验报告材料》由会员分享,可在线阅读,更多相关《面向对象程序设计课程实验报告材料(45页珍藏版)》请在金锄头文库上搜索。

1、word课程实验报告课程名称:面向对象程序设计院系:专业班级:学号:某某:指导教师:目录1111223444555555551212121213131314141414151515151515151519191919202020202021212121212121212121252525252626262626262626272727272731313131313232323232323232333333333737373838383838383839393939393939 / 整型栈是一种先进后出的存储结构,对其进行的操作通常包括判断栈是否为空、向栈顶添加一个整型元素、出栈等。整型栈类型及

2、其操作函数采用非面向对象的纯C语言定义,请将完成上述操作的所有函数采用面向过程的方法编程,然后写一个main函数对栈的所有操作函数进行测试。struct STACK 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);/

3、返回p指的栈的最大元素个数maxint howMany (const STACK *const p);/返回p指的栈的实际元素个数posint getelem (const STACK *const p, int x);/取下标x处的栈元素STACK *const push(STACK *const p, int e); /将e入栈,并返回pSTACK *const pop(STACK *const p, int &e);/出栈到e,并返回pSTACK *const assign(STACK*const p, const STACK&s);/赋给p指栈,返回pvoid print(const

4、STACK*const p);/打印p指向的栈元素void destroySTACK(STACK*const p);/销毁p指向的栈,释放本实验需要实现栈的功能的操作,如元素的进栈,连续进栈,出栈和连续出栈,所以需要设计两个栈,在完成初始化后直接在程序里给定栈内元素。void initSTACK(STACK *const p, int m)入口参数:int m出口参数:无功能:初始化栈,可存m个元素void initSTACK(STACK *const p, const STACK&s)入口参数:const STACK&s出口参数:无功能:用s初始化p指空栈int size (const ST

5、ACK *const p) 入口参数:无出口参数:int max功能:返回p指的栈的最大元素个数maxint howMany (const STACK *const p) 入口参数:无出口参数:int pos功能:返回p指的栈的实际元素个数posint getelem (const STACK *const p, int x) 入口参数:int x出口参数:elemm功能:取下标x处的栈元素STACK *const push(STACK *const p, int e) 入口参数:int e出口参数:(*this)功能:将e入栈,并返回pSTACK *const pop(STACK *cons

6、t p, int &e) 入口参数:int &e出口参数:(*this)功能:出栈到e,并返回pSTACK *const assign(STACK*const p, const STACK&s) 入口参数:STACK&s出口参数:(*this)功能:赋s给p指栈,返回pvoid print(const STACK*const p) 入口参数:无出口参数:无功能:打印p指向的栈元素void destroySTACK(STACK*const p) 入口参数:出口参数:功能:销毁p指向的栈,释放在Codeblocks编译环境下,使用C+语言编写。完成了实验的所有要求,没有错误的地方。没有做人机交互界

7、面,无法自由选择入栈的数据;同时注释较少,对于程序不了解的人可能需要花费更多时间去了解。输出结果数字与预计不同,检查后发现原因是变量初始值未设置。本次实验主要还是通过回顾C语言中栈的知识完成在C+上的编程,所以总体过程没有出现太大的问题;同时也对const变量有了进一步的认识。experiment1.exe可执行文件。experiment1.cpp是程序的源码,可通过修改其中main函数中的变量来测试各个函数。#include#include#includestruct STACK int *elems;/申请内存用于存放栈的元素 int max; /栈能存放的最大元素个数 int pos;

8、/栈实际已有元素个数,栈空时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指向的栈的最大元素个数maxint howMany (const STACK *const p);/返回p指向的栈的实际元素个数posint getelem (const STACK *const p, int x);/取下标x处的栈元素STACK *const

9、 push(STACK *const p, int e); /将e入栈,并返回pSTACK *const pop(STACK *const p, int &e); /出栈到e,并返回pSTACK *const assign(STACK*const p, const STACK&s); /赋s给p指的栈,并返回pvoid print(const STACK*const p);/打印p指向的栈void destroySTACK(STACK*const p);/销毁p指向的栈int main(int argc, char* argv) STACK *s1 = (STACK *)malloc(size

10、of(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(栈的最大元素个数是 %dn,a); b = howMany(s1); printf(栈的实际元素个数是 %

11、dn,b); c = getelem(s1,3); printf(3处栈元素是是%dn,c); 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

12、 = 0; p-max = m; int i; for(i=0;imax);i+) p-elemsi = 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;ielemsi=s.elemsi; printf(%dn,p-elemsi); 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-elemsx; STACK *const push(STACK *const p, int e) /将e入栈,并返回p if(p=NULL) return NULL; else if(p-pos

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号