Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Polinom.h
- #pragma once
- #include <iostream>
- using namespace std;
- class Polinom {
- double *a;
- int s;
- public:
- Polinom();
- Polinom(int s);
- ~Polinom();
- Polinom(const Polinom &x);
- void postaviK(int *s, double *niz, int n);
- void stampaj();
- friend Polinom mul(Polinom &a, Polinom &b);
- void operator=(const Polinom& x);
- };
- //Polinom.cpp
- #include "Polinom.h"
- Polinom::Polinom(int s) {
- this->s = s;
- a = new double[this->s + 1]();
- }
- Polinom::Polinom() : Polinom(2) {}
- Polinom::~Polinom() {
- if (a) {
- delete[] a;
- }
- }
- Polinom::Polinom(const Polinom &x) {
- s = x.s;
- a = new double[s + 1]();
- for (int i = 0; i <= s; i++)
- this->a[i] = x.a[i];
- }
- void Polinom::postaviK(int *s, double *niz, int n) {
- for (int i = 0; i < n; i++) {
- if (s[i] <= this->s && s[i] >= 0)
- a[s[i]] = niz[i];
- }
- }
- void Polinom::stampaj() {
- cout.precision(10);
- for (int i = s; i > 0; i--) {
- if (a[i] != 0)
- cout << " " << showpos << a[i] << " * x^" << noshowpos << i;
- }
- if (a[0] != 0)
- cout << " " << showpos << a[0];
- cout << endl;
- }
- Polinom mul(Polinom &a, Polinom &b) {
- Polinom x(a.s + b.s);
- for (int i = 0; i < a.s + 1; i++) {
- for (int j = 0; j < b.s + 1; j++) {
- x.a[i + j] = a.a[i] * b.a[j] + x.a[i + j];
- }
- }
- return x;
- }
- void Polinom::operator=(const Polinom &x) {
- if (a) {
- delete[] this->a;
- }
- s = x.s;
- a = new double[s + 1]();
- for (int i = 0; i <= s; i++)
- this->a[i] = x.a[i];
- }
- //main
- void main() {
- Polinom x(4);
- int exp[3], e = 3;
- double niz[3];
- for (int i = 0; i < 3; i++) {
- cin >> exp[i] >> niz[i];
- }
- x.postaviK(exp, niz, e);
- x.stampaj();
- Polinom g;
- g = x;
- Polinom z;
- z = mul(x, g);
- z.stampaj();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement