BeloMaximka

Belov_HW_CS3

Feb 3rd, 2022 (edited)
769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.18 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4.  
  5. namespace Example
  6. {
  7.     class MyApplication
  8.     {
  9.         static void CeasarEncode(string input, int shift)
  10.         {
  11.             StringBuilder encoded = new StringBuilder(input);
  12.             for (int i = 0; i < encoded.Length; i++)
  13.                 encoded[i] += Convert.ToChar(shift);
  14.             Console.WriteLine(encoded);
  15.             Console.ReadKey();
  16.             Console.Clear();
  17.         }
  18.         static void CeasarDecode(string input, int shift)
  19.         {
  20.             StringBuilder encoded = new StringBuilder(input);
  21.             for (int i = 0; i < encoded.Length; i++)
  22.                 encoded[i] -= Convert.ToChar(shift);
  23.             Console.WriteLine(encoded);
  24.             Console.ReadKey();
  25.             Console.Clear();
  26.         }
  27.         static void MatrixMult(double[,] matrix, double value)
  28.         {
  29.             for (int i = 0; i < matrix.GetLength(0); i++)
  30.             {
  31.                 for (int j = 0; j < matrix.GetLength(1); j++)
  32.                 {
  33.                     matrix[i, j] *= value;
  34.                     Console.Write("{0:F2} ", matrix[i, j]);
  35.                 }
  36.                 Console.WriteLine();
  37.             }
  38.             Console.ReadKey();
  39.             Console.Clear();
  40.         }
  41.         static void MatrixMult(double[,] first, double[,] second)
  42.         {
  43.             if (first.GetLength(0) == second.GetLength(1) && first.GetLength(1) == second.GetLength(0))
  44.             {
  45.                 int maxCoulmn = Math.Max(first.GetLength(0), second.GetLength(0));
  46.                 int maxRows = Math.Max(first.GetLength(1), second.GetLength(1));
  47.                 double[,] result = new double[maxCoulmn, maxRows];
  48.                 for (int i = 0; i < maxCoulmn; i++)
  49.                 {
  50.                     for (int m = 0; m < first.GetLength(1); m++)
  51.                     {
  52.                         double sum = 0;
  53.                         for (int k = 0; k < first.GetLength(1); k++)
  54.                         {
  55.                             sum += first[i, k] * second[k, m];
  56.                         }
  57.                         result[i, m] = sum;
  58.                         Console.Write($"{result[i, m]} ");
  59.                     }
  60.                     Console.WriteLine();
  61.                 }
  62.             }
  63.             else
  64.             {
  65.                 Console.WriteLine("Неверный формат.");
  66.             }
  67.             Console.ReadKey();
  68.             Console.Clear();
  69.         }
  70.         static void SumMatrix(double[,] first, double[,] second)
  71.         {
  72.             if (first.GetLength(0) == second.GetLength(0) && first.GetLength(1) == second.GetLength(1))
  73.             {
  74.                 double[,] result = new double[first.GetLength(0), first.GetLength(1)];
  75.                 for (int i = 0; i < result.GetLength(0); i++)
  76.                 {
  77.                     for (int j = 0; j < result.GetLength(1); j++)
  78.                     {
  79.                         result[i, j] = first[i, j] + second[i, j];
  80.                         Console.Write("{0} ", result[i, j]);
  81.                     }
  82.                     Console.WriteLine();
  83.                 }
  84.             }
  85.             else
  86.             {
  87.                 Console.WriteLine("Неверный формат.");
  88.             }
  89.         }
  90.         static void CalcString(string input)
  91.         {
  92.             char[] operations = { '+', '-' };
  93.             int operandShift = 1;
  94.             string[] operands = input.Split(new char[] { '+', '-' });
  95.             double result = double.Parse(operands[0]);
  96.             while (true)
  97.             {
  98.                 int index = input.IndexOfAny(operations);
  99.                 if (index == -1)
  100.                     break;
  101.                 if (input[index] == '+')
  102.                     result += double.Parse(operands[operandShift]);
  103.                 else if (input[index] == '-')
  104.                     result -= double.Parse(operands[operandShift]);
  105.                 operandShift++;
  106.                 input = input.Remove(0, index + 1);
  107.             }
  108.             Console.WriteLine($"{result}");
  109.             Console.ReadKey();
  110.             Console.Clear();
  111.         }
  112.         static void UpperSentenceBeginning(string text)
  113.         {
  114.             string[] split = text.Split(new char[] { '.' }, StringSplitOptions.TrimEntries);
  115.             Console.WriteLine(string.Join(". ", split.Select(s => s[0].ToString().ToUpper() + s.Substring(1))));
  116.             Console.ReadKey();
  117.             Console.Clear();
  118.         }
  119.         static void CensorString(string text, params string[] badWords)
  120.         {
  121.             foreach (var item in badWords)
  122.             {
  123.                 int counter = 0;
  124.                 int startIndex = -1;
  125.                 while ((startIndex = (text.IndexOf(item, startIndex + 1))) != -1)
  126.                     counter++;
  127.                 text = text.Replace(item, new string(Enumerable.Repeat('*', item.Length).ToArray()));
  128.                 Console.WriteLine($"{item} - {counter}");
  129.             }
  130.             Console.WriteLine(text);
  131.             Console.ReadKey();
  132.             Console.Clear();
  133.         }
  134.         public static void Main()
  135.         {
  136.             CeasarEncode("Я люблю свою маму", 3);
  137.             CeasarDecode("в#оёдоё#фесё#пгпц", 3);
  138.             MatrixMult(new double[,] { { 1, 2, 3, 4, 5 }, { 2, 3, 4, 5, 6 } }, 2.4);
  139.             SumMatrix(new double[,] { { 1, 2, 3, 4, 5 }, { 2, 3, 4, 5, 6 } }, new double[,] { { 1, 2, 3, 4, 5 }, { 2, 3, 4, 5, 6 } });
  140.             MatrixMult(new double[,] { { 0, -1, 1 }, { 3, 2, 2 }, { 1, 0, -2 } }, new double[,] { { 0, -1, 1 }, { 3, 2, 2 }, { 1, 0, -2 } });
  141.             CalcString("1 + 2 + 4 + 3 - 220");
  142.             UpperSentenceBeginning("today is a good day for walking. i will try to walk near the sea");
  143.             CensorString(@"To be, or not to be, that is the question,
  144. Whether 'tis nobler in the mind to suffer
  145. The slings and arrows of outrageous fortune,
  146. Or to take arms against a sea of troubles,
  147. And by opposing end them? To die: to sleep;
  148. No more; and by a sleep to say we end
  149. The heart-ache and the thousand natural shocks
  150. That flesh is heir to, 'tis a consummation
  151. Devoutly to be wish'd. To die, to sleep", "die", "fortune");
  152.         }
  153.     }
  154. }
Add Comment
Please, Sign In to add comment