Создание структуры - C#
Формулировка задачи:
1)Необходимо создать структуру "Студент"
-ФИО
-Номер телефона
-Группа
-Оценки по трем основным предметам
Верно ли написан код?
2)Далее нужно удалить все элементы из группы с указанным номером, у которого среднее арифметическое оценок меньше заданного, добавить элемент после элемента с заданной фамилией. Как правильно реализовать этот пункт?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace lab7
{
struct Stud
{
public int tel;
public string fio;
public int group;
public int marks;
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("введите количество записей");
int n = Int16.Parse(Console.ReadLine());
Stud[] st = new Stud[n];
for (int i = 0; i < st.Length; i++)
{
Console.WriteLine("введите ФИО");
st[i].fio = Console.ReadLine();
}
for (int j = 0; j < st.Length; j++)
{
Console.WriteLine("введите телефон");
st[j].tel = Int16.Parse(Console.ReadLine());
}
for (int k = 0; k < st.Length; k++)
{
Console.WriteLine("введите группу");
st[k].group = Int16.Parse(Console.ReadLine());
}
for (int l = 0; l < st.Length; l++)
{
Console.WriteLine("введите оценки");
st[l].marks = Int16.Parse(Console.ReadLine());
}
for (int i = 0; i < st.Length; i++)
{
Console.WriteLine("ФИО {0}", st[i].fio);
}
for (int j = 0; j < st.Length; j++)
{
Console.WriteLine("телефон {0}", st[j].tel);
}
for (int k = 0; k < st.Length; k++)
{
Console.WriteLine("группа {0}", st[k].group);
}
for (int l = 0; l < st.Length; l++)
{
Console.WriteLine("оценки {0}", st[l].marks);
}
try
{
FileStream aFile = new FileStream("test.txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(aFile);
for (int i = 0; i < st.Length; i++)
{
sw.WriteLine(" {0} {1} {2} {3}", st[i].fio, st[i].tel, st[i].group, st[i].marks);
}
sw.Close();
aFile.Close();
}
catch (IOException e)
{
Console.WriteLine("файл не найден");
Console.WriteLine(e.ToString());
Console.ReadKey();
}
finally
{
Console.WriteLine("finnaly");
Console.ReadKey();
}
string buf;
try
{
FileStream aFile = new FileStream("test.txt", FileMode.OpenOrCreate);
StreamReader sr = new StreamReader(aFile);
buf = sr.ReadLine();
while (buf != null)
{
Console.WriteLine("{0}", buf);
buf = sr.ReadLine();
}
sr.Close();
aFile.Close();
}
catch (IOException e)
{
Console.WriteLine("файл не найден");
Console.WriteLine(e.ToString());
Console.ReadKey();
}
Console.ReadKey();
}
}
}Решение задачи: «Создание структуры»
textual
Листинг программы
public static void RemoveStudent(List<Stud> nameList, double minAverage)
{
for (int i = 0; i < nameList.Count; i++)
if (nameList[i].AverageMarks() < minAverage) nameList.Remove(nameList[i]);
}