高精度总结(高精度类和重载运算高精度阶乘)

一.高精度类和重载运算

注意重载的运算符只会对判断为bign类型的数据发生改变,不影响其他类型的使用。

#include<cstdio>
#include<cstring> 
#include<iostream>
using namespace std;

const int maxn = 200;
struct bign{
  int len, s[maxn];

  bign() {
    memset(s, 0, sizeof(s));
    len = 1;
  }

  bign(int num) {
    *this = num;
  }

  //定义为const参数,作用是 不能对const参数的值做修改 
  bign(const char* num) {
    *this = num;
  }

  bign operator = (int num) {
    char s[maxn];
    sprintf(s, "%d", num);
    *this = s;
    return *this;
  }
  /*以上是构造方法,初始化时对执行相应的方法*/
  

  //函数定义后的const关键字,它表明“x.str()不会改变x” 
  string str() const {
    string res = "";
    for(int i = 0; i < len; i++) res = (char)(s[i] + '0') + res;
    if(res == "") res = "0";
    return res;
  }
  
  void clean() {
    while(len > 1 && !s[len-1]) len--;
  }
  
  
  /* 以下是重载操作符 */ 
  bign operator = (const char* num) {
  	//逆序存储,方便计算 
    len = strlen(num);
    for(int i = 0; i < len; i++) s[i] = num[len-i-1] - '0';
    return *this;
  }
  
  bign operator + (const bign& b) const{
    bign c;
    c.len = 0;
    for(int i = 0, g = 0; g || i < max(len, b.len); i++) {
      int x = g;
      if(i < len) x += s[i];
      if(i < b.len) x += b.s[i];
      c.s[c.len++] = x % 10;
      g = x / 10;
    }
    return c;
  }

  bign operator * (const bign& b) {
    bign c; c.len = len + b.len;
    for(int i = 0; i < len; i++)
      for(int j = 0; j < b.len; j++)
        c.s[i+j] += s[i] * b.s[j];
    for(int i = 0; i < c.len-1; i++){
      c.s[i+1] += c.s[i] / 10;
      c.s[i] %= 10;
    }
    c.clean();
    return c;
  }

  bign operator - (const bign& b) {
    bign c; c.len = 0;
    for(int i = 0, g = 0; i < len; i++) {
      int x = s[i] - g;
      if(i < b.len) x -= b.s[i];
      if(x >= 0) g = 0;
      else {
        g = 1;
        x += 10;
      }
      c.s[c.len++] = x;
    }
    c.clean();
    return c;
  }

  bool operator < (const bign& b) const{
    if(len != b.len) return len < b.len;
    for(int i = len-1; i >= 0; i--)
      if(s[i] != b.s[i]) return s[i] < b.s[i];
    return false;
  }

  bool operator > (const bign& b) const{
    return b < *this;
  }

  bool operator <= (const bign& b) {
    return !(b > *this);
  }

  bool operator == (const bign& b) {
    return !(b < *this) && !(*this < b);
  }

  bign operator += (const bign& b) {
    *this = *this + b;
    return *this;
  }
};

istream& operator >> (istream &in, bign& x) {
  string s;
  in >> s;
  x = s.c_str();
  return in;
}

ostream& operator << (ostream &out, const bign& x) {
  out << x.str();
  return out;
}

int main() {
  bign a,b;
  cin >> a>>b;
cout<<a+b<<endl;
  cout << a*b<< endl;
  cout<<a-b<<endl;//注意使用a—b的时候,a的值一定要比b大 
  return 0;
}

二.高精度阶乘

//
//  main.cpp
//  高精度阶乘
//
//  Created by 张嘉韬 on 16/1/28.
//  Copyright © 2016年 张嘉韬. All rights reserved.
//

#include <iostream>
#include <cstring>
using namespace std;
const int maxn=3000;
int main(int argc, const char * argv[]) {
    int n,f[3010],c;
    cin>>n;
    memset(f,0,sizeof(f));
    f[1]=1,c=0;
    for(int i=2;i<=n;i++)
    {
        for(int j=1;j<=maxn;j++)
        {
            int temp=f[j]*i+c;
            f[j]=temp%10;
            c=temp/10;
        }
    }
    int lenc=maxn;
    while(f[lenc]==0) lenc--;
    for(int i=lenc;i>=1;i--) cout<<f[i];
    cout<<endl;
    return 0;
}

计算原理详见点击打开链接

你可能感兴趣的:(高精度总结(高精度类和重载运算高精度阶乘))