HIT软件构造Lab1

1.1Magic Squares

(重点)检验正确性:

读入文件
1、 创建FileReader、BufferReader、StringBuilder对象
2、 初始化line
逐行将字符串转换为整型矩阵存储
1、 将非空readline的字符串分割
2、去除头尾空格后转换为数字存储到二维数组中
3、存储时判断该数字是否出现过
      (出现过则报错)
4、判断行列长度是否相等
计算两条斜线的和并比较

1、 分别得出主对角线和次对角线的和
2、比较
若相等则记录,作为基准值
若不相等则报错
计算每条纵线和横线和和并比较
1、分别计算第i条横线和纵线的和
2、与基准值比较
3、不相等则报错

1.2Turtle Graphics

这个任务要求我们首先克隆一个已有的程序,然后使用turtle模块根据指定要求进行绘图。为了简化编程,我们需要运用几何知识设计一些函数。最终,可以通过发挥创造力来制作个人艺术作品。首先,需要分析turtle模块的组成,了解其类和成员。

(重点)阅读并理解所给代码并学会如何进行使用

1.3Social Network

该任务要求设计一张社交网络图,基于连接人与人,并且能计算任意两人之间的联系情况。网络图基于两个类,分别是FriendshipGraph类和Person类。

package P3;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.PatternSyntaxException;

public class FriendshipGraph {
	Set<Person> set = new HashSet<Person>();

	public FriendshipGraph() {

	}

	/**
	 * 添加节点
	 * 
	 * @param p 人
	 * @return 是否成功
	 */
	public boolean addVertex(Person p) {
		if (set.contains(p))
			throw new IllegalArgumentException("已有名字为" + p.getName() + "的人,请更换名字后再试");
		set.add(p);
		return true;
	}

	/**
	 * 添加有向边
	 * 
	 * @param s 开始人
	 * @param e 结束人
	 * @return 是否成功
	 */
	public boolean addEdge(Person s, Person e) {
		s.addFriendChild(e);
		e.addFriendFather(s);
		return true;
	}

	/**
	 * 获取距离
	 * 
	 * @param s 人1
	 * @param e 人2
	 * @return 距离
	 */
	public int getDistance(Person s, Person e) {
		if (s == null || e == null)
			throw new IllegalArgumentException("不能为空");
		if (s.equals(e))
			return 0;
		int count = 1;

		LinkedList<Person> queue = new LinkedList<Person>();
		Set<Person> hasVisit = new HashSet<Person>();
		hasVisit.add(s);
		Person p = s;
		Person[] childs;
		int len;
		queue.addLast(p);
		queue.addLast(null);
		while (!queue.isEmpty()) {
			p = queue.pollFirst();
			if (p == null) {
				count++;
				continue;
			}
			hasVisit.add(p);
			childs = p.getFriendChild().toArray(new Person[0]);
			len = childs.length;
			for (int i = 0; i < len; i++) {
				if (childs[i].equals(e))
					return count;
				if (hasVisit.contains(childs[i]))
					continue;
				queue.addLast(childs[i]);
			}
			queue.addLast(null);
		}
		return -1;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		HashMap<String, Person> map = new HashMap<String, Person>();
		FriendshipGraph graph = new FriendshipGraph();
		Scanner sn = new Scanner(System.in);
		System.out.println("欢迎来到友情图!");
		System.out.println("使用以下命令来操作:");
		System.out.println("new name //创建一个新人物");
		System.out.println("add name //把一个人物加入图");
		System.out.println("dis name1 name2 //计算两个人物的距离");
		System.out.println("edg name1 name2 //添加name1到name2的友情线");
		System.out.println("info name1 //查看name1人的信息");
		System.out.println("exit //退出");
		while (true) {
			System.out.print(">>");
			String input = sn.nextLine();
			if(input.equals("exit")) {
				sn.close();
				break;
			}
			String[] arr;
			try {
				arr = input.split(" ");
			} catch (PatternSyntaxException e) {
				System.out.println("命令错误!");
				continue;
			}
			//System.out.println(Arrays.toString(arr));
			if (arr.length == 0)
				continue;
			try {
				try {
					if (arr[0].equals("new")) {
						if (arr.length > 2) {
							System.out.println("命令错误!");
							continue;
						}
						map.put(arr[1], new Person(arr[1]));

						System.out.println(arr[1]+" 已创建");
					}else if(arr[0].equals("add")) {
						if (arr.length > 2) {
							System.out.println("命令错误!");
							continue;
						}
						Person p = map.get(arr[1]);
						if(p==null) {
							System.out.println("没有这个人!请重新输入");
							continue;
						}
						graph.addVertex(p);
						System.out.println(p.getName()+" 已添加");
						
					}else if(arr[0].equals("info")) {
						if (arr.length > 2) {
							System.out.println("命令错误!");
							continue;
						}
						Person p = map.get(arr[1]);
						if(p==null) {
							System.out.println("没有这个人!请重新输入");
							continue;
						}
						System.out.println(p.toStringAll());
						
					}else if(arr[0].equals("edg")) {
						if (arr.length > 3) {
							System.out.println("命令错误!");
							continue;
						}
						Person s = map.get(arr[1]), e = map.get(arr[2]);
						if(s==null||e==null) {
							System.out.println("没有这个人!请重新输入");
							continue;
						}
						graph.addEdge(s, e);
						System.out.println(s.getName()+"->"+e.getName()+" 已添加");
					}else if(arr[0].equals("dis")) {
						if (arr.length > 3) {
							System.out.println("命令错误!");
							continue;
						}
						Person s = map.get(arr[1]), e = map.get(arr[2]);
						if(s==null||e==null) {
							System.out.println("没有这个人!请重新输入");
							continue;
						}
						int i = graph.getDistance(s, e);
						if(i>=0) {
							System.out.println(s.getName()+"->"+e.getName()+"的距离是:"+i);
						}else {
							System.out.println(s.getName()+"->"+e.getName()+"没有路径!");
						}
						
					}else {
						System.out.println("命令错误!");
						continue;
					}
				} catch (IndexOutOfBoundsException e) {
					System.out.println("命令错误!");
					continue;
				}
			} catch (IllegalArgumentException e) {
					System.out.println(e.getMessage());
					continue;
			}

		}

	}

}

(重点)设计以及集合论与图论相关知识的运用

实验感受:

1、初次接触java,对这门语言不熟悉,对于这次实验花费了较长的时间来设计

2、第一次系统使用github,出现了很多问题,通过网上查阅资料并没有很好的解决

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值