zoukankan      html  css  js  c++  java
  • 重载运算符版高精运算

    都是自己手写的,重载运算符用起来也比较方便

    #include <cstdio>
    #include <algorithm>
    #include <iostream>
    #include <cstring>
    using namespace std;
    int read(){
    	int x = 1,a = 0;char ch = getchar();
    	while (ch < '0'||ch > '9'){if (ch == '-') x = -1;ch = getchar();}
    	while (ch >= '0'&&ch <= '9'){a = a*10+ch-'0';ch = getchar();}
    	return x*a;
    }
    struct INT{
    	int len,a[10007];
    	void clear(){len = 1,memset(a,0,sizeof(a));}
    	void print(){
    		for (int i = len;i >= 1;i--) putchar(a[i]+'0');
    		puts("");
    	}
    	void read(){
    		char ch = getchar();clear();len = 0;
    		while (ch < '0'||ch > '9') ch = getchar();
    		while (ch >= '0'&&ch <= '9'){a[++len] = ch-'0';ch = getchar();}
    		for (int i = 1;i < len-i+1;i++) swap(a[i],a[len-i+1]);
    	}
    	void calc(){
    		for (int i = 1;i <= len;i++){
    			if (a[i] >= 10) a[i+1] += a[i]/10,a[i] %= 10,len = max(len,i+1);
    		}
    	}
    	void operator = (int b){
    		clear();len = 0;
    		while (b) a[++len] = b%10,b /= 10;
    		len = max(1,len);
    	}
    	friend INT operator + (const INT a,const INT b){
    		INT res;res.clear();res.len = max(a.len,b.len);
    		for (int i = 1;i <= res.len;i++) res.a[i] = a.a[i] + b.a[i];
    		res.calc();return res;
    	}
    	friend INT operator + (const INT a,const int b){
    		INT c;c = b;return a+c;
    	}
    	friend INT operator - (const INT a,const INT b){
    		INT res;res.clear();res.len = max(a.len,b.len);
    		for (int i = 1;i <= res.len;i++) res.a[i] = a.a[i] - b.a[i];
    		for (int i = 1;i <= res.len;i++){
    			if (res.a[i] < 0) res.a[i] += 10,res.a[i+1] --;
    		}
    		while (!res.a[res.len]&&res.len > 1) res.len--;
    		return res;
    	}
    	friend INT operator - (const INT a,const int b){
    		INT c;c = b;return a-c;
    	}
    	friend INT operator * (const INT a,const INT b){
    		INT res;res.clear();res.len = a.len+b.len-1;
    		for (int i = 1;i <= a.len;i++){
    			for (int j = 1;j <= b.len;j++) res.a[i+j-1] += a.a[i] * b.a[j];
    		}
    		res.calc();return res;
    	}
    	friend INT operator * (const INT a,const int b){
    		INT c;c = b;return a*c;
    	}
    	friend INT operator / (const INT a,const int b){
    		INT res;res.clear();res.len = a.len;int tmp = 0;
    		for (int i = 1;i <= res.len;i++){
    			tmp = tmp*10+a.a[i];
    			res.a[i] = tmp/b,tmp %= b;
    		}
    		while (!res.a[res.len]&&res.len > 1) res.len--;
    		return res;
    	}
    	friend int operator % (const INT a,const int b){
    		int tmp = 0;
    		for (int i = 1;i <= a.len;i++){
    			tmp = tmp*10+a.a[i];
    			tmp %= b;
    		}
    		return tmp;
    	}
    };
    int main(){
    	INT a,b;int c = read();
    	a.read(),b.read();
    	INT ans,ans1,ans2,ans3;
    	ans = a+b,ans1 = a-b;
    	ans2 = a/c,ans3 = a%c;
    	ans.print(),ans1.print(),ans2.print(),ans3.print();
    	return 0;
    }
    
  • 相关阅读:
    boost::VS2017下编译和配置boost库
    cmake::编译一个工程
    cmake::helloworld
    python模块之urllib
    Python报错:UnicodeEncodeError 'gbk' codec can't encode character
    word页眉添加横线与删除横线
    练习五十九:字典排序
    练习五十八:列表的练习
    练习五十七:for循环 809??=800*?+9*?+1其中?代表的两位数,8*?的结果为两位数,9*?的结果为3位数。求?代表的两位数,及809??后的结果
    windows查看网络常用cmd命令
  • 原文地址:https://www.cnblogs.com/little-uu/p/14284212.html
Copyright © 2011-2022 走看看