• 重载高精


    把高精度的数封装到一个结构体里,有加,减,乘,除(效率极低)

    struct HighNum {
    	int num[2020];
    	bool Fu = 0;
    	void Read () {
    		std :: string Du;
    		std :: getline (std :: cin, Du); 
    		for (int i = 1; i <= Du.size (); i ++) {
    			num[i] = Du[Du.size () - i] - '0';
    		}
    		num[0] = Du.size ();
    		if (Du[0] == '-') num[num[0]] = 0, num[0] --, Fu = 1;
    	}
    	void Write () {
    		if (Fu) putchar ('-');
    		for (int i = num[0]; i >= 1; i --) {
    			printf ("%d", num[i]);
    		}
    	}
    	void Clear () {
    		for (int i = 0; i <= 2000; i ++) num[i] = 0;
    		Fu = 0;
    	}
    	HighNum To_HighNum (long long n) {
    		HighNum To;
    		To.Clear();
    		while (n) {
    			To.num[++ To.num[0]] = n % 10;
    			n /= 10;
    		}
    		return To;
    	}
    	bool operator < (const HighNum& Ri) {
    		if (Fu == 1 && Ri.Fu == 0) return 1;
    		if (Fu == 0 && Ri.Fu == 1) return 0;
    		if (num[0] != Ri.num[0]) return (Fu == 1 && Ri.Fu == 1) ? ! (num[0] < Ri.num[0]) : num[0] < Ri.num[0];
    		for (int i = num[0]; i >= 1; i --) {
    			if (num[i] != Ri.num[i]) return (Fu == 1 && Ri.Fu == 1) ? ! (num[i] < Ri.num[i]) : num[i] < Ri.num[i];
    		}
    		return 0;
    	}
    	bool operator > (const HighNum& Ri) {
    		if (Fu == 1 && Ri.Fu == 0) return 0;
    		if (Fu == 0 && Ri.Fu == 1) return 1;
    		if (num[0] != Ri.num[0]) return (Fu == 1 && Ri.Fu == 1) ? ! (num[0] > Ri.num[0]) : num[0] > Ri.num[0];
    		for (int i = num[0]; i >= 1; i --) {
    			if (num[i] != Ri.num[i]) return (Fu == 1 && Ri.Fu == 1) ? ! (num[i] > Ri.num[i]) : num[i] > Ri.num[i];
    		}
    		return 0;
    	}
    	bool operator <= (const HighNum& Ri) {
    		if (Fu == 1 && Ri.Fu == 0) return 1;
    		if (Fu == 0 && Ri.Fu == 1) return 0;
    		if (num[0] != Ri.num[0]) return (Fu == 1 && Ri.Fu == 1) ? ! (num[0] < Ri.num[0]) : num[0] < Ri.num[0];
    		for (int i = num[0]; i >= 1; i --) {
    			if (num[i] != Ri.num[i]) return (Fu == 1 && Ri.Fu == 1) ? ! (num[i] < Ri.num[i]) : num[i] < Ri.num[i];
    		}
    		return 1;
    	}
    	bool operator >= (const HighNum& Ri) {
    		if (Fu == 1 && Ri.Fu == 0) return 0;
    		if (Fu == 0 && Ri.Fu == 1) return 1;
    		if (num[0] != Ri.num[0]) return (Fu == 1 && Ri.Fu == 1) ? ! (num[0] > Ri.num[0]) : num[0] > Ri.num[0];
    		for (int i = num[0]; i >= 1; i --) {
    			if (num[i] != Ri.num[i]) return (Fu == 1 && Ri.Fu == 1) ? ! (num[i] > Ri.num[i]) : num[i] > Ri.num[i];
    		}
    		return 1;
    	}
    	bool operator == (const HighNum& Ri) {
    		if (Fu == 1 && Ri.Fu == 0) return 0;
    		if (Fu == 0 && Ri.Fu == 1) return 0;
    		if (num[0] != Ri.num[0]) return 0;
    		for (int i = num[0]; i >= 1; i --) {
    			if (num[i] != Ri.num[i]) return 0;
    		}
    		return 1;
    	}
    	HighNum operator + (const HighNum& Ri) {
    		HighNum To, a, b;
    		a = *this; b = Ri;
    		for (int i = 1; i <= max (a.num[0], b.num[0]) + 10; i ++) To.num[i] = 0;
    		if (a.Fu == 0 && b.Fu == 1) {
    			b.Fu = 0;
    			To = a - b;
    			return To;
    		}
    		if (a.Fu == 1 && b.Fu == 0) {
    			a.Fu = 0;
    			To = b - a;
    			return To;
    		}
    		if (a.Fu == 1 && b.Fu == 1) {
    			a.Fu = 0; b.Fu = 0;
    			To = a + b;
    			To.Fu = 1;
    			return To;
    		}
    		To.num[0] = max (a.num[0], b.num[0]) + 1;
    		To.num[1] = 0;
    		for (int i = 1; i <= To.num[0]; i ++) {
    			To.num[i] += a.num[i] + b.num[i];
    			To.num[i + 1] = To.num[i] / 10;
    			To.num[i] %= 10; 
    		}
    		if (To.num[To.num[0]] == 0) To.num[0] --;
    		return To;
    	}
    	HighNum operator - (const HighNum& Ri) {
    		HighNum To;
    		HighNum a, b;
    		a = *this;
    		b = Ri;
    		for (int i = 1; i <= max (a.num[0], b.num[0]) + 10; i ++) To.num[i] = 0;
    		if (a.Fu == 0 && b.Fu == 1) {
    			b.Fu = 0;
    			To = a + b;
    			return To;
    		}
    		if (a.Fu == 1 && b.Fu == 0) {
    			a.Fu = 0;
    			To = a + b;
    			To.Fu = 1;
    			return To;
    		}
    		if (a.Fu == 1 && b.Fu == 1) {
    			a.Fu = 0; b.Fu = 0;
    			To = b - a;
    			return To;
    		}
    		if (a < b) {
    			HighNum SWAP;
    			SWAP = a;
    			a = b;
    			b = SWAP;
    			To.Fu = 1;
    		}
    		To.num[0] = max (this -> num[0], Ri.num[0]);
    		for (int i = 1; i <= To.num[0]; i ++) {
    			if (a.num[i] < b.num[i]) {
    				a.num[i] += 10;
    				a.num[i + 1] -= 1;
    			}
    			To.num[i] = a.num[i] - b.num[i];
    		}
    		while (To.num[To.num[0]] == 0) To.num[0] --;
    		return To;
    	}
    	HighNum operator * (const HighNum& Ri) {
    		HighNum To, a, b;
    		a = *this; b = Ri;
    		for (int i = 1; i <= max (a.num[0], b.num[0]) + 10; i ++) To.num[i] = 0;
    		if (a.Fu == 1 && b.Fu == 0) {
    			a.Fu = 0;
    			To = a * b;
    			To.Fu = 1;
    			return To;
    		}
    		if (a.Fu == 0 && b.Fu == 1) {
    			b.Fu = 0;
    			To = a * b;
    			To.Fu = 1;
    			return To;
    		}
    		if (a.Fu == 1 && b.Fu == 1) {
    			a.Fu = 0; b.Fu = 0;
    			To = a * b;
    			return To;
    		}
    		To.num[0] = a.num[0] * b.num[0] + 10;
    		for (int i = 1; i <= a.num[0]; i ++) {
    			for (int j = 1; j <= b.num[0]; j ++) {
    				To.num[i + j - 1] += a.num[i] * b.num[j];
    				To.num[i + j] += To.num[i + j - 1] / 10;
    				To.num[i + j - 1] %= 10;
    			}
    		}
    		while (To.num[To.num[0]] == 0) To.num[0] --;
    		return To;
    	}
    	HighNum operator / (const HighNum& Ri) {
    		HighNum To, a, b;
    		To.Clear();
    		a = *this; b = Ri;
    		if (a.Fu == 1 && b.Fu == 0) {
    			a.Fu = 0;
    			To = a / b;
    			To.Fu = 1;
    			return To;
    		}
    		if (a.Fu == 0 && b.Fu == 1) {
    			b.Fu = 0;
    			To = a / b;
    			To.Fu = 1;
    			return To;
    		}
    		if (a.Fu == 1 && b.Fu == 1) {
    			a.Fu = 0; b.Fu = 0;
    			To = a / b;
    			return To;
    		}
    		while (a >= b) {
    			a = a - b;
    			To = To + To_HighNum (1);
    		}
    		return To;
    	}
    };
    
  • 相关阅读:
    unidac使用演示
    delphi序列化对象的方法总结
    ReadFileToBuffer
    unidac连接ORACLE免装客户端驱动
    WriteFileFromBuffer
    mvc模式
    unidac宏替换使用
    如何使用Navicat监控mysql数据库服务器
    环信(php)服务器端REST API
    laravel 框架接入环信遇到的坑()
  • 原文地址:https://www.cnblogs.com/binghun/p/14256094.html
Copyright © 2020-2023  润新知