ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

UVA - 210:Concurrency Simulator

2021-10-20 23:03:21  阅读:486  来源: 互联网

标签:readyQueue Statement 210 int self Concurrency UVA line type


题目链接:https://vjudge.net/problem/UVA-210

题目分析

就是一道模拟题,但是细节有点多。
写代码两个小时,调试代码用了两天。。。很长时间不刷题了,这道虽然算法简单但是细节满满的题目对我来说是一个很好的热身。

  • 尽量不要去使用匿名名字空间,发现对调试过程不怎么友好(陈硕大大说的对)。
  • 使用枚举类型对程序的可读性、可维护性的提升非常大
  • 重载输入输出运算符的时候一定要记得返回stream对象
  • 这种带有switch语句的,可以使用Stragety模式,这里没有使用,因为每条语句只有执行,没有复杂的行为
  • 多组数据一定要有init函数清空数据
  • 使用智能指针不要用引用,这一点点内存的消耗不算什么,但是使用引用往往会带来错误:当我们在对象内部不小心将其释放掉的时候就会产生段错误,而且很难排查
  • 良好的抽象是巧妙设计的基础,每个类应该只负责其分内的事,不要尝试让其去做超过他权限的事,因为这样往往会让事情一团糟。封装、抽象能够帮助我们处理复杂的情况。
  • 通过ulimit -c unlimited命令开启生成core文件可以帮助进行调试
  • gdb调试开始的时候可以使用run < input.txt重定向输入和输出
  • cgdb真好用

AC代码

#include <iostream>
#include <array>
#include <vector>
#include <string>
#include <deque>
#include <memory>

using namespace std;

namespace {
enum TYPE {
    ASSIGN, PRINT, LOCK, UNLOCK, END
};
int n, quantum;

constexpr int MAXN = 26;
array<int, MAXN> alpha = {};

bool lock = false;
class Statement {
public:
    static constexpr int MAXN = 5;
    static array<int, MAXN> cost;
    static void init();
    string line;
    TYPE type;
    int var;
    int constant;
    int exec();
    friend istream& operator >> (istream& is, Statement &self);
    friend ostream& operator << (ostream& os, const Statement &self);
};

class Program {
public:
    vector<Statement> statements;
    int idx = 0;
    int id;
    bool exec();
    Program(int _id) : id(_id) {}

    friend istream& operator >> (istream& is, Program& self);
    friend ostream& operator << (ostream& os, const Program &self);
};
deque<shared_ptr<Program>> readyQueue, blockedQueue;
shared_ptr<Program> p;

ostream& operator << (ostream& os, const Statement &self) {
//    os << self.type;
    switch (self.type) {
        case ASSIGN:
            os << static_cast<char>('a' + self.var) << " = " << self.constant;
            break;
        case PRINT:
            os << "print " << static_cast<char>('a' + self.var);
            break;
        case LOCK:
            os << "lock";
            break;
        case UNLOCK:
            os << "unlock";
            break;
        case END:
            os << "end";
            break;
    }
    return os;
}

ostream& operator << (ostream& os, const Program &self) {
    os << "ID:" << self.id << "\n";
    for (auto s : self.statements) {
        os << s << "\n";
    }
    os << "\n";
    return os;
}

int Statement::exec() {
    switch (type) {
    case ASSIGN:
//        cout << "Test:" << line << endl;
//        cout << "Test:" << readyQueue.front()->id << " " << static_cast<char>('a' + var) << " = " << constant << endl;
        alpha[var] = constant;
        return cost[type];
        break;
    case PRINT:
        cout << p->id << ": " << alpha[var] << "\n";
        return cost[type];
        break;
    case END:
//        readyQueue.pop_front();
//        cout << "Test:" << type << " " << cost[type] << endl;
//        for (int i = 0; i < Statement::MAXN; ++i) {
//            cout << cost[i] << " ";
//        }
//        cout << endl;
        return cost[type];
        break;
    case LOCK:
        if (lock) {
            blockedQueue.push_back(p);
            return -1;
        } else {
            lock = true;
            return cost[type];
        }
        break;
    case UNLOCK:
        if (!blockedQueue.empty()) {
            readyQueue.push_front(blockedQueue.front());
            blockedQueue.pop_front();
        }
        lock = false;
        return cost[type];
    default:
        break;
    }
}

bool Program::exec() {
    int time = quantum;
    while (time > 0) {
        int ret = statements[idx].exec();
        if (ret == -1) {
            //lock
            return false;
        }
        if (++idx == statements.size()) {
            //end
            return false;
        }
        time -= ret;
    }
    return true;
}

constexpr int Statement::MAXN;
array<int, Statement::MAXN> Statement::cost;

void Statement::init() {
    for (int i = 0; i < MAXN; ++i) cin >> cost[i];
}

istream& operator >> (istream& is, Statement &self) {
    auto &line = self.line;
    getline(is, line);
    if (line[1] == ' ') {
        self.type = ASSIGN;
        self.var = line[0] - 'a';
        self.constant = stoi(line.substr(4));
    } else if (line[0] == 'p') {
        self.type = PRINT;
        self.var = line[6] - 'a';
    } else if (line[0] == 'l') {
        self.type = LOCK;
    } else if (line[0] == 'u') {
        self.type = UNLOCK;
    } else {
        self.type = END;
    }
    return is;
}
istream& operator >> (istream& is, Program& self) {
    auto &s = self.statements;
    do {
        s.push_back(Statement());
        is >> s.back();
    } while(s.back().type != END);
    return is;
}


}

void init() {
    readyQueue.clear();
    blockedQueue.clear();
    std::fill(alpha.begin(), alpha.end(), 0);
    lock = false;
}

int main(int argc, char *argv[])
{
    ios::sync_with_stdio(false);
    int T, id = 0;
    cin >> T;
    for (int caseIdx = 0; caseIdx < T; ++caseIdx) {
        if (caseIdx) cout << "\n";
        init();
        cin >> n;
        Statement::init();
        cin >> quantum;
        string line;
        getline(cin, line);
        for (int i = 0; i < n; ++i) {
            readyQueue.push_back(make_shared<Program>(i + 1));
            cin >> *readyQueue.back();
        }
//        for (auto p : readyQueue) {
//            cout << *p;
//        }
        while (!readyQueue.empty()) {
            //TODO:加上了&导致出错
            p = readyQueue.front();
            readyQueue.pop_front();
            if (p->exec()) {
                readyQueue.push_back(p);
            }
//            cout << "Test:[readyQueue]\n";
//            for (auto p : readyQueue) {
//                cout << *p;
//            }
//            cout << "Test:[blockedQueue]\n";
//            for (auto p : blockedQueue) {
//                cout << *p;
//            }
//            cout << flush;
        }
//        cout << "====================================\n";
    }
    return 0;
}

标签:readyQueue,Statement,210,int,self,Concurrency,UVA,line,type
来源: https://blog.csdn.net/T_T233333333/article/details/120876783

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有