跳到正文

剪贴板

剪贴板 svr7z25j

Lijunzhuo
原文
剪贴板信息

剪贴板信息

发布
收录
更新
C++
/*
Code_By_Lijunzhuo.
This is evaluation of infix expressions.

I want 50 points!!!
*/
#include<bits/stdc++.h>
using namespace std;
const int N=1e5;
string In_ex;//Infix expression
string In_explus;
bool can_evaluation=true;
int xlen,F[N],tot;
long long A[N];
template<typename T>
class mystack
{
	private:
		struct pointer
		{
			T val;
			pointer *ne=nullptr;
		}*head=nullptr;
		size_t tot=0;
	public:
		~mystack()
		{
        	while (!empty())
            	pop();
    	}
		void push(T i)
		{
			pointer*new_push=new pointer;
			new_push->val=i;
			new_push->ne=head;
			head=new_push;
			++tot;
		}
		bool pop()
		{
			if(!tot) return false;
			head=head->ne;
			--tot;
			return true;
		}
		int size(){return tot;}
		bool empty(){return !tot;}
		T top(){return head->val;}
};
inline int f(char c)
{
	if(c<='9'&&c>='0') return 0;
	else if(c=='-'||c=='+') return 1;
	else if(c=='/'||c=='*'||c=='%') return 2;
	else if(c=='(') return 3;
	else if(c==')') return 4;
	else return -1;
}
inline bool check_test(string x)
{
	In_explus="";
	for(int i=0;i<xlen;i++)
	{
		if(x[i]=='['||x[i]=='{') x[i]='(';
		if(x[i]==']'||x[i]=='}') x[i]=')';
//		printf("%d",f(x[i]));
		if(f(x[i])==-1)
		{
			printf("Error:Unknown operator.\n");
			return true;
		}
		if((i>0&&f(x[i])!=0&&f(x[i-1]))!=0&&(f(x[i])!=3&&f(x[i-1])!=3&&f(x[i])!=4&&f(x[i-1])!=4))
		{
			printf("Error:Undefined operation.\n");
			return true;
		}
		if(i>0&&(f(x[i])==2&&f(x[i-1])==3))
		{
			printf("Error:Undefined operation.\n");
			return true;
		}
		if((i>0)&&((f(x[i])!=0&&f(x[i])!=3&&f(x[i])!=4)&&f(x[i-1])==3))
			In_explus+='0';
		In_explus+=x[i];
	}
	return false;
}
inline void Po_ex(string x)
{
	if(check_test(x))
	{
		can_evaluation=false;
		return ;
	}
	bool is_last_number=false;
	x=In_explus;xlen=x.length();
//	printf("%s\n",x.c_str());
	mystack<char>C;
	for(int i=0;i<xlen;i++)
	{
		if(f(x[i])==0)
		{
			if(!is_last_number) A[++tot]=x[i]-'0',is_last_number=true;
			else A[tot]=A[tot]*10+x[i]-'0',is_last_number=true;
		}else if(f(x[i])==1)
		{
			while(!C.empty()&&(C.top()!='('))
				A[++tot]=(int)(C.top()),F[tot]=1,C.pop();
			C.push(x[i]);
			is_last_number=false;
		}else if(f(x[i])==2)
		{
			while(!C.empty()&&(C.top()!='('&&f(C.top())!=1))
				A[++tot]=(int)(C.top()),F[tot]=1,C.pop();
			C.push(x[i]);
			is_last_number=false;
		}else if(f(x[i])==3)
		{
			C.push(x[i]);
			is_last_number=false;
		}
		else if(f(x[i])==4)
		{
			while(!C.empty()&&C.top()!='(')
				A[++tot]=(int)(C.top()),F[tot]=1,C.pop();
			if(C.empty())
			{
				printf("Error:The parentheses did not match successfully.\n");
				can_evaluation=false;
				return ;
			}
			C.pop();
			is_last_number=false;
		}
	}
	if(!C.empty())
	{
		printf("Error:The parentheses did not match successfully.\n");
		can_evaluation=false;
		return ;
	}
}
inline long long evaluation()
{
	mystack<long long>S;
	long long a,b;
	for(int i=1;i<=tot;i++)
	{
		if(F[i]==0)
			S.push(A[i]);
		else
		{
			b=S.top();S.pop();
			a=S.top();S.pop();
			switch((char)(A[i]))
			{
				case '+':S.push(a+b);break;
				case '-':S.push(a-b);break;
				case '*':S.push(a*b);break;
				case '/':
					if(b==0)
					{
						printf("Error:The divisor cannot be 0.\n");
						can_evaluation=false;
						return 114514;
					}
					S.push(a/b);break;
				case '%':
					if(b==0)
					{
						printf("Error:The divisor cannot be 0.\n");
						can_evaluation=false;
						return 114514;
					}
					S.push(a%b);break;
			}
		}
	}
	return S.top();
}
#define Code_By_Lijunzhuo int main()
Code_By_Lijunzhuo
{
	cin>>In_ex;
	In_ex='('+In_ex+')';
//	printf("%s\n",In_ex.c_str());
	xlen=In_ex.length();
	Po_ex(In_ex);
//	for(int i=1;i<=tot;i++)
//	{
//		if(F[i]) printf("%c ",(char)(A[i]));
//		else printf("%lld ",A[i]);
//		printf("%d\n",F[i]);
//	}
	long long ans=evaluation();
	if(!can_evaluation)
		return 0;
	cout<<ans<<"\n";
	return 0;
}

无注释版:

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e5;
string In_ex;
string In_explus;
bool can_evaluation=true;
int xlen,F[N],tot;
long long A[N];
template<typename T>
class mystack
{
	private:
		struct pointer
		{
			T val;
			pointer *ne=nullptr;
		}*head=nullptr;
		size_t tot=0;
	public:
		~mystack()
		{
        	while (!empty())
            	pop();
    	}
		void push(T i)
		{
			pointer*new_push=new pointer;
			new_push->val=i;
			new_push->ne=head;
			head=new_push;
			++tot;
		}
		bool pop()
		{
			if(!tot) return false;
			head=head->ne;
			--tot;
			return true;
		}
		int size(){return tot;}
		bool empty(){return !tot;}
		T top(){return head->val;}
};
inline int f(char c)
{
	if(c<='9'&&c>='0') return 0;
	else if(c=='-'||c=='+') return 1;
	else if(c=='/'||c=='*'||c=='%') return 2;
	else if(c=='(') return 3;
	else if(c==')') return 4;
	else return -1;
}
inline bool check_test(string x)
{
	In_explus="";
	for(int i=0;i<xlen;i++)
	{
		if(x[i]=='['||x[i]=='{') x[i]='(';
		if(x[i]==']'||x[i]=='}') x[i]=')';
		if(f(x[i])==-1)
		{
			printf("Error:Unknown operator.\n");
			return true;
		}
		if((i>0&&f(x[i])!=0&&f(x[i-1]))!=0&&(f(x[i])!=3&&f(x[i-1])!=3&&f(x[i])!=4&&f(x[i-1])!=4))
		{
			printf("Error:Undefined operation.\n");
			return true;
		}
		if(i>0&&(f(x[i])==2&&f(x[i-1])==3))
		{
			printf("Error:Undefined operation.\n");
			return true;
		}
		if((i>0)&&((f(x[i])!=0&&f(x[i])!=3&&f(x[i])!=4)&&f(x[i-1])==3))
			In_explus+='0';
		In_explus+=x[i];
	}
	return false;
}
inline void Po_ex(string x)
{
	if(check_test(x))
	{
		can_evaluation=false;
		return ;
	}
	bool is_last_number=false;
	x=In_explus;xlen=x.length();
	mystack<char>C;
	for(int i=0;i<xlen;i++)
	{
		if(f(x[i])==0)
		{
			if(!is_last_number) A[++tot]=x[i]-'0',is_last_number=true;
			else A[tot]=A[tot]*10+x[i]-'0',is_last_number=true;
		}else if(f(x[i])==1)
		{
			while(!C.empty()&&(C.top()!='('))
				A[++tot]=(int)(C.top()),F[tot]=1,C.pop();
			C.push(x[i]);
			is_last_number=false;
		}else if(f(x[i])==2)
		{
			while(!C.empty()&&(C.top()!='('&&f(C.top())!=1))
				A[++tot]=(int)(C.top()),F[tot]=1,C.pop();
			C.push(x[i]);
			is_last_number=false;
		}else if(f(x[i])==3)
		{
			C.push(x[i]);
			is_last_number=false;
		}
		else if(f(x[i])==4)
		{
			while(!C.empty()&&C.top()!='(')
				A[++tot]=(int)(C.top()),F[tot]=1,C.pop();
			if(C.empty())
			{
				printf("Error:The parentheses did not match successfully.\n");
				can_evaluation=false;
				return ;
			}
			C.pop();
			is_last_number=false;
		}
	}
	if(!C.empty())
	{
		printf("Error:The parentheses did not match successfully.\n");
		can_evaluation=false;
		return ;
	}
}
inline long long evaluation()
{
	mystack<long long>S;
	long long a,b;
	for(int i=1;i<=tot;i++)
	{
		if(F[i]==0)
			S.push(A[i]);
		else
		{
			b=S.top();S.pop();
			a=S.top();S.pop();
			switch((char)(A[i]))
			{
				case '+':S.push(a+b);break;
				case '-':S.push(a-b);break;
				case '*':S.push(a*b);break;
				case '/':
					if(b==0)
					{
						printf("Error:The divisor cannot be 0.\n");
						can_evaluation=false;
						return 114514;
					}
					S.push(a/b);break;
				case '%':
					if(b==0)
					{
						printf("Error:The divisor cannot be 0.\n");
						can_evaluation=false;
						return 114514;
					}
					S.push(a%b);break;
			}
		}
	}
	return S.top();
}
int main()
{
	cin>>In_ex;
	In_ex='('+In_ex+')';
	xlen=In_ex.length();
	Po_ex(In_ex);
	long long ans=evaluation();
	if(!can_evaluation)
		return 0;
	cout<<ans<<"\n";
	return 0;
}

小数版(暂不支持输入含有“.”的字符串)

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e5;
string In_ex;
string In_explus;
bool can_evaluation=true;
int xlen,F[N],tot;
double A[N];
template<typename T>
class mystack
{
	private:
		struct pointer
		{
			T val;
			pointer *ne=nullptr;
		}*head=nullptr;
		size_t tot=0;
	public:
		~mystack()
		{
        	while (!empty())
            	pop();
    	}
		void push(T i)
		{
			pointer*new_push=new pointer;
			new_push->val=i;
			new_push->ne=head;
			head=new_push;
			++tot;
		}
		bool pop()
		{
			if(!tot) return false;
			head=head->ne;
			--tot;
			return true;
		}
		int size(){return tot;}
		bool empty(){return !tot;}
		T top(){return head->val;}
};
inline int f(char c)
{
	if(c<='9'&&c>='0') return 0;
	else if(c=='-'||c=='+') return 1;
	else if(c=='/'||c=='*'||c=='%') return 2;
	else if(c=='(') return 3;
	else if(c==')') return 4;
	else return -1;
}
inline bool check_test(string x)
{
	In_explus="";
	for(int i=0;i<xlen;i++)
	{
		if(x[i]=='['||x[i]=='{') x[i]='(';
		if(x[i]==']'||x[i]=='}') x[i]=')';
		if(f(x[i])==-1)
		{
			printf("Error:Unknown operator.\n");
			return true;
		}
		if((i>0&&f(x[i])!=0&&f(x[i-1]))!=0&&(f(x[i])!=3&&f(x[i-1])!=3&&f(x[i])!=4&&f(x[i-1])!=4))
		{
			printf("Error:Undefined operation.\n");
			return true;
		}
		if(i>0&&(f(x[i])==2&&f(x[i-1])==3))
		{
			printf("Error:Undefined operation.\n");
			return true;
		}
		if((i>0)&&((f(x[i])!=0&&f(x[i])!=3&&f(x[i])!=4)&&f(x[i-1])==3))
			In_explus+='0';
		In_explus+=x[i];
	}
	return false;
}
inline void Po_ex(string x)
{
	if(check_test(x))
	{
		can_evaluation=false;
		return ;
	}
	bool is_last_number=false;
	x=In_explus;xlen=x.length();
	mystack<char>C;
	for(int i=0;i<xlen;i++)
	{
		if(f(x[i])==0)
		{
			if(!is_last_number) A[++tot]=x[i]-'0',is_last_number=true;
			else A[tot]=A[tot]*10+x[i]-'0',is_last_number=true;
		}else if(f(x[i])==1)
		{
			while(!C.empty()&&(C.top()!='('))
				A[++tot]=(int)(C.top()),F[tot]=1,C.pop();
			C.push(x[i]);
			is_last_number=false;
		}else if(f(x[i])==2)
		{
			while(!C.empty()&&(C.top()!='('&&f(C.top())!=1))
				A[++tot]=(int)(C.top()),F[tot]=1,C.pop();
			C.push(x[i]);
			is_last_number=false;
		}else if(f(x[i])==3)
		{
			C.push(x[i]);
			is_last_number=false;
		}
		else if(f(x[i])==4)
		{
			while(!C.empty()&&C.top()!='(')
				A[++tot]=(int)(C.top()),F[tot]=1,C.pop();
			if(C.empty())
			{
				printf("Error:The parentheses did not match successfully.\n");
				can_evaluation=false;
				return ;
			}
			C.pop();
			is_last_number=false;
		}
	}
	if(!C.empty())
	{
		printf("Error:The parentheses did not match successfully.\n");
		can_evaluation=false;
		return ;
	}
}
inline double evaluation()
{
	mystack<double>S;
	double a,b;
	for(int i=1;i<=tot;i++)
	{
		if(F[i]==0)
			S.push(A[i]);
		else
		{
			b=S.top();S.pop();
			a=S.top();S.pop();
			switch((char)(A[i]))
			{
				case '+':S.push(a+b);break;
				case '-':S.push(a-b);break;
				case '*':S.push(a*b);break;
				case '/':
					if(b==0)
					{
						printf("Error:The divisor cannot be 0.\n");
						can_evaluation=false;
						return 114514;
					}
					S.push(a/b);break;
				case '%':
					if(b==0)
					{
						printf("Error:The divisor cannot be 0.\n");
						can_evaluation=false;
						return 114514;
					}
					S.push(fmod(a,b));break;
			}
		}
	}
	return S.top();
}
int main()
{
	cin>>In_ex;
	In_ex='('+In_ex+')';
	xlen=In_ex.length();
	Po_ex(In_ex);
	double ans=evaluation();
	if(!can_evaluation)
		return 0;
	printf("%.2lf",ans);
	return 0;
}

带乘方版:

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e5;
string In_ex;
string In_explus;
bool can_evaluation=true;
int xlen,F[N],tot;
long long A[N];
long long ans;
long long KSM(long long a,long long b)
{
	ans=1;
	while(b)
	{
		if(b&1)
			ans=ans*a;
		b>>=1;
		a=a*a;
	}
	return ans;
}
template<typename T>
class mystack
{
	private:
		struct pointer
		{
			T val;
			pointer *ne=nullptr;
		}*head=nullptr;
		size_t tot=0;
	public:
		~mystack()
		{
        	while (!empty())
            	pop();
    	}
		void push(T i)
		{
			pointer*new_push=new pointer;
			new_push->val=i;
			new_push->ne=head;
			head=new_push;
			++tot;
		}
		bool pop()
		{
			if(!tot) return false;
			head=head->ne;
			--tot;
			return true;
		}
		int size(){return tot;}
		bool empty(){return !tot;}
		T top(){return head->val;}
};
inline int f(char c)
{
	if(c<='9'&&c>='0') return 0;
	else if(c=='-'||c=='+') return 1;
	else if(c=='/'||c=='*'||c=='%') return 2;
	else if(c=='(') return 3;
	else if(c==')') return 4;
	else if(c=='^') return 5;
	else return -1;
}
inline bool check_test(string x)
{
	In_explus="";
	for(int i=0;i<xlen;i++)
	{
		if(x[i]=='['||x[i]=='{') x[i]='(';
		if(x[i]==']'||x[i]=='}') x[i]=')';
		if(f(x[i])==-1)
		{
			printf("Error:Unknown operator.\n");
			return true;
		}
		if((i>0&&f(x[i])!=0&&f(x[i-1]))!=0&&(f(x[i])!=3&&f(x[i-1])!=3&&f(x[i])!=4&&f(x[i-1])!=4))
		{
			printf("Error:Undefined operation.\n");
			return true;
		}
		if(i>0&&(f(x[i])==2&&f(x[i-1])==3))
		{
			printf("Error:Undefined operation.\n");
			return true;
		}
		if((i>0)&&((f(x[i])!=0&&f(x[i])!=3&&f(x[i])!=4)&&f(x[i-1])==3))
			In_explus+='0';
		In_explus+=x[i];
	}
	return false;
}
inline void Po_ex(string x)
{
	if(check_test(x))
	{
		can_evaluation=false;
		return ;
	}
	bool is_last_number=false;
	x=In_explus;xlen=x.length();
	mystack<char>C;
	for(int i=0;i<xlen;i++)
	{
		if(f(x[i])==0)
		{
			if(!is_last_number) A[++tot]=x[i]-'0',is_last_number=true;
			else A[tot]=A[tot]*10+x[i]-'0',is_last_number=true;
		}else if(f(x[i])==1)
		{
			while(!C.empty()&&(C.top()!='('))
				A[++tot]=(int)(C.top()),F[tot]=1,C.pop();
			C.push(x[i]);
			is_last_number=false;
		}else if(f(x[i])==2)
		{
			while(!C.empty()&&(C.top()!='('&&f(C.top())!=1))
				A[++tot]=(int)(C.top()),F[tot]=1,C.pop();
			C.push(x[i]);
			is_last_number=false;
		}else if(f(x[i])==3)
		{
			C.push(x[i]);
			is_last_number=false;
		}
		else if(f(x[i])==4)
		{
			while(!C.empty()&&C.top()!='(')
				A[++tot]=(int)(C.top()),F[tot]=1,C.pop();
			if(C.empty())
			{
				printf("Error:The parentheses did not match successfully.\n");
				can_evaluation=false;
				return ;
			}
			C.pop();
			is_last_number=false;
		}else
		{
			while(!C.empty()&&(C.top()!='('&&f(C.top())!=1&&f(C.top())!=2))
				A[++tot]=(int)(C.top()),F[tot]=1,C.pop();
			C.push(x[i]);
			is_last_number=false;
		}
	}
	if(!C.empty())
	{
		printf("Error:The parentheses did not match successfully.\n");
		can_evaluation=false;
		return ;
	}
}
inline long long evaluation()
{
	mystack<long long>S;
	long long a,b;
	for(int i=1;i<=tot;i++)
	{
		if(F[i]==0)
			S.push(A[i]);
		else
		{
			b=S.top();S.pop();
			a=S.top();S.pop();
			switch((char)(A[i]))
			{
				case '+':S.push(a+b);break;
				case '-':S.push(a-b);break;
				case '*':S.push(a*b);break;
				case '/':
					if(b==0)
					{
						printf("Error:The divisor cannot be 0.\n");
						can_evaluation=false;
						return 114514;
					}
					S.push(a/b);break;
				case '%':
					if(b==0)
					{
						printf("Error:The divisor cannot be 0.\n");
						can_evaluation=false;
						return 114514;
					}
					S.push(a%b);break;
				case '^':
					S.push((long long)(pow(a,b)));
			}
		}
	}
	return S.top();
}
int main()
{
	cin>>In_ex;
	In_ex='('+In_ex+')';
	xlen=In_ex.length();
	Po_ex(In_ex);
	long long ans=evaluation();
	if(!can_evaluation)
		return 0;
	cout<<ans<<"\n";
	return 0;
}