我的_int8192类型过了?!能优化吗
查看原帖
我的_int8192类型过了?!能优化吗
1421527
ChenHaoQi楼主2024/10/6 02:50
#include<iostream>
#include<string.h>
#include<stack>
using namespace std;

class _int8192{
	private:
		static const int BIT=1000;
		short num[BIT+1];
		
		inline void getsize(){
			int i=BIT;
			while(i>=0 && num[i]==0) i--;
			if(i==-1) size=1;
			else size=i+1;
		}
		
		short cmp[BIT+1];
		bool PrintBool(){
			memset(cmp,0,sizeof(cmp));
			if(memcmp(num,cmp,sizeof(short)*BIT)==0) return true;
			return false;
		}
		
	public:
		int size;
		bool porn=true;
		
		void init(){
			for(register int i=0; i<=BIT; i++){
				num[i]=0;
			}
		}
		
		static _int8192 intTO_int8192(int n){
			_int8192 ans;
			ans.init();
			if(n<0){
				ans.porn=false;
				n=-n;
			}
			else ans.porn=true;
			
			int p=0;
			while(n!=0){
				ans.num[p++]=n%10;
				n/=10;
			}
			
			ans.getsize();
			return ans;
		}
		#define _int8192(x) _int8192::intTO_int8192(x)
		
		void read(){
			init();
			stack<short> st;
			size=0;
			memset(num,0,sizeof(num));
			char a;
			int x;
			int p=0;
			bool c=true;
			bool fr0=true;
			a=getchar();
			while(1){
				if(!((a>='0' && a<='9') || a=='-') || (c==false && a=='-')) break;
				
				if(a=='-'){
					size--;
					porn=false;
				}
				else if(fr0==false) st.push(a-'0');
				else if(fr0==true && a!='0'){
					st.push(a-'0');
					fr0=false;
				}
				else size--;
				
				if(c==true) c=false;
				size++;
				a=getchar();
			}
			
			for(register int i=0; i<size; i++){
				num[i]=st.top();
				st.pop();
			}
			if(size==0){
				size=1;
				num[0]=0;
			}
		}
		
		void print(){
			if(num[0]==-8) return;
			if(PrintBool()==true){
				printf("0");
				return;
			}
			if(porn==false) printf("-");
			int p=size-1;
			while(p!=-1){
				printf("%d",num[p]);
				p--;
			}
		}
		
		void operator^(_int8192 &a){
			_int8192 t;
			t=*this;
			*this=a;
			a=t;
		}	
		
		bool operator=(const _int8192 a){
			memcpy(num,a.num,sizeof(short)*BIT);
			porn=a.porn;
			size=a.size;
		}
		
		_int8192 abs(){
			_int8192 ret;
			ret=*this;
			ret.porn=true;
			return ret;
		}
		
		_int8192 opp(){
			_int8192 ret;
			ret=*this;
			ret.porn=!ret.porn;
			return ret;
		}		
		
		bool operator>(const _int8192 a) const{
			if(porn==true && a.porn==false) return true;
			if(porn==false && a.porn==true) return false;
			bool ret=false;
			int bit=max(size,a.size);
			for(int i=bit-1; i>=0; i--){
				if(num[i]>a.num[i]){
					ret=true;
					break;
				}
				else if(num[i]<a.num[i]){
					ret=false;
					break;
				}
			}
			if(porn==false) return !ret;
			return ret;
		}
		
		bool operator<(const _int8192 a) const{
			if(porn==true && a.porn==false) return false;
			if(porn==false && a.porn==true) return true;
			bool ret=false;
			int bit=max(size,a.size);
			for(int i=bit-1; i>=0; i--){
				if(num[i]<a.num[i]){
					ret=true;
					break;
				}
				else if(num[i]>a.num[i]){
					ret=false;
					break;
				}
			}
			if(porn==false) return !ret;
			return ret;
		}
		
		bool operator==(const _int8192 a) const{
			if(porn!=a.porn) return false;
			int bit=max(size,a.size);
			for(int i=bit-1; i>=0; i--){
				if(num[i]!=a.num[i]) return false;
			}
			return true;
		}
		
		bool operator>=(const _int8192 a) const{
			return *this>a || *this==a;
		}
		
		bool operator<=(const _int8192 a) const{
			return *this<a || *this==a;
		}
		
		_int8192 operator+(const _int8192 a) const{
			_int8192 ans;
			ans.init();
			
			if(porn==a.porn){
				ans.porn=porn;
				int bit=max(size,a.size);
				
				for(register int i=0; i<bit; i++){
					ans.num[i]+=num[i]+a.num[i];
					ans.num[i+1]=ans.num[i]/10;
					ans.num[i]%=10;
				}
			}
			else{
				_int8192 b=a;
				_int8192 _this=*this;
				ans.porn=(b.abs()>_this.abs())?a.porn:porn;
				
				int bit=max(size,a.size);
				label:
				for(register int i=0; i<bit; i++){
					ans.num[i]+=b.num[i]-_this.num[i];
					if(ans.num[i]<0){
						ans.num[i]+=10;
						ans.num[i+1]--;
					}
				}
				if(ans.num[bit]<0){
					memset(ans.num,0,sizeof(short)*BIT);
					b^_this;
					goto label;
				}
			}
			
			ans.getsize();
			return ans;
		}
		
		
		_int8192 operator-(const _int8192 a) const{
			_int8192 ans;
			ans.init();
			
			if(porn==a.porn){
				_int8192 b=a;
				_int8192 _this=*this;
				ans.porn=(b.abs()<_this.abs())?porn:!porn;
				
				int bit=max(size,a.size);
				if(ans.porn!=porn) b^_this;
				for(register int i=0; i<bit; i++){
					ans.num[i]+=_this.num[i]-b.num[i];
					if(ans.num[i]<0){
						ans.num[i]+=10;
						ans.num[i+1]--;
					}
				}
			}
			
			else{
				ans.porn=porn;
				_int8192 b=a;
				b=b.opp();				
				int bit=max(size,b.size);
				for(register int i=0; i<bit; i++){
					ans.num[i]+=num[i]+b.num[i];
					ans.num[i+1]=ans.num[i]/10;
					ans.num[i]%=10;
				}
			}			
			
			ans.getsize();
			return ans;
		}
		
		void operator+=(const _int8192 a){
			*this=*this+a;
		}
		
		void operator-=(const _int8192 a){
			*this=*this-a;
		}
		
		void operator++(){
			*this=*this+_int8192(1);
		}
		
		_int8192 operator++(int){
			_int8192 *ret=this;
			++*this;
			return *ret;
		}
		
		void operator--(){
			*this=*this-_int8192(1);
		}
		
		_int8192 operator--(int){
			_int8192 *ret=this;
			--*this;
			return *ret;
		}
		
		_int8192 operator*(const _int8192 a) const{
			_int8192 ans;
			ans.init();
			ans.porn=(porn==a.porn)?true:false;
			
			for(register int i=0; i<size; i++){
				for(register int j=0; j<a.size; j++){
					ans.num[i+j]+=num[i]*a.num[j];
				} 
			}			
			for(register int i=0; i<BIT; i++){
				ans.num[i+1]+=ans.num[i]/10;
				ans.num[i]%=10;
			}
			
			ans.getsize();
			return ans;
		}
		
		_int8192 operator/(const _int8192 a) const{
			if(a==_int8192(0)){
				cout<<"Runtime Error! Zero(0) cannot be a divisor!";
				_int8192 re;
				re.init();
				re.num[0]=-8;
				return re;
			}
			
			_int8192 ans;
			ans.init();
			
			_int8192 _this=*this;
			_int8192 b=a;
			_this=_this.abs();
			b=b.abs();
			ans=_int8192(0);
			while(_this>=b){
				_this-=b;
				ans++;
			}
			
			ans.porn=ans.porn=(porn==a.porn)?true:false;
			ans.getsize();
			return ans;
		}
		
		_int8192 operator%(const _int8192 a) const{
			_int8192 ans;
			ans.init();
			
			_int8192 _this=*this;
			_int8192 b=a;
			_this=_this.abs();
			b=b.abs();
			while(_this>=b) _this-=b;
			
			if(porn==a.porn) ans=_this;
			else ans=b-_this;
			ans.porn=a.porn;
			
			ans.getsize();
			return ans;
		}
};

int main(){
	_int8192 n,m;
	n.read();
	m.read();
	(n+m).print();
	
	return 0;
} 
2024/10/6 02:50
加载中...