Advertisement
mrAnderson33

средние арифметическое

Mar 19th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.29 KB | None | 0 0
  1. // флаг -с - считка чисел с консоли
  2. // флаг -f <filename> - считка с файла filename
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8.  
  9. namespace lab04_a_
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             var nums = new string [255] ;
  16.  
  17.             if (args.Length == 0)  nums =Console.ReadLine().Split(new Char[] { ' ' });
  18.             else if (args[0] == "-c") nums = Console.ReadLine().Split(new Char[] { ' ' });
  19.             else if (args[0] == "-f")
  20.             {                
  21.                var file = new System.IO.StreamReader(args[1]);
  22.                 string line;
  23.                 string buf = " ";
  24.                 while ((line = file.ReadLine()) != null) { buf += line; }
  25.                 nums = buf.Split(new Char[] { ' ' });
  26.             }
  27.  
  28.                 int i = 0;
  29.                 double res = 0;
  30.                 foreach (var n in nums)
  31.                 {
  32.                     double temp = 0;
  33.                     double.TryParse(n, out temp);
  34.                     res += temp;
  35.                     if (!string.IsNullOrWhiteSpace(n)) i++;
  36.                 }
  37.                 res /= i;
  38.                 Console.WriteLine(res);
  39.            
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement