站外题求助
  • 板块学术版
  • 楼主cygnus_beta
  • 当前回复0
  • 已保存回复0
  • 发布时间2021/7/18 10:46
  • 上次更新2023/11/4 14:19:34
查看原帖
站外题求助
452531
cygnus_beta楼主2021/7/18 10:46

题目:循环队列

代码如下:

#include<exception>
#include<iostream>
#include<string>
#include<list>
using namespace std;

class rq_err:exception{
public:
    bool type;
    int line;
    string data;

    rq_err(const bool& t,const int& l):type(t),line(l){
        if(type)
            data="Queue is empty!\n"+to_string(line)+" step is error!\n";
        else
            data="Queue is full!\n"+to_string(line)+" step is error!\n";
    }

    const char* what(){
        return data.c_str();
    }
};

class round_que{
private:
    int elements[5]{};
    int head,tail,size;
public:
    round_que():head(0),tail(0),size(0){
        for(auto& i:elements)i=0;
    }

    ~round_que()=default;

    void push(const int& value,const int& line){
        elements[tail]=value;
        if(tail==4)tail=0;
        else ++tail;
        ++size;
        if(size==5)throw rq_err(false,line);
    }

    void pop(const int& line){
        if(size==0)throw rq_err(true,line);
        if(head==4)head=0;
        else ++head;
        --size;
    }

    const int& front(){
        return elements[head];
    }

    const int& back(){
        return tail==0?elements[4]:elements[tail-1];
    }

    const int& full_back(){
        return tail==0?elements[3]:(tail==1?elements[4]:elements[tail-2]);
    }

    bool empty()const{
        return size==0;
    }
}rq;

string ope;
list<int>element;
int buf;

int main(){
    cin>>ope;
    while(cin>>buf)element.push_back(buf);
    try{
        for(int i=0;i<ope.size();i++){
            switch(ope[i]){
                case 'A':
                    rq.push(element.front(),i+1);
                    element.pop_front();
                    break;
                case 'B':
                    rq.pop(i+1);
                    break;
                default:
                    break;
            }
        }
    }
    catch(rq_err& rqErr){
        cout<<rqErr.what();
        if(not rqErr.type)cout<<rq.front()<<' '<<rq.full_back()<<endl;
        return 0;
    }
    if(rq.empty())cout<<"Queue is empty!"<<endl;
    else cout<<rq.front()<<' '<<rq.back()<<endl;

    return 0;
}

70pts嘤嘤嘤大佬救救我吧

2021/7/18 10:46
加载中...