Advertisement
desdemona

miss lab1 - wersja z wypisywaniem

Oct 6th, 2015
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 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.     mp_exp_t exp;
  29.    
  30.     if(argc > 1)
  31.     {
  32.         d = atoi(argv[1]);
  33.     }
  34.     double bits_per_digit = 3.32192809488736234789; //log2(10)
  35.     //mpf_set_default_prec((21+d)*bits_per_digit + 1);
  36.     //bo d dotyczy tego co po kropce, a z przodu moze byc jeszcze jakies ok. 20 cyfr dla 2^64
  37.     //a jednak wincyj niz 20, jak w wariancji w przykladzie 4, jest jakies 40 przed kropka..
  38.     //no i jak to oszacowac? moze po prostu od razu 100, po co sie rozdrabniac?
  39.     mpf_set_default_prec((100+d)*bits_per_digit + 1);
  40.     std::string sxn;
  41.     mpf_class xn;xn = 0;
  42.     mpf_class xn_sum; xn_sum = 0;
  43.     mpf_class xn_2sum; xn_2sum = 0;
  44.    
  45.     std::vector<float> XXX;
  46.     float closeInaf;
  47.     while(std::cin >> sxn)
  48.     {
  49.         xn = sxn;
  50.         xn_sum += xn;
  51.         xn_2sum += (xn*xn);
  52.         closeInaf = (float)xn.get_d();
  53.         XXX.push_back(closeInaf);
  54.         if(n!= 0 && XXX[n-p] != closeInaf)
  55.         {
  56.             p++;
  57.         }          
  58.         n+=1;      
  59.     }
  60.    
  61.     mpf_class avg = xn_sum / n;
  62.     mpf_class vari = (xn_2sum / n) - avg*avg;
  63.    
  64.     //std::cout.precision(d+1);
  65.     std::cout.setf(std::ios_base::fixed);
  66.  
  67.     std::cout << fixed(avg, d) << std::endl;
  68.     //std::cout << avg.get_str(exp, 10, (size_t)d) << std::endl;
  69.     std::cout << fixed(vari, d) << std::endl;
  70.     std::cout << p << "\n";
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement