银行家算法 C++实现

操作系统模拟之银行家算法。
文件共4份,其中1份cpp,3份.h,代码如下:

main.cpp

#include <iostream>
#include <stdlib.h>
#include "initialize.h"
#include "check.h"
#include "apply.h"
using namespace std;int main()
{Process_initialize();Check();Apply();cout << endl << "The program ended.Thanks for using." << endl;return 0; 
}/*测试数据
5 
3
0 1 0
7 5 3
2 0 0
3 2 2
3 0 2
9 0 2
2 1 1
2 2 2
0 0 2
4 3 3
3 3 2
*/ 

initialize.h

#include <vector>
using namespace std;
class process{private:int Resource_number;vector<int> Allocation;vector<int> Need;vector<int> Max;bool if_checked;public:process(int Resource_number);void Initialize();vector<int> get_Allocation();vector<int> get_Need();vector<int> get_Max();bool get_if_checked();void set_Allocation(vector<int> Allocation);void set_Need(vector<int> Need);void set_Max(vector<int> Max);void set_if_checked(bool if_checked);
};
vector<process> v;
vector<int> available;process::process(int Resource_number)
{this -> Resource_number = Resource_number;this -> if_checked = false;
}void process::Initialize()
{int temp_allocation, temp_need, temp_max;cout << "Please input the Allocation information:" << endl;for(int i = 0; i < Resource_number; i++){cin >> temp_allocation;Allocation.push_back(temp_allocation);}cout << "Please input the Max information:" << endl;for(int i = 0; i < Resource_number; i++){cin >> temp_max;Max.push_back(temp_max);}for(int i = 0; i < Resource_number; i++){Need.push_back(Max[i] - Allocation[i]);}
}void Process_initialize()
{int process_number, resource_number, temp_available;cout << "Please input the process number:" << endl;cin >> process_number;cout << "Please input the resource number:" << endl;cin >> resource_number;for(int i = 0; i < process_number; i++){cout << endl << "The process you initialize now is P" << i << endl;process temp_process(resource_number);temp_process.Initialize();v.push_back(temp_process);}cout << "Please input the available resources:" << endl;for(int i = 0; i < resource_number; i++){cin >> temp_available;available.push_back(temp_available);}cout << "Initialization successfully!" << endl;
}//设置成员变量的get函数 
vector<int> process::get_Allocation()
{return Allocation;
}
vector<int> process::get_Need()
{return Need;
}
vector<int> process::get_Max()
{return Max;
}
bool process::get_if_checked()
{return if_checked;
}//设置成员变量的set函数 
void process::set_Allocation(vector<int> Allocation)
{this -> Allocation = Allocation;
}
void process::set_Need(vector<int> Need)
{this -> Need = Need;
}
void process::set_Max(vector<int> Max)
{this -> Max = Max;
}
void process::set_if_checked(bool if_checked)
{this -> if_checked = if_checked;
}

check.h

#include <iostream>
#include <vector>
using namespace std;
class check{private:vector<int> temp_check;vector<process> check_process;vector<int> temp_available;public:check(vector<process> v, vector<int> available);void Check_security();
};check::check(vector<process> v, vector<int> available)
{check_process = v;  temp_available = available; 
}void check::Check_security()
{cout << "Now the program will check the security of processes." << endl;for(int i = 0; i < check_process.size(); i++){for(int j = 0; j < check_process.size(); j++){if(check_process[j].get_if_checked() == false){bool check_resource = true;vector<int> temp = check_process[j].get_Need();for(int k = 0; k < temp.size(); k++){if(temp[k] > temp_available[k])check_resource = false;}if(check_resource == true){check_process[j].set_if_checked(true);for(int k = 0; k < temp.size(); k++)temp_available[k] += temp[k];temp_check.push_back(j);}}}}if(temp_check.size() == check_process.size()){system("cls");cout << "There exists a secure sequence." << endl;cout << "The result may not be unique." << endl;cout << "The following sequence is the smallest in numerical ascending order:" << endl << endl;for(int i = 0; i < temp_check.size(); i++){if(i > 0)cout << " -> ";cout << "P" << temp_check[i];}cout << endl;}elsecout << "There dose not exist a secure sequence" << endl;
}void Check()
{check temp_check(v, available);temp_check.Check_security();
}

apply.h

#include <vector>
using namespace std;class apply{private:vector<int> temp_apply;  //拟申请的资源 vector<process> apply_process;  //已有的进程 vector<int> temp_available;  //剩余的资源数量 bool if_true;public:apply();  //进程申请资源 void apply_resource();void update_data();  //更新进程数据 bool get_if_true();void set_if_true(bool if_true);
};apply::apply()
{apply_process = v;temp_available = available;if_true = false;
}bool apply::get_if_true()
{return if_true;
}
void apply::set_if_true(bool if_true)
{this -> if_true = if_true;
}void apply::apply_resource()
{ apply(); int id, temp_resource;cout << endl << "Please input the process id that you want to apply:" << endl;cin >> id;cout << endl << "Please input the resourse that you want to apply:" << endl;bool need_flag = true, avi_flag = true;vector<int> _temp_need, _temp_allocation;for(int i = 0; i < apply_process[id].get_Need().size(); i++){cin >> temp_resource;if(temp_resource > apply_process[id].get_Need()[i])need_flag = false;if(temp_resource > temp_available[i])avi_flag = false;vector<int> _temp_need, _temp_allocation;_temp_need.push_back(apply_process[id].get_Need()[i] - temp_resource);_temp_allocation.push_back(apply_process[id].get_Allocation()[i] + temp_resource);temp_available[i] -= temp_resource;}if(need_flag == false || avi_flag == false){if(need_flag == false)cout << endl << "ERROR! Exceed the resources it needs!" << endl;else if(avi_flag == false)cout << endl << "ERROR! Exceed the total available resources" << endl;cout << "Sorry, please input again." << endl;apply();return;}apply_process[id].set_Need(_temp_need);apply_process[id].set_Allocation(_temp_allocation);check temp_check(apply_process, temp_available);temp_check.Check_security();cout << endl << "Do you want to continue? Input yes or no."	<< endl;string choice;cin >> choice;if(choice == "no")if_true = true;
}
void Apply()
{apply a;while(a.get_if_true() == false)a.apply_resource();
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/312723.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

.NET 应用程序支持直接调用 WebAssembly 模块

WebAssembly Runtime 现已添加 .NET Core API&#xff0c;开发者可直接在 .NET 应用程序中调用 WebAssembly 模块。Mozilla 宣布由 Bytecode Alliance 创建的 Wasmtime&#xff08;WebAssembly runtime&#xff09;现已添加处于早期预览版状态的 .NET Core API&#xff0c;这就…

进程调度算法 C++实现

操作系统模拟之进程调度算法。 文件共2份&#xff0c;其中1份cpp&#xff0c;1份.h&#xff0c;代码如下&#xff1a; main.cpp #include "init.h"int main() {printf("欢迎进入演示系统&#xff01;\n");printf("\n现在需要对进程数据初始化\n&quo…

《RPA、AI、.NET Core 与未来》-中国.NET开发者峰会

未 来第四次工业革命&#xff0c;催生了数字自动化劳动&#xff0c;RPA 与 AI 技术的融合&#xff0c;成为智能信息社会的重要环节。当下RPA平台主要采用.NET Framework框架&#xff0c;也限制了 RPA 只用于 Windows 平台。.NET Core 的开放与跨平台特性赋予RPA更大发展空间。…

地址转换算法 C++实现

操作系统模拟之地址转换算法。 文件共3份&#xff0c;其中1份cpp&#xff0c;2份.h&#xff0c;代码如下&#xff1a; main.cpp #include "init.h"int main() {printf("欢迎进入演示系统&#xff01;\n");printf("\n现在需要对进程数据初始化\n&quo…

浅议gRPC的数据传输机制和回调机制

本文来自DotNET技术圈作者&#xff1a;溪源一、引子如您所知&#xff0c;gRPC是目前比较常见的rpc框架&#xff0c;可以方便的作为服务与服务之间的通信基础设施&#xff0c;为构建微服务体系提供非常强有力的支持。而基于.NET Core的gRPC.NET 组件截至2019年11月30日的最新版本…

请求分页算法 Python实现

操作系统模拟之请求分页算法。 文件共1份&#xff0c;代码如下&#xff1a; import math import os import random import copydef alo_opt():print("您选择了OPT算法&#xff0c;执行结果如下&#xff1a;")print("访问页面 物理块 缺页中断")temp_que…

如何在 Visual Studio 2019 中连接中国版 Azure

点击上方蓝字关注“汪宇杰博客”导语做国内项目很可能用到中国版的 Azure&#xff0c;即世纪互联运营的 azure.cn。然而 Visual Studio 2019 默认连接的是国际版的 Azure&#xff0c;如何添加中国版订阅呢&#xff1f;差点9966年前我曾经写过一篇《图解&#xff1a;如何在Visua…

磁盘寻道算法 Python实现

操作系统模拟之磁盘寻道算法。 文件共1份&#xff0c;代码如下&#xff1a; import math import random import copydef alo_fcfs():print("您选择了FCFS算法&#xff0c;执行结果如下&#xff1a;")print("当前磁道号 下一磁道号 绝对差")print({:6d}{:1…

k8s 使用 Init Container 确保依赖的服务已经启动

k8s 使用 Init Container 确保依赖的服务已经启动Intro最近 helm 3 正式发布了&#xff0c;dotnetcore 3.1 也正式发布了&#xff0c;最近打算把我的活动室预约项目做一个升级&#xff0c;项目已经升级到了 dotnetcore 3.1&#xff0c;最近几天则在准备把项目打包一个 helm 包&…

LeetCode动态规划 最大子序和

给定一个整数数组 nums &#xff0c;找到一个具有最大和的连续子数组&#xff08;子数组最少包含一个元素&#xff09;&#xff0c;返回其最大和。 ps&#xff1a; 清华大学912曾经考过。 状态转移方程 dp[i] max(nums[i], dp[i-1] nums[i]) 边界条件 dp[0] nums[0] 代码…

dotNET面试题汇总系列连载(1):基础语法

点击上方“dotNET全栈开发”&#xff0c;“设为星标”加“星标★”&#xff0c;每天11.50&#xff0c;好文必达全文约4000字&#xff0c;预计阅读时间8分钟马上要到2020年了&#xff0c;这里整理一个.NET 工程师面试题系列&#xff0c;希望年底或者明年金三银四跳槽的程序猿们带…

LeetCode动态规划 环形子数组的最大和

给定一个由整数数组 A 表示的环形数组 C&#xff0c;求 C 的非空子数组的最大可能和。 此外&#xff0c;子数组最多只能包含固定缓冲区 A 中的每个元素一次。 思路 本道题需要分类成两种情况&#xff0c;题目答案为以下两种情况的较大值。 1.最大子区间在序列的中间 这种情况是…

Blazor 版 Bootstrap Admin 通用后台权限管理框架

前言上一篇介绍过了前后台分离的 NET Core 通用权限管理系统 在这篇文章简要的介绍了 Bootstrap Admin 后台管理框架的一些功能。本篇文章带来的是微软最新出的 Blazor 版本的 NET Core 通用权限管理系统Blazor 简介至于 Blazor 是什么&#xff0c;Blazor 的优缺点小伙伴们可以…

xms跨平台基础框架 - 基于.netcore

背景鄙人经过多年开发&#xff0c;数百个项目“打磨(折磨)”&#xff0c;各种国内外框架平台都有涉及&#xff0c;没有一款称心顺手的&#xff0c;原因有三&#xff0c;一是设计反人类&#xff0c;二是不开源根本无法突破框架限制&#xff0c;三是即使开源也是阉割版&#xff0…

如何让 Azure AD 里的应用只允许特定用户登录

点击上方蓝字关注“汪宇杰博客”导语我的博客系统支持 Azure Active Directory 身份认证&#xff0c;然而用VS点点鼠标配出来的 Azure 应用&#xff0c;默认情况下会允许所有 AAD 内的用户访问&#xff0c;如何限制为只让特定用户访问呢&#xff1f;背景情况其实最近&#xff0…

程序员:这10种糟糕的程序命名,你遇到过几个?

点击上方“dotNET全栈开发”&#xff0c;“设为星标”加“星标★”&#xff0c;每天11.50&#xff0c;好文必达全文约2300字&#xff0c;预计阅读时间4分钟有人问&#xff1a;规范的命名风格真的能让你程序员少出bug&#xff1f;当遇到这方面的教训时&#xff0c;就会想到这句话…

评测OJ时间复杂度

平日里做OJ题时&#xff0c;先看数据范围能帮助自己选择算法 &#xff08;可能更多时候是看暴力能骗到几分qwq) 一般而言&#xff0c;评测OJ在1s内能接受的算法时间复杂度是10e8-10e9之间&#xff0c;这里取折中值5*10e8。 对于不同的算法&#xff0c;能够接受的最大数据如下&…

.Net Core使用Ocelot网关(一) -负载,限流,熔断,Header转换

1.什么是API网关API网关是微服务架构中的唯一入口&#xff0c;它提供一个单独且统一的API入口用于访问内部一个或多个API。它可以具有身份验证&#xff0c;监控&#xff0c;负载均衡&#xff0c;缓存&#xff0c;请求分片与管理&#xff0c;静态响应处理等。API网关方式的核心要…

[Asp.net core 3.1] 通过一个小组件熟悉Blazor服务端组件开发

通过一个小组件&#xff0c;熟悉 Blazor 服务端组件开发。github:https://github.com/git-net/NBlazors一、环境搭建vs2019 16.4, asp.net core 3.1 新建 Blazor 应用&#xff0c;选择 asp.net core 3.1。根文件夹下新增目录 Components&#xff0c;放置代码。二、组件需求定义…

【STM32】STM32学习笔记-TIM定时中断(13)

00. 目录 文章目录 00. 目录01. TIM简介02. 定时器类型03. 基本定时器04. 通用定时器05. 高级定时器06. 定时中断基本结构07. 预分频器时序08. 计数器时序09. 计数器无预装时序10. 计数器有预装时序11. RCC时钟树12. 附录 01. TIM简介 TIM&#xff08;Timer&#xff09;定时器…