64


数据的机器级表示

1.将整数真值(十进制表示)转化成补码表示的二进制,默认长度32位。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public static String intToBinary(String numStr) {
int num = Integer.parseInt(numStr);
if(num == Integer.MIN_VALUE) return "10000000000000000000000000000000";
boolean neg = false;
if(num < 0) {
num = -num;
neg = true;
}
StringBuilder temp = new StringBuilder();
while(num > 0) {
temp.insert(0, num % 2 == 0 ? '0' : '1');
num /= 2;
}
int len = temp.length();
for(int i = 0;i < 32 - len; i++) {
temp.insert(0, '0');
}
String ans = temp.toString();
if(neg) {
ans = addOne(negation(ans));
}
return ans;
}

// 对二进制的每一位取反
private static String negation(String num) {
StringBuilder sb = new StringBuilder();
int len = num.length();
for(int i = 0;i < len; i++) {
sb.append(num.charAt(i) == '1' ? '0' :'1');
}
return sb.toString();
}

// 将二进制数加一
private static String addOne(String num) {
StringBuilder sb = new StringBuilder(num);
int len = num.length();
for(int i = len - 1; i >= 0;i--) {
if(sb.charAt(i) == '1') {
sb.setCharAt(i, '0');
} else {
sb.setCharAt(i, '1');
break;
}
}
return sb.toString();
}

2.将补码表示的二进制转化成整数真值(十进制表示)

1
2
3
4
5
6
7
8
9
10
11
12
13
public static String binaryToInt(String binStr) {
int num = 0;
int len = binStr.length();

for (int i = 1; i < len; i++) {
if(binStr.charAt(i) == '0') continue;
num += (1 << (len - i - 1));
}
if (binStr.charAt(0) == '1') {
num -= (1 << (len - 1));
}
return num + "";
}

3.将十进制整数的真值转化成NBCD表示(符号位用4位表示)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
   public static String decimalToNBCD(String decimalStr) {
StringBuffer NBCD = new StringBuffer();
int num = Integer.parseInt(decimalStr);
boolean neg = false;
if (num < 0) {
neg = true;
num = -num;
}
while (num > 0) {
NBCD.insert(0, getNBCD_4(num % 10));
num /= 10;
}
int len = NBCD.length();
for (int i = 0; i < 28 - len; i++) {
NBCD.insert(0, '0');
}
if (neg) {
NBCD.insert(0, "1101");
} else {
NBCD.insert(0, "1100");
}
return NBCD.toString();
}
// 将一位十进制数转化为NBCD码
public static String getNBCD_4(int i) {
StringBuffer sb = new StringBuffer("0000");
int loc = 3;
while (i > 0) {
sb.setCharAt(loc, i % 2 == 1 ? '1' : '0');
i /= 2;
loc--;
}
return sb.toString();
}

4.将NBCD表示(符号位用4位表示)转化成十进制整数的真值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static String NBCDToDecimal(String NBCDStr) {
int num = 0;
for (int i = 0; i < 7; i++) {
num = num * 10 + fourNBCDTOInt(NBCDStr, 7 + 4 * i);
}
if (NBCDStr.charAt(3) == '1') {
num = -num;
}
return String.valueOf(num);
}

public static int fourNBCDTOInt(String bits, int r) {
int num = 0;
for (int i = 0; i < 4; i++) {
num += (1 << i) * (bits.charAt(r - i) == '1' ? 1 : 0);
}
return num;
}

5.将浮点数真值转化成32位单精度浮点数表示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
   public static String floatToBinary(String floatStr) {
float num = Float.valueOf(floatStr);
if (num > Float.MAX_VALUE) {
return "+Inf";
}
if (num < -Float.MAX_VALUE) {
return "-Inf";
}
if (num < Float.MIN_VALUE && num > -Float.MIN_VALUE) {
return (floatStr.charAt(0) == '-' ? "1" : "0") + "0000000000000000000000000000000";
}
boolean neg = false;
if (num < 0) {
num = -num;
neg = true;
}
String ans = neg ? "1" : "0";
int bias = 127;
if (num < Float.MIN_NORMAL) {
ans += "00000000";
System.out.println("n1 " + num);
num *= Math.pow(2, 126);
System.out.println("n2 " + num);
ans += fixToString(num);

} else {
int exp = 0;
while (num >= 2) {
num /= 2;
exp++;
}
while (num < 1) {
num *= 2;
exp--;
}
exp += bias;
ans += intToBinary(exp+"").substring(24);

ans += fixToString(num - 1);
}
return ans;
}
// 将浮点数转化为23位的二进制数(0<num<1)
private static String fixToString(float num) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 23; i++) {
num *= 2;
if (num >= 1) {
sb.append('1');
num -= 1;
} else {
sb.append('0');
}
}
return sb.toString();
}

6.将32位单精度浮点数表示转化成浮点数真值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public static String binaryToFloat(String binStr) {
boolean neg = binStr.charAt(0) == '1';
int exp = bitsToInt(binStr.substring(1, 9));
int frac = bitsToInt(binStr.substring(9, 32));
if (exp == 255) {
if (frac == 0) {
return (neg ? "-" : "+") + "Inf";
} else {
return "NaN";
}
}
if (exp == 0) {
return String.valueOf((float) ((neg ? (-1) : 1) * frac * Math.pow(2, -149)));
}
return String.valueOf((float) ((neg ? (-1) : 1) * (1 + frac * Math.pow(2, -23)) * Math.pow(2, exp - 127)));
}

public static int bitsToInt(String str) {
int num = 0;
int len = str.length();
for (int i = 0; i < len; i++) {
num *= 2;
num += (str.charAt(i) == '1' ? 1 : 0);
}
return num;
}

整数算术运算

1.一位全加器(输出为长度为2 的字符串,包括1 位的进位输入和1 位的加法运算结果)

a1f8e264b3b7dc7bd44becc79bbb698

1
2
3
4
5
private String fullAdder(char x, char y, char c) {
int bit = (x - '0') ^ (y - '0') ^ (c - '0'); //三位异或
int carry = ((x - '0') & (y - '0')) | ((y - '0') & (c - '0')) | ((x - '0') & (c - '0')); //有两位为1则产生进位
return "" + carry + bit; //第一个空串让后面的加法都变为字符串加法
}

2.4位先行进位加法器

779f91b211dbac0dd55bcde8d454722

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static String claAdder(String operand1, String operand2, char c) {
String P = new String();
String G = new String();
String C = new String(c + "");
String result = "";
for(int i = 0;i < 4; i++) {
P = P + ((operand1.charAt(i) == '1' || operand2.charAt(i) == '1') ? '1' : '0');
G = G + ((operand1.charAt(i) == '1' && operand2.charAt(i) == '1') ? '1' : '0');
}
for(int i = 3; i >= 0; i--) {
C = ((G.charAt(i) == '1' || P.charAt(i) == '1' && C.charAt(0) == '1') ? '1' : '0') + C;
}
result = C.charAt(0) + "";
for(int i = 0;i < 4; i++) {
result = result + fullAdder(operand1.charAt(i), operand2.charAt(i), C.charAt(i + 1)).charAt(1);
}
return result;
}

3.实现16 位的部分先行进位加法器(基于上述方法claAdder)

16bc6143fed111070678490bfa0a843

1
2
3
4
5
6
7
8
9
10
public static String pclaAdder(String operand1, String operand2, char c) {
String temp = "";
String result = "";
for(int i = 3;i >= 0; i--) {
temp = claAdder(operand1.substring(i * 4, i * 4 + 4), operand2.substring(i * 4, i * 4 + 4), c);
c = temp.charAt(0);
result = temp.substring(1) + result;
}
return c + result;
}

4.实现32位整数加法(基于fullAdder方法)

1
2
3
4
5
6
7
8
9
10
11
12
public DataType add(DataType src, DataType dest) {
StringBuilder result = new StringBuilder();
char c = '0';
String operand1 = src.toString();
String operand2 = dest.toString();
for(int i = 31; i >=0;i--) {
String temp = fullAdder(operand1.charAt(i), operand2.charAt(i), c);
result.append(temp.charAt(1));
c = temp.charAt(0);
}
return new DataType(result.reverse().toString());
}

5.实现32位整数减法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  public DataType sub(DataType src, DataType dest) {
StringBuilder str = new StringBuilder(src.toString());
negAndAddOne(str);
return add(new DataType(str.toString()), dest);
}
// 取反加一
private void negAndAddOne(StringBuilder str){
boolean flip = false;
for (int i = 31; i >= 0; i--) {
if (flip) {
str.setCharAt(i, str.charAt(i) == '1' ? '0' : '1');
} else {
flip = str.charAt(i) == '1';
}
}
}

一次遍历实现取反加一:假设原二进制数最低位的1在第n位(最低位为第0位),那么该二进制数可以表示为:xx…xx10…0(后缀n个0)。取反之后为:xx…xx01…1,前面的x全部取反。再加一之后变为:xx…xx10…0可以发现,最后的10…0没有改变,只有前面的x取反了一次。所以我们只需要从后面开始遍历,在遇到第一个1之前不作改变,遇到1之后再开始取反。

6.实现32位整数布斯乘法

bdd5337ccfca477eced16a1580b3bd3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   public DataType mul(DataType src, DataType dest) {
String operand1 = src.toString();
String operand2 = src.toString();
StringBuilder sb = new StringBuilder(operand1);
negAndAddOne(sb);
String negOperand1 = sb.toString();
StringBuilder product = new StringBuilder("00000000000000000000000000000000" + operand2 + "0");
for(int i = 0;i < 32; i++) {
if(product.charAt(63) == '0' && product.charAt(64) == '1') {
highAdd(operand1, product);
} else if(product.charAt(63) == '1' && product.charAt(64) == '0') {
highAdd(negOperand1, product);
}
ASR(product);
}
return new DataType(product.substring(32,64));
}
// 在dest的高32位上加上src
private void highAdd(String src, StringBuilder dest) {
String temp = dest.substring(0, 32);
temp = add(new DataType(temp), new DataType(src)).toString();
for(int i = 0;i < 32; i++) {
dest.setCharAt(i, temp.charAt(i));
}
}
// 算数右移
private void ASR(StringBuilder sb) {
sb.insert(0, sb.charAt(0));
sb.deleteCharAt(sb.length() - 1);
}

7.恢复余数的32位整数除法

37a20113d188fa2949334ee757f2a11

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public DataType div(DataType src, DataType dest) {
String operand1 = src.toString();
String operand2 = dest.toString();
StringBuilder temp = new StringBuilder(operand1);
negAndAddOne(temp);
String negOperand1 = temp.toString();
if (operand1.equals("00000000000000000000000000000000")) throw new ArithmeticException();
StringBuilder product;
if (operand2.charAt(0) == '1') {
product = new StringBuilder("11111111111111111111111111111111" + operand2);
} else {
product = new StringBuilder("00000000000000000000000000000000" + operand2);
}
for (int i = 0; i < 32; i++) {
LeftShift(product, '0');
char c = '1';
if (product.charAt(0) == operand1.charAt(0)) {
highAdd(negOperand1, product);
if (product.charAt(0) != operand1.charAt(0)) {
highAdd(operand1, product);
c = '0';
}
} else {
highAdd(operand1, product);
if (product.charAt(0) == operand1.charAt(0)) {
highAdd(negOperand1, product);
c = '0';
}
}
product.setCharAt(63, c);
}
remainderReg = new DataType(product.substring(0, 32));
// 若除数与被除数异号,商取反加一
String quotient = product.substring(32, 64);
if (operand1.charAt(0) != operand2.charAt(0)) {
temp = new StringBuilder(quotient);
negAndAddOne(temp);
quotient = temp.toString();
}
// 处理特殊情况:余数与除数相等,余数改为0,商加一;
// 余数与除数相反,余数改为0,商减一。
if(remainderReg.toString().equals(operand1)) {
remainderReg = new DataType("00000000000000000000000000000000");
quotient = add(new DataType(quotient), new DataType("00000000000000000000000000000001")).toString();
} else if(remainderReg.toString().equals(negOperand1)) {
remainderReg = new DataType("00000000000000000000000000000000");
quotient = add(new DataType(quotient), new DataType("11111111111111111111111111111111")).toString();
}
return new DataType(quotient);
}
private void LeftShift(StringBuilder sb, char c) {
sb.deleteCharAt(0);
sb.append(c);
}

8.不恢复余数的32位整数除法

9a4262aaef43f3886d50bf544803d39

fcb272820a3b29419ddc6112b7b0a80

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public DataType div(DataType src, DataType dest) {
String operand1 = src.toString();
String operand2 = dest.toString();
StringBuilder temp = new StringBuilder(operand1);
negAndAddOne(temp);
String negOperand1 = temp.toString();
if (operand1.equals("00000000000000000000000000000000")) throw new ArithmeticException();
StringBuilder product;
if (operand2.charAt(0) == '1') {
product = new StringBuilder("11111111111111111111111111111111" + operand2);
} else {
product = new StringBuilder("00000000000000000000000000000000" + operand2);
}
if (product.charAt(0) == operand1.charAt(0)) {
highAdd(negOperand1, product);
} else {
highAdd(operand1, product);
}
for (int i = 0; i < 32; i++) {
if (product.charAt(0) == operand1.charAt(0)) {
LeftShift(product, '1');
highAdd(negOperand1, product);
} else {
LeftShift(product, '0');
highAdd(operand1, product);
}
}

StringBuilder quotientSB = new StringBuilder(product.substring(32, 64));
// 商修正:商左移一位,若余数寄存器与除数同号,低位补1;若异号,低位补0.
if (product.charAt(0) == operand1.charAt(0)) {
LeftShift(quotientSB, '1');
} else {
LeftShift(quotientSB, '0');
}
// 若被除数与除数异号,商加一
if(operand1.charAt(0) != operand2.charAt(0)) {
highAdd("00000000000000000000000000000001", quotientSB);
}
// 余数修正:若余数与被除数同号,不修正;
// 若余数与被除数异号:若被除数与除数同号,余数减去除数;
// 若被除数与除数异号,余数加上除数。
StringBuilder remainderSB = new StringBuilder(product.substring(0, 32));
if (remainderSB.charAt(0) != operand2.charAt(0)) {
if (operand1.charAt(0) == operand2.charAt(0)) {
highAdd(negOperand1, remainderSB);
} else {
highAdd(operand1, remainderSB);
}
}

//若余数与除数相等,余数改为0,商加一;
//若余数与除数相反,余数改为0,商减一。
if (remainderSB.toString().equals(operand1)) {
remainderSB = new StringBuilder("00000000000000000000000000000000");
highAdd("00000000000000000000000000000001", quotientSB);
} else if (remainderSB.toString().equals(negOperand1)) {
remainderSB = new StringBuilder("00000000000000000000000000000000");
highAdd("11111111111111111111111111111111", quotientSB);
}
remainderReg = new DataType(remainderSB.toString());
return new DataType(quotientSB.toString());
}

浮点数算数运算

1.计算两个浮点数的和。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public DataType add(DataType src, DataType dest) {
String x = src.toString();
String y = dest.toString();
if (x.matches(IEEE754Float.NaN_Regular) || y.matches(IEEE754Float.NaN_Regular)) {
return new DataType(IEEE754Float.NaN);
}
String temp = cornerCheck(addCorner, x, y);
if (temp != null) return new DataType(temp);
if(isZero(x)) return new DataType(y);
if(isZero(y)) return new DataType(x);
if(isFinite(x)) return new DataType(x);
if(isFinite(y)) return new DataType(y);
int xexp = Integer.valueOf(x.substring(1, 9), 2);
int yexp = Integer.valueOf(y.substring(1, 9), 2);
if (xexp == 0) xexp = 1;
if (yexp == 0) yexp = 1;
String xfrac = getFrac(x);
String yfrac = getFrac(y);
int exp = Math.max(xexp, yexp);
if (yexp < exp) {
yfrac = rightShift(yfrac, exp - yexp);
}
if (xexp < exp) {
xfrac = rightShift(xfrac, exp - xexp);
}
char xs = x.charAt(0);
char ys = y.charAt(0);
char s;
String frac;
if (xs == ys) {
frac = ALU.add("0" + xfrac, "0" + yfrac).toString();
s = xs;
exp++;
} else if (xs == '1' && ys == '0') {
frac = ALU.sub("0" + yfrac, "0" + xfrac).toString();
s = frac.charAt(0);
if (s == '1') {
frac = ALU.negation(frac);
}
frac = frac.substring(1);
} else {
frac = ALU.sub("0" + xfrac, "0" + yfrac).toString();
s = frac.charAt(0);
if (s == '1') {
frac = ALU.negation(frac);
}
frac = frac.substring(1);
}
while (exp > 0 && frac.charAt(0) == '0') {
frac = LeftShift(frac, '0');
exp--;
}
if (exp < 0) return new DataType(IEEE754Float.P_ZERO);
if (exp >= 255) return new DataType(s == '1' ? IEEE754Float.N_INF : IEEE754Float.P_INF);
if (exp == 0) {
frac = rightShift(frac, 1);
}
return new DataType(round(s, Transformer.intToBinary(exp + "").substring(32 - 8), frac.substring(0, 27)));
}

2.计算两个浮点数的差。

1
2
3
4
5
public DataType sub(DataType src, DataType dest) {
String s = src.toString();
src = new DataType((s.charAt(0) == '0' ? '1' : '0') + s.substring(1));
return add(src, dest);
}

3.计算两个浮点数的乘积。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public DataType mul(DataType src, DataType dest) {
String x = src.toString();
String y = dest.toString();
if (x.matches(IEEE754Float.NaN_Regular) || y.matches(IEEE754Float.NaN_Regular)) {
return new DataType(IEEE754Float.NaN);
}
String temp = cornerCheck(mulCorner, x, y);
if (temp != null) return new DataType(temp);
char xs = x.charAt(0);
char ys = y.charAt(0);
char s;
if (xs == ys) s = '0';
else s = '1';
if (isZero(x) || isZero(y)) return new DataType(s == '1' ? IEEE754Float.N_ZERO : IEEE754Float.P_ZERO);
if (isFinite(x) || isFinite(y)) return new DataType(s == '1' ? IEEE754Float.N_INF : IEEE754Float.P_INF);
int xexp = Integer.valueOf(x.substring(1, 9), 2);
int yexp = Integer.valueOf(y.substring(1, 9), 2);
if (xexp == 0) xexp = 1;
if (yexp == 0) yexp = 1;
String xfrac = getFrac(x);
String yfrac = getFrac(y);
int exp = xexp + yexp - 127;
String frac = ALU.mul(xfrac, yfrac).toString();
if (frac.charAt(0) == '1') {
exp++;
} else {
frac = frac.substring(1);
}
while (exp > 0 && frac.charAt(0) == '0') {
frac = LeftShift(frac, '0');
exp--;
}
while (exp < 0) {
frac = rightShift(frac, 1);
exp++;
}
if (exp >= 255) return new DataType(s == '1' ? IEEE754Float.N_INF : IEEE754Float.P_INF);
if (exp == 0) {
frac = rightShift(frac, 1);
}
return new
}

4.计算两个浮点数的商。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public DataType div(DataType src, DataType dest) {
String x = src.toString();
String y = dest.toString();
if (x.matches(IEEE754Float.NaN_Regular) || y.matches(IEEE754Float.NaN_Regular)) {
return new DataType(IEEE754Float.NaN);
}
String temp = cornerCheck(divCorner, x, y);
if (temp != null) return new DataType(temp);
char xs = x.charAt(0);
char ys = y.charAt(0);
char s;
if (xs == ys) s = '0';
else s = '1';
if (isZero(x)) throw new ArithmeticException();
if (isZero(y)) return new DataType(s == '1' ? IEEE754Float.N_ZERO : IEEE754Float.P_ZERO);
if (isFinite(x)) return new DataType(s == '1' ? IEEE754Float.N_ZERO : IEEE754Float.P_ZERO);
if (isFinite(y)) return new DataType(s == '1' ? IEEE754Float.N_INF : IEEE754Float.P_INF);
int xexp = Integer.valueOf(x.substring(1, 9), 2);
int yexp = Integer.valueOf(y.substring(1, 9), 2);
if (xexp == 0) xexp = 1;
if (yexp == 0) yexp = 1;
String xfrac = getFrac(x);
String yfrac = getFrac(y);
int exp = yexp - xexp + 127;
String frac = ALU.div(xfrac, yfrac).toString();
while (exp > 0 && frac.charAt(0) == '0') {
frac = LeftShift(frac, '0');
exp--;
}
if (exp >= 255) return new DataType(s == '1' ? IEEE754Float.N_INF : IEEE754Float.P_INF);
if (exp == 0) {
frac = rightShift(frac, 1);
}
return new DataType(round(s, Transformer.intToBinary(exp + "").substring(32 - 8), frac));
}

磁盘寻道算法

1.计算使用先来先服务算法(FCFS)访问磁道的平均寻道长度。

1
2
3
4
5
6
7
8
public double FCFS(int start, int[] request) {
double result = 0;
for (int loc : request) {
result += Math.abs(start - loc);
start = loc;
}
return result / request.length;
}

2.计算最短寻到时间优先算法(SSTF)访问磁道的平均寻道长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public double SSTF(int start, int[] request) {
double result = 0;
int size = request.length;
LinkedList<Integer> requests = new LinkedList<>();
for (int num : request) {
requests.add(num);
}
while (!requests.isEmpty()) {
int loc = findMinDistance(start, requests);
result += Math.abs(start - requests.get(loc));
start = requests.get(loc);
requests.remove(loc);
}
return result / size;
}
private int findMinDistance(int start, LinkedList<Integer> request) {
int minD = Integer.MAX_VALUE;
int loc = -1;
for (int i = 0; i < request.size(); i++) {
int newD = Math.abs(start - request.get(i));
if (newD < minD) {
minD = newD;
loc = i;
}
}
return loc;
}

3.计算扫描算法(SCAN)访问磁道的平均寻道长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public double SCAN(int start, int[] request, boolean direction) {
double result;
if (start == 0) direction = true;
if (start == Disk.TRACK_NUM) direction = false;
int maxTrace = Arrays.stream(request).max().getAsInt();
int minTrace = Arrays.stream(request).min().getAsInt();
if (direction) {
if (start <= minTrace) {
result = maxTrace - start;
} else
result = Disk.TRACK_NUM - 1 - start + Disk.TRACK_NUM - 1 - minTrace;
} else {
if (start >= maxTrace) {
result = start - minTrace;
} else {
result = start + maxTrace;
}
}
return result / request.length;
}

4.计算C-SCAN算法访问磁道的平均寻道长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public double CSCAN(int start, int[] request) {
int maxTrace = Arrays.stream(request).max().getAsInt();
int minTrace = Arrays.stream(request).min().getAsInt();
int result = 0;
if (start <= minTrace) {
result = maxTrace - start;
} else if (start > maxTrace) {
result = Disk.TRACK_NUM - 1 - start + maxTrace + Disk.TRACK_NUM - 1;
} else {
int lb = 0;
for (int i = 0; i < request.length; i++) {
if (request[i] < start) {
if (request[i] > lb) {
lb = request[i];
}
}
}
result = Disk.TRACK_NUM - 1 - start + lb + Disk.TRACK_NUM - 1;
}
return 1.0 * result / request.length;
}

5.计算LOOK算法访问磁道的平均寻道长度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public double LOOK(int start, int[] request, boolean direction) {
int maxTrace = Arrays.stream(request).max().getAsInt();
int minTrace = Arrays.stream(request).min().getAsInt();
if (start <= minTrace) direction = true;
if (start >= maxTrace) direction = false;
int result = 0;
if (start <= minTrace) {
result = maxTrace - start;
} else if (start >= maxTrace) {
result = start - minTrace;
} else if (direction) {
result = maxTrace - start + maxTrace - minTrace;
} else {
result = start - minTrace + maxTrace - minTrace;
}
return 1.0 * result / request.length;
}

6.计算C-LOOK算法访问磁道的平均寻道长度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    public double CLOOK(int start, int[] request) {
double result = 0;
int maxTrace = Arrays.stream(request).max().getAsInt();
int minTrace = Arrays.stream(request).min().getAsInt();
if(start <= minTrace) {
result = maxTrace - start;
} else if (start > maxTrace) {
result = start - minTrace + maxTrace - minTrace;
} else {
int lb = 0;
for (int i = 0; i < request.length; i++) {
if (request[i] < start) {
if (request[i] > lb) {
lb = request[i];
}
}
}
result = maxTrace - start + maxTrace - minTrace + lb - minTrace;
}
return result / request.length;
}
}