Поэтапно разобрать работу программы - C#
Формулировка задачи:
Помогите поэтапно разобрать работу программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication56
{
class Program
{
static void Main(string[] args)
{
string str = "";
int count = 0, sumbols = 0, check = 0;
do
{
try
{
str = Console.ReadLine();
if (str == "q") break;
else if (!string.IsNullOrWhiteSpace(str))
{
while (str.Contains("\t"))
str = str.Replace("\t", " ");
while (str.Contains(" "))
str = str.Replace(" ", " ");
if (str[str.Length - 1] == ' ')
str = str.Remove(str.Length - 1);
if (str[0] == ' ') str = str.Remove(0, 1);
if (str == "q") break;
str = str.ToLower();
string[] Arr = str.Split(' ');
for (int i = 0; i < Arr.Length; i++)
{
if ((Arr[i] != " ") & !string.IsNullOrWhiteSpace(Arr[i]))
count++;
}
char[] ch = str.ToCharArray();
for (int i = 0; i < ch.Length; i++)
{
for (int a = i - 1; a >= 0; a--)
if (ch[i] == ch[a]) check++;
if (ch[i] != ' ' & check == 0)
for (int j = 0; j < ch.Length; j++)
if (ch[i] == ch[j]) sumbols++;
Array.Sort(ch);
if (check == 0 & ch[i] != ' ')
Console.WriteLine(ch[i] + " : " + sumbols); sumbols = 0;
check = 0;
}
Console.WriteLine(count + " слов");
count = 0;
}
}
catch { }
} while (str != "q");
}
}
}Решение задачи: «Поэтапно разобрать работу программы»
textual
Листинг программы
using System;
namespace ConsoleApplication7
{
class Program
{
////////////////
/// Гавнокод ///
////////////////
static void Main(string[] args)
{
string str = "";//Змінна яка збирігає введений текст з клави
int count = 0, //Кількість слів
sumbols = 0,
check = 0;
do//Початок циклу. Він буде повторюватись поки умова у блоці while не буде істинна
{
try//Якщо в блоці try відбудеться помилка, спрацює блок сatch
{
str = Console.ReadLine();//Отримуємо зтроку з консолі
if (str == "q") break;//Якщо строка дорівнює q, то виходимо з циклу
else if (!string.IsNullOrWhiteSpace(str))//Якщо строка не порожня то виконуємо наступні дії
{
while (str.Contains("\t"))//Змінюємо всі табуляції на пробіли
str = str.Replace("\t", " ");
//while (str.Contains(" "))//Брєд
// str = str.Replace(" ", " ");
if (str[str.Length - 1] == ' ')//Якщо останній символ є пробілом то видаляємо його , краще використати Trim()
str = str.Remove(str.Length - 1);
if (str[0] == ' ') str = str.Remove(0, 1);//Якщо перший символ є пробілом то видаляємо його , краще використати Trim()
if (str == "q") break;//Знову перевіряємо чи строка не дорівнює q, то виходимо з циклу
str = str.ToLower();//переводимо строку в нихній регістр
string[] Arr = str.Split(' ');//Ділимо строку на масим слів, розділених пробілом
for (int i = 0; i < Arr.Length; i++)
{
if ((Arr[i] != " ") & !string.IsNullOrWhiteSpace(Arr[i]))//Якщо буде стояти більше двох пробілів то створяться порожні осередки. Ця строчка зашкоджує цьому
count++;
}
char[] ch = str.ToCharArray();//Конвертуємо строку в масив char//Абсолютно не потрібна дія
for (int i = 0; i < ch.Length; i++)//Перебираємо кожен символ
{
for (int a = i - 1; a >= 0; a--)//Знову перебираємо кожен символ
if (ch[i] == ch[a]) check++;//Якщо цей символ зустрічається, то збільшуємо змінну
if (ch[i] != ' ' & check == 0)
for (int j = 0; j < ch.Length; j++)//Брэд, як і весь код
if (ch[i] == ch[j]) sumbols++;
Array.Sort(ch);//Сортуємо масив, в циклі. В циклі Карл.
if (check == 0 & ch[i] != ' ')//Якщо кількість повторів символу не == 0 і не є пробілом
Console.WriteLine(ch[i] + " : " + sumbols); sumbols = 0;//то виводимо символ та кількість повторів. Обнуляємо змінні
check = 0;
}
Console.WriteLine(count + " слов");//Виводимо кількість слів
count = 0;
}
}
catch {//Якщо відбудеться помилка. То... А ніфіга!!!
}
} while (str != "q");//І ще одна превірка на q
}
}
}