⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 C++编写的模拟银行家算法的实现,内容详细具体,代码清楚易懂,很适合初学者学习
💻 CS
📖 第 1 页 / 共 2 页
字号:
                }
                

                if (re_listview_count(2,0) == 0) break;
                if (flg == true) { timer1.Stop();MessageBox.Show("资源已用尽,停止分配!");break;   }
            
            }
            cw.refr(safe());
            if (progressBar1.Value == progressBar1.Maximum) { timer1.Stop(); timer1.Dispose(); MessageBox.Show("分配完毕!"); button2.Enabled = false; }
        }

        private void bank(object sender, EventArgs e)
        {
            int x, y, max, temp_1;
            System.Random ro = new Random();
            max = listView1.Items.Count;

            for (x = 2; x <= max - 1; x++)
            {

                int[] temp_num = new int[10];
                int[] temp_sys = new int[10];
                int temp_pro = progressBar1.Value;
                for (int i = 2; i < 12; i++)
                {
                    temp_num[i - 2] = int.Parse(listView1.Items[x].SubItems[i].Text.ToString());
                    temp_sys[i - 2] = int.Parse(listView1.Items[1].SubItems[i].Text.ToString());
                }

                for (y = 2; y <= 11; y++)
                {
                    int temp = Int16.Parse(listView1.Items[x].SubItems[y].Text.ToString());
                    int system_y = Int16.Parse(listView1.Items[1].SubItems[y].Text.ToString());

                    if (temp == 0)
                        continue;
                    else if (temp < 10)
                    {
                        listView1.Items[1].SubItems[y].Text = (system_y - temp).ToString();
                        listView1.Items[x].SubItems[y].Text = "0";
                        if (re_listview_count(x, x) == 0 && listView1.Items[x].SubItems[1].Text != "完成")
                        {
                            for (int m = 2; m < 12; m++)
                                listView1.Items[1].SubItems[m].Text = (Int16.Parse(listView1.Items[1].SubItems[m].Text.ToString()) + tp[x - 2].total[m - 2]).ToString();
                            listView1.Items[x].SubItems[1].Text = "完成";
                        }
                        tp[x - 2].have[y - 2] += temp;
                        if ((progressBar1.Maximum - progressBar1.Value) < temp) progressBar1.Value = progressBar1.Maximum;
                        else progressBar1.Value += temp;
                    }
                    else
                    {
                        temp_1 = temp;
                        temp = ro.Next(0, 10);
                        listView1.Items[1].SubItems[y].Text = (system_y - temp).ToString();
                        listView1.Items[x].SubItems[y].Text = (Int16.Parse(listView1.Items[x].SubItems[y].Text.ToString()) - temp).ToString();

                        if (re_listview_count(x, x) == 0 && listView1.Items[x].SubItems[1].Text != "完成")
                        {
                            for (int m = 2; m < 12; m++)
                                listView1.Items[1].SubItems[m].Text = (Int16.Parse(listView1.Items[1].SubItems[m].Text.ToString()) + tp[x - 2].total[m - 2]).ToString();
                            listView1.Items[x].SubItems[1].Text = "完成";
                        }
                        tp[x - 2].have[y - 2] += temp;
                        if ((progressBar1.Maximum - progressBar1.Value) < temp) progressBar1.Value = progressBar1.Maximum;
                        else progressBar1.Value += temp;
                    }
                }
                if (safe().IndexOf("无") != -1)
                {
                    progressBar1.Value = temp_pro;
                    for (int i = 2; i < 12; i++)
                    {
                        listView1.Items[x].SubItems[i].Text = temp_num[i - 2].ToString();
                        listView1.Items[1].SubItems[i].Text = temp_sys[i - 2].ToString();
                        
                    }
                }

                if (re_listview_count(2, 0) == 0) break;
            }
            cw.refr(safe());
            if (progressBar1.Value == progressBar1.Maximum) { timer1.Stop(); timer1.Dispose(); MessageBox.Show("分配完毕!"); button2.Enabled = false; }
        }

        private void anxu(object sender, EventArgs e)
        {
            int x, y, max, temp_1;
            System.Random ro = new Random();
            max = listView1.Items.Count;

            for (x = 2; x <= max - 1; x++)
            {

                int[] temp_num = new int[10];
                int[] temp_sys = new int[10];
                int temp_pro = progressBar1.Value;
                for (int i = 2; i < 12; i++)
                {
                    temp_num[i - 2] = int.Parse(listView1.Items[x].SubItems[i].Text.ToString());
                    temp_sys[i - 2] = int.Parse(listView1.Items[1].SubItems[i].Text.ToString());
                }
                
                for (y = 2; y <= 11; y++)
                {
                    int temp = Int16.Parse(listView1.Items[x].SubItems[y].Text.ToString());
                    int system_y = Int16.Parse(listView1.Items[1].SubItems[y].Text.ToString());
                    if (y == 2 || (y != 2 && Int16.Parse(listView1.Items[x].SubItems[y - 1].Text.ToString()) == 0))
                    {
                        if (temp == 0)
                            continue;
                        else if (temp < 10)
                        {
                            listView1.Items[1].SubItems[y].Text = (system_y - temp).ToString();
                            listView1.Items[x].SubItems[y].Text = "0";
                            if (re_listview_count(x, x) == 0 && listView1.Items[x].SubItems[1].Text != "完成")
                            {
                                for (int m = 2; m < 12; m++)
                                    listView1.Items[1].SubItems[m].Text = (Int16.Parse(listView1.Items[1].SubItems[m].Text.ToString()) + tp[x - 2].total[m - 2]).ToString();
                                listView1.Items[x].SubItems[1].Text = "完成";
                            }
                            tp[x - 2].have[y - 2] += temp;
                            if ((progressBar1.Maximum - progressBar1.Value) < temp) progressBar1.Value = progressBar1.Maximum;
                            else progressBar1.Value += temp;
                        }
                        else
                        {
                            temp_1 = temp;
                            temp = ro.Next(0, 10);
                            listView1.Items[1].SubItems[y].Text = (system_y - temp).ToString();
                            listView1.Items[x].SubItems[y].Text = (Int16.Parse(listView1.Items[x].SubItems[y].Text.ToString()) - temp).ToString();

                            if (re_listview_count(x, x) == 0 && listView1.Items[x].SubItems[1].Text != "完成")
                            {
                                for (int m = 2; m < 12; m++)
                                    listView1.Items[1].SubItems[m].Text = (Int16.Parse(listView1.Items[1].SubItems[m].Text.ToString()) + tp[x - 2].total[m - 2]).ToString();
                                listView1.Items[x].SubItems[1].Text = "完成";
                            }
                            tp[x - 2].have[y - 2] += temp;
                            if ((progressBar1.Maximum - progressBar1.Value) < temp) progressBar1.Value = progressBar1.Maximum;
                            else progressBar1.Value += temp;
                        }



                        if (re_listview_count(2, 0) == 0) break;
                    }
                    else break;
                }

            }
            cw.refr(safe());
            if (progressBar1.Value == progressBar1.Maximum) { timer1.Stop(); timer1.Dispose(); MessageBox.Show("分配完毕!"); button2.Enabled = false; }
        }

        private string safe()
        {
            string sd = "安全序列:";
            string sc = ":";
            int count = 0;
            int count_1 = 0;
            int i = 2;
            int[] system_r = new int[10];
           
            for (int y = 2; y < 12; y++)
                   system_r[y-2]=Int16.Parse(listView1.Items[1].SubItems[y].Text.ToString());

               
               for (;;i++)
               {
                   if (count == listView1.Items.Count - 2) return sd;
                   if (i == listView1.Items.Count) i = 2;
                   if(sc.IndexOf(i.ToString())!=-1) continue;
                   for (int y = 2; y < 12; y++)
                   {
                       if (i == listView1.Items.Count) { i = 2; break; }
                       if (system_r[y - 2] < Int16.Parse(listView1.Items[i].SubItems[y].Text.ToString()))
                           break;
                       if (y == 11)
                       {
                           sc += i.ToString() + " ";
                           sd += (i-1).ToString() + " ";
                           
                           for (int m = 0; m < 10; m++)
                               system_r[m] += tp[i - 2].total[m];
                           count++;
                           count_1=0;
                       }
                   }
                   if (i == listView1.Items.Count - 1)
                   {
                       i = 2;

                       count_1++;
                       if (count_1 == 2) break;

                   }
                      
               }
            return "无安全序列存在";
        }
        
        private int re_listview_count(int k1,int k2)
        {
            int i,j,total;
            
            total=0;
            if (k2 == 0) j = listView1.Items.Count-1;
            else j = k2;
            for(;k1<=j;k1++)
                for(i=2;i<=11;i++)
                    total+=Int16.Parse((listView1.Items[k1].SubItems[i].Text.ToString()));
            
            return total;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            button2.Enabled = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show(safe());
        }

        private void 系统资源总数ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dlg_system dlg2 = new dlg_system();
            dlg2.ShowDialog();
            dlg2.res.CopyTo(ps.total, 0);
             for (int i = 0; i < 10; i++)
                {

                    listView1.Items[0].SubItems[i + 2].Text = dlg2.res[i].ToString();
                }
        }

        private void 已用资源数ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dlg_system dlg2 = new dlg_system();
            dlg2.ShowDialog();
            dlg2.res.CopyTo(ps.have, 0);
             for (int i = 0; i < 10; i++)
                {

                    listView1.Items[1].SubItems[i + 2].Text = dlg2.res[i].ToString();
                }
        }

        private void form1_close(object sender, FormClosedEventArgs e)
        {
            timer1.Stop();
            timer1.Dispose();
            new_p.Abort();
            cw.cls();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Created by laysenya and 陈小荣 and L & F & W ");
        }
            
   

        
    }
    

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -