跳到正文

剪贴板

剪贴板 n03wor9x

原文
剪贴板信息

剪贴板信息

收录
更新

运算工具专栏

高精(一辈子就写一次,写一次用一辈子)

C++
struct INT{
	int len,num[5005];
	void read(){
		stack<int>st;
		char c=getchar();
		while(c>'9'||c<'0'){c=getchar();}
		while(c<='9'&&c>='0'){
			st.push(c-'0');
			c=getchar();
		}
		while(!st.empty()){
			num[++len]=st.top();
			st.pop();
		}
	}
	void write(){
		for(int i=len;i>0;i--){
//			printf("%d",num[i]);
			putchar(num[i]+'0');
		}
	}
};
INT TOINT(int x){//int to INT
	int len=0;
	int num[5005];
	while(x){
		num[++len]=x%10;
		x/=10;
	}
	INT c;
	c.len=len;
	for(int i=1;i<=len;i++){
		c.num[i]=num[i];
	}
	return c;
}
INT operator *(const INT a,const int b){//high * low
	INT c;
	c.len=a.len;
	for(int i=1;i<=a.len;i++){
		c.num[i]=a.num[i]*b;
	}
	for(int i=1;i<a.len;i++){
		c.num[i+1]+=c.num[i]/10;
		c.num[i]%=10;
	}
	while(c.num[c.len]>9){
		c.num[c.len+1]=c.num[c.len]/10;
		c.num[c.len]%=10;
		c.len++;
	}
	return c;
}
INT operator /(const INT a,const int b){//high / low
	INT c;
	c.len=a.len;
	int x=0;
	for(int i=a.len;i>0;i--){
		c.num[i]=(a.num[i]+x*10)/b;
		x=(x*10+a.num[i])%b;
	}
	c.len=a.len;
	while(c.num[c.len]==0&&c.len>1)c.len--;
	return c;
}
//#define high_and_high
#ifdef high_and_high//+-
INT operator +(const INT a,const INT b){//high + high
	INT c;
	c.len=max(a.len,b.len);
	for(int i=1;i<=c.len;i++){
		c.num[i]=a.num[i]+b.num[i];
	}
	for(int i=1;i<a.len;i++){
		c.num[i+1]+=c.num[i]/10;
		c.num[i]%=10;
	}
	while(c.num[c.len]>9){
		c.num[c.len+1]=c.num[c.len]/10;
		c.num[c.len]%=10;
		c.len++;
	}
	return c;
}
#endif
//---------------------------------------------------
#ifndef high_and_high//+-
INT operator +(const INT a,const int b){//high + low
	INT c=a;
	int x=b;
	c.num[1]+=x;
	int blen=0;
	while(x){
		blen++;
		x/=10;
	}
	for(int i=1;i<=max(blen,c.len);i++){
		c.num[i+1]+=c.num[i]/10;
		c.num[i]%=10;
	}
	return c;
}
#endif

bool operator <(const INT a,const INT b){
	if(a.len!=b.len)return a.len<b.len;
	for(int i=a.len;i>0;i--){
		if(a.num[i]!=b.num[i])return a.num[i]<b.num[i];
	}
}
bool operator >(const INT a,const INT b){
	if(a.len!=b.len)return a.len>b.len;
	for(int i=a.len;i>0;i--){
		if(a.num[i]!=b.num[i])return a.num[i]>b.num[i];
	}
}

OMG It`s 万能计算器

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
string in,s;
int t[114514];
int cmd(char c){
	if(c=='+'||c=='-')return 1;
	if(c=='*'||c=='/')return 2;
	if(c=='^')return 3;
	if(c=='('||c==')')return 0;
}
void out(int pos,int now){
	for(int i=1;i<=pos;i++){
		cout<<t[i]<<' ';
	}
	for(int i=now+2;i<s.size();i++){
		cout<<s[i];
	}
	cout<<'\n';
}
signed main(){
	stack<char> stc;
	stack<int> stn;
	cin>>in;
//	cout<<i<<'\n';
	for(int i=0;i<in.size();i++){
//		cout<<i<<":"<<s<<'.'<<'\n';
		if(in[i]>='0'&&in[i]<='9'){
			while(in[i]>='0'&&in[i]<='9'){
				s+=in[i];
				i++;
			}
			s+=" ";
			i--;
		}else{
			if(stc.empty()){
				stc.push(in[i]);
			}else if(in[i]=='('){
				stc.push(in[i]);
			}else if(stc.top()=='('){
				stc.push(in[i]);
			}else if(in[i]=='^'){
				stc.push(in[i]);
			}else if(cmd(stc.top())<cmd(in[i])){
				stc.push(in[i]);
			}else{
				if(in[i]==')'){
					while(stc.top()!='('){
						s+=stc.top();s+=" ";
						stc.pop();
					}
					stc.pop();
					continue;
				}
				while(!stc.empty()&&cmd(stc.top())>=cmd(in[i])){
					s+=stc.top();s+=" ";
					stc.pop();
				}
				stc.push(in[i]);
			}
		}
	}
	while(!stc.empty()){
		s+=stc.top();s+=" ";
		stc.pop();
	}
///////////////////////////////////////////////////////////
//	cout<<s<<'\n';
	int pos=0;
	for(int i=0;i<s.size();i++){
		if(s[i]>='0'&&s[i]<='9'){
			int x=0;
			while(s[i]>='0'&&s[i]<='9'){
				x=x*10+s[i]-'0';
				i++;
			}
			i--;
			t[++pos]=x;
		}else if(s[i]!=' '){
			pos--;
			if(s[i]=='+'){
				t[pos]+=t[pos+1];
			}else if(s[i]=='-'){
				t[pos]-=t[pos+1];
			}else if(s[i]=='*'){
				t[pos]*=t[pos+1];
			}else if(s[i]=='/'){
				t[pos]/=t[pos+1];
			}else{
				t[pos]=pow(t[pos],t[pos+1]);
			}
//			out(pos,i);
		}
	}
	cout<<t[pos];
	return 0;
}