当前位置:网站首页>OOP stack class template (template +ds)
OOP stack class template (template +ds)
2022-06-25 04:43:00 【SZU healing system bug】
Catalog
Title Description
With the help of function template, stack operation is realized .
Stack is an advanced and backward data structure , Its insertion 、 Deletion can only be performed at the top of the stack .Push For stack operation , Insert ,Pop For stack out operation , Delete immediately .
Stack operation is similar to stacking plates , The first plate is at the bottom , Put it on the plate after . When you want to pick up the plate , Just take it from the top .
For example, stack data 1 To 2 Until then 3, that 3 At the top ,1 At the bottom . When you want to stack data , Namely 3 First out , Next is 2, And finally 1 Out of the stack .
You are required to define the stack structure by yourself , And use function template and class template to complete char,int and float Data processing . among , We define :
Input
The first line is the number of test data
For each group of test data , The first row is data type , The second line is the operand
For each operation Push or Pop operation . it is to be noted that , When the stack is empty, you should give Error Tips
Output
For each stack , No output required . For each stack , Output out of stack elements or give Error Tips . When all operations are completed , Output the remaining elements in the stack in reverse order
sample input 1
3
I
6
Push 6
Push 1
Push 1
Pop
Pop
Pop
C
4
Pop
Push a
Push a
Pop
F
8
Push 4.1
Push 4.2
Push 14.1
Push 4.2
Push 1314
Push 1314
Pop
Pop
sample output 1
1 is popped from stack!
1 is popped from stack!
6 is popped from stack!
Empty Stack!
Error!
a is popped from stack!
a
1314 is popped from stack!
1314 is popped from stack!
4.2 14.1 4.2 4.1
AC Code
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<iomanip>
using namespace std;
template<class kind>
class stack {
kind p[10000];
int top = -1;
public:
void push() {
kind num;
cin >> num;
p[++top] = num;
}
void pop() {
if (empty()) {
cout << "Error!" << endl;
} else {
cout << p[top--] << " is popped from stack!" << endl;
}
}
bool empty() {
if (top < 0)
return 1;
return 0;
}
void display() {
if (empty())
cout << "Empty Stack!" << endl;
else {
for (int i = top;i>=0;i--)
cout << p[i] << ' ';
cout << endl;
}
}
};
int main() {
int test, count;
char code;
string index;
cin >> test;
while (test--) {
cin >> code >> count;
if (code == 'I') {
stack<int> temp;
while (count--) {
cin >> index;
if (index == "Push")
temp.push();
else
temp.pop();
}
temp.display();
} else if (code == 'C') {
stack<char> temp;
while (count--) {
cin >> index;
if (index == "Push")
temp.push();
else
temp.pop();
}
temp.display();
} else {
stack<float> temp;
while (count--) {
cin >> index;
if (index == "Push")
temp.push();
else
temp.pop();
}
temp.display();
}
}
}边栏推荐
- Thorough understanding of database transactions
- Gbase 8s stored procedure execution and deletion
- LabVIEW开发气体调节器
- cannot import name ‘escape’ from ‘jinja2’【成功解决】
- Use text analysis to identify the main gender in a text
- js的arguments
- Mongodb cluster
- [Flink] problems and solutions of the continuous growth of checkpoint size in rocksdb incremental mode
- Why PHP is not safe
- php封装curl发送get、post请求方法,并使用
猜你喜欢

Easyrecovery15 very easy to use computer data recovery software

2.0SpingMVC使用RESTful

Mongodb cluster

LabVIEW development gas regulator

Xiaobai learns MySQL - Statistical 'opportunism'

Php7.2 add JPEG extension
![[esp32 learning path 6 - Flash encryption]](/img/4c/f317ca4823dca50a9bccd285967ab0.png)
[esp32 learning path 6 - Flash encryption]

js的sort()函数

Record the problem of C # print size once

CTF_ Web:php weak type bypass and MD5 collision
随机推荐
GBASE 8s的隔离级别介绍
大话云原生数据库中的存算分离
leetcode1221. Split balance string
Upgrade PHP to php7 The impact of X (2), the obsolescence of mcrypt decryption
Gbase 8s stored procedure syntax structure
Easyrecovery15 very easy to use computer data recovery software
CTF_ Web: Learn flask template injection (SSTI) from 0
Unit test coverage
How to screen out words related to products and eliminate invalid words accurately
Gbase 8s stored procedure execution and deletion
[esp32 learning path 6 - Flash encryption]
Musk released humanoid robot. Why is AI significant to musk?
GBase 8s的封锁技术的基本介绍
计算学生成绩等级(虚函数和多态)
Upgrade PHP to php7 X (III) failure of wechat payment callback
Separation of storage and computing in Dahua cloud native database
Solution of gbase 8s livelock and deadlock
记录小知识点
GBASE 8s 总体架构
Introduction to the isolation level of gbase 8s