资源分配银行家算法(c++)

由于是很久之后整理的,具体理论部分就不叙述了,代码如下

//--------------------------------------------------
//Codes from Frankie Liu
//--------------------------------------------------
/*#include<iostream>
  #include<cmath>
  #include<algorithm>
  #include<string>
  #include<cstring>
  #include<cstdio>
  #include<cstdlib>
  #include<map>
  #include<vector>
*/
/*测试数据
	  3 5
	  p0  7 5 3 0 1 0
	  p1  3 2 2 2 0 0
	  p2  9 0 2 3 0 2
	  p3  2 2 2 2 1 1
	  p4  4 3 3 0 0 2
	  10 5 7
	  3 3 2
	*/
#include<bits/stdc++.h>
#define ll long long

using namespace std;
const int N=11;
const int M=11;

bool judge=false;
int n,m;
int resource[M];    //各类资源总数
int available[M];   //每类资源的可用数目;
string name[N]; //进程名
int work[N][M];
int maximum[N][M]; //最大资源需求数。
int allocation[N][M]; //资源的可用数目。
int need[N][M]; //还需多少资源。
char state[N];//状态

void Input();//输入
void main_menu();//主菜单
bool System_Security_Test();//安全性测试
void Request_Security_Test();//请求测试
void Output(int pos);
void Out();

int main() {//主控
	Input();
	main_menu();
	return 0;
}

void Input() {
	printf("Input the type of resource and number of customer:\n");
	cin>>m>>n;
	printf("Input the amount of resource (maximum , allocated) of each customer:\n");
	for(int i=1; i<=n; i++) {
		cin>>name[i];
		for(int j=0; j<m; j++) {
			cin>>maximum[i][j];
		}
		for(int j=0; j<m; j++) {
			cin>>allocation[i][j];
		}
		for(int j=0; j<m; j++) {
			need[i][j]=maximum[i][j]-allocation[i][j];
		}
		for(int i=1; i<=n; i++)
			state[i]='W';
	}
	printf("Input the total amount of each resource\n");
	for(int i=0; i<m; i++)
		cin>>resource[i];

	printf("Input the amount of resources(available):\n");
	for(int i=0; i<m; i++) {
		cin>>available[i];
	}
}

void main_menu() { //主菜单
	printf("\t\t\t****************************************************\n");
	printf("\t\t\t*              T0初始时刻资源分配情况              *\n");
	printf("\t\t\t****************************************************\n");
	printf("\t\t\t*Name        Max        Allocation        Available*\n");
	for(int i=1; i<=n; i++) {
		cout<<"\t\t\t* "<<name[i];
		printf("       ");
		for(int j=0; j<m; j++) {
			printf("%2d",maximum[i][j]);
		}
		printf("        ");
		for(int j=0; j<m; j++) {
			printf("%2d",allocation[i][j]);
		}
		printf("            ");
		if(i==1) {
			for(int j=0; j<m; j++) {
				printf("%2d",available[j]);
			}
			printf("  *\n");
		} else printf("        *\n");
	}

	while(1) {
		printf("\t\t\t****************************************************\n");
		printf("\nInput your selection:\n");
		printf("(1)Judge the system security;\n");
		printf("(2)Judge the request security;\n");
		printf("(3)Exit;\n");
		int choice;
		cin>>choice;
		switch(choice) {
			case 1:
				printf("\t\t****************************************************************\n");
				printf("\t\t*                 Welcome to System Security Test              *\n");
				printf("\t\t****************************************************************\n");
				printf("\n\n\n");
				Out();
				if(System_Security_Test())
					printf("\t\t                       System security!                         \n");
				else
					printf("\t\t                      System Insecurity!                        \n");
				break;

			case 2:
				printf("\t\t****************************************************************\n");
				printf("\t\t*                Welcome to Request Security Test              *\n");
				printf("\t\t****************************************************************\n");
				printf("\n\n\n");
				Request_Security_Test();

				break;
			case 3:
				return;
		}
	}

}

bool System_Security_Test() {
	for(int i=1; i<=n; i++) {
		for(int j=0; j<m; j++) {
			work[i][j]=available[j];
		}
	}

	int pos=1;

	while(state[n]=='W') {

		int i;
		for(i=pos; i<=n; i++) {
			bool flag=false;
			for(int j=0; j<m; j++) {
				if(need[i][j]>work[i-1][j]) {
					flag=true;
					// cout<<i<<" "<<j<<" "<<need[i][j]<<" "<<work[i][j]<<endl;
					break;
				}
			}

			if(flag)  continue;
			else {
				state[i]='T';
				for(int j=pos; j<i; j++) {
					name[j+n-pos+1]=name[j];
					for(int k=0; k<m; k++) {
						need[j+n-pos+1][k]=need[j][k];
						maximum[j+n-pos+1][k]=maximum[j][k];
						allocation[j+n-pos+1][k]=allocation[j][k];
						need[j+n-pos+1][k]=need[j][k];
					}
					state[j+n-pos+1]=state[j];
				}

				for(int j=i; j<=n+i-pos; j++) {
					name[j-i+pos]=name[j];
					for(int k=0; k<m; k++) {
						need[j-i+pos][k]=need[j][k];
						maximum[j-i+pos][k]=maximum[j][k];
						allocation[j-i+pos][k]=allocation[j][k];
						need[j-i+pos][k]=need[j][k];
					}
					state[j-i+pos]=state[j];
				}
				Output(pos);
				pos++;
				break;

			}
		}

		if(i==6) {
			if(pos==1) state[pos]=='F';
			else state[pos]='T';
			for(int j=pos+1; j<=5; j++)
				state[j]='F';
			Output(pos);
			return false;
		}

	}
	if(state[n]=='T')
		return true;

}

