Az alábbi letöltési lehetőségek közül választhatsz: (
segítség)
Típus: text/plain
Tartalmaz szöveget
Karakterkódolás: utf-8
Méret: 4 KB
using System;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace Osztaly
{
class Tanulo
{
List<Tanulo> ismerosok = new List<Tanulo>(20);
bool felhivtak = false;
public bool Felhivtak
{
get { return felhivtak; }
set { felhivtak = value; }
}
public void UjTelefonszam(Tanulo tanulo)
{
if (tanulo == null)
throw new ArgumentNullException("tanulo");
if (tanulo == this)
throw new ArgumentException("Ismerős nem lehet saját maga.", "tanulo");
ismerosok.Add(tanulo);
}
public void Korbetelefonal()
{
Felhivtak = true;
for (int i = 0; i < ismerosok.Count; i++) {
if (!ismerosok[i].Felhivtak)
ismerosok[i].Korbetelefonal();
}
}
}
class Osztaly
{
int n;
Tanulo[] tanulok;
public Osztaly(string fileName)
{
Betolt(fileName);
}
private int N
{
get
{
return n;
}
set
{
if (value < 1 && value > 100)
throw new ArgumentOutOfRangeException("value");
n = value;
tanulok = new Tanulo[n];
for (int i = 0; i < n; i++)
tanulok[i] = new Tanulo();
}
}
private void Betolt(string fileName)
{
Regex elso = new Regex(@"^\s*(?<darab>\d{1,3})\s*$");
Regex tobbi = new Regex(@"^\s*((?<index>\d{1,3})\s+)*0\s*$");
StreamReader reader = null;
try {
Match match;
reader = new StreamReader(fileName);
match = elso.Match(reader.ReadLine());
if (match.Success) {
int darab = int.Parse(match.Groups["darab"].Value);
if (darab >= 1 && darab <= 100)
N = darab;
else
throw new InvalidDataException("Az N csak 1 és 100 közé eshet.");
}
else {
throw new InvalidDataException("Hiba az első sorban.");
}
for (int i = 0; i < N; i++) {
Group ismerosok;
if (reader.EndOfStream)
throw new InvalidDataException("Kevés a sorok száma.");
if ((match = tobbi.Match(reader.ReadLine())).Success)
ismerosok = match.Groups["index"];
else
throw new InvalidDataException("Hiba a(z) " + (i + 2) + ". sorban.");
for (int j = 0; j < ismerosok.Captures.Count; j++) {
int index = int.Parse(ismerosok.Captures[j].Value);
if (index < 1 || index > N) {
throw new InvalidDataException(
"Hibás szám található a(z) " + (i + 2) + ". sorban");
}
tanulok[i].UjTelefonszam(tanulok[index - 1]);
}
}
}
catch (IOException ex) {
throw new IOException("Nem lehetett betölteni a fájlt.", ex);
}
finally {
if (reader != null)
reader.Close();
}
}
private void Torol()
{
for (int i = 0; i < tanulok.Length; i++)
tanulok[i].Felhivtak = false;
}
private int InformaltakSzama()
{
int db = 0;
for (int i = 0; i < tanulok.Length; i++) {
if (tanulok[i].Felhivtak)
db++;
}
return db;
}
public int Megoldas()
{
int max = 0;
int maxIndex = -1;
for (int i = 0; i < tanulok.Length; i++) {
Torol();
tanulok[i].Korbetelefonal();
int informaltakSzama = InformaltakSzama();
if (informaltakSzama > max) {
max = informaltakSzama;
maxIndex = i;
}
}
return maxIndex + 1;
}
public override string ToString()
{
return "Tanulók száma: " + N;
}
}
class Program
{
static void Main(string[] args)
{
string[] files = Directory.GetFiles(Environment.CurrentDirectory, "*.be");
for (int i = 0; i < files.Length; i++) {
Osztaly osztaly = new Osztaly(files[i]);
Console.WriteLine("{0}: {1}", Path.GetFileName(files[i]), osztaly.Megoldas());
}
}
}
}