Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // h
- #include "Polinom.h"
- void main() {
- Polinom x(3);
- Polinom y(4);
- x.Coef(1, 2.34);
- x.Coef(2, 3.35);
- x.Coef(3, 15.89);
- y.Coef(1, 100);
- y.Coef(0, 52.11);
- y.Coef(4, 67.22);
- x.stampaj();
- y.stampaj();
- Polinom z = saberi(x, y);
- z.stampaj();
- }
- // main
- #pragma once
- #include <iostream>
- using namespace std;
- class Polinom{
- int stepen;
- double *a;
- public:
- Polinom();
- Polinom(int s);
- ~Polinom();
- Polinom(const Polinom &p);
- void Coef(int exp, double coef);
- friend Polinom saberi(Polinom &a, Polinom &b);
- void stampaj();
- void operator=(const Polinom &p);
- };
- // cpp
- #include "Polinom.h"
- Polinom::Polinom(int s) {
- stepen = s;
- a = new double[stepen + 1]();
- }
- Polinom::Polinom() {
- stepen = 0;
- a = 0;
- }
- Polinom::~Polinom() {
- if (a)
- delete[] a;
- }
- Polinom::Polinom(const Polinom &p) {
- stepen = p.stepen;
- a = new double[stepen + 1]();
- for (int i = 0; i < stepen + 1; i++)
- a[i] = p.a[i];
- }
- void Polinom::Coef(int exp, double coef) {
- if (exp > stepen || exp < 0) {
- cout << "Eksponent mora biti veci od 0 i manji li jednak " << endl;
- return;
- }
- a[exp] = coef;
- }
- Polinom saberi(Polinom &a, Polinom &b) {
- int s;
- a.stepen < b.stepen ? s = b.stepen : s = a.stepen;
- Polinom vrati(s);
- for (int i = 0; i < a.stepen + 1; i++) {
- vrati.a[i] = a.a[i];
- }
- for (int i = 0; i < b.stepen + 1; i++) {
- vrati.a[i] = vrati.a[i] + b.a[i];
- }
- return vrati;
- }
- void Polinom::stampaj() {
- for (int i = stepen; i > 0; i--) {
- if (a[i] != 0)
- cout << showpos << a[i] << "*x^" << noshowpos << i << " ";
- }
- if (a[0] != 0)
- cout << showpos << a[0];
- cout << endl;
- }
- void Polinom::operator=(const Polinom &p) {
- this->~Polinom();
- stepen = p.stepen;
- a = new double[stepen + 1]();
- for (int i = 0; i < stepen + 1; i++)
- a[i] = p.a[i];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement