Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // bigNumbers.h
- // Faku
- //
- // Created by Lukas Adam on 10.12.13.
- // Copyright (c) 2013 Lukas Adam. All rights reserved.
- //
- #include <cmath>
- #include <iostream>
- #ifndef Faku_bigNumbers_h
- #define Faku_bigNumbers_h
- class bigNumbers{
- private:
- int *val = new int[100000];
- int index;
- void overloadPositiv(const int& pos);
- void overloadNegativ(const int& pos);
- friend std::ostream& operator << (std::ostream &cout, bigNumbers num);
- public:
- bigNumbers(){
- for(int i = 0; i < 100000; i++){
- val[i] = 0;
- }
- index = 0;
- }
- bigNumbers(int num){
- *this = num;
- }
- ~bigNumbers(){
- delete []val;
- }
- int& getIndex();
- int& getValue(const int& n);
- bigNumbers& dec();
- bigNumbers operator * (const bigNumbers& num);
- bigNumbers operator * (int num);
- bigNumbers operator + (const bigNumbers& num);
- bigNumbers& operator + (const int& num);
- bigNumbers& operator - (const int& num);
- std::ostream& operator << (std::ostream &cout);
- bigNumbers& operator = (const bigNumbers& num);
- bigNumbers& operator = (int num);
- };//Klassen ende
- #endif
- //
- // bigNumbers.cpp
- // Faku
- //
- // Created by Lukas Adam on 16.12.13.
- // Copyright (c) 2013 Lukas Adam. All rights reserved.
- //
- #include "bigNumbers.h"
- #include <iostream>
- //Gets the index of the bigNumber, which presents the number of values.
- int& bigNumbers::getIndex(){
- return this->index;
- }
- //Gets a specific Value of the Array
- int& bigNumbers::getValue(const int& n){
- return this->val[n];
- }
- //After a operation this Method will correct the positiv Values which are higher than 10
- void bigNumbers::overloadPositiv(const int& pos){
- for(int i = pos; i <= this->index; i++){
- if(this->val[i] > 9){
- this->val[i + 1] = this->val[i + 1] + (this->val[i] / 10);
- this->val[i] = this->val[i] % 10;
- if(i == this->index){
- this->index++;
- } else {
- overloadPositiv(--i);
- }
- }
- }
- }
- //After a operation this Method will correct the negativ Values
- void bigNumbers::overloadNegativ(const int &pos){
- for(int i = 0; i <= this->index; i++){
- if(this->val[i] < 0){
- this->val[i] += 10;
- this->val[i + 1] -= 1;
- overloadNegativ(--i);
- } else if(!this->val[i] && i == this->index && i){
- index--;
- }
- }
- }
- //Reduces the Value of the bigNumbers about the integer Value 1
- bigNumbers& bigNumbers::dec(){
- if(this->val[0] > 0){
- this->val[0]--;
- } else {
- for(int i = 1; i <= this->index; i++){
- if(this->val[i] > 0){
- this->val[i]--;
- for(int j = i - 1; j >= 0; j--) val[j] = 9;
- if (this->val[i] == 0 && i == this->index) {
- this->index--;
- }
- return *this;
- }
- }
- }
- return *this;
- }
- //To initiate a bigNumber with a bigNumbers with a nother bigNumbers
- bigNumbers& bigNumbers::operator = (const bigNumbers& num){
- for(int i = 0; i <= num.index; i++){
- this->val[i] = num.val[i];
- }
- this->index = num.index;
- return *this;
- }
- //To initiate a bigNumber with a bigNumbers with a Integer Number
- bigNumbers& bigNumbers::operator = (int num){
- int count = 0;
- while(num > 9){
- this->val[count] = num % 10;
- num = num / 10;
- count++;
- }
- this->val[count] = num;
- this->index = count;
- return *this;
- }
- //Addition of two bigNumbers
- bigNumbers bigNumbers::operator + (const bigNumbers& num){
- bigNumbers erg;
- int tempIndex = this->index > erg.index ? this->index : erg.index;
- for(int i = 0; i <= tempIndex; i++){
- erg.val[i] = this->val[i] + num.val[i];
- }
- erg.index = tempIndex;
- erg.overloadPositiv(0);
- return erg;
- }
- //Addition of a bigNumber and a Integer
- bigNumbers& bigNumbers::operator + (const int& num){
- val[0] += num;
- overloadPositiv(0);
- return *this;
- }
- bigNumbers& bigNumbers::operator - (const int& num){
- this->val[0] -= num;
- overloadNegativ(0);
- return *this;
- }
- //Multiplikation with other bigNumbers
- bigNumbers bigNumbers::operator * (const bigNumbers& num){
- bigNumbers erg;
- for (int i = 0; i <= this->index; i++) {
- for (int j = 0; j <= num.index; j++) {
- erg.val[i+j] = erg.val[i+j] + (this->val[i] * num.val[j]);
- if(erg.index < (i + j)){
- erg.index = i + j;
- }
- }
- }
- erg.overloadPositiv(0);
- return erg;
- }
- //Multiplikation with Integer Values
- bigNumbers bigNumbers::operator * (int num){
- bigNumbers erg;
- for(int i = 0; i <= this->index; i++){
- erg.val[i] = this->val[i] * num;
- }
- erg.index = this->index;
- erg.overloadPositiv(0);
- return erg;
- }
- //For normal use of the << operator, to print bigNumbers
- std::ostream& operator<<(std::ostream &cout, bigNumbers num){
- std::cout << "Value: ";
- for(int i = num.index; i >= 0; i--){
- std::cout << num.val[i] << " ";
- }
- std::cout << " ,Index: " << num.index<< std::endl;
- return cout;
- }
- //
- // main.cpp
- // Faku
- //
- // Created by Lukas Adam on 10.12.13.
- // Copyright (c) 2013 Lukas Adam. All rights reserved.
- //
- #include <iostream>
- #include "bigNumbers.h"
- #include <time.h>
- //Fakulty of a bigNumber, waste of memory
- bigNumbers faku(bigNumbers &num){
- //std::cout << num << std::endl;
- if((num.getIndex() == 0) && (num.getValue(0) == 1)){
- return 1;
- } else {
- bigNumbers tmp = num;
- return tmp * faku(num - 1);
- }
- }
- //fakulty of an integer, returns bigNumber
- bigNumbers fakuIterate(const unsigned int& num){
- bigNumbers res = 1;
- for(int i = 2; i <= num; i++){
- std::cout << i << std::endl;
- res = res*i;
- }
- return res;
- }
- int main(int argc, const char * argv[])
- {
- double time1 = 0.0, tstart;
- tstart = clock();
- unsigned int faku = 5000;
- std::cout << fakuIterate(faku);
- time1 = clock() - tstart;
- std::cout.precision(2);
- std::cout << "\nLaufzeit: " << time1/CLOCKS_PER_SEC << "s" << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement