Last active
October 11, 2015 22:17
-
-
Save Kedrigern/3927526 to your computer and use it in GitHub Desktop.
Zadání vypadalo: Čtěte vstupní textový soubor text.in a pro dvacet nejčetnějších slov v něm určete jejich četnost. Tato nejčetnější slova spolu s jejich četnostmi zapište na výstup v pořadí podle klesající četnosti. Pokud mají dvě různá slova shodnou četnost vypište je v abecedním pořadí.
This file contains 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
/** | |
* Author: Ondřej Profant, 2010 | |
* Velmi stará a špatná školní práce... | |
*/ | |
using System; | |
using CodEx; | |
namespace Nejcastejsi | |
{ | |
class MainClass | |
{ | |
public static void Main (string[] args) | |
{ | |
int max = 1000; | |
try | |
{ | |
if(args[0] != null) max = int.Parse(args[0]); | |
} | |
catch { } | |
Nejcastejsi.Vyskyt[] pole = new Nejcastejsi.Vyskyt[max]; | |
NactiSlova(pole); | |
Array.Sort(pole) ; | |
int kolik; | |
try | |
{ | |
kolik = int.Parse(args[1]); | |
} | |
catch | |
{ | |
kolik = 20; | |
} | |
PrintOutput(pole, kolik); | |
} | |
private static void NactiSlova(Nejcastejsi.Vyskyt[] pole) | |
{ | |
int i = 0; | |
bool byl; | |
string actual; | |
Reader vstup = new Reader("text.in"); | |
while( (actual = Cti(vstup) ) != null ) | |
{ | |
if(actual == "konec") break; | |
byl = false; | |
for(int j = 0; j < pole.Length; j++) /* Prohledáme již známá slova */ | |
{ | |
if(pole[j].slova == actual) | |
{ | |
pole[j].Inc(); | |
byl = true; | |
break; | |
} | |
} | |
if(!byl) /* Pokud slovo ještě neznáme, tak ho přidáme */ | |
{ | |
pole[i] = new Vyskyt(actual); | |
i++; | |
} | |
} | |
vstup.Close(); | |
} | |
/** | |
* Just print output to screen | |
*/ | |
private static void PrintOutputNejcastejsi.Vyskyt[] pole, int kolik) | |
{ | |
for(int i = 0; i<= (kolik-1);i++) | |
{ | |
if(pole[i].slova == null) continue; | |
Console.WriteLine("{0} {1}", pole[i].slova, pole[i].pocet); | |
} | |
} | |
/** | |
* Reads one word from Reader. If there are no word returns null, otherwise the word. | |
* @param Reader vstup | |
* @return null|string | |
*/ | |
private static string Cti(Reader vstup) | |
{ | |
string slovo=""; | |
char c; | |
while(vstup.Char(out c)) | |
{ | |
if( ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')) | |
{ | |
slovo += c; | |
} | |
else | |
{ | |
if(slovo.Length >= 1) | |
{ | |
return slovo; | |
} | |
} | |
} | |
if(slovo.Length >= 1) return slovo; | |
return null; | |
} | |
} | |
/** | |
* @class Vyskyt reprezentuje dvojici slovo a počet výskytů. | |
* Implementuje interface, aby šla snadno porovnat. | |
*/ | |
public struct Vyskyt : IComparable<Vyskyt> | |
{ | |
public int CompareTo(Vyskyt v) | |
{ | |
return v.pocet.CompareTo(this.pocet); | |
} | |
public string slova; | |
public int pocet; | |
/** | |
* @constructor | |
*/ | |
public Vyskyt(string slovo) | |
{ | |
this.slova = slovo; | |
pocet = 1; // pokud slovo naleznu, tak budu mít právě 1 výskyt | |
} | |
public void Inc() | |
{ | |
this.pocet++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment