Source Code ของการคำนวณวิเคาระห์ จำนวนและแจกแจงคำสั่ง MIPS onstruction
โปรแกรมที่ใช้เขียน คือ : NetBeans IDE 8.0.2
SOURCE CODE
package command;
import java.io.*;
import java.util.Scanner;
public class Command {
public static void main(String[] args) {
// call input
File input = new File("C:\\Users\\Administrator\\Documents"
+ "\\NetBeansProjects\\Wordcommand\\assemblyCode.txt");
try {
Scanner sc = new Scanner(input);
String s;
// คำสั่งที่มีใน code assembly
final String[] wordsCommand = {"LW","ADDIU","SW","NOP","ADDU"
,"JAL","BNE","SLLV","LUI","JR"
,"SRAV","ANDI","XOR","AND","OR"};
//สร้างตัวนับคำเป็น array ความยาวเท่ากับarray ใน wordsCommand
double countWords[] = new double[wordsCommand.length];
int AllCountCommand = 0;
while(sc.hasNext()){
s = sc.next();
/*
*วน loop ใน array wordsCommand หาว่า ใน txt.(assembly code)
*มีค่า ที่ตรงกับใน array wordsCommand รึป่าว
*/
for(int i = 0 ; i < wordsCommand.length ; i=i+1){
if(s.equals(wordsCommand[i])){
// นับคำ ที่เหมือนในแต่ละindex ใน array wordsCommand
countWords[i]=countWords[i]+1;
AllCountCommand=AllCountCommand+1; // นับคำที่ตรงกันทั้งหมด
}
}
}
System.out.println("Command Assembly has :");
// Show Output And Compute Percent
for(int i = 0 ; i < wordsCommand.length ; i=i+1){
double percent = (countWords[i]/AllCountCommand)*100;
System.out.print(wordsCommand[i]+" ="+countWords[i]+"(");
System.out.printf("%.2f",percent);
System.out.print("%"+")");
System.out.println();
}
System.out.println("All command ="+AllCountCommand);
} catch(Exception ex) {
}
}
}
ผลการทำงานของโปรแกรม