Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* You are given a sequence of text lines. Assume these text lines form a matrix of characters (pad the missing positions with spaces to build a rectangular matrix). Write a program to rotate the matrix by 90, 180, 270, 360, … degrees. Print the result at the console as sequence of strings.
- * The first line holds a command in format "Rotate(X)" where X are the degrees of the requested rotation. The next lines contain the lines of the matrix for rotation. The input ends with the command "END". */
- namespace _11.StringMatrixRotation
- {
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Text.RegularExpressions;
- class StringMatrixRotation
- {
- static void Main(string[] args)
- {
- string pattern = @"Rotate\((\d{1,5})\)";
- string input = Console.ReadLine();
- MatchCollection matches = Regex.Matches(input, pattern);
- int degrees = (int.Parse(matches[0].Groups[1].Value)) % 360;
- List<string> inputText = new List<string>();
- int width = 0;
- input = Console.ReadLine();
- while (input != "END")
- {
- inputText.Add(input);
- if (input.Length > width)
- {
- width = input.Length;
- }
- input = Console.ReadLine();
- }
- switch (degrees)
- {
- case 90:
- RotateTheMatrixBy90Degrees(inputText, width);
- break;
- case 180:
- RotateTheMatrixBy180Degrees(inputText, width);
- break;
- case 270:
- RotateTheMatrixBy270Degrees(inputText, width);
- break;
- default:
- RotateTheMatrixBy0or360Degrees(inputText, width);
- break;
- }
- }
- private static void RotateTheMatrixBy0or360Degrees(List<string> text, int cols)
- {
- foreach (string currentRow in text)
- {
- StringBuilder result = new StringBuilder();
- for (int col = 0; col < cols; col++)
- {
- result.Append(col < currentRow.Length ? currentRow[col] : ' ');
- }
- Console.WriteLine(result);
- }
- }
- private static void RotateTheMatrixBy90Degrees(List<string> text, int rows)
- {
- text.Reverse();
- for (int row = 0; row < rows; row++)
- {
- StringBuilder result = new StringBuilder();
- foreach (string t in text)
- {
- result.Append(row < t.Length ? t[row] : ' ');
- }
- Console.WriteLine(result);
- }
- }
- private static void RotateTheMatrixBy180Degrees(List<string> text, int cols)
- {
- text.Reverse();
- foreach (string currentRow in text)
- {
- StringBuilder result = new StringBuilder();
- for (int col = cols - 1; col >= 0; col--)
- {
- result.Append(col < currentRow.Length ? currentRow[col] : ' ');
- }
- Console.WriteLine(result);
- }
- }
- private static void RotateTheMatrixBy270Degrees(List<string> text, int rows)
- {
- for (int row = rows - 1; row >= 0; row--)
- {
- StringBuilder result = new StringBuilder();
- foreach (string t in text)
- {
- result.Append(row < t.Length ? t[row] : ' ');
- }
- Console.WriteLine(result);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement