C++作业

1、思维导图:

2、模板类的课上练习

cpp 复制代码
#include <iostream>
#include <stdexcept>
using namespace std;
template <typename T, int MAXSIZE>
class Stack {
private:
    T data[MAXSIZE];
    int top;
public:
    Stack() : top(-1) {}
    bool isEmpty() const {
        return top == -1;
    }
    void push(const T& x) {
        if (top == MAXSIZE - 1) {
            cout<<"stack full"<<endl;
        }
        data[++top] = x;
    }
    void pop() {
        if (isEmpty()) {
           cout<<"stack empty"<<endl;
        }
        top--;
    }
};
int main() {
    Stack<int, 10> s;
    try {
        s.push(1);
        s.push(2);
        s.push(3);
    } catch (const exception& e) {
        cout << "Error: " << e.what() << endl;
    }
    return 0;
}

3、异常处理的代码重新写一遍

相关推荐
阿里嘎多学长1 小时前
2026-10-20 GitHub 热点项目精选
开发语言·程序员·github·代码托管
布吉岛的石头1 小时前
Java 程序员第 50 阶段2:预训练的目标函数:自回归语言建模本质
java·开发语言·人工智能·深度学习·回归·transformer
小羊没烦恼!2 小时前
铁道部新客票系统设计(二)
开发语言·前端·数据库·python·oracle·c#·php
quantdash_cc3 小时前
从实时行情到价格观察表:用 Python 和 Pandas 构建策略输入数据
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
昂好的day3 小时前
VisionMaster 通信模块实战(上):Modbus、TCP 收发、协议组装解析
开发语言·网络·网络协议·tcp/ip·c#
努力实干4 小时前
面向对象设计原则——C++
开发语言·c++·数码相机
NetX行者6 小时前
四语言 Hello World 背后:编译模型与运行时对比
开发语言
Pin_Qing7 小时前
账单没超,请求先被拒了
大数据·开发语言·人工智能
2601_962885727 小时前
如何用 Python 计算 VaR 和 CVaR(在险价值)?
开发语言·python
郝亚军7 小时前
manifest 中 `sha256` 与 `signValue` 的作用
java·开发语言·数据结构