C++Day6作业

1、模板实现顺序栈

#include <iostream>

using namespace std;

template <typename T>
class Stack
{
    T* S;  //数组
    int size;//栈容量
    int top; //栈顶元素下标
public:
    //有参构造
    Stack(int size):S(new T[size]),size(size),top(-1){}
    //初始化时直接将栈顶元素下标设置为-1

    //析构函数
    ~Stack(){delete[] S;}

    //判空
    bool empty(){return top == -1;}

    //判满
    bool full(){return top == size-1;}

    //入栈
    void push(const T &item)
    {
        if(full())
        {
            cout<<"栈满!无法入栈!";
        }else
        {
            S[++top] = item;
            cout<<"入栈成功"<<endl;
        }
    }

    //出栈
    void pop()
    {
        if(empty())
        {
            cout<<"栈空!无法出栈!"<<endl;
        }else
        {
            top--;
            cout<<"出栈成功!"<<endl;
        }
    }

    //输出栈中元素
    void show()
    {
        int i=0;
        if(empty())
        {
            cout<<"栈空"<<endl;
        }
        else
        {
           for(i=top;i>=0;i--)
           {
               cout<<S[i]<<endl;
           }
        }
    }

    //获取当前栈内元素个数
    void num()
    {
        cout<<"当前栈中元素个数:"<<top+1<<endl;
    }
};
int main()
{
    Stack<int> sta(5);
    sta.empty();
    sta.push(1);
    sta.push(2);
    sta.push(3);
    sta.push(4);
    sta.push(5);
    sta.full();
    sta.show();
    sta.pop();
    sta.pop();
    sta.num();
    sta.show();

    return 0;
}

2、异常处理

#include <iostream>

using namespace std;

int fun(int a, int b)
{
    if(b == 0)//判断可能发生的异常
    {
        throw int(2);//抛出异常
    }else if(b == 2)
    {
        throw int(3);
    }
    return a/b;
}
int main()
{
    //cout<<fun(2,0)<<endl; //除数为零导致程序无法运行
    //尝试使用try...catch对异常进行处理
    try
    {
        //存放所有可能抛出异常的代码
        fun(2,0);
        fun(2,2); //catch中只能接收一条异常,建议try中只放一条代码
    } catch (int ret) //catch中对具体的异常类型进行判断
    {
        if(ret ==2)
        {
        cout<<"除数为零"<<endl;
        }else if(ret ==3)
        {
            cout<<"test"<<endl;
        }
    }
    cout << "Hello World!" << endl;
    return 0;
}

3、思维导图

相关推荐

  1. <span style='color:red;'>作业</span><span style='color:red;'>6</span>.<span style='color:red;'>6</span>

    作业6.6

    2024-05-03 23:34:02      33 阅读
  2. 作业2024/2/6

    2024-05-03 23:34:02       44 阅读
  3. 2.6作业

    2024-05-03 23:34:02       46 阅读
  4. <span style='color:red;'>作业</span>2.<span style='color:red;'>6</span>

    作业2.6

    2024-05-03 23:34:02      42 阅读
  5. 2.6 作业

    2024-05-03 23:34:02       51 阅读

最近更新

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

    2024-05-03 23:34:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-03 23:34:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-03 23:34:02       87 阅读
  4. Python语言-面向对象

    2024-05-03 23:34:02       96 阅读

热门阅读

  1. web server apache tomcat11-29-Windows Authentication

    2024-05-03 23:34:02       32 阅读
  2. npm详解

    2024-05-03 23:34:02       40 阅读
  3. 基于FPGA的数字信号处理(4)--浮点数的定点化

    2024-05-03 23:34:02       35 阅读
  4. 【C++】右值引用

    2024-05-03 23:34:02       39 阅读
  5. Golang 设计模式(行为型)

    2024-05-03 23:34:02       35 阅读
  6. 互斥关系和同步关系

    2024-05-03 23:34:02       34 阅读
  7. SDWebImage源码分析

    2024-05-03 23:34:02       27 阅读
  8. TypeScript 的 interface

    2024-05-03 23:34:02       31 阅读
  9. Qt :浅谈在大型项目中使用信号管理器

    2024-05-03 23:34:02       37 阅读
  10. Python的主要应用领域

    2024-05-03 23:34:02       28 阅读
  11. windows qt sdk 安装

    2024-05-03 23:34:02       41 阅读
  12. 数据结构---查找法

    2024-05-03 23:34:02       36 阅读
  13. Vue表单项赋值后无法输入问题解决

    2024-05-03 23:34:02       27 阅读
  14. Arrays

    2024-05-03 23:34:02       35 阅读
  15. Linux 内核 delayacct 原理分析

    2024-05-03 23:34:02       33 阅读