package experiment4;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class VGroup {
private VNode[] VList = new VNode[12];//创建顶点结点
public void createAdjacencyList(){
for (int i = 0;i<12;i++){
VList[i] = new VNode();
}
VList[0].course = "CO1";
VList[0].credit = 2;
//co1是co2\3\4\12的先行课,创建四个边结点
VList[0].next = new EdgeNode(1,new EdgeNode(2,new EdgeNode(3,new EdgeNode(11,null))));
VList[1].course = "CO2";
VList[1].credit = 3;
VList[1].next = new EdgeNode(2,null);
VList[2].course = "CO3";
VList[2].credit = 4;
VList[2].next = new EdgeNode(4,new EdgeNode(6,new EdgeNode(7,null)));
VList[3].course = "CO4";
VList[3].credit = 3;
VList[3].next = new EdgeNode(4,null);
VList[4].course = "CO5";
VList[4].credit = 2;
VList[4].next = new EdgeNode(6,null);
VList[5].course = "CO6";
VList[5].credit = 3;
VList[5].next = new EdgeNode(7,null);
VList[6].course = "CO7";
VList[6].credit = 4;
VList[6].next = new EdgeNode();
VList[7].course = "CO8";
VList[7].credit = 4;
VList[7].next = new EdgeNode();
VList[8].course = "CO9";
VList[8].credit = 7;
VList[8].next = new EdgeNode(9,new EdgeNode(10,new EdgeNode(11,null)));
VList[9].course = "CO10";
VList[9].credit = 5;
VList[9].next = new EdgeNode(11,null);
VList[10].course = "CO11";
VList[10].credit = 2;
VList[10].next = new EdgeNode(5,new EdgeNode());
VList[11].course = "CO12";
VList[11].credit = 3;
VList[11].next = new EdgeNode();
for (int i = 0;i<VList.length;i++){
VList[i].inDegree = findInDegree()[i];
}
}
public int[] findInDegree(){//寻找入度
int[] inDegree = new int[VList.length];
for (int i = 0;i<inDegree.length;i++){
for (EdgeNode edge = VList[i].next;edge!=null;edge = edge.nextEdge){
if (edge.next != -1 ){
inDegree[edge.next]++;
}
}
}
return inDegree;
}
public void refreshList(EdgeNode edge){//更新邻接表
while (edge!=null){
if (edge.next != -1){
VList[edge.next].inDegree--;
}
edge = edge.nextEdge;
}
}
public void sortAdvanced() throws IOException {//排课尽早
Scanner sc = new Scanner(System.in);
for (int i = 0;i<VList.length;i++){
VList[i].inDegree = findInDegree()[i];
}
int creditLimit;
int count = 0;
System.out.println("请输入学期数:");
int termNum = sc.nextInt();
System.out.println("请输入一学期的学分上限:");
creditLimit = sc.nextInt();
LinkedList<VNode> queue = new LinkedList<>();
for (int j = 0;j<termNum;j++){
int credit = 0;
for (VNode a:VList){
if (a.flag&&a.inDegree==0){//改课程还未访问过同时入度为0即先行课为或已经上完
credit = credit + a.credit;
if (credit <= creditLimit ){
queue.offer(a);
a.flag = false;
count++;
String s = a.course + "学分为:" + a.credit + " 在第"+(j+1)+"学期";
BufferedWriter bw=null;
bw=new BufferedWriter(new FileWriter("course.txt",true));
bw.append(s+"\r\n");
bw.flush();
bw.close();
System.out.println(s);
}
}
}
while (!queue.isEmpty()){
refreshList(queue.poll().next);
}
}
if (count<VList.length){//是否有遗漏的课程
for (VNode a:VList){
if (a.flag){
System.out.println("课程"+a.course+"不存在");
}
}
}
System.out.println();
}
public void sortAverage() throws IOException {//排课平均
Scanner sc = new Scanner(System.in);
for (int i = 0;i<VList.length;i++){
VList[i].inDegree = findInDegree()[i];
}
int creditLimit;
int count = 0;
System.out.println("请输入学期数:");
int termNum = sc.nextInt();
System.out.println("请输入一学期的学分上限:");
creditLimit = sc.nextInt();
Queue<VNode> queue = new LinkedList<>();
int courseAverage;
if (VList.length%termNum == 0){
courseAverage = VList.length/termNum;//平均一学期排课数
}else {
courseAverage = VList.length / termNum + 1;
}
for (int j = 0;j<termNum;j++){
int credit = 0;
for (VNode a:VList){
if (a.flag&&a.inDegree==0){
credit = credit + a.credit;
if (credit <= creditLimit && queue.size()<courseAverage){
queue.offer(a);
a.flag = false;
count++;
String s = a.course + "学分为:" + a.credit + " 在第"+(j+1)+"学期";
BufferedWriter bw=null;
bw=new BufferedWriter(new FileWriter("course.txt",true));
bw.append(s+"\r\n");
bw.flush();
bw.close();
System.out.println(s);
}
}
}
while (!queue.isEmpty()){
refreshList(queue.poll().next);
}
}
if (count < VList.length){
for (VNode a: VList){
if (a.flag){
System.out.println("课程"+a.course+"不存在");
}
}
}
System.out.println();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
experiment4.rar (6个子文件)
experiment4
VGroup.java 6KB
VGroupInput.java 6KB
EdgeNode.java 685B
TextInput.java 1KB
VNode.java 606B
Test.java 1KB
共 6 条
- 1
ZemelWhitesell
- 粉丝: 4
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0