学生成绩管理系统【顺序表的应用】

练习:
编写学生成绩管理系统。此系统具有查询、修改、删除等方法。此题采用顺序存储结构。
成绩表

应用知识点:
应用到了线性表的顺序存储结构:
所谓顺序表就是顺序存储的线性表。顺序存储是用一组练习的存储单元一次存放线性表中各个元素数据的存储结构。

实现步骤:
首先创建线性表的顺序存储结构,因为学生成绩管理系统需要使用到。

  1. 创建线性表顺序存储结构的接口
```java
public interface IList {
	//置空
	public void clear();
	//判段线性表是否为空
	public boolean isElmpty();
	//求线性表的数据元素个数
	public int length();
	//获取
	public Object get(int i) throws Exception;
	//查找
	public int indexOf(Object x);
	//插入
	public void insert(int i,Object x) throws Exception;
	//删除
	public void remove(int i)throws Exception;
	
}
  1. 线性表的顺序存储结构
//线性表的顺序存储结构
public class Sqlist implements IList {
	private Object[] listElem;
	private int curlen;
	
	public Sqlist(int maxSize){
		curlen = 0;
		listElem = new Object[maxSize];
	}
	public void clear() {
		curlen = 0;
	}
	public boolean isElmpty() {
		return curlen == 0?true:false;
	}
	public int length() {
		return curlen;
	}
	public Object get(int i) throws Exception {
		if(i < 0||i>curlen-1) {
			throw new Exception("查找的位置不合法!");
		}else {
			return listElem[i];
		}
	}
	public int indexOf(Object x) {
		int j = 0;
		if(j<curlen&&!listElem[j].equals(x)) {
			j++;
		}
		if(j<curlen) {
			return j;
		}else {
			return -1;
		}
	}
	public void insert(int i,Object x) throws Exception {
		if(i>=listElem.length) {
			throw new Exception("该顺序表已满!");
		}
		if(i < 0||i>curlen) {
			throw new Exception("插入的位置不合法!");
		}else {
			for(int j = curlen;j>i;j--) {
				listElem[j] = listElem[j-1];	//后移
			}
			listElem[i] = x;
			curlen++;		//长度加1
		}
	}
	public void remove(int i) throws Exception {
		if(i < 0||i>curlen-1) {
			throw new Exception("删除的位置不合法!");
		}
		for(int j = i;j<curlen-1;j++) {			
			listElem[j] = listElem[j+1];		//前移
		}
		curlen--;			//长度减1
	}
	public void dispay() {
		for(int i = 0;i<curlen;i++) {
			System.out.println(listElem[i] + " ");
		}
		System.out.println();
	}
}

下面具体创建学生成绩管理系统

  1. 创建学生结点类(每一学生应包含的信息数据)
import java.util.Scanner;

/**
 * 学生成绩管理系统
 *学生结点类
 * @懒惰的小黑
 *
 */
public class StudentNode {
	private int number;
	private String name;
	private String sex;
	private double english;
	private double math;
	
	public StudentNode() {
		this(0,null,null,0.0,0.0);
	}
	public StudentNode(int number,String name,String sex,double english,double math) {
		this.number = number;
		this.name  = name;
		this.sex = sex;
		this.english = english;
		this.math = math;
	}
	//用输入的信息构造结点
	public StudentNode(Scanner sc) {
		this(sc.nextInt(),sc.next(),sc.next(),sc.nextDouble(),sc.nextDouble());
	}
	public int getNumber() {
		return number;
	}
	public String getName() {
		return name;
	}
	public String getSex() {
		return sex;
	}
	public double getEnglish() {
		return english;
	}
	public double getMath() {
		return math;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public void setEnglish(double english) {
		this.english = english;
	}
	public void setMath(double math) {
		this.math = math;
	}
	public String toString() {
		return "学号:" + getNumber() + " 姓名:" + getName() + " 性别:" + getSex() + 
				"  英语成绩:" + getEnglish() + " 数学成绩:" + getMath();
	}
}

  1. 创建学生成绩管理系统类:包含构造、插入、删除等方法
import java.util.Scanner;
/**
 * 采用顺序存储结构实现学生成绩管理系统
 * @懒惰的小黑
 *
 */
public class StudentManageSystem extends Sqlist {
	//构造顺序表
	public StudentManageSystem(int maxSize,int n)throws Exception {
		super(maxSize);
		Scanner sc = new Scanner(System.in);
		for(int i = 1;i<=n;i++) {
			StudentNode node = new StudentNode(sc);		//创建有n个数据元素的顺序表,即每一个的学生信息
			if(node!=null) {
				insert(node);						//插入顺序表中
			}
		}
	}
	//取出指定学号的学生信息
	public StudentNode get(int number) throws Exception {
		for(int i=0;i<length();i++) {		//遍历顺序表
			StudentNode node = (StudentNode) super.get(i);	//获取顺序表的每一个结点位置数据
			if(node.getNumber() == number) {				//学号匹配,则返回该结点
				return node;	
			}
		}
		throw new Exception("获取的学号" + number +"不存在!");	
	}
	//重写insert方法,在顺序表表尾插入一个学生信息
	public void insert(StudentNode node) throws Exception {
		super.insert(this.length(), node);
	}
	//重写remove方法
	public void remove(int number) throws Exception {
		for(int i = 0; i<length();i++) {
			StudentNode node = (StudentNode) super.get(i);
			if(node.getNumber() == number) {
				super.remove(i);			//调用父类方法取出第i个位置的学生信息
				return;						//结束该循环
			}
		}
		throw new Exception("删除的学号" + number +"不存在!");
	}
	//重写父类的dispay,输出所有的数据元素
	public void dispay() {
		System.out.println("------学生成绩表------");
		for(int i=0;i<length();i++) {
			try {
				StudentNode node = (StudentNode) super.get(i);
				dispayNode(node);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}
	//输出一个数据元素的信息
	public void dispayNode(StudentNode node) {
		System.out.println(node.toString());
	}
	
}

  1. 测试学生成绩管理系统
import java.util.Scanner;

/**
 * 学生成绩系统测试
 * @懒惰的小黑
 *
 */
public class SystemManageSystemTest {
	public static void made() {
		System.out.println("-----学生成绩管理系统-----");
		System.out.println("操作选项菜单:"+ "\n1.插入学生信息" + "\n2.查找学生信息" +"\n3.删除学生信息"
		+ "\n4.显示学生成绩表" + "\n0.退出");
	}
	public static void main(String[] args) throws Exception {
		int maxSize = 100;
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入学生数量:");
		int n = sc.nextInt();
		System.out.println("按学号,姓名,性别,英语成绩,数学成绩输入学生信息:");
		StudentManageSystem s = new StudentManageSystem(maxSize,n);
		s.dispay();
		
		made();
		int num;
		do {
			System.out.println("请输入操作代码(0—退出):");
			num = sc.nextInt();
			switch(num) {
			case 1:
				System.out.println("按学号,姓名,性别,英语成绩,数学成绩输入学生信息:");
				s.insert(new StudentNode(sc));
				break;
			case 2:
				System.out.println("输入需要查询成绩的学号:");
				System.out.println(s.get(sc.nextInt()).toString());
				break;
			case 3:
				System.out.println("输入需要删除成绩的学号:");
				s.remove(sc.nextInt());
				break;
			case 4:
				s.dispay();
				break;
			case 0:
				System.out.println("over!");
				break;
			}
		}while(num!=0);
		sc.close();
	}
}

  1. 测试结果
    测试1

测试2

总结:
掌握顺序存储结构的使用。
注意do while循环结构和switch的结合使用,Scanner中的close方法是关闭字节流。

  • 8
    点赞
  • 81
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值