123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- package main
- import (
- "fmt"
- "strconv"
- )
- type ProcessQueue struct {
- ProcessQueue []*Process
- typeOfAl int
- time int
- }
- func (s ProcessQueue) Len() int {
- return len(s.ProcessQueue)
- }
- func (s ProcessQueue) Swap(i, j int) {
- s.ProcessQueue[i], s.ProcessQueue[j] = s.ProcessQueue[j], s.ProcessQueue[i]
- }
- // Less 实现接口,用于sort排序
- func (s ProcessQueue) Less(i, j int) bool {
- switch s.typeOfAl {
- case 1:
- return s.ProcessQueue[i].arriveTime < s.ProcessQueue[j].arriveTime
- case 2:
- return s.ProcessQueue[i].serveTime < s.ProcessQueue[j].serveTime
- case 3:
- return (s.time-s.ProcessQueue[i].arriveTime+s.ProcessQueue[i].serveTime)*s.ProcessQueue[j].serveTime > (s.time-s.ProcessQueue[j].arriveTime+s.ProcessQueue[j].serveTime)*s.ProcessQueue[i].serveTime
- case 4:
- return s.ProcessQueue[i].priority > s.ProcessQueue[j].priority
- case 5:
- return s.ProcessQueue[i].beginTime < s.ProcessQueue[j].beginTime
- }
- return s.ProcessQueue[i].arriveTime < s.ProcessQueue[j].arriveTime
- }
- func initProcessQueue() ProcessQueue {
- var processQueue ProcessQueue
- var numOfProcess int
- var name string
- var arriveTime int
- var serveTime int
- fmt.Print("[+]请输入进程数量:")
- fmt.Scan(&numOfProcess)
- for i := 0; i < numOfProcess; i++ {
- fmt.Print("[+]请输入第" + strconv.Itoa(i) + "个进程的信息(到达时间 服务时间 进程名称): ")
- fmt.Scan(&arriveTime, &serveTime, &name)
- processQueue.ProcessQueue = append(processQueue.ProcessQueue, newProcess(name, arriveTime, serveTime))
- }
- fmt.Println("[-]进程ID\t进程名称\t到达时间\t服务时间")
- for i := 0; i < numOfProcess; i++ {
- fmt.Println("[-]" + strconv.Itoa(i) + "\t\t" + processQueue.ProcessQueue[i].name + "\t\t" + strconv.Itoa(processQueue.ProcessQueue[i].arriveTime) + "\t\t" + strconv.Itoa(processQueue.ProcessQueue[i].serveTime))
- }
- return processQueue
- }
|