Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2021
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3.  
  4. using namespace std;
  5.  
  6. static int offsets[2303];
  7.  
  8. static int merge(int position, int mask) {
  9.     return position << 8 | mask;
  10. }
  11.  
  12. static bool is_bit_set(int mask, int position) {
  13.     return ((mask >> position) & 1) == 1;
  14. }
  15.  
  16. static int count_bits_left(int mask, int position) {
  17.     int result = 0;
  18.     for (int pos = 7; pos > position; pos--) {
  19.         if (is_bit_set(mask, pos)) {
  20.             result++;
  21.         }
  22.     }
  23.     return result;
  24. }
  25.  
  26. static void fill_offsets() {
  27.     for (int position = 0; position < 8; position++) {
  28.         for (int mask = 0; mask < 256; mask++) {
  29.             int index = merge(position, mask);
  30.             int value = count_bits_left(mask, position);
  31.             offsets[index] = value;
  32.         }
  33.     }
  34. }
  35.  
  36. class Polynom {
  37.    
  38.     private:
  39.         int mask = 0;
  40.         double* koeffs;
  41.    
  42.     public:
  43.         Polynom (double , double , double , double , double , double , double , double );
  44.         double get_koeff(int);
  45.         void print(void);
  46.    
  47. };
  48.  
  49. Polynom::Polynom (double k7, double k6, double k5, double k4, double k3, double k2, double k1, double k0) {
  50.     std::list<double> ks;
  51.     int size = 0;
  52.     if (k7 != 0) {
  53.         mask |= (1 << 7);
  54.         ks.push_back(k7);
  55.     }
  56.     if (k6 != 0) {
  57.         mask |= (1 << 6);
  58.         ks.push_back(k6);
  59.     }
  60.     if (k5 != 0) {
  61.         mask |= (1 << 5);
  62.         ks.push_back(k5);
  63.     }
  64.     if (k4 != 0) {
  65.         mask |= (1 << 4);
  66.         ks.push_back(k4);
  67.     }
  68.     if (k3 != 0) {
  69.         mask |= (1 << 3);
  70.         ks.push_back(k3);
  71.     }
  72.     if (k2 != 0) {
  73.         mask |= (1 << 2);
  74.         ks.push_back(k2);
  75.     }
  76.     if (k1 != 0) {
  77.         mask |= (1 << 1);
  78.         ks.push_back(k1);
  79.     }
  80.     if (k0 != 0) {
  81.         mask |= (1 << 0);
  82.         ks.push_back(k0);
  83.     }
  84.     koeffs = new double[ks.size()];
  85.     int i = 0;
  86.     for (double const &k: ks) {
  87.         koeffs[i++] = k;
  88.     }
  89. }
  90.  
  91. double Polynom::get_koeff(int position) {
  92.     if (!is_bit_set(mask, position)) {
  93.         return 0.0;
  94.     } else {
  95.         int idx = merge(position, mask);
  96.         int leftBits = offsets[idx];
  97.         return koeffs[leftBits];
  98.     }
  99. }
  100.  
  101. void Polynom::print(){
  102.     for (int i = 7; i >= 0; i--){
  103.         cout << get_koeff(i) << "*x^" << i;
  104.         if (i > 0){
  105.             cout << " + ";
  106.         }
  107.     }
  108.     cout << "\n";
  109. }
  110.  
  111. int main()
  112. {
  113.     fill_offsets();
  114.    
  115.     Polynom* p = new Polynom(1, 2, 3, 4, 5, 6, 7, 8);
  116.     p->print();
  117.     //1*x^7 + 2*x^6 + 3*x^5 + 4*x^4 + 5*x^3 + 6*x^2 + 7*x^1 + 8*x^0
  118.    
  119.     Polynom* p1 = new Polynom(1, 0, 0, 0, 0, 6, 0, 8);
  120.     p1->print();
  121.     //1*x^7 + 0*x^6 + 0*x^5 + 0*x^4 + 0*x^3 + 6*x^2 + 0*x^1 + 8*x^0                                                                                  
  122.    
  123.     return 0;
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement