銀行家算法-C++源程序_第1頁
銀行家算法-C++源程序_第2頁
銀行家算法-C++源程序_第3頁
已閱讀5頁,還剩3頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、#include "stdafx.h"#include<stdio.h>#include<string.h>#include<iostream.h>#include<stdlib.h>#include<iomanip.h>#include<conio.h>const int MAX_P=20;const int MAXA=10; / 定義 A 類資源的數(shù)量 const int MAXB=5;const int MAXC=7;typedef struct nodeint a;int b;int c;int

2、 remain_a;int remain_b;int remain_c;bank;typedef struct node1 char name20; int a; int b; int c; int need_a; int need_b; int need_c;process;bank banker;process processesMAX_P;int quantity;/初始化函數(shù)void initial()int i;banker.a=MAXA;banker.b=MAXB;banker.c=MAXC;banker.remain_a=MAXA; banker.remain_b=MAXB; b

3、anker.remain_c=MAXC;for(i=0;i<MAX_P;i+) strcpy(,""); processesi.a=0; processesi.b=0; processesi.c=0; processesi.need_a=0; processesi.need_b=0; processesi.need_c=0;/新加作業(yè)void add()char name20;int flag=0;int t;int need_a,need_b,need_c;int i;cout<<endl;"<<end

4、l;cout<<" 新加作業(yè) "<<endl;cout<<"cout<<" 請輸入新加作業(yè)名 :" cin>>name;for(i=0;i<quantity;i+) if(!strcmp(,name) flag=1;break;if(flag)cout<<" 錯誤 ,作業(yè)已存在 "<<endl;elsecout<<" 本作業(yè)所需 A 類資源 :"cin>>need

5、_a;cout<<"本作業(yè)所需B類資源:";cin>>need_b;cout<<"本作業(yè)所需C類資源:"cin>>need_c;t=1;cout<<need_a<<banker.remain_a; if(need_a>banker.remain_a)cout<<"錯誤,所需A類資源大于銀行家所剩 t=0;if(need_b>banker.remain_b)cout<<"錯誤,所需B類資源大于銀行家所剩t=0;if(need_c&

6、gt;banker.remain_c)cout<<"錯誤,所需C類資源大于銀行家所剩t=0;if(t)strcpy(,name);processesquantity.need_a=need_a;processesquantity.need_b=need_b; processesquantity.need_c=need_c;quantity+;cout<<" 新加作業(yè)成功 "<<endl;elsecout<<" 新加作業(yè)失敗 "<<endl;/

7、為作業(yè)申請資源void bid()char name20;int i,p;int a,b,c;int flag;A 類資源 "<<endl;B 類資源 "<<endl;C 類資源 "<<endl;cout<<endl<<" 為作業(yè)申請資源 "<<endl;"<<endl;cout<<"cout<<" 要申請資源的作業(yè)名 :" cin>>name;p=-1;for(i=0;i<qua

8、ntity;i+) if(!strcmp(,name) p=i;break;if(p!=-1)cout<<" 該作業(yè)要申請 A 類資源數(shù)量 :"cin>>a;cout<<" 該作業(yè)要申請 B 類資源數(shù)量 :"cin>>b;cout<<" 該作業(yè)要申請 C 類資源數(shù)量 :"cin>>c;flag=1;if(a>banker.remain_a)|(a>processesp.need_a-processesp.a)"&

9、lt;<endl;cout<<"錯誤,所申請A類資源大于銀行家所剩A類資源或該進程還需數(shù)量flag=0;if(b>banker.remain_b)|(b>processesp.need_b-processesp.b)"<<endl;cout<<"錯誤,所申請B類資源大于銀行家所剩B類資源或該進程還需數(shù)量flag=0;if(c>banker.remain_c)|(c>processesp.need_c-processesp.c)"<<endl;cout<<"

10、錯誤,所申請C類資源大于銀行家所剩C類資源或該進程還需數(shù)量flag=0;if(flag)banker.remain_a-=a;banker.remain_b-=b;banker.remain_c-=c;processesp.a+=a;processesp.b+=b;processesp.c+=c;cout<<" 為作業(yè)申請資源成功 "<<endl; elsecout<<" 為作業(yè)申請資源失敗 "<<endl; elsecout<<" 該作業(yè)不存在 "<<endl;

11、/撤消作業(yè)void finished()char name20;int i,p;"<<endl;cout<<endl<<" 撤消作業(yè) "<<endl;cout<<"cout<<" 要撤消作業(yè)名 :"cin>>name;p=-1;for(i=0;i<quantity;i+)if(!strcmp(,name) p=i;break;if(p!=-1)banker.remain_a+=processesp.a; banker

12、.remain_b+=processesp.b; banker.remain_c+=processesp.c;for(i=p;i<quantity-1;i+)processesi=processesi+1;strcpy(,"");processesquantity-1.a=0;processesquantity-1.b=0;processesquantity-1.c=0;processesquantity-1.need_a=0;processesquantity-1.need_b=0;processesquantity-

13、1.need_c=0;quantity-;cout<<" 撤消作業(yè)成功 "<<endl;elsecout<<" 撤消作業(yè)失敗 "<<endl;/查看資源情況void view()int i;"<<endl;cout<<endl<<" 查看資源情況 "<<endl;cout<<"cout<<" 銀行家所剩資源 (剩余資源 /總共資源 )"<<endl;cout<

14、<"A 類 :"<<banker.remain_a<<"/"<<banker.a;cout<<" B 類 :"<<banker.remain_b<<"/"<<banker.b;cout<<" C 類 :"<<banker.remain_c<<"/"<<banker.c;cout<<endl<<endl<<

15、" 作業(yè)占用情況 ( 已占用資源 /所需資源 )"<<endl<<endl; if(quantity>0)for(i=0;i<quantity;i+)cout<<" 作業(yè)名 :"<<<<endl;cout<<"A 類 :"<<processesi.a<<"/"<<processesi.need_a;cout<<" B 類 :"<&

16、lt;processesi.b<<"/"<<processesi.need_b;cout<<" C 類 :"<<processesi.c<<"/"<<processesi.need_c;cout<<endl;elsecout<<" 當前沒有作業(yè) "<<endl;/顯示版權信息函數(shù)void version()cout<<endl<<endl;cout<<"銀 行 家 算 法"<<endl;void main()int chioce;int flag=1;initial();version();"<<endl;while(flag)cout<<"cout<<"1. 新加作業(yè) 2.為作業(yè)申請資源 3.撤消作業(yè) "<<endl; cout<<"4. 查

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論