os.java
来自「这是一个简单的操作系统,主要用文件来模拟磁盘,主要是实现CPU的调度及操作系统的」· Java 代码 · 共 2,762 行 · 第 1/5 页
JAVA
2,762 行
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
import java.io.*;
import java.awt.*;
import java.util.*;
public class OS extends JFrame implements ActionListener
{
char[] comand=new char[30];
char[] comand1=new char[6];
String stt,stt1;
JPanel row1,row2,row3,adt;
JLabel JLabel1,JLabel2,JLabel3,JLabel4;
JLabel L1,L2,L3,L4;
JTextArea ja1,ja2,ja3,ja4;
JScrollPane js1,js2,js3,js4;
JTextArea a1,a2,a3;
JTextArea t1,t2,t3,t4,t5;
JButton b1,b2;
char[][] pcb=new char[11][17];
int pc,use;
int[] list=new int[32];
char[] ir=new char[5];
char[][] user=new char[32][16];
char[] answer=new char[5];
String stt0;
int psw,time,l,m,n;
char[] mainbuff=new char[128];
double record=0;
char[][] A=new char[1][2];
char[][] B=new char[2][2];
char[][] C=new char[2][2];
public OS()
{
super("OS模拟");
Container con=getContentPane();
con.setLayout(new GridLayout(3,1,0,0));
//JLabel1=new JLabel("系统时间:");
JLabel2=new JLabel("CPU运行的进程:");
JLabel3=new JLabel("结束剩余时间片:");
JLabel4=new JLabel("用户接口区:");
a1=new JTextArea(1,8);
a2=new JTextArea(1,8);
a3=new JTextArea(1,24);
b1=new JButton("执行");
b2=new JButton("创建新DISK");
row1=new JPanel(new FlowLayout());
//row1.add(JLabel1);
row1.add(JLabel2);
row1.add(a1);
row1.add(JLabel3);
row1.add(a2);
row1.add(JLabel4);
row1.add(a3);
row1.add(b1);
row1.add(b2);
L1=new JLabel("就绪队列进程");
L2=new JLabel("等待队列进程");
L3=new JLabel("执行进程中间结果");
L4=new JLabel("磁盘目录结构");
ja1=new JTextArea(2,8);
ja2=new JTextArea(2,8);
ja3=new JTextArea(1,8);
ja4=new JTextArea(4,8);
js1=new JScrollPane(ja1);
js2=new JScrollPane(ja2);
js3=new JScrollPane(ja3);
js4=new JScrollPane(ja4);
row2=new JPanel(new GridLayout(2,4,2,2));
row2.add(L1);row2.add(L2);
row2.add(L3);row2.add(L4);
row2.add(js1);row2.add(js2);
row2.add(js3);row2.add(js4);
t1=new JTextArea(1,10);
t2=new JTextArea(1,10);
t3=new JTextArea(3,10);
t4=new JTextArea(8,8);
t5=new JTextArea(8,16);
adt=new JPanel(new GridLayout(3,1,2,2));
adt.add(t1);
adt.add(t2);
adt.add(t3);
row3=new JPanel(new GridLayout(1,3,2,2));
row3.add(adt);
row3.add(t4);
row3.add(t5);
con.add(row1);
con.add(row2);
con.add(row3);
b1.addActionListener(this);
b2.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent eee)
{
new disk();
}});
psw=0;
for(l=0;l<5;l++)
{
answer[l]=' ';
}
for(m=0;m<32;m++)
{
for(n=0;n<16;n++)
{
user[m][n]='^';
}
}
for(l=0;l<11;l++)
{
for(m=0;m<17;m++)
{
pcb[l][m]=' ';
}
}
A[0][0]='0';A[0][1]='0';
B[0][0]='0';B[0][1]='0';B[1][0]='0';B[1][1]='0';
C[0][0]='0';C[0][1]='0';C[1][0]='0';C[1][1]='0';
a1.setText("闲逛");
a2.setText("未知");
ja1.setText("无");
ja2.setText("无");
t3.setText("无正在使用设备");
checkmem();
look();
lookmem();
showpc1();
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
javax.swing.SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception e)
{
e.printStackTrace();
}
setLocation(300,130);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// public static void main(String[] args)
// {
// new clock();
// new OS();
// }
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
stt=a3.getText();
comand=stt.toCharArray();
}
if(comand[0]=='@')
{
if(comand[1]==' ')
{
comand1[0]=comand[2];comand1[1]=comand[3];comand1[2]=comand[4];comand1[3]=comand[5];comand1[4]=comand[6];comand1[5]=comand[7];
stt1=stt1.valueOf(comand1);
//ja1.setText(stt1);
if(stt1.compareTo("create")==0)
{
if(comand[8]==' ')
{
stt1=stt.substring(9)+" ";
Create(stt1);
t5.setText("\n");
lookmem();
showpc1();
a1.setText("闲逛");
a2.setText("未知");
}
}
else if(stt1.compareTo("rmdir ")==0)
{
if(comand[8]==' ')
{
stt1=stt.substring(9)+" ";
Rmidir(stt1);
t5.setText("\n");
lookmem();
showpc1();
a1.setText("闲逛");
a2.setText("未知");
t3.setText("无正在使用设备");
}
}
else if(stt1.compareTo("delete")==0)
{
if(comand[8]==' ')
{
stt1=stt.substring(9)+" ";
Delete(stt1);
t5.setText("\n");
lookmem();
showpc1();
a1.setText("闲逛");
a2.setText("未知");
t3.setText("无正在使用设备");
}
}
else if(stt1.compareTo("type ")==0)
{
if(comand[8]==' ')
{
stt1=stt.substring(9)+" ";
stt1=Type(stt1);
t4.setText(stt1);
ja4.setText("\n");
look();
t5.setText("\n");
lookmem();
showpc1();
a1.setText("闲逛");
a2.setText("未知");
t3.setText("无正在使用设备");
}
}
else if(stt1.compareTo("copy ")==0)
{
if(comand[8]==' ')
{
JOptionPane.showMessageDialog(OS.this,"对不起!暂无此功能!");
// stt1=stt.substring(9)+" ";
// Copy(stt1);
}
}
else if(stt1.compareTo("edit ")==0)
{
if(comand[8]==' ')
{
stt1=stt.substring(9)+" ";
Edit(stt1);
t5.setText("\n");
lookmem();
showpc1();
a1.setText("闲逛");
a2.setText("未知");
t3.setText("无正在使用设备");
}
}
else if(stt1.compareTo("mkdir ")==0)
{
if(comand[8]==' ')
{
JOptionPane.showMessageDialog(OS.this,"对不起!暂无此功能!");
// stt1=stt.substring(9)+" ";
// Mkdir(stt1);
}
}
else if(stt1.compareTo("exe ")==0)
{
if(comand[8]==' ')
{
stt1=stt.substring(9)+" ";
Exe(stt1);
t5.setText("\n");
lookmem();
}
}
else{JOptionPane.showMessageDialog(OS.this,"不是一个命令!");}
}
else
{JOptionPane.showMessageDialog(OS.this,"不是一个命令!");}
}
else
{JOptionPane.showMessageDialog(OS.this,"不是一个命令!");}
}
public void Create(String st)
{
char[][] disk=new char[128][64];
char[] buff=new char[30];
char[] buff1=new char[6];
String string;
int sign=0;
int sign1,sign2,sign3,sign4,sign5;
int a,b,c,d;
for(c=0;c<6;c++)
{
buff1[c]=0;
}
buff=st.toCharArray();
try
{
File file=new File("C:/disk.txt");
FileInputStream input=new FileInputStream(file);
DataInputStream data=new DataInputStream(input);
for(a=0;a<128;a++)
{
for(b=0;b<64;b++)
{
disk[a][b]=data.readChar();
}
}
data.close();
}
catch(IOException e)
{
e.printStackTrace();
}
if(buff[0]=='\\')
{
sign=1;
sign1=sign;
for(;buff[sign]!='\\'&&sign<sign1+4;sign++)
{}
b=0;
if(sign==sign1)
{JOptionPane.showMessageDialog(OS.this,"路径错误!");}
else if(sign>=sign1+4)
{
c=0;
for(a=0;buff[sign1]!=0&&a<5;sign1++,a++)
{
buff1[a]=buff[sign1];
buff1[a+1]=0;
if(buff1[a]=='.')
{
b=a;
c=1;
if(b>3)
{b=3;}
}
else
{
b++;
}
}
sign5=0;
for(d=0;d<8;d++)
{
if(disk[2][sign5]==0)
{
d=7;
sign4=sign5;
for(a=0;a<b;a++)
{
disk[2][sign4]=buff1[a];
sign4++;
}
if(c==1)
{
disk[2][sign5+3]='e';
}
else
{
disk[2][sign5+3]='t';
}
JOptionPane.showMessageDialog(OS.this,"创建成功!");
}
else
{
sign5=sign5+8;
}
}
}
else
{
sign2=sign-sign1;
for(int i=0;sign2>0;sign2--)
{
buff1[i]=buff[sign1];
i++;
sign1++;
}
sign3=2;
sign4=0;
sign5=sign4;
b=0;
for(a=0;a<8;a++)
{
for(;buff1[b]==disk[sign3][sign4]&&sign4<sign5+3;sign4++,b++)
{
}
if(sign4==sign5+3)
{
a=8;
sign3=disk[sign3][sign5+5];
}
sign4=sign5;
sign4=sign4+8;
sign5=sign4;
b=0;
}
if(a==9)
{
b=0;
c=0;
sign++;
sign1=sign;
for(a=0;buff[sign]!=0&&a<5;sign++,a++)
{
buff1[a]=buff[sign];
buff1[a+1]=0;
if(buff[sign]=='.')
{
b=a;
c=1;
if(b>3)
{b=3;}
}
else
{
b++;
}
}
sign4=0;
for(d=0;d<8;d++)
{
if(disk[sign3][sign4]==0)
{
d=7;
sign5=sign4;
for(a=0;a<b;a++)
{
disk[sign3][sign5]=buff1[a];
sign5++;
}
if(c==1)
{
disk[sign3][sign4+3]='e';
}
else
{
disk[sign3][sign4+3]='t';
}
if(sign3>63)
{
sign3=sign3-64;
disk[1][sign3]=1;
}
else
{
disk[0][sign3]=1;
}
JOptionPane.showMessageDialog(OS.this,"创建成功!");
}
else
{
sign4=sign4+8;
}
}
}
else
{
JOptionPane.showMessageDialog(OS.this,"路径错误!");
}
}
}
else
{
JOptionPane.showMessageDialog(OS.this,"路径错误!");
System.out.println("sasasas");
}
try
{
File file1=new File("C:/disk.txt");
FileOutputStream input1=new FileOutputStream(file1);
DataOutputStream data1=new DataOutputStream(input1);
for(a=0;a<128;a++)
{
for(b=0;b<64;b++)
{
data1.writeChar(disk[a][b]);
}
}
data1.close();
}
catch(IOException e)
{
e.printStackTrace();
}
ja4.setText("\n");
look();
}
public void Delete(String st)
{
char[][] disk=new char[128][64];
char[] buff=new char[30];
char[] buff1=new char[6];
String string;
int a=0;
int b=0;
int c=0;
int d;
int sign,sign1,sign2,sign3,sign4,sign5,sign6;
boolean note=true;
for(c=0;c<6;c++)
{
buff1[c]=0;
}
for(c=0;c<30;c++)
{
buff[c]=0;
}
buff=st.toCharArray();
System.out.println(buff);
try
{
File file=new File("C:/disk.txt");
FileInputStream input=new FileInputStream(file);
DataInputStream data=new DataInputStream(input);
for(a=0;a<128;a++)
{
for(b=0;b<64;b++)
{
disk[a][b]=data.readChar();
}
}
data.close();
}
catch(IOException e)
{
e.printStackTrace();
}
if(buff[0]=='\\')
{
sign=1;
sign1=sign;
for(;buff[sign]!='\\'&&sign<sign1+4;sign++)
{}
b=0;
if(sign==sign1)
{JOptionPane.showMessageDialog(OS.this,"路径错误!");}
else if(sign>=sign1+4)
{
c=0;
for(a=0;buff[sign1]!=0&&a<5;sign1++,a++)
{
buff1[a]=buff[sign1];
buff1[a+1]=0;
if(buff1[a]=='.')
{
b=a;
if(b>3)
{b=3;}
break;
}
else
{
if(buff1[a]!=' ')
{ b++;}
}
}
System.out.println(b);
sign5=0;
sign4=0;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?