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、思维导图

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,短作业优先(Shortest Job First,SJF)算法是一种非抢占式调度算法,它按照作业的执行时间来对它们进行排序,然后按照顺序依次执行。在实现这个算法时,我们可以使用C++编写一个简单的程序。 以下是一个使用SJF算法调度作业的C++程序: ```cpp #include<iostream> #include<algorithm> using namespace std; struct task { int id; // 任务编号 int burst_time; // 任务执行时间 }; bool cmp(task a, task b) { // 按执行时间从小到大排序 return a.burst_time < b.burst_time; } int main() { int n; cout << "请输入任务数量:"; cin >> n; task t[n]; for(int i = 0; i < n; i++) { cout << "请输入任务" << i+1 << "的执行时间:"; cin >> t[i].burst_time; t[i].id = i+1; } sort(t, t+n, cmp); // 按执行时间排序 cout << "SJF算法执行顺序为:"; int cur_time = 0; for(int i = 0; i < n; i++) { cout << t[i].id << " "; cur_time += t[i].burst_time; } cout << endl; cout << "平均等待时间为:" << (double)cur_time/n << endl; return 0; } ``` 在这个程序中,我们首先定义了一个结构体 `task` 来存储每个任务的编号和执行时间。然后,我们通过用户输入来获取任务数量和每个任务的执行时间,并将它们存储在一个数组 `t` 中。接下来,我们使用 `sort` 函数按照执行时间从小到大对任务进行排序。最后,我们按照任务的执行顺序输出它们的编号,并计算出平均等待时间。 希望这个程序能够满足您的需求!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值