【12.4】c++ primer plus 课后编程答案
来源:互联网 发布:com口测试软件 编辑:程序博客网 时间:2024/06/07 02:18
C++ PRIMER PLUS 课后答案 使用IDE为window7系统下的VS2010
/*user.h*/#ifndef USERSH_H_#define USERSH_H_#include <string>using std::ostream;using std::istream;typedef unsigned long Item;class Stack{private:enum {MAX = 10};Item * pitems;int size;int top;public:Stack(int n = MAX);Stack(const Stack & st);~Stack();bool isempty()const;bool isfull()const;bool push(const Item & item);bool pop(Item & item);Stack & operator = (const Stack & st);int showtop()const;int showpop()const;};#endif
/*userfucntion.cpp*/#include "usersh.h"#include <iostream>#include <cstring>#include <cctype>using std::cout;using std::cin;using std::ostream;using std::istream;Stack::Stack(int n /* = MAX */){if(n>MAX)n=MAX;size=n;pitems = new Item [size];top=0;}Stack::Stack(const Stack & st){size=st.size;pitems = new Item [size];for (int i=0;i<size;i++)pitems[i]=st.pitems[i];top=st.top;}Stack::~Stack(){delete [] pitems;}bool Stack::isempty()const{return top==0;}bool Stack::isfull()const{return top==size;}Stack & Stack::operator =(const Stack & st){if(this==&st)return * this;delete [] pitems;size=st.size;pitems = new Item [size];for (int i=0;i<size;i++)pitems[i]=st.pitems[i];top=st.top;return * this;}bool Stack :: push(const Item & item){if (top<size){pitems[top++]=item;return true;}elsereturn false;}bool Stack::pop(Item & item){if (top>0){item = pitems[--top];return true;}elsereturn false;}int Stack::showtop()const{return top;}int Stack::showpop()const{return pitems[top];}
/*main*/#include <iostream>#include <Windows.h>#include "usersh.h"#include <cstdlib>#include <ctime>using std::cout;using std::cin;using std::endl;int main(){ int i=0;unsigned long Po;Stack s1(5);Stack s2(s1);Stack s3=s1;Stack s4(3);s4=s1;for (;i<20;i++){if (!s1.isfull()){cin>>Po;s1.push(Po);cout<<"s1 top:"<<s1.showtop()<<endl;}elsebreak;}cout<<endl;for(i=0;i<20;i++){if (!s1.isempty()){s1.pop(Po);cout<<s1.showpop()<<" has pop"<<endl;cout<<"s1 top:"<<s1.showtop()<<endl;}elsebreak;}system("pause");return 0;}
阅读全文
0 0
- C++primer plus第六版课后编程题答案12.4
- C++primer plus第六版课后编程练习答案12.4
- 【12.4】c++ primer plus 课后编程答案
- C++PRIMER PLUS第六版课后编程答案 4.1-4.5
- C++PRIMER PLUS第六版课后编程答案 4.6-4.10
- C++PRIMER PLUS第六版课后编程答案 5.1-5.5
- C++PRIMER PLUS第六版课后编程答案 5.6-510
- C++primer plus第六版课后编程题答案 6.1
- C++primer plus第六版课后编程题答案 6.2
- C++primer plus第六版课后编程题答案 6.3
- C++primer plus第六版课后编程题答案 6.4
- C++primer plus第六版课后编程题答案 6.5
- C++primer plus第六版课后编程题答案 6.6
- C++primer plus第六版课后编程题答案 6.7
- C++primer plus第六版课后编程题答案 6.8
- C++primer plus第六版课后编程题答案 6.9
- C++primer plus第六版课后编程题答案7.1
- C++primer plus第六版课后编程题答案7.2
- Centos7 Redis安装_第四节
- 守护线程
- 2017四川省赛F题Simple Algebra
- Android集训第二天
- 蓝桥-ALGO-37-Hankson的趣味题
- 【12.4】c++ primer plus 课后编程答案
- (转)侯捷在华中科技大学的一次演讲:真正佩服的,是那些超越自己局限的人
- nodeJs爬虫小程序练习
- hashmap 中put实现的源代码
- 浏览器的重绘、回流及网页优化
- mysql数据库中文乱码问题
- 1131. Subway Map (30)[dfs剪枝+回溯]
- IOS核心动画高级一:图层树
- 死锁