void Output(int pos) {
	printf("\t\t****************************************************************\n");
	printf("\t\t*                       资源分配情况                           *\n");
	printf("\t\t****************************************************************\n");
	printf("\t\t*Name   Work     Need    Allocation    Work+Allocation    State*\n");
	printf("\t\t****************************************************************\n");
	for(int i=1; i<=pos; i++) {
		cout<<"\t\t  "<<name[i];
		cout<<"   ";
		for(int j=0; j<m; j++)
			cout<<work[i][j]<<" ";
		cout<<"   ";
		for(int j=0; j<m; j++)
			cout<<need[i][j]<<" ";
		cout<<"      ";
		for(int j=0; j<m; j++)
			cout<<allocation[i][j]<<" ";
		cout<<"         ";
		for(int j=0; j<m; j++)
			cout<<work[i][j]+allocation[i][j]<<" ";
		for(int j=0; j<m; j++) {
			work[i+1][j]=work[i][j]+allocation[i][j];
		}
		cout<<"           "<<state[i]<<"  \n";
	}

	for(int i=pos+1; i<=n; i++) {
		cout<<"\t\t  "<<name[i];
		cout<<"         ";
		cout<<"   ";
		for(int j=0; j<m; j++)
			cout<<need[i][j]<<" ";
		cout<<"      ";
		for(int j=0; j<m; j++)
			cout<<allocation[i][j]<<" ";
		cout<<"               ";
		cout<<"           "<<state[i]<<"  \n";
	}
	printf("\t\t****************************************************************\n");
	printf("\n\n\n\n");
	system("pause\n");
}

void Request_Security_Test() {
	printf("Please input the customer’s name and request:\n");
	string s;
	int request[m];
	cin>>s;
	for(int i=0; i<m; i++) {
		cin>>request[i];
	}

	int i;
	for(i=1; i<=n; i++) {
		if(name[i]==s) {
			for(int j=0; j<m; j++) {
				if(request[j]>need[i][j])   {
					cout<<s<<"'s request>its need. Error";
					return ;
				}
				if(request[j]>available[j]) {
					cout<<s<<"'s request>available. Insufficient resources.";
					return;
				}
				available[j]-=request[j];
				allocation[i][j]+=request[j];
				need[i][j]-=request[j];
			}
		}
	}
	printf("\t\t\t****************************************************\n");
	printf("\t\t\t*              T0初始时刻资源分配情况              *\n");
	printf("\t\t\t****************************************************\n");
	printf("\t\t\t*Name        Max        Allocation        Available*\n");
	for(int i=1; i<=n; i++) {
		cout<<"\t\t\t* "<<name[i];
		printf("       ");
		for(int j=0; j<m; j++) {
			printf("%2d",maximum[i][j]);
		}
		printf("        ");
		for(int j=0; j<m; j++) {
			printf("%2d",allocation[i][j]);
		}
		printf("            ");
		if(i==1) {
			for(int j=0; j<m; j++) {
				printf("%2d",available[j]);
			}
			printf("  *\n");
		} else printf("        *\n");
	}
	printf("\t\t\t****************************************************\n\n\n");
	if(System_Security_Test()) {
		printf("\t\t                       System security!                         \n");
		cout<<"\t\t      customer "<<s<<" can get resources immediately.            \n";
	} else {
		printf("\t\t                      System Insecurity!                        \n");
		cout<<"\t\t       customer "<<s<<" cannot get resources immediately.         \n";
	}
}

void Out() {
	printf("\t\t****************************************************************\n");
	printf("\t\t*                       资源分配情况                           *\n");
	printf("\t\t****************************************************************\n");
	printf("\t\t*Name   Work     Need    Allocation    Work+Allocation    State*\n");
	printf("\t\t****************************************************************\n");
	for(int i=1; i<=n; i++) {
		cout<<"\t\t  "<<name[i];
		cout<<"   ";
		for(int j=0; j<m; j++)
			cout<<work[i][j]<<" ";
		cout<<"   ";
		for(int j=0; j<m; j++)
			cout<<need[i][j]<<" ";
		cout<<"      ";
		for(int j=0; j<m; j++)
			cout<<allocation[i][j]<<" ";
		cout<<"         ";
		for(int j=0; j<m; j++)
			cout<<work[i][j]+allocation[i][j]<<" ";
		for(int j=0; j<m; j++) {
			work[i+1][j]=work[i][j]+allocation[i][j];
		}
		cout<<"           "<<state[i]<<"  \n";
	}


	printf("\t\t****************************************************************\n");
	printf("\n\n\n\n");
	system("pause\n");
}



在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值