Advertisement
desdemona

miss lab1 znów

Oct 6th, 2015
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <gmpxx.h>
  3.  
  4. #include <iostream>
  5. #include <iomanip>
  6. #include <string>
  7. #include <vector>
  8. #include <sstream>
  9.  
  10. std::string fixed(mpf_class x, int prec)
  11. {
  12.    
  13.     std::ostringstream ss;
  14.     ss << std::fixed;
  15.     ss << std::setprecision(prec);
  16.     ss << x;
  17.     std::string str = ss.str();
  18.     return str.substr(0, str.find_last_not_of('0') + 1);
  19. }
  20.  
  21. int main(int argc, char **argv)
  22. {
  23.  
  24.     int d = 20; //dokladnosc
  25.     int n = 0; //ilosc liczb
  26.     int p = 1; //okres
  27.    
  28.     if(argc > 1)
  29.     {
  30.         d = atoi(argv[1]);
  31.     }
  32.     double bits_per_digit = 3.32192809488736234789; //log2(10)
  33.     //mpf_set_default_prec((21+d)*bits_per_digit + 1);
  34.     //bo d dotyczy tego co po kropce, a z przodu moze byc jeszcze jakies ok. 20 cyfr dla 2^64
  35.     //a jednak wincyj niz 20, jak w wariancji w przykladzie 4, jest jakies 40 przed kropka..
  36.     //no i jak to oszacowac? moze po prostu od razu 100, po co sie rozdrabniac?
  37.     mpf_set_default_prec((100+d)*bits_per_digit + 1);
  38.    
  39.     std::string sxn;
  40.     mpf_class xn;xn = 0;
  41.     mpf_class xn_sum; xn_sum = 0;
  42.     mpf_class xn_2sum; xn_2sum = 0;
  43.    
  44.     std::vector<float> XXX;
  45.     float closeInaf;
  46.     while(std::cin >> sxn)
  47.     {
  48.         xn = sxn;
  49.         xn_sum += xn;
  50.         xn_2sum += (xn*xn);
  51.         closeInaf = (float)xn.get_d();
  52.         XXX.push_back(closeInaf);
  53.         if(n!= 0 && XXX[n-p] != closeInaf)
  54.         {
  55.             p++;
  56.         }          
  57.         n+=1;      
  58.     }
  59.    
  60.     mpf_class avg = xn_sum / n;
  61.     mpf_class vari = (xn_2sum / n) - avg*avg;
  62.  
  63.     std::cout.setf(std::ios_base::fixed);
  64.  
  65.     std::cout << fixed(avg, d) << std::endl;
  66.     //std::cout << avg.get_str(exp, 10, (size_t)d) << std::endl;
  67.     std::cout << fixed(vari, d) << std::endl;
  68.     std::cout << p << "\n";
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement