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

📄 disk.cs

📁 os 课程设计不含文档 123456789
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace os
{
    class disk
    {
        public string[] diskfile;
        public string diskfilename = "";//磁盘文件名
        private static int blocksize = 128;//块大小
        private static int blocknum = 256;//每个磁盘的快数
        public char[] buffer=new char[128];//缓冲区
        public byte[] buffer1=new byte[128];
        public disk()
        {
            diskfile = new string[2];
            diskfile[0] = "disk0.txt";
            diskfile[1] = "disk1.txt";
            if (diskfilename == "")
            {
                diskfilename = diskfile[0];
            }
            if(File.Exists(diskfilename)==false)
            {
                initdisk();
            }
            diskfilename = diskfile[1];
            if (File.Exists(diskfilename) == false)
            {
                initdisk();
            }
        }
        public void initdisk()//磁盘初始化
        {
            byte[] bydata = new byte[8];
            char[] chdata = new char[8];
            FileStream afile = new FileStream(diskfilename, FileMode.OpenOrCreate);
            for (int i = 0; i < 8; i++)
            {
                chdata[i] = Convert.ToChar(0x00);
            }
            Encoder e = Encoding.UTF8.GetEncoder();
            e.GetBytes(chdata,0,chdata.Length,bydata,0,true);
            for (int i = 0; i < 4096; i++)
            {
                afile.Seek(i * 8, SeekOrigin.Begin);
                afile.Write(bydata,0,bydata.Length);
            }
            afile.Close();
            for (int i = 0; i < 128; i++)
            {
                buffer1[i] = Convert.ToByte(0);
            }
            //for (int i = 0; i < 8; i++)
            //{
            //    buffer[i * 16] = Convert.ToChar(0x00);
            //    buffer[i * 16 + 1] = Convert.ToChar(0x00);
            //    buffer[i * 16 + 2] = Convert.ToChar(0x00);
            //    buffer[i * 16 + 3] = Convert.ToChar(0x00);
            //    buffer[i * 16 + 4] = Convert.ToChar(0x00);
            //    buffer[i * 16 + 5] = Convert.ToChar(0x00);
            //    buffer[i * 16 + 6] = Convert.ToChar(0x00);
            //    buffer[i * 16 + 7] = Convert.ToChar(0x00);
            //    buffer[i * 16 + 8] = Convert.ToChar(0x00);
            //    buffer[i * 16 + 9] = Convert.ToChar(0x00);
            //    buffer[i * 16 + 10] = Convert.ToChar(0x00);
            //    buffer[i * 16 + 11] = Convert.ToChar(0x00);
            //    buffer[i * 16 + 12] = Convert.ToChar(0x00);
            //    buffer[i * 16 + 13] = Convert.ToChar(0x00);
            //    buffer[i * 16 + 14] = Convert.ToChar(0x00);
            //    buffer[i * 16 + 15] = Convert.ToChar(0x00);
            //}
            //writeblock(1);
            //writeblock(2);
            for (int i = 255; i > 2; i--)
            {
                if ((i % 10) == 6)
                {
                    if (i == 246)
                    {
                        buffer1[0] = Convert.ToByte(10);
                        buffer1[1] = Convert.ToByte(0);
                        buffer1[2] = Convert.ToByte(i + 9);
                        buffer1[3] = Convert.ToByte(i + 8);
                        buffer1[4] = Convert.ToByte(i + 7);
                        buffer1[5] = Convert.ToByte(i + 6);
                        buffer1[6] = Convert.ToByte(i + 5);
                        buffer1[7] = Convert.ToByte(i + 4);
                        buffer1[8] = Convert.ToByte(i + 3);
                        buffer1[9] = Convert.ToByte(i + 2);
                        buffer1[10] = Convert.ToByte(i + 1);
                        writeblock1(i);
                    }
                    else 
                    {
                        buffer1[0] = Convert.ToByte(10);
                        buffer1[1] = Convert.ToByte(i + 10);
                        buffer1[2] = Convert.ToByte(i + 9);
                        buffer1[3] = Convert.ToByte(i + 8);
                        buffer1[4] = Convert.ToByte(i + 7);
                        buffer1[5] = Convert.ToByte(i + 6);
                        buffer1[6] = Convert.ToByte(i + 5);
                        buffer1[7] = Convert.ToByte(i + 4);
                        buffer1[8] = Convert.ToByte(i + 3);
                        buffer1[9] = Convert.ToByte(i + 2);
                        buffer1[10] = Convert.ToByte(i + 1);
                        writeblock1(i);
                    }
                }
            }
            buffer1[0] = Convert.ToByte(4);
            buffer1[1] = Convert.ToByte(6);
            buffer1[2] = Convert.ToByte(5);
            buffer1[3] = Convert.ToByte(4);
            buffer1[4] = Convert.ToByte(3);
            writeblock1(0);

        }
        public bool writeblock(int block)
        {
            byte[] bydata = new byte[blocksize];
            char[] chdata = new char[blocksize];
            FileStream afile = new FileStream(diskfilename, FileMode.OpenOrCreate);
            chdata = buffer;
            Encoder e = Encoding.UTF8.GetEncoder();
            e.GetBytes(chdata,0,chdata.Length,bydata,0,true);
            if (seekblock(block) == -1)
            {
                return false;					//如果寻道出错,直接返回
            }
            afile.Seek(seekblock(block),SeekOrigin.Begin);
            afile.Write(bydata,0,bydata.Length);
            afile.Close();
            return true;
        }
        public bool writeblock1(int block)
        {
            byte[] bydata = new byte[blocksize];
            //char[] chdata = new char[blocksize];
            FileStream afile = new FileStream(diskfilename, FileMode.OpenOrCreate);
            bydata = buffer1;
            //Encoder e = Encoding.UTF8.GetEncoder();
            //e.GetBytes(chdata, 0, chdata.Length, bydata, 0, true);
            if (seekblock(block) == -1)
            {
                return false;					//如果寻道出错,直接返回
            }
            afile.Seek(seekblock(block), SeekOrigin.Begin);
            afile.Write(bydata, 0, bydata.Length);
            afile.Close();
            return true;
        }
        public int seekblock(int block)
        {
            if (block < 0 || block >= blocknum - 1)
            {
                return -1;							//如果寻道出错(及盘号越界),则返回 -1
            }
            else
            {
                return block * blocksize;
            }
        }
        public bool readblock(int block)
        {
            byte[] bydata = new byte[blocksize];
            char[] chdata = new char[blocksize];
            FileStream afile = new FileStream(diskfilename,FileMode.Open);
            if (seekblock(block) == -1)
            {
                return false;					//如果寻道出错,直接返回
            }
            afile.Seek(seekblock(block), SeekOrigin.Begin);
            afile.Read(bydata, 0, blocksize);
            Decoder d = Encoding.UTF8.GetDecoder();
            d.GetChars(bydata,0,bydata.Length,chdata,0);
            buffer = chdata;
            afile.Close();
            return true;
        }
        public bool readblock1(int block)
        {
            byte[] bydata = new byte[blocksize];
            //char[] chdata = new char[blocksize];
            FileStream afile = new FileStream(diskfilename, FileMode.Open);
            if (seekblock(block) == -1)
            {
                return false;					//如果寻道出错,直接返回
            }
            afile.Seek(seekblock(block), SeekOrigin.Begin);
            afile.Read(bydata, 0, blocksize);
            //Decoder d = Encoding.UTF8.GetDecoder();
            //d.GetChars(bydata, 0, bydata.Length, chdata, 0);
            buffer1 = bydata;
            afile.Close();
            return true;
        }
        public string diskview(int disk)
        {
            if (disk == 0)
                diskfilename = diskfile[0];
            if (disk == 1)
                diskfilename = diskfile[1];
            string diskview = "";
            int[] fat = new int[256];
            for (int i = 0; i < 256; i++)
            {
                fat[i] = 1;
            }
            readblock1(0);
            while (Convert.ToInt32(buffer1[1]) != 0)
            {
                for (int i = 0; i < Convert.ToInt32(buffer1[0]); i++)
                {
                    fat[Convert.ToInt32(buffer1[i + 1])] = 0;
                }
                readblock1(Convert.ToInt32(buffer1[1]));
            }
            for (int i = 1; i < Convert.ToInt32(buffer1[0]); i++)
            {
                fat[Convert.ToInt32(buffer1[i + 1])] = 0;
            }
            //fat[16] = 0;
            for (int i = 0; i < 256; i++)
            {
                if (fat[i] == 0)
                    diskview += "□";
                else
                    diskview += "■";
            }
            return diskview;
        }
        public int qblock()//申请磁盘块
        {
            int b = 0;
            int num = 0;
            readblock1(0);
            num=Convert.ToInt32(buffer1[0]);
            if (num > 1)
            {
                b = Convert.ToInt32(buffer1[num]);              
                buffer1[0] = Convert.ToByte(num - 1);
                writeblock1(0);
                return b;
            }
            else
            {
                b = Convert.ToInt32(buffer1[num]);
                readblock1(b);
                writeblock1(0);
                return b;
            }

        }
        public bool returnblock(int block)//回收磁盘块
        {
            if (block > 2 & block < 256)
            {
                readblock1(0);
                int num = 0;
                num = Convert.ToInt32(buffer1[0]);
                if (num < 10)
                {
                    buffer1[num + 1] = Convert.ToByte(block);
                    buffer1[0] = Convert.ToByte(num + 1);
                    writeblock1(0);
                    return true;
                }
                else
                {
                    writeblock1(block);
                    buffer1[0] = Convert.ToByte(1);
                    buffer1[1] = Convert.ToByte(block);
                    writeblock1(0);
                    return true;
                }
            }
            else 
            {
                return false;
            }
        }
    }
}

⌨️ 快捷键说明

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