Last active
October 13, 2016 06:21
-
-
Save olecksamdr/5b347953d15b2713f98d84e729a40630 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
public class Person | |
{ | |
string name = ""; | |
string surname = ""; | |
public Person(string name, string surname) | |
{ | |
this.name = name; | |
this.surname = surname; | |
} | |
public Person(string fullName) | |
{ | |
string[] arr = fullName.Split(' '); | |
this.name = arr[0]; | |
this.surname = arr[1]; | |
} | |
public static implicit operator string(Person person) | |
{ | |
return person.name + " " + person.surname; | |
} | |
} | |
public class Ship | |
{ | |
public string name; | |
public List<Person> team; | |
protected string getShipName(string str) | |
{ | |
return Regex.Match(str, "shipName:\\s*\"(\\S+)\"").Groups[1].Value; | |
} | |
protected List<Person> getTeam(string str) | |
{ | |
List<Person> team = new List<Person>(); | |
MatchCollection teamMatch = Regex.Matches(str, "\"(\\S+\\s\\S+)\""); | |
foreach (Match match in teamMatch) | |
{ | |
team.Add(new Person(match.Groups[1].Value)); | |
} | |
return team; | |
} | |
public Ship() | |
{ | |
this.name = ""; | |
this.team = new List<Person>(); | |
} | |
public Ship(string name, Person[] team) | |
{ | |
this.name = name; | |
foreach (Person person in team) | |
{ | |
this.team.Add(person); | |
} | |
} | |
// Приклад вхідних даних: | |
// { shipName: "Winner"; team: ["Petro Verovkin", "Volodumur Velukiy"] } | |
public Ship(string info) | |
{ | |
string[] category = Regex.Split(info, @";\s"); | |
this.name = getShipName(category[0]); | |
this.team = getTeam(category[1]); | |
} | |
public static implicit operator string(Ship ship) | |
{ | |
string str = "{ shipName: \"" + ship.name + "\"; team: ["; | |
foreach (Person person in ship.team) | |
{ | |
str += "\"" + person + "\"" + ", "; | |
} | |
str = str.Remove(str.Length - 2, 2); | |
str += "]; }"; | |
return str; | |
} | |
} | |
// пароплав | |
// Приклад вхідних даних: | |
// { shipName: "Winner"; team: ["Petro Verovkin", "Volodumur Velukiy"]; engines: 4; maxSpeed: 120,5 } | |
public class Steamship : Ship | |
{ | |
int numberOfEngines = 0; | |
double maxSpeed = 0; | |
protected int getEnginesNumber(string str) | |
{ | |
return int.Parse(Regex.Match(str, "\\d+").Value); | |
} | |
protected double getMaxSpeed(string str) | |
{ | |
return double.Parse(Regex.Match(str, "\\d+,\\d+").Value); | |
} | |
public Steamship(string name, Person[] team, int numberOfEngines, double maxSpeed) | |
: base(name, team) | |
{ | |
this.numberOfEngines = numberOfEngines; | |
this.maxSpeed = maxSpeed; | |
} | |
public Steamship(string info) | |
{ | |
string[] category = Regex.Split(info, @";\s"); | |
this.name = getShipName(category[0]); | |
this.team = getTeam(category[1]); | |
this.numberOfEngines = getEnginesNumber(category[2]); | |
this.maxSpeed = getMaxSpeed(category[3]); | |
} | |
public static implicit operator string(Steamship ship) | |
{ | |
string str = "{ shipName: \"" + ship.name + "\"; team: ["; | |
foreach (Person person in ship.team) | |
{ | |
str += "\"" + person + "\"" + ", "; | |
} | |
str = str.Remove(str.Length - 2, 2); | |
str += "]; engines: " + ship.numberOfEngines + "; maxSpeed: " + ship.maxSpeed + "; }"; | |
return str; | |
} | |
} | |
// вітрильник | |
// Приклад вхідних даних: | |
// { shipName: "Winner"; team: ["Petro Verovkin", "Volodumur Velukiy"]; sails: 4; destinationPort: "SuperPort" } | |
public class Windjammer : Ship | |
{ | |
// кількість вітрил | |
int numberOfSails = 0; | |
string destinationPort = ""; | |
protected int getSailsNumber(string str) | |
{ | |
return int.Parse(Regex.Match(str, "\\d+").Value); | |
} | |
protected string getDestinationPort(string str) | |
{ | |
return Regex.Match(str, "\"(\\S+)\"").Groups[1].Value; | |
} | |
public Windjammer(string name, Person[] team, int numberOfSails, string destinationPort) | |
: base(name, team) | |
{ | |
this.numberOfSails = numberOfSails; | |
this.destinationPort = destinationPort; | |
} | |
public Windjammer(string info) | |
{ | |
string[] category = Regex.Split(info, @";\s"); | |
this.name = getShipName(category[0]); | |
this.team = getTeam(category[1]); | |
this.numberOfSails = getSailsNumber(category[2]); | |
this.destinationPort = getDestinationPort(category[3]); | |
} | |
public static implicit operator string(Windjammer ship) | |
{ | |
string str = "{ shipName: \"" + ship.name + "\"; team: ["; | |
foreach (Person person in ship.team) | |
{ | |
str += "\"" + person + "\"" + ", "; | |
} | |
str = str.Remove(str.Length - 2, 2); | |
str += "]; sails: " + ship.numberOfSails + "; destinationPort: \"" + ship.destinationPort+ "\"; }"; | |
return str; | |
} | |
} | |
// ескадра | |
// Приклад вхідних даних: | |
// { | |
// squadronName: "Winner"; | |
// steamships: [{ shipName: "Winner"; team: ["Petro Verovkin", "Volodumur Velukiy"]; engines: 4; maxSpeed: 120,5 }, { shipName: "Winner"; team: ["Petro Verovkin", "Volodumur Velukiy"]; engines: 4; maxSpeed: 120,5 }]; | |
// winjammers: [{ shipName: "Winner"; team: ["Petro Verovkin", "Volodumur Velukiy"]; sails: 4; destinationPort: "SuperPort" }, { shipName: "Winner"; team: ["Petro Verovkin", "Volodumur Velukiy"]; sails: 4; destinationPort: "SuperPort" }]; | |
// | |
// } | |
public class Squadron | |
{ | |
string name = ""; | |
List<Steamship> steamships = new List<Steamship>(); | |
List<Windjammer> windjammers = new List<Windjammer>(); | |
protected string getName(string str) | |
{ | |
return Regex.Match(str, "squadronName:\\s*\"(\\S+)\"").Groups[1].Value; | |
} | |
protected List<Steamship> getSteamships(string str) | |
{ | |
List<Steamship> steamships = new List<Steamship>(); | |
string steamshipsStr = Regex.Match(str, @"steamships:\s\[\{([\s\S]+?)\}\];").Groups[1].Value; | |
string[] ships = Regex.Split(steamshipsStr, @"\},\s+"); | |
foreach (string ship in ships) | |
{ | |
steamships.Add(new Steamship(ship)); | |
} | |
return steamships; | |
} | |
protected List<Windjammer> getWindjummers(string str) | |
{ | |
List<Windjammer> windjamers = new List<Windjammer>(); | |
string steamshipsStr = Regex.Match(str, @"winjammers:\s\[\{([\s\S]+?)\}\];").Groups[1].Value; | |
string[] ships = Regex.Split(steamshipsStr, @"\},\s+"); | |
foreach (string ship in ships) | |
{ | |
windjammers.Add(new Windjammer(ship)); | |
} | |
return windjammers; | |
} | |
public Squadron(string name, Steamship[] steamships, Windjammer[] windjummers) | |
{ | |
this.name = name; | |
foreach (Steamship steamship in steamships) | |
{ | |
this.steamships.Add(steamship); | |
} | |
foreach (Windjammer windjammer in windjammers) | |
{ | |
this.windjammers.Add(windjammer); | |
} | |
} | |
public Squadron(string info) | |
{ | |
this.name = getName(info); | |
this.steamships = getSteamships(info); | |
this.windjammers = getWindjummers(info); | |
} | |
public static implicit operator string(Squadron squadron) | |
{ | |
string str = "{ squadronName: \"" + squadron.name + "\"; steamships: ["; | |
foreach (Steamship ship in squadron.steamships) | |
{ | |
str += ship + ", "; | |
} | |
str = str.Remove(str.Length - 2, 2); | |
str += "]; }; "; | |
foreach (Windjammer ship in squadron.windjammers) | |
{ | |
str += ship + ", "; | |
} | |
str = str.Remove(str.Length - 2, 2); | |
str += "]; }; "; | |
return str; | |
} | |
} | |
namespace testShips | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Ship testShip = new Ship("{ shipName: \"Winner\"; team: [\"Petro Verovkin\", \"Volodumur Velukiy\"] }"); | |
Steamship testSteamship = new Steamship("{ shipName: \"Winner\"; team: [\"Petro Verovkin\", \"Volodumur Velukiy\"]; engines: 4; maxSpeed: 120,5 }"); | |
Windjammer testWindjammer = new Windjammer("{ shipName: \"Winner\"; team: [\"Petro Verovkin\", \"Volodumur Velukiy\"]; sails: 4; destinationPort: \"SuperPort\" }"); | |
Squadron testSquadron = new Squadron("{ squadronName: \"Winner\"; steamships: [{ shipName: \"Winner\"; team: [\"Petro Verovkin\", \"Volodumur Velukiy\"]; engines: 4; maxSpeed: 120,5 }, { shipName: \"Tester\"; team: [\"Petro Verovkin\", \"Volodumur Velukiy\"]; engines: 4; maxSpeed: 120,5 }]; winjammers: [{ shipName: \"Winner\"; team: [\"Petro Verovkin\", \"Volodumur Velukiy\"]; sails: 4; destinationPort: \"SuperPort\" }, { shipName: \"Winner\"; team: [\"Petro Verovkin\", \"Volodumur Velukiy\"]; sails: 4; destinationPort: \"SuperPort\" }]; destinationPort: \"SuperPort\" }"); | |
//int a = 6; | |
//Steamship myShip = new Steamship("shipName: \"myShip\",\n team: [\"Olekcsandr Veleshchuk\"],\n engines: 4,\n maxSpeed: 134,5"); | |
//Console.WriteLine(myShip.name); | |
//Console.WriteLine(myShip.team.ElementAt(0)); | |
//Console.ReadLine(); | |
//MatchCollection match = Regex.Matches("team: [\"Olekcsandr Veleshchuk\"]", "\"(\\S+\\s\\S+)\""); | |
//Console.WriteLine("end"); | |
Console.WriteLine(testShip); | |
Console.WriteLine(testSteamship); | |
Console.WriteLine(testWindjammer); | |
Console.WriteLine(testSquadron); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment