Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- class Series {
- public:
- virtual int sum(int a0, int n, int rad) = 0;
- virtual int j_count(int j) = 0;
- virtual void show() = 0;
- protected:
- vector <int> serie;
- };
- class Linear : public Series {
- public:
- Linear() {
- serie.clear();
- }
- Linear(int a0, int n, int rad) {
- for (int i = 0; i < n; i++) {
- serie.push_back(a0 + i*rad);
- }
- }
- int sum(int a0, int n, int rad) {
- int s = 0;
- s = ((a0 + serie[n-1]) / 2) * n;
- return s;
- }
- int j_count(int j) {
- return serie[j - 1];
- }
- void show() {
- cout << "Ваша последовательность: ";
- for (int i = 0; i < serie.size(); i++) {
- cout << serie[i] << ' ';
- }
- cout << endl;
- }
- };
- class Exponential : public Series {
- public:
- Exponential() {
- serie.clear();
- }
- Exponential(int a0, int n, int rad) {
- for (int i = 0; i < n; i++) {
- serie.push_back(a0*pow(rad, i));
- }
- }
- int sum(int a0, int n, int rad) {
- int s = 0;
- if (rad == 1) {
- return n*a0;
- } else {
- s = (a0 - a0 * pow(rad, n)) / (1 - rad);
- return s;
- }
- }
- int j_count(int j) {
- return serie[j - 1];
- }
- void show() {
- cout << "Ваша последовательность: ";
- for (int i = 0; i < serie.size(); i++) {
- cout << serie[i] << ' ';
- }
- cout << endl;
- }
- };
- int main() {
- setlocale(LC_ALL, "Russian");
- int n, a, rad;
- cout << "Введите количество элементов прогрессий: ";
- cin >> n;
- cout << "\nВведите первый элемент прогрессии: ";
- cin >> a;
- cout << "\nВведите радиус: ";
- cin >> rad;
- Linear l(a, n, rad);
- Exponential e(a, n, rad);
- char choice;
- label : cout << "Выберите, с какой прогрессией работать (1 - арифметическая, 2 - геомтрическая, 3 - exit) : ";
- cin >> choice;
- if (choice == '1') {
- l.show();
- cout << "Сумма прогрессии равна: " << l.sum(a, n, rad) << endl;
- label_one : cout << "Введите житый элемент, который хотите отобразить: ";
- int j;
- cin >> j;
- if (j > n) {
- cout << "Неверный ввод!";
- cout << endl;
- goto label_one;
- } else {
- cout << l.j_count(j) << endl;
- }
- goto label;
- } else if (choice == '2') {
- e.show();
- cout << "Сумма прогрессии равна: " << e.sum(a, n, rad) << endl;
- label_two : cout << "Введите житый элемент, который хотите отобразить: ";
- int j;
- cin >> j;
- if (j > n) {
- cout << "Неверный ввод!";
- cout << endl;
- goto label_two;
- } else {
- cout << e.j_count(j) << endl;
- }
- goto label;
- } else {
- system("pause");
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment