作业要求
学生成绩管理
(1)class Student { int id; string name; int score; Student next; }
(2)用链表或系统List类实现。用数组实现也可(上限100条记录)?
(3)增:检查学号无重复
(4)删:若输入整数则按学号定位要删除的记录;
若输入字符串则按姓名(可缩写)定位要删除的记录
技巧:先按字符串读入到临时变量,检查其是否含有数字,便知是学号还是姓名。
(5)改:若输入整数则按学号定位要修改的记录;
若输入字符串则按姓名(可缩写)定位要修改的记录
(4)、(5)若因为姓名缩写导致多于一条记录,拒执行并报错。
修改时,逐一字段进行下列操作:
(a) 显示字段名和当前值
(b) 输入该字段的新值。如果只敲回车不输入新值,则该字段不修改,保持当前值不变。
技巧:先按字符串读入到临时变量,检查其长度是否为0,便知需不需要覆盖原值。
(6)查:输入格式为:字段名 关系运算符 查询值
例如,score > 80,查询成绩大于80分者。相当于一条SQL查询操作:
select * from Student where score > 80
如果按字符串类型字段(name)查询,查询值可以缩写。
(7)存:存盘
(8)取:还原
主类Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace classManager
{
class Program
{
static void Main(string[] args)
{
ClassList a = new ClassList();
String i = "";
Boolean t = true;
Console.WriteLine("-----------------------欢迎使用学生成绩管理系统1.0.0版-------------------->");
Console.WriteLine();
Console.WriteLine(" || || |||||||||| || || || ||");
Console.WriteLine(" || || || || || || ||");
Console.WriteLine(" |||||||||| ||||||||| || || || ||");
Console.WriteLine(" || || || || || || ||");
Console.WriteLine(" || || |||||||||| ||||||||||| ||||||||||| || ||");
Console.WriteLine();
Console.WriteLine("-------------------------------------------------------------------------->");
Console.WriteLine();
while (t)
{
Console.WriteLine("----------操作台----------->");
Console.WriteLine("\t1-------->添加学生");
Console.WriteLine("\t2-------->删除学生");
Console.WriteLine("\t3-------->编辑学生信息");
Console.WriteLine("\t4-------->查询学生信息");
Console.WriteLine("\t5-------->打印学生信息");
Console.WriteLine("\t6-------->存盘");
Console.WriteLine("\t7-------->打开");
Console.WriteLine("\t8-------->退出");
Console.WriteLine("--------------------------->");
i = Console.ReadLine();
while (!isNumber(i)){
Console.WriteLine("请输入正确的命令!");
i =Console.ReadLine();
}
switch (int.Parse(i))
{
case 1:
a.add();
break;
case 2:
a.delete();
break;
case 3:
a.edit();
break;
case 4:
a.find();
break;
case 5:
a.print();
break;
case 6:
a.Write();
break;
case 7:
a.Read();
break;
case 8:
t = false;
break;
}
}
}
protected static bool isNumber(string message)
{/*
* 判断传入的字符串是否是数字
* 如果是返回true 否则返回false
*/
try
{
int.Parse(message);
return true;
}
catch
{
return false;
}
}
}
}
学生类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace classManager
{
class Student
{ /**
*
* 学生类
* 学号 id
* 姓名 name
* 成绩 score
*/
private int id;
private String name;
int score;
//Student next;
public Student (){
}
public Student(int i,String n,int s)
{
id = i;
name = n;
score = s;
}
public int Id
{
get { return id; }
set { id = value; }
}
public String Name
{
get { return name; }
set { name = value; }
}
public int Score
{
get { return score; }
set { score = value; }
}
//internal student next
//{
// get { return next; }
// set { next = value; }
//}
public override string ToString()
{/*
* 重写ToString
*
*/
return "ID: " + id + " 姓名: " + name+" 成绩"+score;
}
}
}
链表操作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.IO;
namespace classManager
{
class ClassList
{/*
* 链表操作类
*
*/
List<Student> s = new List<Student>();
int count = 0;
public ClassList()
{
//s.Add(new Student(201502, "obama", 80));
//s.Add(new Student(201503, "bush", 90));
}
public void add()
{/*
* 添加学生
* 检查学号是否冲突
*/
String i = "";
String n = "";
String c = "";
Console.WriteLine("请输入学号:");
i = Console.ReadLine();
while (!isNumberic(i))
{
Console.WriteLine("请输入正确的学号(纯数字):");
i = Console.ReadLine();
}
Console.WriteLine("请输入姓名:");
n = Console.ReadLine();
while (n == "")
{
Console.WriteLine("请输入正确的姓名:");
n = Console.ReadLine();
}
Console.WriteLine("请输入成绩:");
c = Console.ReadLine();
while (!isNumberic(c))
{
Console.WriteLine("请输入正确的成绩(纯数字):");
c = Console.ReadLine();
}
while (true)
{
if (count == 0) //检查
{
s.Add(new Student(int.Parse(i), n, int.Parse(c)));
count++;
Console.WriteLine("------添加成功!");
break;
}
else if (tooId(int.Parse(i)) == -1) //检查id有没有重复和是不是最后一个学生信息
{
Console.WriteLine("------学号重复!请重新输入学号:");
i = Console.ReadLine();
}
else
{
s.Add(new Student(int.Parse(i), n, int.Parse(c)));
count++;
Console.WriteLine("------添加成功!");
break;
}
}
}
public void delete()
{/*
* 删除学生类
* 检查stu 若stu内含有数字则是按学号删除 否则按姓名删除
*/
int index = 0;
String stu = "";
Console.WriteLine("请输入要删除的学生信息");
stu = Console.ReadLine();
if (isNumberic(stu))
{/*
* 判断stu如果是数字则按学号定位
* 首先判断学号是否存在
*/
index = fID(stu);
if (index == -1)
{
Console.WriteLine("错误:学号未找到");
}
else
{
Console.WriteLine(s[index].ToString());
s.Remove(s[index]);
count--;
Console.WriteLine("删除成功");
}
}
else
{
index = fName(stu);
if (index == -1)
{
Console.WriteLine("错误:名字未找到");
}else if(index == -2)
{
Console.WriteLine("错误:匹配到多条记录!");
}
else
{
Console.WriteLine(s[index].ToString());
s.Remove(s[index]);
count--;
Console.WriteLine("删除成功");
}
}
}
public void edit()
{/*
* 修改学生数据
* 若输入整数则按学号定位要修改的记录;
* 若输入字符串则按姓名(可缩写)定位要修改的记录
*/
String stu = "";
int index = 0;
Console.WriteLine("请输入要修改的学生信息");
stu = Console.ReadLine();
String id = "0";
String name = "0";
String score = "0";
if (isNumberic(stu))
{
index = fID(stu);
if (index == -1)
{
Console.WriteLine("错误:学号未找到");
}
else
{
Console.WriteLine("您要修改的数据" + s[index].ToString());
Console.WriteLine("请输入学号:");
id = Console.ReadLine();
if (id == "")
id = Convert.ToString(s[index].Id);
Console.WriteLine("请输入姓名:");
name = Console.ReadLine();
if (name == "")
name = s[index].Name;
Console.WriteLine("请输入成绩:");
score = Console.ReadLine();
if (score == "")
score = Convert.ToString(s[index].Score);
//Console.WriteLine(id+" "+name+" "+score);
while (true)
{
if (tooId(int.Parse(id)) == -1 && id != Convert.ToString(s[index].Id))
{
Console.WriteLine("学号重复,请重新输入学号:");
id = Console.ReadLine();
if (id == "")
id = Convert.ToString(s[index].Id);
}
else
{
s[index].Id = int.Parse(id);
s[index].Name = name;
s[index].Score = int.Parse(score);
Console.WriteLine("修改成功");
break;
}
}
}
}
else
{
index = fName(stu);
if (index == -1)
{
Console.WriteLine("错误:名字未找到");
}
else if (index == -2)
{
Console.WriteLine("错误:匹配到多条记录!");
}
else
{
Console.WriteLine("您要修改的数据" + s[index].ToString());
Console.WriteLine("请输入学号:");
id = Console.ReadLine();
if (id == "")
id = Convert.ToString(s[index].Id);
Console.WriteLine("请输入姓名:");
name = Console.ReadLine();
if (name == "")
name = s[index].Name;
Console.WriteLine("请输入成绩:");
score = Console.ReadLine();
if (score == "")
score = Convert.ToString(s[index].Score);
while (true)
{
if (tooId(int.Parse(id)) == -1 && id != Convert.ToString(s[index].Id))
{
Console.WriteLine("学号重复,请重新输入学号:");
id = Console.ReadLine();
if (id == "")
id = Convert.ToString(s[index].Id);
}
else
{
s[index].Id = int.Parse(id);
s[index].Name = name;
s[index].Score = int.Parse(score);
Console.WriteLine("修改成功");
break;
}
}
}
}
}
public void find()
{/*
* 查询类
* 查:输入格式为:字段名 关系运算符 查询值
* 例如,score > 80,查询成绩大于80分者。相当于一条SQL查询操作:select * from Student where score > 80
* 如果按字符串类型字段(name)查询,查询值可以缩写。
*/
String sql = "";
Console.WriteLine("请输查询语句---->例如score > 80,查询成绩大于80分者");
sql = Console.ReadLine();
if (sql == "")
{
sql = " 1 ";
}
String where = getWhereSql(sql);
String ro = getROSql(sql);
String value = getValueSql(sql);
switch (ro){
case ">":
sqla(where,value);
break;
case "<":
sqlb(where, value);
break;
case"=":
sqlc(where, value);
break;
case">=":
sqle(where, value);
break;
case"<=":
sqld(where, value);
break;
default:
Console.WriteLine("查询语句不正确!例如,score > 80,查询成绩大于80分者");
break;
}
}
public void print()
{/*
* 打印所有的学生
*/
if (count == 0)
Console.WriteLine("没有数据!");
else
for (int i = 0; i < count;i++ )
Console.WriteLine(s[i].ToString());
}
public void print(int i)
{/*
*打印第i个学生信息
*/
if (i<count)
Console.WriteLine(s[i-1].ToString());
else
Console.WriteLine("没有数据!");
}
protected bool isNumberic(string message)
{/*
* 判断传入的字符串是否是数字
* 如果是返回true 否则返回false
*/
try
{
int.Parse(message);
return true;
}
catch
{
return false;
}
}
public int tooId(int id)
{/*
* 检查学号是否重复
*
*/
for (int i = 0; i <count; i++)
{
if (s[i].Id == id)
return -1;
}
return id;
}
public int fID(String id)
{/*
* 检查学号是否存在
*
*/
int rid = -1;
Boolean a = false;
for (int i = 0; i < count; i++)
{
if (Convert.ToString(s[i].Id) == id)
{
a = true;
rid = i;
}
}
if (a)
return rid;
else
return -1;
}
public int fName(String name)
{/*
* 姓名缩写匹配
*/
int number = 0;
int rindex = -1;
for (int j = 0;j<count;j++)
{
for (int m=0,n=0; m < s[j].Name.Length; m++)
{
if (s[j].Name[m] == name[n])
n++;
if (n == name.Length)
{
rindex = j;
number++;
break;
}
}
}
if (number == 1)
return rindex;
else if (number > 1)
return -2;
else
return -1;
}
public String getWhereSql(String sql)
{/*
* 返回sql中的条件
*/
String[] rsql = sql.Split(' '); ;
return rsql[0];
}
public String getROSql(String sql)
{/*
* 返回sql中的关系运算符
*/
String[] rsql = sql.Split(' '); ;
return rsql[1];
}
public string getValueSql(string sql)
{/*
* 返回sql中的查询值
*/
String[] rsql = sql.Split(' '); ;
return rsql[2];
}
/*
*
* 查询方法
*
*/
public void sqla(String In,String value)
{/*
* >
*/
switch(In){
case "id":
Console.WriteLine("错误:id请用等于符号(=)");
break;
case "name":
Console.WriteLine("错误:名字请用等于符号(=)");
break;
case "score":
Console.WriteLine("查询结果-->");
for (int i = 0; i < count; i++)
{
if (s[i].Score > int.Parse(value))
Console.WriteLine(s[i].ToString());
}
break;
default:
Console.WriteLine("查询语句不正确!例如,score > 80,查询成绩大于80分者");
break;
}
}
public void sqlb(String In, String value)
{/*
* <
*/
switch (In)
{
case "id":
Console.WriteLine("错误:id请用等于符号(=)");
break;
case "name":
Console.WriteLine("错误:名字请用等于符号(=)");
break;
case "score":
Console.WriteLine("查询结果-->");
for (int i = 0; i < count; i++)
{
if (s[i].Score < int.Parse(value))
Console.WriteLine(s[i].ToString());
}
break;
default:
Console.WriteLine("查询语句不正确!例如,score > 80,查询成绩大于80分者");
break;
}
}
public void sqlc(String In, String value)
{/*
* =
*/
switch (In)
{
case "id":
Console.WriteLine("查询结果-->");
for (int i = 0; i < count; i++)
{
if (s[i].Id == int.Parse(value))
Console.WriteLine(s[i].ToString());
}
break;
case "name":
Console.WriteLine("查询结果-->");
for (int i = 0; i < count; i++)
{
if (s[i].Name == value)
Console.WriteLine(s[i].ToString());
}
break;
case "score":
Console.WriteLine("查询结果-->");
for (int i = 0; i < count; i++)
{
if (s[i].Score == int.Parse(value))
Console.WriteLine(s[i].ToString());
}
break;
default:
Console.WriteLine("查询语句不正确!例如,score > 80,查询成绩大于80分者");
break;
}
}
public void sqld(String In, String value)
{/*
* <=
*/
switch (In)
{
case "id":
Console.WriteLine("错误:id请用等于符号(=)");
break;
case "name":
Console.WriteLine("错误:名字请用等于符号(=)");
break;
case "score":
Console.WriteLine("查询结果-->");
for (int i = 0; i < count; i++)
{
if (s[i].Score <= int.Parse(value))
Console.WriteLine(s[i].ToString());
}
break;
default:
Console.WriteLine("查询语句不正确!例如,score > 80,查询成绩大于80分者");
break;
}
}
public void sqle(String In, String value)
{/*
* <=
*/
switch (In)
{
case "id":
Console.WriteLine("错误:id请用等于符号(=)");
break;
case "name":
Console.WriteLine("错误:名字请用等于符号(=)");
break;
case "score":
Console.WriteLine("查询结果-->");
for (int i = 0; i < count; i++)
{
if (s[i].Score >= int.Parse(value))
Console.WriteLine(s[i].ToString());
}
break;
default:
Console.WriteLine("查询语句不正确!例如,score > 80,查询成绩大于80分者");
break;
}
}
/*
*
* 查询方法
*
*/
public void Write()
{/*
* 文件存盘
*
*/
Console.WriteLine("请输入文件存储路径-->");
Console.WriteLine(@"例如:默认路径D:\\classScore.txt");
string fPath = "";
string output = "";
FileStream file = null;
StreamWriter sw = null;
fPath = Console.ReadLine();
if (fPath == "")
{
fPath = "D:\\classScore.txt";
}
file = new FileStream(fPath, FileMode.Create);
sw = new StreamWriter(file);
for (int i = 0; i < count; i++)
{
output = s[i].Id +" "+s[i].Name +" "+s[i].Score + "\r\n";
sw.Write(output);
sw.Flush();
}
Console.WriteLine("保存完成! 共" + s.Count + "条信息");
sw.Close();
file.Close();
}
public void Read()
{/*
* 文件读取
*
*/
Console.WriteLine("请输入文件读取路径-->");
Console.WriteLine(@"例如:默认路径D:\\classScore.txt");
string fPath = "";
string input = null;
StreamReader sr = null;
fPath = Console.ReadLine();
if (fPath == "")
{
fPath = "D:\\classScore.txt";
}
sr = new StreamReader(fPath, Encoding.UTF8);
while ((input = sr.ReadLine()) != null)
{
string[] arr = input.Split(' ');//字符串分离
s.Add(new Student(Int32.Parse(arr[0]), arr[1], Int32.Parse(arr[2])));
count++;
}
Console.WriteLine("读取完成! 共" + s.Count + "条信息");
sr.Close();
}
}
}
注:本文内容来自互联网,旨在为开发者提供分享、交流的平台。如有涉及文章版权等事宜,请你联系站长进行处理。