c++day6

尝试使用模板类,实现顺序栈。 

#include <iostream>
#define MAX 8
using namespace std;
typedef struct{
    int a;
    int b;
}test_t;

template <typename T>
class Stack
{
private:
    T *data;
    int top;
public:
    Stack() :data(nullptr),top(-1){}
    //创建顺序栈
    int create_stack();
    //出栈
    int pop_stack();
    //入栈
    int push_stack(T data);
    //遍历
    int show_stack();
    //释放顺序表
    int free_stack();
};
template <typename T>
int Stack<T>::create_stack(){
    this->data=new T[MAX];
    this->top=-1;
    return 0;
}
template <typename T>
int Stack<T>::pop_stack(){
    if(this->data==nullptr||this->top==-1){
        cout<<"入参错误"<<endl;
        return -1;
    }
    cout<<this->data[this->top]<<endl;
    this->top--;
    return 0;
}
template <typename T>
int Stack<T>::push_stack(T data){
    if(this->data==nullptr){
        cout<<"入参错误"<<endl;
        return -1;
    }
    top++;
    this->data[this->top]=data;
    return 0;
}
void show(test_t data){
    cout<<"a="<<data.a<<" "<<"b="<<data.b<<endl;
}
template <typename T>
int Stack<T>::show_stack(){
    if(this->data==nullptr){
        cout<<"入参错误"<<endl;
        return -1;
    }
    for(int i=this->top;i>=0;i--){
        show(*(data+i));
    }
    return 0;
}
template <typename T>
int Stack<T>::free_stack(){
    if(this->data==nullptr){
        cout<<"入参错误"<<endl;
        return -1;
    }
    delete []this->data;
    return 0;
}
int main()
{

    Stack<test_t> s;
    test_t t1={10,20};
    test_t t2={30,40};
    test_t t3={50,60};
    s.create_stack();
    s.push_stack(t1);
    s.push_stack(t2);
    s.push_stack(t3);
    s.show_stack();
//    s.pop_stack();
    s.free_stack();
    return 0;
}

 

异常处理 

#include <iostream>

using namespace std;
int fun(int a,int b){
    if(0==b){
        throw int(1);
    }
    return a/b;
}

int main()
{
    try {
        fun(2,0);
    } catch (int a) {
        if(a==1){
            cout<<"除数为0"<<endl;
        }
    }
    cout<<"hello"<<endl;
    return 0;
}

 

相关推荐

  1. MSc CDA Take-Home

    2024-04-30 10:34:02       61 阅读
  2. CDA一级备考策略分享

    2024-04-30 10:34:02       32 阅读
  3. CDA-LevelⅡ【考题整理-带答案】

    2024-04-30 10:34:02       50 阅读
  4. Spring Data访问Elasticsearch----CDI集成

    2024-04-30 10:34:02       37 阅读
  5. CDA Level Ⅰ 2023认证考试大纲

    2024-04-30 10:34:02       79 阅读
  6. web server apache tomcat11-33-CDI

    2024-04-30 10:34:02       34 阅读
  7. Spring Data访问 MongoDB(十六)----CDI集成

    2024-04-30 10:34:02       42 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-30 10:34:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-30 10:34:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-30 10:34:02       87 阅读
  4. Python语言-面向对象

    2024-04-30 10:34:02       96 阅读

热门阅读

  1. spring-ioc

    2024-04-30 10:34:02       32 阅读
  2. 【蓝桥杯2024真题】好数

    2024-04-30 10:34:02       35 阅读
  3. 算法的时间复杂度和空间复杂度

    2024-04-30 10:34:02       32 阅读
  4. 无人机证书的含金量

    2024-04-30 10:34:02       28 阅读
  5. Spring MVC、Spring Boot和Spring Cloud 三者区别和联系

    2024-04-30 10:34:02       25 阅读
  6. 消息队列 RabbitMQ python实战

    2024-04-30 10:34:02       33 阅读
  7. Codeforces Round 941 (Div. 2) F.Missing Subarray Sum

    2024-04-30 10:34:02       34 阅读
  8. Cocos Creator 3D物理引擎的碰撞检测与触发器详解

    2024-04-30 10:34:02       30 阅读
  9. Go语言 Channel

    2024-04-30 10:34:02       31 阅读
  10. html如何实现按钮跳转,以及访问随机跳转

    2024-04-30 10:34:02       34 阅读
  11. Halcon如何制作标定板的关键点

    2024-04-30 10:34:02       33 阅读