Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.22 KB | None | 0 0
  1. /// III.4
  2.    class Program
  3.     {
  4.         struct Skiers
  5.         {
  6.             public string name;
  7.             public double res;
  8.             public Skiers(string name0, double res0)
  9.             {
  10.                 name = name0;
  11.                 res = res0;
  12.             }
  13.         }
  14.         static void Sort(Skiers[] gr)
  15.         {
  16.             double min;
  17.             int imin;
  18.             for (int i = 0; i < gr.Length - 1; i++)
  19.             {
  20.                 min = gr[i].res;
  21.                 imin = i;
  22.                 for (int k = i + 1; k < gr.Length; k++)
  23.                 {
  24.                     if (gr[k].res < min)
  25.                     {
  26.                         min = gr[k].res;
  27.                         imin = k;
  28.                     }
  29.                 }
  30.                 Skiers temp;
  31.                 temp = gr[i];
  32.                 gr[i] = gr[imin];
  33.                 gr[imin] = temp;
  34.             }
  35.         }
  36.         static void Merge(Skiers[] gr1, Skiers[] gr2, Skiers[] gr)
  37.         {
  38.             int i = 0, j = 0, k = 0;
  39.             while ((i < gr1.Length) && (j < gr2.Length))
  40.             {
  41.                 if (gr1[i].res <= gr2[j].res)
  42.                 {
  43.                     gr[k] = gr1[i];
  44.                     i++;
  45.                     k++;
  46.                 }
  47.                 else
  48.                 {
  49.                     gr[k] = gr2[j];
  50.                     j++;
  51.                     k++;
  52.                 }
  53.             }
  54.             while (i < gr1.Length)
  55.             {
  56.                 gr[k] = gr1[i];
  57.                 i++;
  58.                 k++;
  59.             }
  60.             while (j < gr2.Length)
  61.             {
  62.                 gr[k] = gr2[j];
  63.                 j++;
  64.                 k++;
  65.             }
  66.         }
  67.         static void Main(string[] args)
  68.         {
  69.             Skiers[] gr1 = new Skiers[5];
  70.             gr1[0] = new Skiers("Иванов", 8.54);
  71.             gr1[1] = new Skiers("Петров", 9.03);
  72.             gr1[2] = new Skiers("Сидоров", 9.11);
  73.             gr1[3] = new Skiers("Попов", 8.53);
  74.             gr1[4] = new Skiers("Крылов", 8.48);
  75.             Skiers[] gr2 = new Skiers[7];
  76.             gr2[0] = new Skiers("Титов", 8.51);
  77.             gr2[1] = new Skiers("Козлов", 8.59);
  78.             gr2[2] = new Skiers("Матросов", 8.59);
  79.             gr2[3] = new Skiers("Терешков", 8.47);
  80.             gr2[4] = new Skiers("Краснов", 8.50);
  81.             gr2[5] = new Skiers("Коваленко", 9.01);
  82.             gr2[6] = new Skiers("Тарасов", 8.56);
  83.             Sort(gr1);
  84.             Console.WriteLine("Таблица результатов 1 группы:");
  85.             for (int i = 0; i < gr1.Length; i++)
  86.                 Console.WriteLine(
  87.                     "{0}. {1}\t Результат {2,4:f2}",
  88.                     i + 1, gr1[i].name, gr1[i].res);
  89.             Console.WriteLine();
  90.             Sort(gr2);
  91.             Console.WriteLine("Таблица результатов 2 группы:");
  92.             for (int i = 0; i < gr2.Length; i++)
  93.                 Console.WriteLine(
  94.                     "{0}. {1}\t Результат {2,4:f2}",
  95.                     i + 1, gr2[i].name, gr2[i].res);
  96.             Console.WriteLine();
  97.             Skiers[] gr = new Skiers[12];
  98.             Merge(gr1, gr2, gr);
  99.             Console.WriteLine("Общая таблица результатов:");
  100.             for (int i = 0; i < gr.Length; i++)
  101.                 Console.WriteLine(
  102.                     "{0}. {1}\t Результат {2,4:f2}",
  103.                     i + 1, gr[i].name, gr[i].res);
  104.             Console.ReadKey();
  105.         }
  106.     }
  107.  
  108. /// III.1
  109.     struct Student
  110.     {
  111.         public string name;
  112.         public double[] x;
  113.         public Student(string name0, double[] x0)
  114.         {
  115.             name = name0;
  116.             x = x0;
  117.         }
  118.     }
  119.     struct Group
  120.     {
  121.         public int num;
  122.         public double avrg;
  123.         public Group (Student[] gr, int num0)
  124.         {
  125.             num = num0;
  126.             avrg = 0;
  127.             for (int i = 0; i < gr.Length; i++)
  128.                 for (int j = 0; j < 5; j++)
  129.                 {
  130.                     avrg += gr[i].x[j];
  131.                 }
  132.             avrg /= 5 * gr.Length;
  133.         }
  134.     }
  135.     class Program
  136.     {
  137.         static void Main(string[] args)
  138.         {
  139.             Student[] st1 = new Student[5];
  140.             st1[0] = new Student("Иванов", new double[] { 5.0, 4.0, 5.0, 5.0, 4.0 });
  141.             st1[1] = new Student("Петрова", new double[] { 3.0, 2.0, 4.0, 2.0, 2.0 });
  142.             st1[2] = new Student("Сидоров", new double[] { 5.0, 4.0, 4.0, 4.0, 5.0 });
  143.             st1[3] = new Student("Смирнова", new double[] { 3.0, 4.0, 3.0, 3.0, 4.0 });
  144.             st1[4] = new Student("Кузнецов", new double[] { 2.0, 4.0, 5.0, 3.0, 4.0 });
  145.             Student[] st2 = new Student[5];
  146.             st2[0] = new Student("Соколова", new double[] { 3.0, 2.0, 4.0, 5.0, 4.0 });
  147.             st2[1] = new Student("Попов", new double[] { 4.0, 2.0, 2.0, 5.0, 2.0 });
  148.             st2[2] = new Student("Лебедев", new double[] { 5.0, 2.0, 5.0, 5.0, 5.0 });
  149.             st2[3] = new Student("Иванова", new double[] { 3.0, 2.0, 3.0, 3.0, 4.0 });
  150.             st2[4] = new Student("Новиков", new double[] { 5.0, 2.0, 5.0, 4.0, 4.0 });
  151.             Student[] st3 = new Student[5];
  152.             st3[0] = new Student("Лазарева", new double[] { 3.0, 4.0, 5.0, 5.0, 2.0 });
  153.             st3[1] = new Student("Медведева", new double[] { 4.0, 2.0, 5.0, 4.0, 3.0 });
  154.             st3[2] = new Student("Ершов", new double[] { 5.0, 3.0, 4.0, 4.0, 5.0 });
  155.             st3[3] = new Student("Смирнов", new double[] { 3.0, 5.0, 3.0, 5.0, 4.0 });
  156.             st3[4] = new Student("Орехова", new double[] { 2.0, 5.0, 4.0, 3.0, 4.0 });
  157.             Group[] gr = new Group[3] {
  158.                 new Group(st1, 1), new Group(st2, 2), new Group(st3, 3) };
  159.             double max = 0, p = 0;
  160.             int imax = 0;
  161.             for (int i = 0; i < gr.Length - 1; i++)
  162.             {
  163.                 max = gr[i].avrg;
  164.                 imax = i;
  165.                 for (int j = i + 1; j < gr.Length; j++)
  166.                 {
  167.                     if (gr[j].avrg > max)
  168.                     {
  169.                         max = gr[j].avrg;
  170.                         imax = j;
  171.                     }
  172.                 }
  173.                 Group temp;
  174.                 temp = gr[i];
  175.                 gr[i] = gr[imax];
  176.                 gr[imax] = temp;
  177.             }
  178.             for (int i = 0; i < gr.Length; i++)
  179.             {
  180.                 Console.WriteLine("{0}. Группа {1}\t Средний балл {2}", i + 1, gr[i].num, gr[i].avrg);
  181.             }
  182.             Console.ReadKey();
  183.         }
  184.  
  185.     }
  186.  
  187. /// III.1
  188. /// с классами
  189.  
  190.        class Student
  191.     {
  192.         public string name;
  193.         public double[] x;
  194.         public double avrg;
  195.         public Student(string name0, double[] x0)
  196.         {
  197.             avrg = 0;
  198.             name = name0;
  199.             x = x0;
  200.             for (int i = 0; i < x.Length; i++)
  201.             {
  202.                 avrg += x[i];
  203.             }
  204.             avrg /= 5;
  205.         }
  206.     }
  207.     class Stipend : Student
  208.     {
  209.         public string yn;
  210.         public Stipend(string name0, double[] x0)
  211.             : base(name0, x0)
  212.         {
  213.             if (base.avrg > 4)
  214.                 yn = "да";
  215.             else
  216.                 yn = "нет";
  217.         }
  218.     }
  219.     class Program
  220.     {
  221.         static void Sort(Stipend[] gr)
  222.         {
  223.             for (int i = 0; i < gr.Length - 1; i++)
  224.             {
  225.                 double amax = gr[i].avrg;
  226.                 int imax = i;
  227.                 for (int j = i + 1; j < gr.Length; j++)
  228.                 {
  229.                     if (gr[j].avrg > amax)
  230.                     {
  231.                         amax = gr[j].avrg;
  232.                         imax = j;
  233.                     }
  234.                 }
  235.                 Stipend temp;
  236.                 temp = gr[imax];
  237.                 gr[imax] = gr[i];
  238.                 gr[i] = temp;
  239.             }
  240.         }
  241.         static void Main(string[] args)
  242.         {
  243.             Stipend[] gr1 = new Stipend[5];
  244.             gr1[0] = new Stipend("Иванов", new double[] { 5.0, 4.0, 5.0, 5.0, 4.0 });
  245.             gr1[1] = new Stipend("Петрова", new double[] { 3.0, 2.0, 4.0, 2.0, 2.0 });
  246.             gr1[2] = new Stipend("Сидоров", new double[] { 5.0, 4.0, 4.0, 4.0, 5.0 });
  247.             gr1[3] = new Stipend("Смирнова", new double[] { 3.0, 4.0, 3.0, 3.0, 4.0 });
  248.             gr1[4] = new Stipend("Кузнецов", new double[] { 2.0, 4.0, 5.0, 3.0, 4.0 });
  249.             Sort(gr1);
  250.             Console.WriteLine("Первая группа:");
  251.             for (int i = 0; i < gr1.Length; i++)
  252.             {
  253.                 Console.WriteLine("Фамилия {0}  \tСредний балл {1,4:f2} \tСтипендия {2}",
  254.                     gr1[i].name, gr1[i].avrg, gr1[i].yn);
  255.             }
  256.             Console.WriteLine();
  257.             Stipend[] gr2 = new Stipend[5];
  258.             Console.WriteLine("Вторая группа:");
  259.             gr2[0] = new Stipend("Соколова", new double[] { 3.0, 2.0, 4.0, 5.0, 4.0 });
  260.             gr2[1] = new Stipend("Попов", new double[] { 4.0, 2.0, 4.0, 5.0, 2.0 });
  261.             gr2[2] = new Stipend("Лебедев", new double[] { 5.0, 2.0, 5.0, 5.0, 5.0 });
  262.             gr2[3] = new Stipend("Иванова", new double[] { 3.0, 2.0, 3.0, 3.0, 4.0 });
  263.             gr2[4] = new Stipend("Новиков", new double[] { 5.0, 3.0, 5.0, 4.0, 4.0 });
  264.             Sort(gr2);
  265.             for (int i = 0; i < gr2.Length; i++)
  266.             {
  267.                 Console.WriteLine("Фамилия {0}   \tСредний балл {1,4:f2} \tСтипендия {2}",
  268.                     gr2[i].name, gr2[i].avrg, gr2[i].yn);
  269.             }
  270.             Console.WriteLine();
  271.             Stipend[] gr3 = new Stipend[5];
  272.             Console.WriteLine("Третья группа:");
  273.             gr3[0] = new Stipend("Лазарева", new double[] { 3.0, 4.0, 5.0, 5.0, 2.0 });
  274.             gr3[1] = new Stipend("Медведева", new double[] { 4.0, 2.0, 2.0, 4.0, 3.0 });
  275.             gr3[2] = new Stipend("Ершов", new double[] { 5.0, 3.0, 4.0, 4.0, 5.0 });
  276.             gr3[3] = new Stipend("Смирнов", new double[] { 3.0, 4.0, 3.0, 5.0, 4.0 });
  277.             gr3[4] = new Stipend("Орехова", new double[] { 2.0, 5.0, 4.0, 3.0, 4.0 });
  278.             Sort(gr3);
  279.             for (int i = 0; i < gr3.Length; i++)
  280.             {
  281.                 Console.WriteLine("Фамилия {0}  \tСредний балл {1,4:f2} \tСтипендия {2}",
  282.                     gr1[i].name, gr1[i].avrg, gr1[i].yn);
  283.             }
  284.             Console.ReadKey();
  285.         }
  286.  
  287.     }
  288.  
  289. /// III.6
  290.  
  291.     struct Question
  292.     {
  293.         public string answer;
  294.         public int votes;
  295.         public Question(string a)
  296.         {
  297.             votes = 0;
  298.             answer = a;
  299.         }
  300.     }
  301.     class Program
  302.     {
  303.         static void CountVotes(Question[] q, string a, ref int k, ref double v)
  304.         {
  305.             bool b = true;
  306.             for (int i = 0; i < k; i++)
  307.             {
  308.                 if (a == q[i].answer)
  309.                 {
  310.                     q[i].votes++;
  311.                     v++;
  312.                     b = false;
  313.                     break;
  314.                 }
  315.             }
  316.             if ((a != "-") && b)
  317.             {
  318.                 q[k].answer = a;
  319.                 q[k].votes++;
  320.                 v++;
  321.                 k++;
  322.             }
  323.         }
  324.         static void Sort(Question[] q)
  325.         {
  326.             int max = 0, imax = 0;
  327.             for (int i = 0; i < 5; i++)
  328.             {
  329.                 max = q[i].votes;
  330.                 imax = i;
  331.                 for (int j = i + 1; j < q.Length; j++)
  332.                 {
  333.                     if (q[j].votes > max)
  334.                     {
  335.                         max = q[j].votes;
  336.                         imax = j;
  337.                     }
  338.                 }
  339.                 Question temp;
  340.                 temp = q[i];
  341.                 q[i] = q[imax];
  342.                 q[imax] = temp;
  343.             }
  344.         }
  345.         static void Print(Question[] q, ref double v, StreamWriter sw, string path)
  346.         {
  347.             double p = 0;
  348.             for (int i = 0; i < 5; i++)
  349.             {
  350.                 p = q[i].votes / v;
  351.                 Console.WriteLine("{0}. {1}\t {2:f2}%", i + 1, q[i].answer, p);
  352.                 sw.WriteLine("{0}. {1}\t {2:f2}%", i + 1, q[i].answer, p);
  353.             }
  354.         }
  355.         static void Main(string[] args)
  356.         {
  357.  
  358.             string path = "C:\\Users\\Камила\\Downloads\\jp.txt";
  359.             StreamReader sr = new StreamReader(path,Encoding.GetEncoding(1251));
  360.             string line;
  361.             line = sr.ReadLine();
  362.             int n = int.Parse(line);
  363.             Question[] animal = new Question[n];
  364.             Question[] trait = new Question[n];
  365.             Question[] concept = new Question[n];
  366.             double av = 0, tv = 0, cv = 0;
  367.             int ak = 0, tk = 0, ck = 0;
  368.             while ((line = sr.ReadLine()) != null)
  369.             {
  370.                 string[] answers = line.Split('|');
  371.                 {
  372.                     CountVotes(animal, answers[0], ref ak, ref av);
  373.                     CountVotes(trait, answers[1], ref tk, ref tv);
  374.                     CountVotes(concept, answers[2], ref ck, ref cv);
  375.                 }
  376.             }
  377.             string path1 = "C:\\Users\\Камила\\Downloads\\jp1.txt";
  378.             StreamWriter sw = new StreamWriter(path1);
  379.             Sort(animal);
  380.             Console.WriteLine("5 наиболее частых ответов на 1 вопрос:");
  381.             Print(animal, ref av, sw, path1);
  382.             Sort(trait);
  383.             Console.WriteLine("5 наиболее частых ответов на 2 вопрос:");
  384.             Print(trait, ref tv, sw, path1);
  385.             Sort(concept);
  386.             Console.WriteLine("5 наиболее частых ответов на 3 вопрос:");
  387.             Print(concept, ref cv, sw, path1);
  388.             sw.Close();
  389.             Console.ReadKey();
  390.         }
  391.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement