Описать структуру с именем Student - C# (212243)
Формулировка задачи:
Описать структуру с именем Student, содержащую следующие поля:
• Фамилия, имя, отчество;
• Форма обучения (очная, заочная, дистанционная);
• Факультет;
• Курс;
Написать программу, которая позволяет выполнять следующие действия:
1. Чтение данных из текстового файла в массив, состоящий из восьми элементов типа Student оформить в виде функции;
2. Вывод данных о студентах оформить в виде функции;
3. Вывод на экран информации о студентах определенного факультета,, название которого вводится с клавиатуры (если такого факультета нет – вывести соответствующее сообщение);
4. Описать функцию, позволяющую выполнять перевод студентов на следующий курс;
5. Применить к массиву сортировку по алфавиту по полю фамилия. Упорядоченный массив вывести на экран.
Решение задачи: «Описать структуру с именем Student»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
namespace StudentSimpleProject
{
public static class StudentComponent
{
public static Student CreateStudent(string name, string lastname,string middelname,FormofTraining formofTraining,Course course,string faculty)
{
return new Student
{
Name = name,
LastName = lastname,
MiddleName = middelname,
Formoftraining = formofTraining,
CurrentCourse = course,
Faculty = faculty
};
}
public static void WriteInFile(Student student,StreamWriter writer)
{
writer.WriteLine(student.Name+";"+student.LastName+";"+student.MiddleName+";"+
((int)student.Formoftraining).ToString(CultureInfo.InvariantCulture)+";"+((int)student.CurrentCourse).ToString(CultureInfo.InvariantCulture)+";"+student.Faculty);
}
public static Student ReadfromFile(StreamReader reader)
{
var rez = reader.ReadLine();
if(rez==null) return new Student{Name = null,LastName = null,CurrentCourse = Course.Empty,Formoftraining = FormofTraining.Empty,Faculty = null,MiddleName = null};
var temp = rez.Split(';');
return new Student
{
Name = temp[0],
LastName = temp[1],
MiddleName = temp[2],
Formoftraining =(FormofTraining) Int32.Parse(temp[3]),
CurrentCourse = (Course)Int32.Parse(temp[4]),
Faculty = temp[5]
};
}
public static Student[] Read(int count)
{
if (count < 0) return null;
var array = new List<Student>(count);
using (var stream = new FileStream("Data.txt",FileMode.Open,FileAccess.Read))
{
var reader = new StreamReader(stream);
if (count == 0)
{
while (!reader.EndOfStream)
array.Add(ReadfromFile(reader));
}
else
for (int i = 0; i < count || !reader.EndOfStream; i++)
array.Add(ReadfromFile(reader));
}
return array.ToArray();
}
public static void Write(Student[] array,FileMode mode)
{
using (var stream = new FileStream("Data.txt",mode, FileAccess.Write))
{
var writer = new StreamWriter(stream);
foreach (var student in array)
WriteInFile(student, writer);
writer.Flush();
}
}
public static void Print(Student[] array)
{
foreach (var student in array)
PrintOne(student);
}
public static void PrintOne(Student student)
{
Console.WriteLine(student);
}
public static Student[] FindByFaculty(string faculty)
{
var result = Read(0);
return result.Where(studet => studet.Faculty == faculty).ToArray();
}
public static void Sort(Student[] array)
{
Array.Sort(array,0,array.Length);
}
public static void Perevod(Course oldcourse,Course newcourse)
{
var allstudents = Read(0);
for (int i = 0; i < allstudents.Length; i++)
{
if(allstudents[i].CurrentCourse==oldcourse)
allstudents[i].CurrentCourse = newcourse;
}
Write(allstudents,FileMode.Create);
}
}
}