📄 ve-6b.tmp
字号:
// ListDialog.cpp : implementation file
//
#include "stdafx.h"
#include "Banker.h"
#include "ListDialog.h"
#include "RsDialog.h"
// ListDialog dialog
IMPLEMENT_DYNAMIC(ListDialog, CDialog)
ListDialog::ListDialog(CWnd* pParent /*=NULL*/,UINT p,UINT r)
: CDialog(ListDialog::IDD, pParent)
, nP(p)
, nR(r)
, SafeSquence(_T(""))
, safety(_T(""))
, nRelease(0)
{
}
ListDialog::~ListDialog()
{
delete []Available;
for(UINT i=0; i<nP; i++)
delete []Allocation[i];
delete []Allocation;
for(UINT i=0; i<nP; i++)
delete []MAX[i];
delete []MAX;
for(UINT i=0; i<nP; i++)
delete []Need[i];
delete []Request;
delete []Work;
delete []Finish;
delete []Path;
}
void ListDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, listCtrl);
DDX_Text(pDX, IDC_SAFE, SafeSquence);
DDX_Text(pDX, IDC_EDIT1, safety);
}
BEGIN_MESSAGE_MAP(ListDialog, CDialog)
ON_BN_CLICKED(IDOK, &ListDialog::OnBnClickedOk)
ON_BN_CLICKED(IDC_BUTTON1, &ListDialog::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &ListDialog::OnBnClickedButton2)
ON_NOTIFY(NM_RCLICK, IDC_LIST1, &ListDialog::OnNMRclickList1)
END_MESSAGE_MAP()
// ListDialog message handlers
void ListDialog::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
OnOK();
}
BOOL ListDialog::OnInitDialog()
{
CDialog::OnInitDialog();
LONG lStyle;
lStyle = GetWindowLongW(listCtrl.m_hWnd, GWL_STYLE);//获取当前窗口style
lStyle &= ~LVS_TYPEMASK; //清除显示方式位
lStyle |= LVS_REPORT; //设置style
SetWindowLong(listCtrl.m_hWnd, GWL_STYLE, lStyle);//设置style
DWORD dwStyle = listCtrl.GetExtendedStyle();
dwStyle |= LVS_EX_FULLROWSELECT;//选中某行使整行高亮(只适用与report风格的listctrl)
dwStyle |= LVS_EX_GRIDLINES;//网格线(只适用与report风格的listctrl)
dwStyle |= LVS_EX_CHECKBOXES;//item前生成checkbox控件
listCtrl.SetExtendedStyle(dwStyle); //设置扩展风格
listCtrl.SetBkColor(RGB(180,150,240));
listCtrl.SetTextBkColor(RGB(180,150,240));
listCtrl.InsertColumn(0,_T("资源---->"), LVCFMT_LEFT, 60 );//插入列
CString str;
for(UINT i=0; i<nR; i++)
{
str.Format("R%d",i);
listCtrl.InsertColumn( i+1,str, LVCFMT_LEFT, 30);//插入列
}
str.Format("总 量");
listCtrl.InsertItem(0,str);
for(UINT i=0; i<nR; i++)
{
str.Format("%d",this->Available[i]);
listCtrl.SetItemText(0,i+1,str);
}
str.Format("余 量");
listCtrl.InsertItem(1,str);
for(UINT i=0; i<nR; i++)
{
str.Format("%d",this->Available[i]);
listCtrl.SetItemText(1,i+1,str);
}
for(UINT i=1; i<=nP; i++)
{
str.Format("P%d已占",i-1);
listCtrl.InsertItem(i*2,str);
for(UINT j=1; j<=nR; j++)
{
str.Format("%d",this->Allocation[i-1][j-1]);
listCtrl.SetItemText(i*2,j,str);
}
str.Format("P%d总需",i-1);
listCtrl.InsertItem(i*2+1,str);
for(UINT j=1; j<=nR; j++)
{
str.Format("%d",this->MAX[i-1][j-1]);
listCtrl.SetItemText(i*2+1,j,str);
}
}
return true;
}
void ListDialog::OnBnClickedButton1()
{
this->Allot();
// TODO: Add your control notification handler code here
}
bool ListDialog::Safe()
{
int flag;
int l = 0;
for (UINT i=0; i<nP;i++)
{
if (Finish[i]==0 && !Release[i])
{
flag = 0;
for (UINT j=0; j<nR; j++)
{
if (Work[j]>=Need[i][j])
flag += 1;
}
if(flag==nR)
{
Path[l]=i;
Finish[i]=1;
for (UINT j=0; j<nR;j++)
Work[j] += Allocation[i][j];
l++;
i = -1;//!!!!!!!!!!!!!!!!!!!
}
}
}
for (UINT i=0; i<nP && !Release[i]; i++)
this->Finish[i]=0;
return (l==nP-nRelease);
// TODO: Add your control notification handler code here
}
void ListDialog::OnBnClickedButton2()
{
AfxMessageBox(_T(" 先在列表框中选中一进程,\n然后单击右键 OR “增添资源”键即可为其分配更多资源!"));
// TODO: Add your control notification handler code here
}
void ListDialog::Allot()
{
POSITION pos = listCtrl.GetFirstSelectedItemPosition();
if (pos == NULL)
{
AfxMessageBox(_T("No items were selected!"));
return;
}
int nItem = listCtrl.GetNextSelectedItem(pos);
if(nItem<2)
{
AfxMessageBox(_T("The item selected is not a process!"));
return;
}
//Lclass.GetItemText(nItem,0,(LPTSTR)cl.clno,10);
int record = nItem;
nItem /= 2;
nItem -= 1;
if(Release[nItem])
{
AfxMessageBox(_T("The process selected has been executed!"));
return;
}
CString hint = _T("");
hint.Format("请为P%d增添更多资源!",nItem);
RsDialog* rd = new RsDialog(NULL,nP,nR,hint);
do
{
if(rd->DoModal()==IDOK && rd->boolean)
for(UINT i=0; i<nR; i++)
Request[i] = rd->Data[i];
else
return;
}while(!rd->boolean);
for (UINT j=0; j<nR; j++)
{
if (Request[j] > Need[nItem][j])
{
hint.Format("出错!资源%d申请量(%d)大于剩余需要量(%d)!",j,Request[j],Need[nItem][j]);
AfxMessageBox(hint);
return;
}
}
for (UINT j=0; j<nR; j++)
{
if (Request[j]>Available[j])
{
hint.Format("出错!资源%d余量(%d)小于申请量(%d)!",j,Available[j],Request[j]);
AfxMessageBox(hint);
return;
}
}
//改变Avilable、Allocation、Need的值
for (UINT j=0; j<nR; j++)
{
Available[j] = Available[j]-Request[j];
Allocation[nItem][j] = Allocation[nItem][j]+Request[j];
Need[nItem][j] = Need[nItem][j]-Request[j];
Work[j] = Available[j];
}
bool release = true;
for (UINT j=0; j<nR; j++)
{
if (Need[nItem][j]!=0)
{
release = false;
break;
}
}
if(release)
{
for (UINT j=0; j<nR; j++)
{
Work[j] += Allocation[nItem][j];
Available[j] += Allocation[nItem][j];
Allocation[nItem][j] = 0;
}
Release[nItem] = true;
Finish[nItem] = 1;
nRelease += 1;
hint.Format("进程%d已完成退出,以后不可再为其分配资源!",nItem);
AfxMessageBox(hint);
}
if(!Safe())
{
GetDlgItem(IDC_EDIT1)->SetWindowTextA(_T("X"));
GetDlgItem(IDC_SAFE)->SetWindowTextA(_T(""));
//恢复数据
for (UINT j=0; j<nR; j++)
{
Available[j] = Available[j]+Request[j];
Allocation[nItem][j] = Allocation[nItem][j]-Request[j];
Need[nItem][j] = Need[nItem][j]+Request[j];
Work[j] = Available[j];
}
}
else
{
GetDlgItem(IDC_EDIT1)->SetWindowTextA(_T("√"));
hint.Format("");
UINT i;
for (i=0; i<nP-nRelease-1; i++)
{
CString temp;
temp.Format("P%d-->",Path[i]);
hint += temp;
}
CString temp;
temp.Format("P%d",Path[i-1]);
hint += temp;
GetDlgItem(IDC_SAFE)->SetWindowTextA(hint);
}
for(UINT i=0; i<nR; i++)
{
hint.Format("%d",this->Available[i]);
listCtrl.SetItemText(1,i+1,hint);
}
for(UINT j=1; j<=nR; j++)
{
hint.Format("%d",this->Allocation[nItem][j-1]);
listCtrl.SetItemText(record,j,hint);
}
}
void ListDialog::OnNMRclickList1(NMHDR *pNMHDR, LRESULT *pResult)
{
this->Allot();
// TODO: Add your control notification handler code here
*pResult = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -