Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <string>
- #include <unistd.h>
- #include <windows.h>
- #include "stdafx.h"
- /*
- * bigNum v0.2. - operacje na DUZYCH liczbach
- *
- * author: Jakub Ziółkowski (jakub.ziolkowski@hotmail.com)
- */
- class bigNum {
- public:
- std::string z; /* Wynik */
- unsigned char r; /* Reszta */
- double time; /* Licznik czasu obliczen */
- void clean(){ this->z = ""; this->r = 0; }; /* Czysci pamiec */
- /* czas CPU w usec */
- double timer(){
- timeval t; gettimeofday(&t, NULL);
- return (double)t.tv_sec+(t.tv_usec/1000000.0);
- }
- /* dodawanie 2ch liczb calkowitych dodatnich */
- std::string plus(const std::string& a, const std::string& b) {
- this->clean();
- double start = timer();
- int a_s = a.size(), b_s = b.size(),
- limit = a_s > b_s ? a_s : b_s,
- mov_a = a_s-1, mov_b = b_s-1;
- this->z.resize(limit + 1);
- for (int i = limit; i >= 0; i--){
- if (mov_a >= 0)this->r += a[mov_a--] - '0';
- if (mov_b >= 0)this->r += b[mov_b--] - '0';
- this->z[i] = this->r % 10 + '0';
- this->r /= 10;
- }
- this->time += this->timer()-start;
- return (this->z[0] == '0')?this->z.substr(1):this->z;
- }
- /* mnozenie 2ch liczb calkowitych dodatnich */
- std::string mul(const std::string& a, const std::string& b)
- {
- this->clean();
- double start = timer();
- int a_s = a.size(), b_s = b.size(),
- o_l = a_s + b_s;//,
- // mov_a = a_s - 1, mov_b = b_s - 1;
- this->z.resize(o_l);
- for (int i = 0; i < o_l; i++) this->z[i] = '0';
- for (int i = a_s - 1; i >=0; i--) {
- this->r = 0;
- for (int j = b_s - 1; j >= 0; j--){
- this->r += this->z[i + j + 1] - '0';
- this->r += (a[i] - '0') * (b[j] - '0');
- this->z[i + j + 1] = this->r % 10 + '0';
- this->r /= 10;
- }
- this->r += this->z[i] - '0';
- this->z[i] = this->r % 10 + '0';
- }
- this->time += this->timer()-start;
- return (this->z[0] == '0')?this->z.substr(1):this->z;
- }
- /* Potega */
- std::string pow(const std::string a, long long int x){
- std::string out = "1";
- while(x--) out = this->mul(out,a);
- return out;
- }
- /* Silnia z liczby calkowitej */
- std::string silnia(int n){
- std::string _r = "1", _m = "2";
- for (int i = 2; i <= n; i++) {
- _r = this->mul(_r, _m);
- _m = this->plus(_m, "1");
- if(i%100 == 0) std::cout << "n~=" << i << "\r"; // DEBUG
- }
- return _r;
- }
- };
- int main(){
- int x;
- bigNum* handler = new bigNum();
- std::string WYNIK;
- printf("* Silnia * (N! = x),\nn=");
- //std::cout << "n! = x podaj n\r";
- std::cin >> x;
- WYNIK = handler->silnia(x);
- std::cout << WYNIK << std::endl;
- handler->clean();
- printf("\n %d cyfr\nCzas procesora): %.6lf\n",WYNIK.size(), handler->time);
- WYNIK = "";
- std::string a,b;
- printf("* Iloczyn (AxB = x),\nA=");
- std::cin >> a;
- printf("B=");
- std::cin >> b;
- WYNIK = handler->mul(a,b);
- std::cout << WYNIK << std::endl;
- delete handler;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement