当前位置:网站首页>Vector 6 (inheritance)
Vector 6 (inheritance)
2022-06-23 01:04:00 【SZU healing system bug】
Catalog
Title Description
Definition CStack Class implements stack operations .CStack Class inheritance CVector class , Add private data member :
int top; // To the top of the stack
by CStack Class to add constructors CStack(int n1), Initialize stack space and stack top . This procedure requires calling the corresponding constructor of the base class to complete stack space initialization .
by CStack Class add stack , Out of the stack , Judge stack empty , The member function that determines whether the stack is full .
The main function , input data , test CStack class .
Input
The first row of stack space n
Test data for each line , The format is : operation [ data ]. Among them, the operating function is in Means put on the stack ,out Show the stack ,end Indicates the end of stack operation .
Output
After the output stack operation , Bottom to top data in the stack ; If empty stacks , Output empty.
sample input 1
5
in 10
in 20
out
in 30
in 40
out
end
out
sample output 1
10 30
Thought analysis
With a computer system 1LC-3 Knowledge , use top As a pointer offset to press the stack bounce stack .
AC Code
#include<iostream>
#include<string>
using namespace std;
class CVector
{
protected:
int* data;
int n;
public:
CVector(int n1){
n=n1;
data=new int[n1];
}
void display(){
int i;
for(i=0;i<n-1;i++)
cout<<data[i]<<' ';
cout<<data[i]<<endl;
}
~CVector(){
if(data)
delete[] data;
data=NULL;
}
};
class CStack:public CVector{
int top;
public:
CStack(int n1):CVector(n1){top=-1;}
void push(int num){data[++top]=num;}
void pop(){top--;}
bool empty(){if(top==-1)return 1;return 0;}
bool full(){if(top>=n-1)return 1;return 0;}
void display(){
for(int i=0;i<top;i++)
cout<<data[i]<<' ';
cout<<data[top]<<endl;
}
};
int main() {
int n,num;
string code;
cin>>n;
CStack stack(n);
while(cin>>code)
{
if(code=="in")
{
cin>>num;
if(stack.full()!=1)
stack.push(num);
}
else if(code=="out")
{
if(stack.empty()!=1)
stack.pop();
}
else
{
if(stack.empty())
cout<<"empty"<<endl;
else stack.display();
}
}
}边栏推荐
- OSPF综合实验
- Some thoughts about the technology of test / development programmers are very advanced, and they can't go on
- 图神经网络有哪些用途和应用?
- Because I said: volatile is a lightweight synchronized, the interviewer asked me to go back and wait for the notice!
- BGP federal comprehensive experiment
- Extend your kubernetes API using the aggregation API
- Add expiration time for localstorage
- Sélecteur de hiérarchie
- Figure what are the uses and applications of neural networks?
- 層次選擇器
猜你喜欢
![[initial launch] there are too many requests at once, and the database is in danger](/img/c1/807575e1340b8f8fe54197720ef575.png)
[initial launch] there are too many requests at once, and the database is in danger

EasyCVR使用RTMP推流时不显示界面如何解决?

最安全的现货白银的心理分析

关于测试/开发程序员技术的一些思考,水平很高超的,混不下去了......

SAP MM ME27 创建公司内STO单

SAP ui5 application development tutorial 102 - detailed explanation of the print function of SAP ui5 applications

SAP ui5 application development tutorial 103 - how to consume third-party libraries in SAP ui5 applications

Cadence spb17.4 - Chinese UI settings

07 project cost management

I've been outsourcing for four years, but I feel it's useless
随机推荐
What financial product does the new bond belong to?
SAP ui5 application development tutorial 102 - detailed explanation of the print function of SAP ui5 applications
3DMAX modeling notes (I): introducing 3DMAX and creating the first model Hello World
B tree and b+ tree
Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + commercial realization
贵金属现货白银如何呢?
股票在哪个平台买比较安全呢?
SAP mm transaction code vl04 create outbound delivery for sto
Swiftui swift tutorial 14 useful array operators
通过天天基金投资基金安全吗?我打算开户买基金
Is it safe to open a new bond? How
权限想要细化到按钮,怎么做?
2022 TIANTI match - National Finals rematch
SAP mm me27 create intra company sto order
Local deployment and problem solving of IIS in ArcGIS JS 4.23
你好,启牛商学院商学院赠送的证券账户安全吗?我该怎么开安全的股票账户来炒股
How do beginners get started quickly and learn deeply?
EasyCVR使用RTMP推流时不显示界面如何解决?
How about China International Futures Co., Ltd.? Is it a regular futures company? Is it safe to open an account online?
What is the storage structure and mode of data in the database?