Считать из файла - C#
Формулировка задачи:
По заданию, нужно вывести в файл out.txt фамилии и имена учащихся в порядке убывания их среднего балла.
Но в коде ( и по заданию) предполагается, что оценок будет 3. МНЕ НУЖНО, чтобы количество оценок считывалось автоматически (гарантирую то, что количество будет одинаковое у всех студентов) и средний балл соответственно находился исходя из количества оценок.
Листинг программы
- using System;
- using System.IO;
- namespace Lab06
- {
- class Program
- {
- struct Student
- {
- public string Name;
- public double AverageScore;
- public uint Position;
- };
- static string Third_GetName(string Line)
- {
- string Ret = "";
- int Spaces = 2;
- foreach (char c in Line)
- {
- if ((int)c == 32) Spaces--;
- if ((Char.IsLetter(c) || ((int)c == 32)) && Spaces != 0)
- {
- Ret = Ret + c;
- }
- }
- return Ret;
- }
- static double Third_GetAverageScore(string Line)
- {
- uint Ret = 0;
- uint Score = 0;
- foreach (char c in Line)
- {
- if (Char.IsDigit(c))
- {
- Score = UInt16.Parse(c.ToString());
- if ((Score < 2 || Score > 5)) Console.WriteLine("Один из баллов за предмет не входит в средний балл, т.к он не в [2, 5]");
- else
- {
- Ret += Convert.ToUInt16(c.ToString());
- }
- }
- }
- return ((double)(Ret) / 3.0);
- }
- static void Third()
- {
- try
- {
- StreamReader f = new StreamReader("вход.txt");
- uint CountOfMembers = 0;
- try
- {
- CountOfMembers = Convert.ToUInt32(f.ReadLine());
- }
- catch (Exception e)
- {
- Console.WriteLine("Ошибка: {0}", e.Message);
- }
- Console.WriteLine("Количество учащихся: {0}", CountOfMembers);
- string[] DataList = new string[CountOfMembers];
- Student[] MemberList = new Student[CountOfMembers];
- for (int i = 0; i < CountOfMembers; i++)
- {
- DataList[i] = f.ReadLine();
- MemberList[i] = new Student();
- MemberList[i].Name = Third_GetName(DataList[i]);
- MemberList[i].AverageScore = Third_GetAverageScore(DataList[i]);
- }
- f.Close();
- StreamWriter _f = new StreamWriter("out.txt");
- uint counter = 0;
- uint[] ScoreMap = new uint[CountOfMembers];
- for (int i = 0; i < CountOfMembers; i++)
- {
- counter = 0;
- for (int j = 0; j < CountOfMembers; j++)
- {
- if (MemberList[i].AverageScore > MemberList[j].AverageScore) counter++;
- }
- MemberList[i].Position = CountOfMembers - counter;
- }
- Student[] t = new Student[CountOfMembers];
- for (int i = 0; i < CountOfMembers; i++) t[MemberList[i].Position - 1] = MemberList[i];
- for (int i = 0; i < CountOfMembers; i++) _f.WriteLine("{0} {1}", t[i].Name, t[i].AverageScore);
- _f.Close();
- }
- catch (Exception e)
- {
- Console.WriteLine("Ошибка: {0}\nФайл будет создан", e.Message);
- StreamWriter f = new StreamWriter("вход.txt");
- f.Close();
- }
- }
- static void Main (string[] args)
- {
- Third();
- Console.ReadKey();
- }
- }
- }
Решение задачи: «Считать из файла»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace ConsoleApplication5
- {
- class Program
- {
- static void Main(string[] args)
- {
- using (StreamReader sr = new StreamReader(@"E:\1.txt", Encoding.Default))
- {
- using (StreamWriter sw = new StreamWriter(@"E:\2.txt", true, Encoding.Default))
- {
- while (true)
- {
- string temp = sr.ReadLine();
- if (temp == null)
- break;
- String[] stringArr = temp.Split(' ');
- IEnumerable<double> intArr = stringArr[2].ToCharArray().Select((x) => Char.GetNumericValue(x)); ;
- double result = 0;
- foreach (var el in intArr)
- result += el;
- result = result / intArr.Count();
- foreach (var el in stringArr.Take(2))
- sw.Write(" " + el);
- sw.WriteLine(" " + result);
- }
- }
- }
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д