Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.28 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace zadanie2
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int[] tab1 = { 1, 3, 5, 7, 9 };
  14.             int[] tab2 = { 1, 1, 1, 0, 1, 0, 0, 0 };
  15.             int[,] tab2D = new int[10, 10];
  16.            
  17.             Console.WriteLine("1.1) " + getArithAverage(tab1));
  18.             Console.Write("1.2) "); makeMirrorTable(tab1);
  19.             wyswietlTablice(tab1);
  20.             Console.Write("1.3) "); exchangeMinMax(tab1);
  21.             Console.Write("1.4) "); insertZeros(tab1);
  22.             wyswietlTablice(tab1);
  23.             Console.WriteLine("1.5) "); doExORTable(tab2);
  24.             Console.WriteLine("1.6) "); makeIdentity(tab2D);
  25.             Console.WriteLine("1.7) "); makeMatrixProduct(tab2D);
  26.             Console.WriteLine(/*1.8)*/getRowColSum(tab2D,9,9));//wartosci 1-9
  27.  
  28.  
  29.             Console.ReadKey();
  30.  
  31.             string tekst = "przykladowy tekst";
  32.             string imie = "Krzysztof";
  33.             string zdanie = "PrzyklAdoweZdanie";
  34.             string zdanie2 = "Nazywam sie Przywara";
  35.             string liczba = "12345";
  36.             char znak = 'e';
  37.  
  38.             Console.WriteLine("2.1) {0}", getCharIndex(tekst, znak));
  39.             Console.WriteLine("2.2) {0}", getWordsNumber(tekst));
  40.             Console.WriteLine("2.3) {0}", isMale(imie));
  41.             Console.WriteLine("2.4) {0}", insertSpaces(zdanie));
  42.             Console.WriteLine("2.5) {0}", insertWord(zdanie2, imie, 2));
  43.             Console.WriteLine("2.6) {0}", toLower(zdanie));
  44.             Console.WriteLine("2.7) {0}", convertFromStringToNumber(liczba));
  45.             Console.WriteLine("2.8) {0}", caeserCipher(tekst));
  46.  
  47.             Console.ReadKey();
  48.  
  49.             Console.WriteLine("3.1) {0}", makeLogicSum(true, false));
  50.             Console.WriteLine("3.2) {0}", getRootsNumber(1, 8, 12));
  51.             Console.WriteLine("3.3) {0}", factorial(3));
  52.             Console.WriteLine("3.4) {0}", absoluteValue(-30));
  53.             Console.WriteLine("3.5) {0}", getIntegerFromReal((float)12.345));
  54.             Console.WriteLine("3.6) {0}", getDecimalDigits(12345670));
  55.             Console.WriteLine("3.7) {0}", inverseNumber(8));
  56.             Console.WriteLine("3.8) {0}", reverseNumber(159));
  57.  
  58.             Console.ReadKey();
  59.         }
  60.         static void wyswietlTablice(int[] tab)
  61.         {
  62.             for (int i = 0; i < tab.Length; i++)
  63.             {
  64.                 Console.Write(tab[i]+", ");
  65.             }
  66.             Console.WriteLine();
  67.         }
  68.  
  69.    
  70.         static float getArithAverage(int[] table)
  71.         {
  72.             int suma = 0;
  73.             float avarage;
  74.             for(int i=0; i < table.Length;i++)
  75.             {
  76.  
  77.                 suma += table[i];
  78.             }
  79.             avarage = suma / table.Length;
  80.             return avarage;
  81.         }
  82.         static void makeMirrorTable(int[] table)
  83.         {
  84.             int tmp;
  85.             for (int i = 0; i < table.Length; i++)
  86.             {
  87.                 tmp = table[i];
  88.                 table[i] = table[table.Length - 1-i];
  89.                 table[table.Length - 1 - i] = tmp;
  90.  
  91.             }
  92.         }
  93.         static void exchangeMinMax(int[] table)
  94.         {
  95.             int min = table[0];
  96.             int max = table[0];
  97.             for (int i=0; i < table.Length; i++)
  98.             {
  99.                 if (min > table[i])
  100.                 {
  101.                     min = table[i];
  102.                 }
  103.                 if (max < table[i])
  104.                 {
  105.                     max = table[i];
  106.                 }
  107.             }
  108.             Console.WriteLine("Minimalna: " + min + ", Maksymalna: " + max);
  109.         }
  110.         static void insertZeros(int[] table)
  111.         {
  112.             int dziel;
  113.             for (int i = 0; i < table.Length; i++)
  114.             {
  115.                 dziel = table[i] % 3;
  116.                 if (dziel == 0)
  117.                 {
  118.                     table[i] = 0;
  119.                 }
  120.             }
  121.         }
  122.         static void doExORTable(int[] binaryTable)
  123.         {
  124.             int sum,xor;
  125.             for (int i = 0; i < (binaryTable.Length - 1); i++)
  126.             {
  127.                 sum = binaryTable[i]+binaryTable[i+1];
  128.                 xor = sum % 2;
  129.                 Console.WriteLine("Xor " + (i + 1) + "-ego elementu tablicy i " + (i + 2)+"-ego: "+xor);
  130.  
  131.             }
  132.             // ostatni i pierwszy element
  133.             sum = binaryTable[0] + binaryTable[binaryTable.Length-1];
  134.             xor = sum % 2;
  135.             Console.WriteLine("Xor 1-ego elementu tablicy i " + (binaryTable.Length) + "-ego: " + xor);
  136.         }
  137.         static void makeIdentity(int[,] tab2D)
  138.         {
  139.             for(int i = 0; i < tab2D.GetLength(0);i++)
  140.             {
  141.                 for(int j = 0; j < tab2D.GetLength(1); j++)
  142.                 {
  143.                     if (i == j)
  144.                     {
  145.                         tab2D[i, j] = 1;
  146.                     }
  147.                     Console.Write(tab2D[i, j] + " ");
  148.                 }
  149.                 Console.WriteLine();
  150.             }
  151.         }
  152.         static void makeMatrixProduct(int[,] tab2D)
  153.         {
  154.             for (int i = 0; i < tab2D.GetLength(0); i++)
  155.             {
  156.                 tab2D[i, 0] = (i + 1);
  157.                 for (int j = 0; j < tab2D.GetLength(1); j++)
  158.                 {
  159.                     tab2D[0, j] = (j + 1);
  160.                     //Console.Write(tab2D[i, j] + " ");
  161.                 }
  162.                 //Console.WriteLine();
  163.             }
  164.             for (int i = 1; i < tab2D.GetLength(0); i++)
  165.             {
  166.                 for (int j = 1; j < tab2D.GetLength(1); j++)
  167.                 {
  168.                     tab2D[i, j] = i * j;
  169.                     Console.Write( "{0:D2} ",tab2D[i, j]);
  170.                 }
  171.                 Console.WriteLine();
  172.             }
  173.         }
  174.         static int getRowColSum(int[,] tab2D, int row, int col)
  175.         {
  176.             int sum = 0;
  177.             for (int i = 1; i < tab2D.GetLength(0); i++)
  178.             {
  179.                 sum += tab2D[i, col];
  180.                // Console.WriteLine(sum);
  181.             }
  182.             for (int j = 1; j < tab2D.GetLength(1); j++)
  183.             {
  184.                
  185.                 sum += tab2D[row, j];
  186.              //   Console.WriteLine(sum);
  187.             }
  188.             Console.Write("1.8) Suma " + col + "-ej kolumny i " + row + "-ego wiersza to: ");
  189.             return sum;
  190.         }
  191.         static int getCharIndex(string text, char mark)
  192.         {
  193.             for (int i = 0; i < text.Length; i++)
  194.             {
  195.                 if (mark == text[i])
  196.                 {
  197.                     return i;
  198.                 }
  199.             }
  200.             return -1;
  201.         }
  202.         static int getWordsNumber(string text)
  203.         {
  204.             int licz = 1;
  205.             for (int i = 0; i < text.Length; i++)
  206.             {
  207.                 if (text[i] == ' ')
  208.                 {
  209.                     licz++;
  210.                 }
  211.             }
  212.             return licz;
  213.         }
  214.         static bool isMale(string text)
  215.         {
  216.             if (text[text.Length - 1] == 'a')
  217.             {
  218.                 return false;
  219.             }
  220.             else return true;
  221.         }
  222.         static string insertSpaces(string text)
  223.         {
  224.             string newText = "";
  225.             newText += text[0];
  226.             for (int i = 1; i < text.Length; i++)
  227.             {
  228.                 if ((int)text[i] < 91)
  229.                 {
  230.                     newText += " " + text[i];
  231.                 }
  232.                 else newText += text[i];
  233.             }
  234.             return newText;
  235.         }
  236.         static string insertWord(string text, string word, int spaceIndex)
  237.         {
  238.             int licz = 0;
  239.             string newText = "";
  240.             for (int i = 0; i < text.Length; i++)
  241.             {
  242.                 if (text[i] == ' ')
  243.                 {
  244.                     licz++;
  245.                 }
  246.                 if (licz == spaceIndex)
  247.                 {
  248.                     newText += " " + word;
  249.                     licz = 9999;
  250.  
  251.                 }
  252.                 newText += text[i];
  253.             }
  254.             return newText;
  255.         }
  256.         static string toLower(string text)
  257.         {
  258.             string newText = "";
  259.             for (int i = 0; i < text.Length; i++)
  260.             {
  261.                 if ((int)text[i] < 91)
  262.                 {
  263.                     newText += (char)((int)text[i] + 32);
  264.                 }
  265.                 else newText += text[i];
  266.             }
  267.             return newText;
  268.         }
  269.         static int convertFromStringToNumber(string number)
  270.         {
  271.             int liczba = Convert.ToInt32(number);
  272.             return liczba;
  273.  
  274.         }
  275.         static string caeserCipher(string text)
  276.         {
  277.             string newText = "";
  278.             for (int i = 0; i < text.Length; i++)
  279.             {
  280.                 if (text[0] != ' ')
  281.                 {
  282.                     if (((int)text[i] > 64 && (int)text[i] < 91) || ((int)text[i] > 96 && (int)text[i] < 123))
  283.                     {
  284.                         if (((int)text[i] > 64 && (int)text[i] < 88) || ((int)text[i] > 96 && (int)text[i] < 120))
  285.                         {
  286.                             newText += (char)((int)text[i] + 3);
  287.                         }
  288.                         else
  289.                         {
  290.                             newText += (char)((int)text[i] - 23);
  291.                         }
  292.                     }
  293.                     else
  294.                     {
  295.                         newText += text[i];
  296.                     }
  297.  
  298.                 }
  299.             }
  300.             return newText;
  301.         }
  302.         static bool makeLogicSum(bool val1, bool val2)
  303.         {
  304.             if (val1 == false && val2 == false)
  305.                 return false;
  306.             else
  307.                 return true;
  308.         }
  309.         static int getRootsNumber(float a, float b, float c)
  310.         {
  311.             float delta = b * b - 4 * a * c;
  312.             if (delta > 0)
  313.                 return 2;
  314.             else if (delta == 0)
  315.                 return 1;
  316.             else return 0;
  317.         }
  318.         static long factorial(int n)
  319.         {
  320.             if (n < 1)
  321.                 return 1;
  322.             else
  323.                 return n * factorial(n - 1);
  324.         }
  325.         static float absoluteValue (float number)
  326.         {
  327.             if (number < 0)
  328.             {
  329.                 return number * (-1);
  330.             }
  331.             else
  332.                 return number;
  333.         }
  334.         static int getIntegerFromReal(float number)
  335.         {
  336.             return (int)number;
  337.         }
  338.         static int getDecimalDigits(int number)
  339.         {
  340.             int licz = 0;
  341.             while (number > 0)
  342.             {
  343.                 licz++;
  344.                 number = number / 10;
  345.             }
  346.             return licz;
  347.         }
  348.         static double inverseNumber(double number)
  349.         {
  350.             if (number == 0)
  351.             {
  352.                 Console.WriteLine("Dzielenie przez zero");
  353.                 return 0;
  354.             }
  355.             else
  356.             {
  357.                 return 1 / number;
  358.             }
  359.         }
  360.         static int reverseNumber(int number)
  361.         {
  362.             return number * (-1);
  363.         }
  364.     }
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement