Guest User

Untitled

a guest
Mar 11th, 2017
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.86 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace FirstLaba
  5. {
  6.     class Program
  7.     {
  8.         static void Create(string f)
  9.         {
  10.             Console.WriteLine("Введите кол-во чисел в файле");
  11.             int n = int.Parse(Console.ReadLine());
  12.             using (BinaryWriter writer = new BinaryWriter(File.Open(f, FileMode.OpenOrCreate)))
  13.             {
  14.                 for (int i = 0; i < n; i++)
  15.                 {
  16.                     int a = int.Parse(Console.ReadLine());
  17.                     writer.Write(a);
  18.                 }
  19.             }
  20.         }
  21.         static void Min(string f1,ref int x,ref int y,ref int z)
  22.         {
  23.             using (BinaryReader reader = new BinaryReader(File.Open(f1, FileMode.Open)))
  24.             {
  25.                 x = 0;
  26.                 y = 0;
  27.                 z = 0;
  28.                 while (reader.BaseStream.Position != reader.BaseStream.Length)
  29.                 {
  30.                     int b = reader.ReadInt32();
  31.                     if (b == 0)
  32.                     {
  33.                         x++;
  34.                     }
  35.                     if (b > 0)
  36.                     {
  37.                         y++;
  38.                     }
  39.                     if (b < 0)
  40.                     {
  41.                         z++;
  42.                     }
  43.                 }
  44.                
  45.             }
  46.         }
  47.         static void Min1(string f2, int x, int y, int z)
  48.         {
  49.             using (BinaryReader reader = new BinaryReader(File.Open(f2, FileMode.Open)))
  50.             {
  51.                 int x1 = 0;
  52.                 int y1 = 0;
  53.                 int z1 = 0;
  54.                 while (reader.BaseStream.Position != reader.BaseStream.Length)
  55.                 {
  56.                     int c = reader.ReadInt32();
  57.                     if (c == 0)
  58.                     {
  59.                         x1++;
  60.                     }
  61.                     if (c > 0)
  62.                     {
  63.                         y1++;
  64.                     }
  65.                     if (c < 0)
  66.                     {
  67.                         z1++;
  68.                     }
  69.                 }
  70.                 bool nul = false;
  71.                 bool plus = false;
  72.                 bool minus = false;
  73.  
  74.                 if (x1 > x)
  75.                 {
  76.                     nul = true;
  77.                 }
  78.                 if  (y1 > y)
  79.                 {
  80.                     plus = true;
  81.                 }
  82.                 if (z1 > z)
  83.                 {
  84.                     minus = true;
  85.                 }
  86.                 if (x1 == x)
  87.                 {
  88.                     Console.WriteLine("Нулевых - одинаковое кол-во");
  89.                 }
  90.                 else
  91.                 {
  92.                     if (nul == true)
  93.                     {
  94.                         Console.WriteLine("Нулевых больше во втором файле, их {0} шт.", x1);
  95.                     }
  96.                     else
  97.                     {
  98.                         Console.WriteLine("Нулевых больше в первом файле, их {0} шт.", x);
  99.                     }
  100.                 }
  101.                 if (y1 == y)
  102.                 {
  103.                     Console.WriteLine("Положительных - одинаковое кол-во");
  104.                 }
  105.                 else
  106.                 {
  107.                     if (plus == true)
  108.                     {
  109.                         Console.WriteLine("Положительных больше во втором файле, их {0} шт.", y1);
  110.                     }
  111.                     else
  112.                     {
  113.                         Console.WriteLine("Положительных больше в первом файле, их {0} шт.", y);
  114.                     }
  115.                 }
  116.                 if (z1 == z)
  117.                 {
  118.                     Console.WriteLine("Отрицательных - одинаковое кол-во");
  119.                 }
  120.                     if (minus == true)
  121.                     {
  122.                         Console.WriteLine("Отрицательных больше во втором файле, их {0} шт.", z1);
  123.                     }
  124.                     else
  125.                     {
  126.                         Console.WriteLine("Отрицательных больше в первом файле, их {0} шт.", z);
  127.                     }
  128.             }
  129.         }
  130.         static void Main()
  131.         {
  132.  
  133.             try
  134.             {
  135.                 string f1 = @"C:\c\f1.dat";
  136.                 string f2 = @"C:\c\f2.dat";
  137.                 int x = 0;
  138.                 int y = 0;
  139.                 int z = 0;
  140.                 Create(f1);
  141.                 Min(f1,ref x,ref y,ref z);
  142.                 Create(f2);
  143.                 Min1(f2, x, y, z);
  144.             }
  145.             catch (Exception e)
  146.             {
  147.                 Console.WriteLine(e);
  148.             }
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment