Advertisement
maxlarin2

DashaClass

Mar 30th, 2014
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.57 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 Consolee
  8. {
  9.     class series
  10.     {
  11.         private int i;
  12.         progression[] massive = new progression[4];
  13.         public series(bool is_linear, params int[] files)
  14.         {
  15.             foreach (int elem in files)
  16.             {
  17.                 if (is_linear)
  18.                 {
  19.                     massive[i] = new linear(elem);
  20.                     i++;
  21.                 }
  22.                 else
  23.                 {
  24.                     massive[i] = new exponential(elem);
  25.                     i++;
  26.                 }
  27.             }
  28.         }
  29.         public void cons()
  30.         {
  31.             foreach (progression elem in massive)
  32.             {
  33.                 elem.print();
  34.             }
  35.         }
  36.     }
  37.     abstract class progression
  38.     {
  39.         protected short a1, d, position, an, n;
  40.         public progression(int first)
  41.         {
  42.             this.a1 = (short)first;
  43.             Console.WriteLine("Введите номер последнего суммируемого элемена:");
  44.             while (!(Int16.TryParse(Console.ReadLine(), out n)))
  45.             {
  46.                 Console.WriteLine("Число!");
  47.             }
  48.             Console.WriteLine("Введите значение последнего суммируемого элемена:");
  49.             while (!(Int16.TryParse(Console.ReadLine(), out an)))
  50.             {
  51.                 Console.WriteLine("Число!");
  52.             }
  53.             Console.WriteLine("Введите номер искомого элемента:");
  54.             while (!(Int16.TryParse(Console.ReadLine(), out position)))
  55.             {
  56.                 Console.WriteLine("Число!");
  57.             }
  58.             Console.WriteLine("Введите разность прогрессии:");
  59.             while (!(Int16.TryParse(Console.ReadLine(), out d)))
  60.             {
  61.                 Console.WriteLine("Число!");
  62.             }
  63.         }
  64.         public abstract double get();
  65.         public abstract double sum();
  66.         public abstract void print();
  67.     }
  68.     class linear : progression
  69.     {
  70.         public override double get()
  71.         {
  72.             return a1 + (position - 1) * d;
  73.         }
  74.         public override double sum()
  75.         {
  76.             return (a1 + an) * n / 2;
  77.         }
  78.         public linear(int first)
  79.             : base(first)
  80.         {
  81.         }
  82.         public override void print()
  83.         {
  84.             Console.WriteLine("Сумма арифметической прогрессии: {0}, Значение {1} элемента равно {2} ", sum(), position, get());
  85.         }
  86.     }
  87.     class exponential : progression
  88.     {
  89.         public override double get()
  90.         {
  91.             return a1 * Math.Pow(d, (position - 1));
  92.         }
  93.         public override double sum()
  94.         {
  95.             return (a1 + an) * n / 2;
  96.         }
  97.         public exponential(int first)
  98.             : base(first)
  99.         {
  100.  
  101.         }
  102.         public override void print()
  103.         {
  104.             Console.WriteLine("Сумма геометрической прогрессии: {0}, Значение {1} элемента равно {2} ", sum(), position, get());
  105.         }
  106.     }
  107.     class maindd
  108.     {
  109.         static void Main(string[] args)
  110.         {
  111.             series a = new series(true, 1, 4, 5, 6);
  112.             series b = new series(false, 1, 4, 5, 6);
  113.             a.cons();
  114.             b.cons();
  115.             Console.ReadKey();
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement