Concurrency Simulator UVA - 210

18 篇文章 0 订阅
1 篇文章 0 订阅

 



#include <iostream>
#include <cstdio>
#include <deque>
#include <queue>
#include <cstring>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 1005;

int t[5], n, tim, m;
bool lock;
queue<int> qb; //阻塞block
deque<int> qw; //等待wait
vector<string> a[maxn];
int var[maxn], q[maxn];

void Print(int x){
    int tem = tim;

    while(tem > 0){
        string s = a[x][q[x]];

        if(s[2] == '='){
            tem -= t[0];

            int v = s[4] - '0';

            if(s.size() == 6)
                v = v * 10 + (s[5] - '0');
            var[s[0] - 'a'] = v;
        }
        else if(s[0] == 'p'){
            tem -= t[1];

            printf("%d: %d\n", x, var[s[6] - 'a']);
        }
        else if(s[0] == 'l'){
            tem -= t[2];

            if(lock){//当前状态已经是锁的状态,其他程序则要放到阻塞队列
                qb.push(x);
                return ;
            }
            else lock = true;
        }
        else if(s[0] == 'u'){
            tem -= t[3];
            lock = false;
            if(!qb.empty()){
                qw.push_front(qb.front());
                qb.pop();
            }
        }
        else return ;

        q[x] ++;
    }
    qw.push_back(x);
}

int main(){
    scanf("%d", &m);
    while(m --){
        scanf("%d", &n);

        for(int i = 0; i < 5; i ++)
            scanf("%d", &t[i]);

        scanf("%d", &tim);

        string s;

        for(int i = 1; i <= n; i ++){
            a[i].clear();

            while(1){
                getline(cin,s);

                if(s == "")
                    continue;

                a[i].push_back(s);

                if(s == "end")
                    break;
            }
            qw.push_back(i);
        }

        memset(var,0,sizeof(var));
        memset(q,0,sizeof(q));

        while(!qw.empty()){
            int x = qw.front();

            qw.pop_front();
            Print(x);
        }

        if(m) printf("\n");
    }
    return 0;

//下面那个想用下strstr来比较,结果只能char*   ,,,,后来改着改着就没输出了.......先放上,以后想起来了

#include <iostream>
#include <cstdio>
#include <deque>
#include <queue>
#include <cstring>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 1005;

int t[5], n, tim, m;
bool lock;
queue<int> qb; //阻塞block
deque<int> qw; //等待wait
vector<char*> a[maxn];
int var[maxn], q[maxn];

void Print(int x){
    int temp = tim;

    char* p;
    while(temp > 0){
        char* s = a[x][q[x]];

        if(strstr(s, " = ")){
            temp -= t[0];

            int v = s[4] - '0';

            if(strlen(s) == 6)
                v = v * 10 + (s[5] - '0');

            var[s[0] - 'a'] = v;
        }
        else if(strstr(s, "print")){
            temp -= t[1];

            printf("%d: %d\n", x, var[s[6] - 'a']);
        }
        else if(strstr(s, "lock")){
            temp -= t[2];

            if(lock){
                qb.push(x);
                return ;
            }
            else lock = true;
        }
        else if(strstr(s, "unlock")){
            temp -= t[3];
            lock = false;

            if(!qb.empty()){
                qw.push_front(qb.front());
                qb.pop();
            }
        }
        else return ;

        q[x] ++;
    }
    qw.push_back(x);
}

int main(){
    scanf("%d", &m);
    while(m--){
        scanf("%d", &n);

        for(int i = 0; i < 5; i ++)
            scanf("%d", &t[i]);

        scanf("%d", &tim);

        char s[16];

        for(int i = 1; i <= n; i ++){
            a[i].clear();

            while(fgets(s, 16, stdin)){

//                getchar();
//                printf("%s", s);
                a[i].push_back(s);

                if(strstr(s, "end"))
                    break;
            }
            qw.push_back(i);
        }

        memset(var, 0, sizeof(var));
        memset(q, 0, sizeof(q));

        while(!qw.empty()){
            int x = qw.front();

            qw.pop_front();
            Print(x);
        }

        if(m) printf("\n");
    }
    return 0;
}

改下

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值