processQueue.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. type ProcessQueue struct {
  7. ProcessQueue []*Process
  8. typeOfAl int
  9. time int
  10. }
  11. func (s ProcessQueue) Len() int {
  12. return len(s.ProcessQueue)
  13. }
  14. func (s ProcessQueue) Swap(i, j int) {
  15. s.ProcessQueue[i], s.ProcessQueue[j] = s.ProcessQueue[j], s.ProcessQueue[i]
  16. }
  17. // Less 实现接口,用于sort排序
  18. func (s ProcessQueue) Less(i, j int) bool {
  19. switch s.typeOfAl {
  20. case 1:
  21. return s.ProcessQueue[i].arriveTime < s.ProcessQueue[j].arriveTime
  22. case 2:
  23. return s.ProcessQueue[i].serveTime < s.ProcessQueue[j].serveTime
  24. case 3:
  25. 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
  26. case 4:
  27. return s.ProcessQueue[i].priority > s.ProcessQueue[j].priority
  28. case 5:
  29. return s.ProcessQueue[i].beginTime < s.ProcessQueue[j].beginTime
  30. }
  31. return s.ProcessQueue[i].arriveTime < s.ProcessQueue[j].arriveTime
  32. }
  33. func initProcessQueue() ProcessQueue {
  34. var processQueue ProcessQueue
  35. var numOfProcess int
  36. var name string
  37. var arriveTime int
  38. var serveTime int
  39. fmt.Print("[+]请输入进程数量:")
  40. fmt.Scan(&numOfProcess)
  41. for i := 0; i < numOfProcess; i++ {
  42. fmt.Print("[+]请输入第" + strconv.Itoa(i) + "个进程的信息(到达时间 服务时间 进程名称): ")
  43. fmt.Scan(&arriveTime, &serveTime, &name)
  44. processQueue.ProcessQueue = append(processQueue.ProcessQueue, newProcess(name, arriveTime, serveTime))
  45. }
  46. fmt.Println("[-]进程ID\t进程名称\t到达时间\t服务时间")
  47. for i := 0; i < numOfProcess; i++ {
  48. 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))
  49. }
  50. return processQueue
  51. }