计算器2.0

#include<bits/stdc++.h>
using namespace std;
__int128_t Mod=(__int128_t)10000000000000000;
struct node{
	vector<__int128_t> huge_int;
	bool ne;
};
struct node2{
	node ds,ys;
};
struct node3{
	char f;
	__int128_t yx;
};
stack<node> num;
stack<node3> fh;
string s,t;
__int128_t tot,yxj[1000],kh;
string _change_to_string(__int128_t n)
{
	if(n==0)
		return "0";
	string value="";
	while(n){
		value=char((__int128_t)n%10+'0')+value;
		n=(__int128_t)n/10;
	}
	return value;
}
bool q_bigger_h(node q,node h)
{
	if(q.huge_int.size()!=h.huge_int.size())
		return q.huge_int.size()>h.huge_int.size();
	for(__int128_t i=q.huge_int.size()-1;i>=0;i--)
		if(q.huge_int[i]!=h.huge_int[i])
			return q.huge_int[i]>h.huge_int[i];
	return false;
}
bool operator>(node q,node h)
{
	if(!q.ne){
		if(!h.ne)
			return q_bigger_h(q,h);
		return true;
	}
	else{
		if(h.ne)
			return q_bigger_h(h,q);
		return false;
	}
}
bool operator<(node q,node h)
{
	if(!q.ne){
		if(!h.ne)
			return q_bigger_h(h,q);
		return false;
	}
	else{
		if(h.ne)
			return q_bigger_h(q,h);
		return true;
	}
}
bool operator==(node q,node h)
{
	if(q.ne!=h.ne)
		return false;
	if(q.huge_int.size()!=h.huge_int.size())
		return false;
	for(__int128_t i=0;i<q.huge_int.size();i++)
		if(q.huge_int[i]!=h.huge_int[i])
			return false;
	return true;
}
__int128_t to_10_n(__int128_t n)
{
	__int128_t value=1;
	for(__int128_t i=1;i<=n;i++)
		value*=10;
	return value;
}
__int128_t log_10(__int128_t n)
{
	__int128_t value=0;
	if(n==0)
		return 1;
	while(n){
		value++;
		n/=10;
	}
	return value;
}
__int128_t to_n(string s)
{
	__int128_t value=0;
	__int128_t len=s.size();
	for(__int128_t i=0;i<len;i++)
		value=value*10+s[i]-'0';
	return value;
}
node change_to_huge_int(string s)
{
	node value; 
	value.huge_int.clear();
	value.ne=false;
	if(s[0]=='-'){
		s=s.substr(1,s.size()-1);
		value.ne=true;
	}
	__int128_t len=s.size();
	while(len>15){
		value.huge_int.push_back(to_n(s.substr(len-16,16)));
		len-=16; 
	}
	if(len>0)
		value.huge_int.push_back(to_n(s.substr(0,len)));
	return value;
}
string change_to_string(node s)
{
	string value="";
	if(s.ne)
		value="-";
	value+=_change_to_string(s.huge_int[(__int128_t)s.huge_int.size()-1]);
	for(__int128_t i=(__int128_t)s.huge_int.size()-2;i>=0;i--){
		for(__int128_t j=1;j<=16-log_10(s.huge_int[i]);j++)
			value+="0";
		value=value+_change_to_string(s.huge_int[i]);
	}
	return value;
} 
node jia(node jia1,node jia2)
{
	node ans;
	__int128_t jw=0,t;
	if(jia1.huge_int.size()>jia2.huge_int.size()){
		jia2.huge_int.resize(jia1.huge_int.size()+1);
		jia1.huge_int.push_back(0);
	}
	else if(jia1.huge_int.size()<jia2.huge_int.size()){
		jia1.huge_int.resize(jia2.huge_int.size()+1);
		jia2.huge_int.push_back(0);
	}
	else{
		jia1.huge_int.push_back(0);
		jia2.huge_int.push_back(0);
	}
	for(__int128_t i=0;i<jia1.huge_int.size();i++){
		t=jia1.huge_int[i]+jia2.huge_int[i]+jw;
		jw=t/Mod;
		ans.huge_int.push_back(t%Mod);
	}
	if(ans.huge_int[ans.huge_int.size()-1]==0&&ans.huge_int.size()>1)
		ans.huge_int.pop_back();
	return ans;
} 
node jian(node jian1,node jian2)
{
	if(jian1==jian2)
		return (node){{0},false};
	node ans;
	ans.ne=false;
	ans.huge_int.clear();
	if(jian1.ne)
		if(jian2.ne)
			if(q_bigger_h(jian1,jian2))
				ans.ne=true;
			else{
				node s=jian1;
				jian1.huge_int=jian2.huge_int;
				jian2.huge_int=s.huge_int;
			}
		else
			return {jia(jian1,jian2).huge_int,true};
	else
		if(jian2.ne)
			return {jia(jian1,jian2).huge_int,false};
		else
			if(!q_bigger_h(jian1,jian2)){
				ans.ne=true;
				swap(jian1,jian2);
			}
	if(jian2.huge_int.size()<jian1.huge_int.size()){
		__int128_t len1=jian1.huge_int.size(),len2=jian2.huge_int.size();
		jian2.huge_int.resize(jian1.huge_int.size());
	}
	__int128_t t,jw=0;
	for(__int128_t i=0;i<jian1.huge_int.size();i++){
		if(jian1.huge_int[i]-jw>=jian2.huge_int[i]){
			ans.huge_int.push_back(jian1.huge_int[i]-jian2.huge_int[i]-jw);
			jw=0;
		}
		else{
			ans.huge_int.push_back(Mod+jian1.huge_int[i]-jian2.huge_int[i]-jw);
			jw=1;
		}
	}
	while(ans.huge_int[ans.huge_int.size()-1]==0&&ans.huge_int.size()>1)
		ans.huge_int.pop_back();
	return ans;
} 
node cheng(node s1,node s2)
{
	node ans;
	ans.ne=(s1.ne!=s2.ne);
	ans.huge_int.clear();
	__int128_t t,jw=0;
	ans.huge_int.resize(s1.huge_int.size()+s2.huge_int.size());
	for(__int128_t i=1;i<=s1.huge_int.size();++i)
		for(__int128_t j=1;j<=s2.huge_int.size();++j){
			t=(__int128_t)s1.huge_int[i-1]*s2.huge_int[j-1]+jw;
			ans.huge_int[i+j-2]+=t;
		}
	for(__int128_t i=0;i<ans.huge_int.size()-1;++i){
		ans.huge_int[i+1]+=(__int128_t)ans.huge_int[i]/Mod;
		ans.huge_int[i]%=Mod;
	}
	while(ans.huge_int[ans.huge_int.size()-1]==0&&ans.huge_int.size()>1)
		ans.huge_int.pop_back();
	return ans;
}
node add(node s,__int128_t n)
{
	s.huge_int[0]=(__int128_t)(s.huge_int[0]*10+n);
	__int128_t j=0,s_len=(__int128_t)s.huge_int.size()-1;
	while(j<s_len&&s.huge_int[j]>=Mod){
		s.huge_int[j+1]=(__int128_t)((__int128_t)(s.huge_int[j]/Mod)+(__int128_t)(s.huge_int[j+1]*10));
		s.huge_int[j]=(__int128_t)s.huge_int[j]%Mod;
		j++;
	}
	__int128_t s_num=s.huge_int[s.huge_int.size()-1];
	s_len=s.huge_int.size();
	s.huge_int.reserve(s.huge_int.size()+1);
	if(s_num>=Mod){
		s.huge_int.push_back((__int128_t)s_num/Mod);
		s.huge_int[(__int128_t)s_len-1]=(__int128_t)s_num%Mod;
	}
	return s;
}
node2 chu(node bcs,node cs)
{
	bool ans_ds_ne=(bcs.ne!=cs.ne);
	bool ans_ys_ne=bcs.ne;
	bcs.ne=false;
	cs.ne=false;
	if(bcs<cs)
		return (node2){(node){{0},0},bcs};
	string bcs_string=change_to_string(bcs),cs_string=change_to_string(cs),ans_string="";
	node bcs_sub;
	node ans;
	ans.huge_int.clear();
	ans.huge_int.push_back(0);
	ans.ne=bcs.ne;
	bcs_sub.huge_int.push_back(0);
	bcs_sub.ne=false;
	for(__int128_t i=0;i<bcs_string.size();i++){
		if(bcs_sub<cs){
			bcs_sub=add(bcs_sub,bcs_string[i]-'0');
			if(bcs_sub<cs){
				ans_string+="0";
				continue;
			}
		}
		__int128_t now_ans=0;
		while(bcs_sub>cs||bcs_sub==cs){
			now_ans++;
			bcs_sub=jian(bcs_sub,cs);
		}
		ans_string+=char((__int128_t)now_ans+'0');
	}
	int j=0;
	while(j<ans_string.size()-1&&ans_string[j]=='0')
		j++;
	ans_string=ans_string.substr(j,ans_string.size()-j);
	if(bcs_sub.huge_int.size()==1&&bcs_sub.huge_int[0]==0)
		ans_ys_ne=false;
	return (node2){(node){change_to_huge_int(ans_string).huge_int,ans_ds_ne},(node){bcs_sub.huge_int,ans_ys_ne}};
}
node _pow(node d,node z)
{
	bool ans_ne=false;
	if(d.ne){
		if(z.huge_int[0]&1)
			ans_ne=true;
		d.ne=false;
	}
	node ans,a=d,redef2;
	ans.huge_int.clear();
	ans.ne=false;
	ans.huge_int.push_back(1);
	redef2.huge_int.clear();
	redef2.ne=false;
	redef2.huge_int.push_back(2);
	while(z.huge_int.size()>1||z.huge_int[0]!=0){
		if(z.huge_int[0]&1)
			ans=cheng(ans,a);
		a=cheng(a,a);
		z=chu(z,redef2).ds;
	}
	return (node){ans.huge_int,ans_ne};
}
node operator+(node q,node h)
{
	return jian(q,(node){h.huge_int,!h.ne});
}
node operator-(node q,node h)
{
	return jian(q,h);
}
node operator*(node q,node h)
{
	return cheng(q,h);
}
node2 operator/(node q,node h)
{
	return chu(q,h);
}
node operator^(node q,node h)
{
	return _pow(q,h);
}
void cal()
{
	char c=fh.top().f;
	fh.pop();
	node h=num.top();
	num.pop();
	node q=num.top();
	num.pop();
	if(c=='+')
		num.push(q+h);
	else if(c=='-')
		num.push(q-h);
	else if(c=='*')
		num.push(q*h);
	else if(c=='/')
		num.push((q/h).ds);
	else if(c=='^')
		num.push(q^h);
	return;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	yxj[int('#')]=1;
	yxj[int('+')]=2;
	yxj[int('-')]=2;
	yxj[int('*')]=3;
	yxj[int('/')]=3;
	yxj[int('^')]=4;
	cin>>s;
	fh.push((node3){'#',0});
	for(__int128_t i=0;i<s.size();i++){
		if(s[i]=='('||s[i]=='['||s[i]=='{'){
			kh+=5;
			continue;
		}
		if(s[i]==')'||s[i]==']'||s[i]=='}'){
			kh-=5;
			continue;
		}
		if(s[i]<'0'||s[i]>'9'){
			num.push(change_to_huge_int(t));
			while(fh.top().yx>=yxj[s[i]]+kh) cal();
			fh.push((node3){s[i],yxj[s[i]]+kh});
			t="";
		}
		else
			t+=s[i];
	}
	num.push(change_to_huge_int(t));
	while(fh.top().f!='#')
		cal();
	cout<<change_to_string(num.top());
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值