#include <iostream>
#include "Hash.h"
#include <stack>
using namespace std;
int main()
{
for (long i = 0;i<10000;i++)//初始化哈希数组 即哈希表表头,全为空
{
Hash_Array[i].next = NULL;
}
for (long k = 1;k<=999999;k++)// 插入资源 LO
{
Hash_Insert(k);
}
stack <int> App;//保存批准号的栈
for (int i = 100;i > 0;i--)
{
App.push(i);
}
cout<<"*****封锁管理子系统模拟 ****"<<endl;
cout<<"* ----请输入: *"<<endl;
cout<<"* 1.封锁资源 *"<<endl;
cout<<"* 2.解锁资源 *"<<endl;
cout<<"* 3.查询封锁信息 *"<<endl;
cout<<"****************************"<<endl;
int flag=100;
while (flag != 0)
{
cin.clear(); cin.sync();
cin>>flag;
switch(flag)
{
case 1:
{
long temp_num;
cout<<"请输入要封锁的资源号:"<<endl;
cin>>temp_num;
int sta;
cout<<"请输入封锁方式: 0为排他 1为共享"<<endl;
cin>>sta;
string str;
cout<<"请输入用户名:"<<endl;
cin>>str;
LR *lr = new LR;
lr->name = str;
lr->status = sta;
lr->Appnum = App.top();
App.pop();
LO *temp_lo = new LO;
temp_lo->next = Hash_Array[Hash_Function(temp_num)].next;
while(temp_num != temp_lo->next->num)
{
temp_lo->next = temp_lo->next->next;
}
if (temp_lo->next->status == 0)
{
cout<<"该资源正被排他封锁,本请求已进入等待队列"<<endl;
cout<<"批准号为:"<<lr->Appnum<<endl;
temp_lo->next->waite.enqueue(*lr);//入队
temp_lo->next->status = sta;
}
else
{
if (temp_lo->next->status == 1&&sta ==0)
{
cout<<"该资源正被共享封锁,本请求为排他封锁,已进入等待队列"<<endl;
cout<<"批准号为:"<<lr->Appnum<<endl;
temp_lo->next->waite.enqueue(*lr);//入队
temp_lo->next->status = sta;
}
else
{
temp_lo->next->active.enqueue(*lr);//入队
temp_lo->next->status = sta;
cout<<"已成功获得封锁,批准号为:"<<lr->Appnum<<endl;
}
}
break;
}
case 2:
{
cout<<"请输入要解封的资源号:";
long temp_num;
cin>>temp_num;
LO *temp_lo = new LO;
temp_lo->next = Hash_Array[Hash_Function(temp_num)].next;
while(temp_num != temp_lo->next->num)
{
temp_lo->next = temp_lo->next->next;
}
if (temp_lo->next->status == -1)
{
cout<<"该资源未被封锁"<<endl;
}
else
{
cout<<"活动队列:"<<endl;
cout<<"用户名-批准号 "<<endl;
temp_lo->next->active.show();
cout<<"请输入要解锁的批准号";
int temp_app;
cin>>temp_app;
int it;
if (temp_lo->next->active.remove(it,temp_app))
{
App.push(it);
if (temp_lo->next->active.size == 0)
{
temp_lo->next->status = -1;
}
cout<<"解锁成功"<<endl;
temp_lo->next->move();
}
else cout<<"解锁失败"<<endl;
}
break;
}
case 3:
{
cout<<"请输入要查询的资源号";
long temp_num;
cin>>temp_num;
//查找。
LO *temp_lo = new LO;
temp_lo->next = Hash_Array[Hash_Function(temp_num)].next;
while(temp_num != temp_lo->next->num)
{
temp_lo->next = temp_lo->next->next;
}
cout<<"活动队列:"<<endl;
cout<<"用户名-批准号 "<<endl;
temp_lo->next->active.show();
cout<<endl<<endl;
cout<<"等待队列:"<<endl;
cout<<"用户名-批准号 "<<endl;
temp_lo->next->waite.show();
delete temp_lo;
break;
}
default: {
cout<<"输入有误!请重新输入……"<<endl;
break;
}
}
}
return 0;
}