aLT22

4semlab4 (C++) KRISTINA'S LAB

May 11th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. class Series {
  7. public:
  8.     virtual int sum(int a0, int n, int rad) = 0;
  9.     virtual int j_count(int j) = 0;
  10.     virtual void show() = 0;
  11. protected:
  12.     vector <int> serie;
  13. };
  14.  
  15. class Linear : public Series {
  16. public:
  17.     Linear() {
  18.         serie.clear();
  19.     }
  20.     Linear(int a0, int n, int rad) {
  21.         for (int i = 0; i < n; i++) {
  22.             serie.push_back(a0 + i*rad);
  23.         }
  24.     }
  25.     int sum(int a0, int n, int rad) {
  26.         int s = 0;
  27.         s = ((a0 + serie[n-1]) / 2) * n;
  28.         return s;
  29.     }
  30.     int j_count(int j) {
  31.         return serie[j - 1];
  32.     }
  33.     void show() {
  34.         cout << "Ваша последовательность: ";
  35.         for (int i = 0; i < serie.size(); i++) {
  36.             cout << serie[i] << ' ';
  37.         }
  38.         cout << endl;
  39.     }
  40. };
  41.  
  42. class Exponential : public Series {
  43. public:
  44.     Exponential() {
  45.         serie.clear();
  46.     }
  47.     Exponential(int a0, int n, int rad) {
  48.         for (int i = 0; i < n; i++) {
  49.             serie.push_back(a0*pow(rad, i));
  50.         }
  51.     }
  52.     int sum(int a0, int n, int rad) {
  53.         int s = 0;
  54.         if (rad == 1) {
  55.             return n*a0;
  56.         } else {
  57.             s = (a0 - a0 * pow(rad, n)) / (1 - rad);
  58.             return s;
  59.         }
  60.     }
  61.     int j_count(int j) {
  62.         return serie[j - 1];
  63.     }
  64.     void show() {
  65.         cout << "Ваша последовательность: ";
  66.         for (int i = 0; i < serie.size(); i++) {
  67.             cout << serie[i] << ' ';
  68.         }
  69.         cout << endl;
  70.     }
  71. };
  72.  
  73. int main() {
  74.     setlocale(LC_ALL, "Russian");
  75.     int n, a, rad;
  76.     cout << "Введите количество элементов прогрессий: ";
  77.     cin >> n;
  78.     cout << "\nВведите первый элемент прогрессии: ";
  79.     cin >> a;
  80.     cout << "\nВведите радиус: ";
  81.     cin >> rad;
  82.     Linear l(a, n, rad);
  83.     Exponential e(a, n, rad);
  84.     char choice;
  85.     label : cout << "Выберите, с какой прогрессией работать (1 - арифметическая, 2 - геомтрическая, 3 - exit) : ";
  86.     cin >> choice;
  87.     if (choice == '1') {
  88.         l.show();
  89.         cout << "Сумма прогрессии равна: " << l.sum(a, n, rad) << endl;
  90.         label_one : cout << "Введите житый элемент, который хотите отобразить: ";
  91.         int j;
  92.         cin >> j;
  93.         if (j > n) {
  94.             cout << "Неверный ввод!";
  95.             cout << endl;
  96.             goto label_one;
  97.         } else {
  98.             cout << l.j_count(j) << endl;
  99.         }
  100.         goto label;
  101.     } else if (choice == '2') {
  102.         e.show();
  103.         cout << "Сумма прогрессии равна: " << e.sum(a, n, rad) << endl;
  104.         label_two : cout << "Введите житый элемент, который хотите отобразить: ";
  105.         int j;
  106.         cin >> j;
  107.         if (j > n) {
  108.             cout << "Неверный ввод!";
  109.             cout << endl;
  110.             goto label_two;
  111.         } else {
  112.             cout << e.j_count(j) << endl;
  113.         }
  114.         goto label;
  115.     } else {
  116.         system("pause");
  117.         return 0;
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment