Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Student_Teacher
{
struct StructOfTeacher
{
public string Name;
public string Password;
public string Course;
}
class Program
{
static void Main(string[] args)
{
StructOfTeacher TheLoginTeacher = new StructOfTeacher();
WelComeTeacher(ref TheLoginTeacher);
if (TheLoginTeacher.Name!="")
ShowTeacherInfo(TheLoginTeacher);
Console.ReadKey();
}
//教师登录模块1--用途:询问“注册教师信息还是教师登录”,并调用相应函数
static void WelComeTeacher(ref StructOfTeacher Teacher)
{
Console.WriteLine("欢迎使用学生管理系统,为确保你是教师身份,请输入密钥:");
string Key = Console.ReadLine().Trim();
if (Key == "123")
{
Console.WriteLine("老师好,现在你可以开始使用学生管理系统了!");
Console.WriteLine("1.没有账号?请先注册教师账号!");
Console.WriteLine("2.我有账号?请先使用账号登录!");
Console.WriteLine("3.时机未成熟,放弃使用本系统!");
Console.WriteLine("<------------选择操作选项------------>");
string Type=Console.ReadLine().Trim();
Console.WriteLine("<------------选择操作完毕------------>");
Console.WriteLine();
switch (Type)
{
case "1": { RegisterTeacher(); Teacher=LoginTeacher(); } break;
case "2": Teacher=LoginTeacher(); break;
default: Teacher.Name=""; break;
}
}
else
{
Console.WriteLine("真不好意思,你不是教师,本系统不能为你服务!");
Teacher.Name = "";
}
}
//教师登录模块2--用途:注册教师信息,并追加至教师文件中
static void RegisterTeacher()
{
Console.WriteLine("<------------注册教师信息------------>");
bool ReDoIt = true;
while (ReDoIt)
{
bool HaveName = false;
Console.WriteLine("请在以下一行填写你的信息,格式为‘姓名-密码-课程’:");
string TeacherInfo = Console.ReadLine().Trim();
StrWillBeOk1(ref TeacherInfo);//验证输入的字符串是否有效
string[] theTeacherInfo = TeacherInfo.Split('-');
if (File.Exists(@"..\..\Teacher.txt"))
{
string[] TeachersHave = File.ReadAllLines(@"..\..\Teacher.txt");
foreach (string s in TeachersHave)
{
string[] oneTeacherInfo = s.Split('-');
if (oneTeacherInfo[0] == theTeacherInfo[0])
{
Console.WriteLine("用户名已经存在!");
HaveName = true;
break;
}
}
if (!HaveName)
{
AppendTextToFile(@"..\..\Teacher.txt", TeacherInfo);
ReDoIt = false;
}
else
{
ReDoIt = true;
}
}
else
{
AppendTextToFile(@"..\..\Teacher.txt", TeacherInfo);
ReDoIt = false;
}
}
Console.WriteLine("注册成功!");
Console.WriteLine("<------------注册教师完毕------------>");
Console.WriteLine();
}
//教师登录模块3--用途:教师登录界面
static StructOfTeacher LoginTeacher()
{
bool HaveYourName = false;//用于验证姓名是否存在
bool YourPasswordRight = false;//验证密码是否正确
bool toBecontinue = true;//如果登录不成功,则需要重新输入信息以便登录
StructOfTeacher TheLoginTeacher = new StructOfTeacher();
Console.WriteLine("<------------教师登录验证------------>");
while (toBecontinue)
{
Console.WriteLine("请在以下一行输入你的信息,格式为‘姓名-密码’:");
string yourInfo = Console.ReadLine();
StrWillBeOk2(ref yourInfo);//验证输入的字符串是否有效
string[] theTeacherInfo = yourInfo.Split('-');
if (File.Exists(@"..\..\Teacher.txt"))
{
string[] TeachersInfo = File.ReadAllLines(@"..\..\Teacher.txt");
foreach (string s in TeachersInfo)
{
string[] oneTeacherInfo = s.Split('-');
if (oneTeacherInfo[0] == theTeacherInfo[0])//验证姓名是否存在
{
HaveYourName = true;
if (oneTeacherInfo[1] == theTeacherInfo[1])//验证密码是否正确
{
YourPasswordRight = true;
toBecontinue = false;
//保存成功登陆的教师的所有信息
TheLoginTeacher.Name = oneTeacherInfo[0];
TheLoginTeacher.Password = oneTeacherInfo[1];
TheLoginTeacher.Course = oneTeacherInfo[2];
}
break;
}
}
if (!HaveYourName)
{
Console.WriteLine("姓名不存在!");
toBecontinue = true;
}
else
if (!YourPasswordRight)
{
Console.WriteLine("密码不正确!");
toBecontinue = true;
}
}
}
Console.WriteLine("登录成功!");
Console.WriteLine("<------------登录验证完毕------------>");
Console.WriteLine();
return TheLoginTeacher;
}
//教师登录模块4--用途:显示教师的详细信息
static void ShowTeacherInfo(StructOfTeacher Teacher)
{
Console.WriteLine(" ----------------");
Console.WriteLine("|姓名:{0,-10}|", Teacher.Name);
Console.WriteLine("|密码:{0,-10}|", Teacher.Password);
Console.WriteLine("|课程:{0,-10}|", Teacher.Course);
Console.WriteLine(" ----------------");
}
//文件操作函数1--用途:追加若干字符串到现有的文件当中。如果文件不存在,则自动创建文件,然后追加。
static void AppendTextToFile(string path,params string[] strWillAdd)
{
try
{
using (StreamWriter sw = File.AppendText(path))
{
foreach (string s in strWillAdd)
{
sw.WriteLine(s);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
//文件操作函数2--用途:创建一个新文件,在其中写入若干行字符串,然后关闭该文件。如果目标文件已存在,则覆盖该文件以及文件以内的所有内容。
static void CreateTextToFile(string path, params string[] strWillAdd)
{
try
{
using (StreamWriter sw = File.CreateText(path))
{
foreach (string s in strWillAdd)
{
sw.WriteLine(s);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
//文件操作函数3--用途:创建一个新文件,在其中写入指定的字符串数组,然后关闭该文件。如果目标文件已存在,则覆盖该文件。
static void WriteAllLinesToFile(string path, string[] strArrayWillAdd)
{
try
{
File.WriteAllLines(path, strArrayWillAdd);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
//输入格式验证1--用途:验证输入的‘姓名-密码-课程’是否符合格式,不符合,则提示重新输入,并覆盖原来的值
static void StrWillBeOk1(ref String str)
{
bool toBeContinue = false;
string[] strSplit = new string[3];
while (!toBeContinue)
{
strSplit = str.Split('-');
try
{
bool strIsOk = true;
if (strSplit.Length != 3)
strIsOk = false;
foreach (string s in strSplit)
if (s == "")
{
strIsOk = false;
break;
}
if (!strIsOk)
throw new ArgumentException();
else
toBeContinue = true;
}
catch (ArgumentException e)
{
Console.WriteLine("格式不正确,请严格按照‘姓名-密码-课程’输入!");
str = Console.ReadLine();
}
}
}
//输入格式验证2--用途:验证输入的‘姓名-密码’是否符合格式,不符合,则提示重新输入,并覆盖原来的值
static void StrWillBeOk2(ref String str)
{
bool toBeContinue = false;
string[] strSplit = new string[2];
while (!toBeContinue)
{
strSplit = str.Split('-');
try
{
bool strIsOk = true;
if (strSplit.Length != 2)
strIsOk = false;
foreach (string s in strSplit)
if (s == "")
{
strIsOk = false;
break;
}
if (!strIsOk)
throw new ArgumentException();
else
toBeContinue = true;
}
catch (ArgumentException e)
{
Console.WriteLine("格式不正确,请严格按照‘姓名-密码’输入!");
str = Console.ReadLine();
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Student_Teacher
{
struct StructOfTeacher
{
public string Name;
public string Password;
public string Course;
}
class Program
{
static void Main(string[] args)
{
StructOfTeacher TheLoginTeacher = new StructOfTeacher();
WelComeTeacher(ref TheLoginTeacher);
if (TheLoginTeacher.Name!="")
ShowTeacherInfo(TheLoginTeacher);
Console.ReadKey();
}
//教师登录模块1--用途:询问“注册教师信息还是教师登录”,并调用相应函数
static void WelComeTeacher(ref StructOfTeacher Teacher)
{
Console.WriteLine("欢迎使用学生管理系统,为确保你是教师身份,请输入密钥:");
string Key = Console.ReadLine().Trim();
if (Key == "123")
{
Console.WriteLine("老师好,现在你可以开始使用学生管理系统了!");
Console.WriteLine("1.没有账号?请先注册教师账号!");
Console.WriteLine("2.我有账号?请先使用账号登录!");
Console.WriteLine("3.时机未成熟,放弃使用本系统!");
Console.WriteLine("<------------选择操作选项------------>");
string Type=Console.ReadLine().Trim();
Console.WriteLine("<------------选择操作完毕------------>");
Console.WriteLine();
switch (Type)
{
case "1": { RegisterTeacher(); Teacher=LoginTeacher(); } break;
case "2": Teacher=LoginTeacher(); break;
default: Teacher.Name=""; break;
}
}
else
{
Console.WriteLine("真不好意思,你不是教师,本系统不能为你服务!");
Teacher.Name = "";
}
}
//教师登录模块2--用途:注册教师信息,并追加至教师文件中
static void RegisterTeacher()
{
Console.WriteLine("<------------注册教师信息------------>");
bool ReDoIt = true;
while (ReDoIt)
{
bool HaveName = false;
Console.WriteLine("请在以下一行填写你的信息,格式为‘姓名-密码-课程’:");
string TeacherInfo = Console.ReadLine().Trim();
StrWillBeOk1(ref TeacherInfo);//验证输入的字符串是否有效
string[] theTeacherInfo = TeacherInfo.Split('-');
if (File.Exists(@"..\..\Teacher.txt"))
{
string[] TeachersHave = File.ReadAllLines(@"..\..\Teacher.txt");
foreach (string s in TeachersHave)
{
string[] oneTeacherInfo = s.Split('-');
if (oneTeacherInfo[0] == theTeacherInfo[0])
{
Console.WriteLine("用户名已经存在!");
HaveName = true;
break;
}
}
if (!HaveName)
{
AppendTextToFile(@"..\..\Teacher.txt", TeacherInfo);
ReDoIt = false;
}
else
{
ReDoIt = true;
}
}
else
{
AppendTextToFile(@"..\..\Teacher.txt", TeacherInfo);
ReDoIt = false;
}
}
Console.WriteLine("注册成功!");
Console.WriteLine("<------------注册教师完毕------------>");
Console.WriteLine();
}
//教师登录模块3--用途:教师登录界面
static StructOfTeacher LoginTeacher()
{
bool HaveYourName = false;//用于验证姓名是否存在
bool YourPasswordRight = false;//验证密码是否正确
bool toBecontinue = true;//如果登录不成功,则需要重新输入信息以便登录
StructOfTeacher TheLoginTeacher = new StructOfTeacher();
Console.WriteLine("<------------教师登录验证------------>");
while (toBecontinue)
{
Console.WriteLine("请在以下一行输入你的信息,格式为‘姓名-密码’:");
string yourInfo = Console.ReadLine();
StrWillBeOk2(ref yourInfo);//验证输入的字符串是否有效
string[] theTeacherInfo = yourInfo.Split('-');
if (File.Exists(@"..\..\Teacher.txt"))
{
string[] TeachersInfo = File.ReadAllLines(@"..\..\Teacher.txt");
foreach (string s in TeachersInfo)
{
string[] oneTeacherInfo = s.Split('-');
if (oneTeacherInfo[0] == theTeacherInfo[0])//验证姓名是否存在
{
HaveYourName = true;
if (oneTeacherInfo[1] == theTeacherInfo[1])//验证密码是否正确
{
YourPasswordRight = true;
toBecontinue = false;
//保存成功登陆的教师的所有信息
TheLoginTeacher.Name = oneTeacherInfo[0];
TheLoginTeacher.Password = oneTeacherInfo[1];
TheLoginTeacher.Course = oneTeacherInfo[2];
}
break;
}
}
if (!HaveYourName)
{
Console.WriteLine("姓名不存在!");
toBecontinue = true;
}
else
if (!YourPasswordRight)
{
Console.WriteLine("密码不正确!");
toBecontinue = true;
}
}
}
Console.WriteLine("登录成功!");
Console.WriteLine("<------------登录验证完毕------------>");
Console.WriteLine();
return TheLoginTeacher;
}
//教师登录模块4--用途:显示教师的详细信息
static void ShowTeacherInfo(StructOfTeacher Teacher)
{
Console.WriteLine(" ----------------");
Console.WriteLine("|姓名:{0,-10}|", Teacher.Name);
Console.WriteLine("|密码:{0,-10}|", Teacher.Password);
Console.WriteLine("|课程:{0,-10}|", Teacher.Course);
Console.WriteLine(" ----------------");
}
//文件操作函数1--用途:追加若干字符串到现有的文件当中。如果文件不存在,则自动创建文件,然后追加。
static void AppendTextToFile(string path,params string[] strWillAdd)
{
try
{
using (StreamWriter sw = File.AppendText(path))
{
foreach (string s in strWillAdd)
{
sw.WriteLine(s);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
//文件操作函数2--用途:创建一个新文件,在其中写入若干行字符串,然后关闭该文件。如果目标文件已存在,则覆盖该文件以及文件以内的所有内容。
static void CreateTextToFile(string path, params string[] strWillAdd)
{
try
{
using (StreamWriter sw = File.CreateText(path))
{
foreach (string s in strWillAdd)
{
sw.WriteLine(s);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
//文件操作函数3--用途:创建一个新文件,在其中写入指定的字符串数组,然后关闭该文件。如果目标文件已存在,则覆盖该文件。
static void WriteAllLinesToFile(string path, string[] strArrayWillAdd)
{
try
{
File.WriteAllLines(path, strArrayWillAdd);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
//输入格式验证1--用途:验证输入的‘姓名-密码-课程’是否符合格式,不符合,则提示重新输入,并覆盖原来的值
static void StrWillBeOk1(ref String str)
{
bool toBeContinue = false;
string[] strSplit = new string[3];
while (!toBeContinue)
{
strSplit = str.Split('-');
try
{
bool strIsOk = true;
if (strSplit.Length != 3)
strIsOk = false;
foreach (string s in strSplit)
if (s == "")
{
strIsOk = false;
break;
}
if (!strIsOk)
throw new ArgumentException();
else
toBeContinue = true;
}
catch (ArgumentException e)
{
Console.WriteLine("格式不正确,请严格按照‘姓名-密码-课程’输入!");
str = Console.ReadLine();
}
}
}
//输入格式验证2--用途:验证输入的‘姓名-密码’是否符合格式,不符合,则提示重新输入,并覆盖原来的值
static void StrWillBeOk2(ref String str)
{
bool toBeContinue = false;
string[] strSplit = new string[2];
while (!toBeContinue)
{
strSplit = str.Split('-');
try
{
bool strIsOk = true;
if (strSplit.Length != 2)
strIsOk = false;
foreach (string s in strSplit)
if (s == "")
{
strIsOk = false;
break;
}
if (!strIsOk)
throw new ArgumentException();
else
toBeContinue = true;
}
catch (ArgumentException e)
{
Console.WriteLine("格式不正确,请严格按照‘姓名-密码’输入!");
str = Console.ReadLine();
}
}
}
}
}