Advertisement
Guest User

Untitled

a guest
Aug 28th, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.72 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4.  
  5. class FakeTextMarkUpLanguage
  6. {
  7.     static Dictionary<string, string> tags = new Dictionary<string, string>();
  8.     static StringBuilder global = new StringBuilder();
  9.     static Stack<string> tagove = new Stack<string>();
  10.      const string del = "<del>";
  11.     static void Main()
  12.     {
  13.         tags.Add("<upper>", "</upper>");
  14.         tags.Add("<lower>", "</lower>");
  15.         tags.Add("<toggle>", "</toggle>");
  16.         tags.Add("<del>", "</del>");
  17.         tags.Add("<rev>", "</rev>");
  18.         int n = int.Parse(Console.ReadLine());
  19.         for (int i = 0; i < n; i++)
  20.         {
  21.             string input = Console.ReadLine();
  22.             global.AppendLine(input);
  23.         }
  24.         while (true)
  25.         {
  26.             if (global.ToString().Contains("<") == false) break;
  27.             for (int i = 0; i < global.Length; i++)
  28.             {
  29.                 if (global.ToString().IndexOf(del) > global.ToString().IndexOf(tags[del]) ||
  30.                     (global.ToString().IndexOf(del) == -1 && global.ToString().IndexOf(tags[del]) != -1))
  31.                 {
  32.                     int index = global.ToString().IndexOf(tags[del]);
  33.                     index+= tags[del].Length;
  34.                     string toBeDeleted = global.ToString().Substring(0, index);
  35.                     global = new StringBuilder(global.ToString().Replace(toBeDeleted,string.Empty));
  36.                     break;
  37.                 }
  38.                 else if (global[i] == '<')
  39.                 {
  40.                     string tag = GetTag(i);
  41.                     if (tag == del)
  42.                     {
  43.                         int endClosedDel = global.ToString().IndexOf(tags[del]) + tags[del].Length;
  44.                         global.Remove(i, endClosedDel - i);
  45.                         break;
  46.                     }
  47.                     else
  48.                     {
  49.                         tagove.Push(tag);
  50.                         i += tag.Length - 1;
  51.                     }
  52.                 }
  53.                 else
  54.                 {
  55.                     int openTag = global.ToString().IndexOf('<', i + 1);
  56.                     if (global[openTag + 1] == '/')
  57.                     {
  58.                         ProcessData(i, openTag);
  59.                         break;
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.         Console.WriteLine(global.ToString());
  65.     }
  66.     private static void ProcessData(int start, int end)
  67.     {
  68.         string toBeModified = global.ToString().Substring(start, end - start);
  69.         string currentTag = tagove.Pop();
  70.         switch (currentTag)
  71.         {
  72.             case "<upper>":
  73.                 for (int i = start; i < end; i++)
  74.                 {
  75.                     global[i] = char.ToUpper(global[i]);
  76.                     char ch = global[i];
  77.                 }
  78.                 break;
  79.             case "<lower>":
  80.                 for (int i = start; i < end; i++)
  81.                 {
  82.                     global[i] = char.ToLower(global[i]);
  83.                     char ch = global[i];
  84.                 }
  85.                 break;
  86.             case "<toggle>":
  87.                 for (int i = start; i < end; i++)
  88.                 {
  89.                     if (global[i] >= 65 && global[i] <= 90)
  90.                     {
  91.                         global[i] = char.ToLower(global[i]);
  92.                         char ch = global[i];
  93.                     }
  94.                     else if (global[i] >= 97 && global[i] <= 122)
  95.                     {
  96.                         global[i] = char.ToUpper(global[i]);
  97.                         char ch = global[i];
  98.                     }
  99.                 }
  100.                 break;
  101.             case "<rev>":
  102.                 StringBuilder tmp = new StringBuilder(toBeModified.Length);
  103.                 foreach (var item in toBeModified)
  104.                 {
  105.                     tmp.Append(item);
  106.                 }
  107.                 for (int i = tmp.Length - 1,j = start; i >= 0; i--,j++)
  108.                 {
  109.                     global[j] = tmp[i];
  110.                 }
  111.                 break;
  112.         }
  113.         int startOpenTag = 0;
  114.         for (int i = start; i >= 0; i--)
  115.         {
  116.             if (global[i] == '<')
  117.             {
  118.                 startOpenTag = i;
  119.                 break;
  120.             }
  121.         }
  122.         global.Remove(end, tags[currentTag].Length);
  123.         //Console.WriteLine(global.ToString());
  124.         global.Remove(startOpenTag, start - startOpenTag);
  125.         //Console.WriteLine(global.ToString());
  126.     }
  127.     private static string GetTag(int start)
  128.     {
  129.         int endOpenTag = global.ToString().IndexOf('>', start + 1);
  130.         string tag = global.ToString().Substring(start, endOpenTag - start + 1 );
  131.         return tag;
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement