25.07.2017 Views

Intro-CSharp-Book-v2015

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

640 Въведение в програмирането със C#<br />

{<br />

// Getting the Cyrillic encoding<br />

System.Text.Encoding encoding =<br />

System.Text.Encoding.GetEncoding(1251);<br />

// Create reader with the Cyrillic encoding<br />

StreamReader streamReader =<br />

new StreamReader(INPUT_FILE, encoding);<br />

// Create writer with the Cyrillic encoding<br />

StreamWriter streamWriter =<br />

new StreamWriter(OUTPUT_FILE, false, encoding);<br />

}<br />

using (streamReader)<br />

{<br />

using (streamWriter)<br />

{<br />

string line;<br />

while ((line = streamReader.ReadLine()) != null)<br />

{<br />

streamWriter.WriteLine(FixLine(line));<br />

}<br />

}<br />

}<br />

}<br />

catch (IOException exc)<br />

{<br />

Console.WriteLine("Error: {0}.", exc.Message);<br />

}<br />

static string FixLine(string line)<br />

{<br />

// Find closing brace<br />

int bracketFromIndex = line.IndexOf('}');<br />

// Extract 'from' time<br />

string fromTime = line.Substring(1, bracketFromIndex - 1);<br />

// Calculate new 'from' time<br />

int newFromTime = (int) (Convert.ToInt32(fromTime) *<br />

COEFFICIENT + ADDITION);<br />

// Find the following closing brace<br />

int bracketToIndex = line.IndexOf('}',<br />

bracketFromIndex + 1);<br />

// Extract 'to' time<br />

string toTime = line.Substring(bracketFromIndex + 2,

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!