Concurrency Simulator UVA - 210

版权声明:菜鸟一枚~~ 有想法可在下面评论, 转载标明出处即可。 https://blog.csdn.net/KLFTESPACE/article/details/84779134


#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;
}

改下

猜你喜欢

转载自blog.csdn.net/KLFTESPACE/article/details/84779134