Advertisement
mrniceguy

bigNumbers.cpp

Dec 16th, 2013
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.35 KB | None | 0 0
  1. //
  2. //  bigNumbers.h
  3. //  Faku
  4. //
  5. //  Created by Lukas Adam on 10.12.13.
  6. //  Copyright (c) 2013 Lukas Adam. All rights reserved.
  7. //
  8. #include <cmath>
  9. #include <iostream>
  10.  
  11. #ifndef Faku_bigNumbers_h
  12. #define Faku_bigNumbers_h
  13.  
  14. class bigNumbers{
  15. private:
  16.     int *val = new int[100000];
  17.     int index;
  18.     void overloadPositiv(const int& pos);
  19.     void overloadNegativ(const int& pos);
  20.     friend std::ostream& operator << (std::ostream &cout, bigNumbers num);
  21. public:
  22.     bigNumbers(){
  23.         for(int i = 0; i < 100000; i++){
  24.             val[i] = 0;
  25.         }
  26.         index = 0;
  27.     }
  28.     bigNumbers(int num){
  29.         *this = num;
  30.     }
  31.    
  32.     ~bigNumbers(){
  33.         delete []val;
  34.     }
  35.     int& getIndex();
  36.     int& getValue(const int& n);
  37.     bigNumbers& dec();
  38.     bigNumbers operator * (const bigNumbers& num);
  39.     bigNumbers operator * (int num);
  40.     bigNumbers operator + (const bigNumbers& num);
  41.     bigNumbers& operator + (const int& num);
  42.     bigNumbers& operator - (const int& num);
  43.     std::ostream& operator << (std::ostream &cout);
  44.     bigNumbers& operator = (const bigNumbers& num);
  45.     bigNumbers& operator = (int num);
  46. };//Klassen ende
  47.  
  48. #endif
  49. //
  50. //  bigNumbers.cpp
  51. //  Faku
  52. //
  53. //  Created by Lukas Adam on 16.12.13.
  54. //  Copyright (c) 2013 Lukas Adam. All rights reserved.
  55. //
  56.  
  57. #include "bigNumbers.h"
  58. #include <iostream>
  59.  
  60. //Gets the index of the bigNumber, which presents the number of values.
  61. int& bigNumbers::getIndex(){
  62.     return this->index;
  63. }
  64.  
  65. //Gets a specific Value of the Array
  66. int& bigNumbers::getValue(const int& n){
  67.     return this->val[n];
  68. }
  69.  
  70. //After a operation this Method will correct the positiv Values which are higher than 10
  71. void bigNumbers::overloadPositiv(const int& pos){
  72.     for(int i = pos; i <= this->index; i++){
  73.         if(this->val[i] > 9){
  74.             this->val[i + 1] = this->val[i + 1] + (this->val[i] / 10);
  75.             this->val[i] = this->val[i] % 10;
  76.             if(i == this->index){
  77.                 this->index++;
  78.             } else {
  79.                 overloadPositiv(--i);
  80.             }
  81.         }
  82.     }
  83. }
  84.  
  85. //After a operation this Method will correct the negativ Values
  86. void bigNumbers::overloadNegativ(const int &pos){
  87.     for(int i = 0; i <= this->index; i++){
  88.         if(this->val[i] < 0){
  89.             this->val[i] += 10;
  90.             this->val[i + 1] -= 1;
  91.             overloadNegativ(--i);
  92.         } else if(!this->val[i] && i == this->index && i){
  93.             index--;
  94.         }
  95.     }
  96. }
  97.  
  98. //Reduces the Value of the bigNumbers about the integer Value 1
  99. bigNumbers& bigNumbers::dec(){
  100.     if(this->val[0] > 0){
  101.         this->val[0]--;
  102.     } else {
  103.         for(int i = 1; i <= this->index; i++){
  104.             if(this->val[i] > 0){
  105.                 this->val[i]--;
  106.                 for(int j = i - 1; j >= 0; j--) val[j] = 9;
  107.                 if (this->val[i] == 0 && i == this->index) {
  108.                     this->index--;
  109.                 }
  110.                 return *this;
  111.             }
  112.         }
  113.     }
  114.     return *this;
  115. }
  116.  
  117. //To initiate a bigNumber with a bigNumbers with a nother bigNumbers
  118. bigNumbers& bigNumbers::operator = (const bigNumbers& num){
  119.     for(int i = 0; i <= num.index; i++){
  120.         this->val[i] = num.val[i];
  121.     }
  122.     this->index = num.index;
  123.    
  124.     return *this;
  125. }
  126.  
  127. //To initiate a bigNumber with a bigNumbers with a Integer Number
  128. bigNumbers& bigNumbers::operator = (int num){
  129.     int count = 0;
  130.     while(num > 9){
  131.         this->val[count] = num % 10;
  132.         num = num / 10;
  133.         count++;
  134.     }
  135.     this->val[count] = num;
  136.     this->index = count;
  137.    
  138.     return *this;
  139. }
  140.  
  141. //Addition of two bigNumbers
  142. bigNumbers bigNumbers::operator + (const bigNumbers& num){
  143.     bigNumbers erg;
  144.     int tempIndex = this->index > erg.index ? this->index : erg.index;
  145.     for(int i = 0; i <= tempIndex; i++){
  146.         erg.val[i] = this->val[i] + num.val[i];
  147.     }
  148.     erg.index = tempIndex;
  149.     erg.overloadPositiv(0);
  150.    
  151.     return erg;
  152. }
  153. //Addition of a bigNumber and a Integer
  154. bigNumbers& bigNumbers::operator + (const int& num){
  155.     val[0] += num;
  156.     overloadPositiv(0);
  157.    
  158.     return *this;
  159. }
  160.  
  161. bigNumbers& bigNumbers::operator - (const int& num){
  162.     this->val[0] -= num;
  163.     overloadNegativ(0);
  164.    
  165.     return *this;
  166. }
  167.  
  168. //Multiplikation with other bigNumbers
  169. bigNumbers bigNumbers::operator * (const bigNumbers& num){
  170.     bigNumbers erg;
  171.     for (int i = 0; i <= this->index; i++) {
  172.         for (int j = 0; j <= num.index; j++) {
  173.             erg.val[i+j] = erg.val[i+j] + (this->val[i] * num.val[j]);
  174.             if(erg.index < (i + j)){
  175.                 erg.index = i + j;
  176.             }
  177.         }
  178.     }
  179.     erg.overloadPositiv(0);
  180.    
  181.     return erg;
  182. }
  183.  
  184. //Multiplikation with Integer Values
  185. bigNumbers bigNumbers::operator * (int num){
  186.     bigNumbers erg;
  187.     for(int i = 0; i <= this->index; i++){
  188.         erg.val[i] = this->val[i] * num;
  189.     }
  190.     erg.index = this->index;
  191.     erg.overloadPositiv(0);
  192.    
  193.     return erg;
  194. }
  195.  
  196. //For normal use of the << operator, to print bigNumbers
  197. std::ostream& operator<<(std::ostream &cout, bigNumbers num){
  198.     std::cout << "Value: ";
  199.     for(int i = num.index; i >= 0; i--){
  200.         std::cout << num.val[i] << " ";
  201.     }
  202.     std::cout << " ,Index: " << num.index<< std::endl;
  203.    
  204.     return cout;
  205. }
  206.  
  207. //
  208. //  main.cpp
  209. //  Faku
  210. //
  211. //  Created by Lukas Adam on 10.12.13.
  212. //  Copyright (c) 2013 Lukas Adam. All rights reserved.
  213. //
  214.  
  215. #include <iostream>
  216. #include "bigNumbers.h"
  217. #include <time.h>
  218.  
  219. //Fakulty of a bigNumber, waste of memory
  220. bigNumbers faku(bigNumbers &num){
  221.     //std::cout << num << std::endl;
  222.     if((num.getIndex() == 0) && (num.getValue(0) == 1)){
  223.         return 1;
  224.     } else {
  225.         bigNumbers tmp = num;
  226.         return tmp * faku(num - 1);
  227.     }
  228. }
  229. //fakulty of an integer, returns bigNumber
  230. bigNumbers fakuIterate(const unsigned int& num){
  231.     bigNumbers res = 1;
  232.     for(int i = 2; i <= num; i++){
  233.         std::cout << i << std::endl;
  234.         res = res*i;
  235.     }
  236.    
  237.    
  238.     return res;
  239. }
  240.  
  241. int main(int argc, const char * argv[])
  242. {
  243.     double time1 = 0.0, tstart;
  244.     tstart = clock();
  245.    
  246.     unsigned int faku = 5000;
  247.     std::cout << fakuIterate(faku);
  248.    
  249.     time1 = clock() - tstart;
  250.     std::cout.precision(2);
  251.     std::cout << "\nLaufzeit: " << time1/CLOCKS_PER_SEC << "s" << std::endl;
  252.    
  253.     return 0;
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement