跳到正文

个人记录

cmd+函数计算器=?

NanakoCodamorTulip约 31 分钟
原文
文章信息与版本

文章信息

发布
2024年9月30日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 31 分钟
获赞
2

版本

  1. v1当前

闲的蛋疼弄得,欢迎来找BUG。

C++
#include<iostream>
#include<cmath>
#include<string.h>
#include<stack>
#include<map>
#include<random>
#include<ctime>
#include<time.h>
#include<vector>
#include<iomanip>
#include<windows.h>
#define double long double 
#define BBH "0.2.2"
using namespace std;
char s[1000001];
stack<double>a1;
stack<char>a2;
map<string,string>t;
map<string,string>func;
map<string,string>def;
bool open_deal=false;
double SA_initial_temperature;
double SA_min_temperature;
double SA_cooling_rate;
int SA_times; 
int Long_double_setprecision; 
int level(char a){
	if(a=='x')return 0;
	if(a=='|')return 0;
	if(a=='~')return 0;
	if(a=='&')return 0;
	if(a=='+'||a=='-')return 1;
	if(a=='*'||a=='/')return 2;
	if(a=='^'||a=='r')return 3;
	if(a=='l'||a=='s'||a=='c'||a=='t')return 4;
	return 0;
}
double jisuan(double a,double b,char fh){
	if(fh=='+')return b+a;
	if(fh=='-')return b-a;
	if(fh=='*')return b*a;
	if(fh=='/')return b/a;
	if(fh=='%')return (int)b%(int)a;
	if(fh=='^')return pow(b,a);
	if(fh=='r')return pow(a,1/b);
	if(fh=='l')return log(a);
	if(fh=='s')return sin(a);
	if(fh=='c')return cos(a);
	if(fh=='t')return tan(a);
	if(fh=='x')return (int)a^(int)b;
	if(fh=='|')return (int)a|(int)b;
	if(fh=='~')return ~(int)a;
	if(fh=='&')return (int)a&(int)b;
	cout<<"ERROR:表达式输入错误\n";
	return 0.0/0.0;
}
void cz(){
	double c1=a1.top();
	if(a1.empty()){
		cout<<"ERROR:表达式输入错误\n";
		return;
	}
	a1.pop();
	double c2=a1.top();
	if(a1.empty()){
		cout<<"ERROR:表达式输入错误\n";
		return;
	}
	a1.pop();
	a1.push(jisuan(c1,c2,a2.top()));
}
double deal(){
	if(open_deal)cout<<"deal "<<s+1<<endl;
	while(!a1.empty())a1.pop();
	while(!a2.empty())a2.pop();
	int n=strlen(s+1)+1,cut=0;
	double sum=0;
	a1.push(0);
	for(int i=1;i<n;i++){
		if(s[i]>='0'&&s[i]<='9'||s[i]=='.'){
			if(s[i]=='.')cut=i;
			else{
				if(cut==0)sum=sum*10+s[i]-'0';
				else sum+=(double)pow(10,cut-i)*(s[i]-'0');
			}
			if((s[i+1]<'0'||s[i+1]>'9')&&s[i+1]!='.'){
				a1.push(sum);
				sum=0,cut=0;
			}
			continue;
		}
		if(s[i]=='('){
			a2.push(s[i]);
		}
		else{
			if(s[i]==')'){
				while(!a2.empty()&&a2.top()!='('){
					cz();
					if(a2.empty()){
						cout<<"ERROR:表达式输入错误\n";
						return 0.0/0.0;
					}
					a2.pop();
				}
				if(!a2.empty()&&a2.top()=='('){
					if(a2.empty()){
						cout<<"ERROR:表达式输入错误\n";
						return 0.0/0.0;
					}
					a2.pop();
				}
			}
			else{
				while(!a2.empty()&&level(s[i])<=level(a2.top())){
					cz();
					if(a2.empty()){
						cout<<"ERROR:表达式输入错误\n";
						return 0.0/0.0;
					}
					a2.pop();
				}
				a2.push(s[i]);
			}
		}
	}
	while(!a2.empty()&&a2.top()!='('&&a2.top()!=')'){
		cz();
		a2.pop();
	}
	return a1.top();
}
string get_string(string str,int l,int r){
	string ans;
	for(int i=l;i<=r;i++){
		ans.push_back(str[i]);
	}
	return ans;
}
string op;
string chan(string str,string str2,string str3){//将str1中所有str2替换成str3 
	if(str2.size()>str.size())return str;
	vector<int>p;int cnt=0;
	for(int i=0;i<str.size()-str2.size()+1;i++){
		bool flag=true;
		for(int j=0;j<str2.size();j++){
			if(str[i+j]!=str2[j])flag=false;
		}
		if(flag)p.push_back(i);
	}
	string ans;
	if(p.size()==0)return str;
	for(int i=0;i<str.size();){
		if(p[cnt]==i){
			ans+=str3,cnt++;
			i+=str2.size();
		}else ans+=str[i],i++;
	}
	return ans;
}
char s1[114][114]={"sqrt","root","e","pi","log","sin","cos","tan","xor","~"},s2[114][114]={"2r","r","(2.718281828459)","(3.1415926535)","1l","1s","1c","1t","x","1~"};
double count(string str,bool f){
	str=chan(str," ","");
	for(int i=0;i<10;i++){
		str=chan(str,s1[i],s2[i]);
	}
	int _t=114514,_t2=0;
	while(_t!=0){
		_t=0,_t2++;
		for(auto it=def.begin();it!=def.end();it++){
			string str2=chan(str,it->first,'('+it->second+')');
			if(str!=str2)_t++,str=str2;
    	}
    	if(_t2%10==0){
    		cout<<"WARNING:宏定义可能出错导致死循环\n";
		}
		if(_t2>=100){
			cout<<"ERROR:宏定义出错或过于复杂\n";
			cout<<"OP:操作终止\n";
			return 0.0/0.0;
		}
	}
	for(int i=1;i<str.size()-1;i++){
		if((str[i+1]=='l'||str[i+1]=='s'||str[i+1]=='t'||str[i+1]=='c')&&((str[i-1]<='9'&&str[i-1]>='0')||str[i-1]=='('||str[i-1]==')')){
			str=get_string(str,0,i-1)+'*'+get_string(str,i,str.size()-1);
		}
	}
	for(int i=0;i<str.size()-1;i++){
		if(str[i]==')'&&str[i+1]=='('){
			str=get_string(str,0,i)+'*'+get_string(str,i+1,str.size()-1);
		}
	}
	if(!f)
	for(int i=0;i<str.size();i++){
		if(str[i]=='-'&&(i==0||str[i-1]<'0'||str[i-1]>'9')){
			int cnt=i+1;
			while((str[cnt]>='0'&&str[cnt]<='9')||str[cnt]=='.')cnt++;
			cnt--;
			if(cnt!=i)str=get_string(str,0,i-1)+"(0"+get_string(str,i,cnt)+")"+get_string(str,cnt+1,str.size()-1);
		}
	}
	if(!f)
	for(int i=0;i<str.size()-1;i++){
		if(str[i]==')'&&str[i+1]=='('){
			str=get_string(str,0,i)+'+'+get_string(str,i+1,str.size()-1);
		}
	}
	for(int i=0;i<str.size();i++){
		s[i+1]=str[i];
	}
	s[str.size()+1]='\0';
	return deal();
}
double count_function(string name,double val){
	string bds=func[name];
	if(val<0)bds=chan(bds,"x","(0"+to_string(val)+')');
	else bds=chan(bds,"x",'('+to_string(val)+')');
	return count(bds,true);
}
double SAMAX(string name,double l,double r){
	double ans=(l+r)/2,answ=count_function(name,ans);
	double tmp=SA_initial_temperature;
	while(tmp>SA_min_temperature){
		int rd=rand();
		double now=ans+(2*rand()-RAND_MAX)*tmp;
		if(now<l||now>r)now=1.0*l+1.0*(r-l)/(1.0*rand()/RAND_MAX+1.0);
		double noww=count_function(name,now);
		double delta=answ-noww;
		if(delta<0)ans=now,answ=noww;
		else if(exp(delta/tmp)*RAND_MAX<rand()){
			ans=now,answ=noww;
		}
		tmp*=SA_cooling_rate;
	}
	return ans;
}
double SAMIN(string name,double l,double r){
	double ans=(l+r)/2,answ=count_function(name,ans);
	double tmp=SA_initial_temperature;
	while(tmp>SA_min_temperature){
		int rd=rand();
		double now=ans+(2*rand()-RAND_MAX)*tmp;
		if(now<l||now>r)now=1.0*l+1.0*(r-l)/(1.0*rand()/RAND_MAX+1.0);
		double noww=count_function(name,now);
		double delta=answ-noww;
		if(delta>0)ans=now,answ=noww;
		else if(exp(-delta/tmp)*RAND_MAX<rand()){
			ans=now,answ=noww;
		}
		tmp*=SA_cooling_rate;
	}
	return ans;
}
double function_max(string name,double l,double r){
	double ans=-1e18;
	for(int i=1;i<=SA_times;i++){
		ans=max(ans,SAMAX(name,l,r));
		cout<<"running("<<i<<"/"<<SA_times<<")......\n";
	}
	return ans;
}
double function_min(string name,double l,double r){
	double ans=1e18;
	for(int i=1;i<=SA_times;i++){
		ans=min(ans,SAMIN(name,l,r));
		cout<<"running("<<i<<"/"<<SA_times<<")......\n";
	}
	return ans;
}
void out_time(){
	time_t now=time(0); 
	tm *ltm=localtime(&now); 
	cout<<1900+ltm->tm_year<<"/"; 
	cout<<1+ltm->tm_mon<<"/"; 
	cout<<ltm->tm_mday<<" "; 
	cout<<ltm->tm_hour<<":"; 
	cout<<ltm->tm_min<<":"; 
	cout<<ltm->tm_sec;
}
void get_initn(){
	freopen("count_pro_initn.txt","r",stdin);
	scanf("SA_initial_temperature=%Lf\nSA_min_temperature=%Lf\nSA_cooling_rate=%Lf\nLong_double_setprecision=%d\nSA_times=%d\n",&SA_initial_temperature,&SA_min_temperature,&SA_cooling_rate,&Long_double_setprecision,&SA_times);
	freopen("CON","r",stdin);
}
int main(){
	get_initn();
	int wt=0;def["ans"]="0";
//	printf("%Lf %Lf %Lf %d %d\n",SA_initial_temperature,SA_min_temperature,SA_cooling_rate,Long_double_setprecision,SA_times);
	cout<<"Fertrity count_pro[版本 "<<BBH<<"]\n(c)Fertrity。保留所有权利。\n\n";
	freopen("history_count_pro.txt","a",stdout);
	cout<<"-----------------------------------------\n";
	out_time();
	cout<<"version number:"<<BBH<<"open>"<<endl;
	freopen("CON","w",stdout);
	while(true){
		cin.clear();
		cout<<"C:\\Users\\Administrator>";
		double t1=clock();
		cin>>op;
		double t2=clock();
		if(t2-t1<=100){
			cout<<"ERROR:输入流发生错误\n";
			cin.clear();
			cout<<"尝试重新读入\nC:\\Users\\Administrator>";
			cin>>op;
		}
		if(op=="lf"){
			string name,bds;
			cin>>name;
			getline(cin,bds);
			bds=chan(bds,"\n","");
			freopen("history_count_pro.txt","a",stdout);
			out_time();
			cout<<">"<<op<<" "<<name<<" "<<bds<<endl;
			freopen("CON","w",stdout);
			cout<<"OP:定义函数\n";
			bds=chan(bds," ","");
			func[name]=bds;
			cout<<name<<"(x)="<<bds<<"\n";
			wt=0;
		}
		else if(op=="df"){
			string name;
			cin>>name;
			freopen("history_count_pro.txt","a",stdout);
			out_time();
			cout<<">"<<op<<" "<<name<<endl;
			freopen("CON","w",stdout);
			if(func.count(name)==0){
				cout<<"ERROR:函数\""<<name<<"\"不存在\n";
				cout<<"OP:操作终止\n";
				string str;getline(cin,str);
				wt=0;
				continue;
			}
			func.erase(name);
			cout<<"OP:delat "<<name<<"(x)\n";
			wt=0;
		}
		else if(op=="max"){
			string name;double l,r;
			cin>>name>>l>>r;
			freopen("history_count_pro.txt","a",stdout);
			out_time();
			cout<<">"<<op<<" "<<name<<' '<<l<<' '<<r<<endl;
			freopen("CON","w",stdout);
			cout<<"OP:find max("<<name<<")in["<<l<<","<<r<<"]\n";
			if(func.count(name)==0){
				cout<<"ERROR:函数\""<<name<<"\"不存在\n";
				cout<<"OP:操作终止\n";
				string str;getline(cin,str);
				wt=0;
				continue;
			}
			double maxp=function_max(name,l,r);
			double maxv=count_function(name,maxp);
			cout<<"max:"<<name<<"("<<fixed<<setprecision(Long_double_setprecision)<<maxp<<")="<<fixed<<setprecision(Long_double_setprecision)<<maxv<<endl;
			wt=0,def["ans"]=to_string(maxv);
		}else if(op=="min"){
			string name;double l,r;
			cin>>name>>l>>r;
			freopen("history_count_pro.txt","a",stdout);
			out_time();
			cout<<">"<<op<<" "<<name<<' '<<l<<' '<<r<<endl;
			freopen("CON","w",stdout);
			cout<<"OP:find min("<<name<<")in["<<l<<","<<r<<"]\n";
			if(func.count(name)==0){
				cout<<"ERROR:函数\""<<name<<"\"不存在\n";
				cout<<"OP:操作终止\n";
				string str;getline(cin,str);
				wt=0;
				continue;
			}
			double minp=function_min(name,l,r);
			double minv=count_function(name,minp);
			cout<<"min:"<<name<<"("<<fixed<<setprecision(Long_double_setprecision)<<minp<<")="<<fixed<<setprecision(Long_double_setprecision)<<minv<<"\n";
			wt=0,def["ans"]=to_string(minv);
		}else if(op=="cf"){
			double x;string name;
			cin>>name>>x;
			freopen("history_count_pro.txt","a",stdout);
			out_time();
			cout<<">"<<op<<" "<<name<<' '<<x<<endl;
			freopen("CON","w",stdout);
			if(func.count(name)==0){
				cout<<"ERROR:函数\""<<name<<"\"不存在\n";
				cout<<"OP:操作终止\n";
				continue;
			}
			cout<<name<<"("<<fixed<<setprecision(Long_double_setprecision)<<x<<")="<<fixed<<setprecision(Long_double_setprecision)<<count_function(name,x)<<endl;
			wt=0,def["ans"]=to_string(count_function(name,x));
		}else if(op=="count"){
			string str;getline(cin,str);
			freopen("history_count_pro.txt","a",stdout);
			out_time();
			cout<<">"<<op<<" "<<str<<endl;
			freopen("CON","w",stdout);
			str=chan(str,"\n","");
			str=chan(str," ","");
			cout<<fixed<<setprecision(Long_double_setprecision)<<count(str,false)<<endl;
			wt=0,def["ans"]=to_string(count(str,false));
		}else if(op=="help"){
			freopen("history_count_pro.txt","a",stdout);
			out_time();
			cout<<">"<<op<<endl;
			freopen("CON","w",stdout);
			cout<<"Fertrity count_pro[版本 "<<BBH<<"]\n\n";
			cout<<"Made in China.By 8008008 in Luogu\n\n";
			cout<<"Language:简体中文 支持实数运算 请保证输入的表达式正确\n\n";
			cout<<"count:输入表达式,计算结果|example:count (e^pi)^2-(4*3+sin45)^cos(pi)+114\n\n";
			cout<<"lf:定义/覆盖函数 lf name expression|example:lf y (x^2+1)/2\n\n"; 
			cout<<"cf:计算函数值 cf name val|example:cf y 114514\n\n";
			cout<<"max:计算函数在[l,r]中的最大值,使用模拟退火算法进行求解 max name l r|example:max y -10 10\n\n";
			cout<<"min:计算函数在[l,r]中的最小值,使用模拟退火算法进行求解 min name l r|example:min y -10 10\n\n";
			cout<<"df:删除函数 df name|example:df y\n\n";
			cout<<"define:宏定义 define name name2|example:define N 114514\n\n";
			cout<<"undef:取消宏定义 undef name|example:undef N\n\n";
			cout<<"ans:上一次的答案 ans|example:ans*ans\n\n";
			cout<<"initn:配置SA算法参数&小数精度|example initn\n\n";
			cout<<"history:查看历史记录|example:history\n\n";
			cout<<"touch:联系作者|emample touch\n\n";
			cout<<"end:终止程序|emample end\n\n";
			cout<<"help:帮助|example:help\n\n";
			cout<<"WARNING:本程序根本没有做过优化,因此不建议尝试输入过于复杂的表达式\n\n";
			cout<<"配置count_pro_initn.txt文件(建议参数):\n";
			cout<<"SA_initial_temperature=1000000\n";
			cout<<"SA_min_temperature=0.00000000001\n";
			cout<<"SA_cooling_rate=0.995\n";
			cout<<"Long_double_setprecision=15\n";
			cout<<"SA_times=25\n";
			cout<<"配置完成请重启\n\n";
			cout<<"自动配置请输入\"make_initn\"\n\n";
			wt=0;
		}
		else if(op=="end"){
			freopen("history_count_pro.txt","a",stdout);
			out_time();
			cout<<">"<<op<<endl;
			freopen("CON","w",stdout);
			break;
		}else if(op=="history"){
			freopen("history_count_pro.txt","a",stdout);
			out_time();
			cout<<">"<<op<<endl;
			freopen("CON","w",stdout);
			system("history_count_pro.txt");
			wt=0;
		}else if(op=="touch"){
			freopen("history_count_pro.txt","a",stdout);
			out_time();
			cout<<">"<<op<<endl;
			freopen("CON","w",stdout);
			system("start https://www.luogu.com.cn/user/803885");
			wt=0;
		}else if(op=="define"){
			wt=0;
			string str1,str2;
			cin>>str1>>str2;
			freopen("history_count_pro.txt","a",stdout);
			out_time();
			cout<<">"<<op<<' '<<str2<<' '<<str2<<endl;
			freopen("CON","w",stdout);
			cout<<"OP:宏定义\n";
			for(int i=0;i<8;i++){
				if(str1==s1[i]){
					cout<<"ERROR:"<<str1<<"不可以被定义\n";
					cout<<"OP:操作终止\n";
					continue;
				}
			}
			if(str1==str2){
				cout<<"无效的宏定义\n";
				cout<<"OP:操作终止\n";
				continue;
			}
			if(str1=="define"){
				cout<<"ERROR:不可以定义\"define\"\n";
				cout<<"OP:操作终止\n";
				continue;
			}
			if(str1=="ans"){
				cout<<"ERROR:不可以定义\"ans\"\n";
				cout<<"OP:操作终止\n";
				continue;
			}
			cout<<"\""<<str1<<"\" "<<"to \""<<str2<<"\"\n";
			def[str1]=str2;
		}else if(op=="undef"){
			wt=0;
			string str;
			cin>>str;
			freopen("history_count_pro.txt","a",stdout);
			out_time();
			cout<<">"<<op<<' '<<str<<endl;
			freopen("CON","w",stdout);
			cout<<"OP:取消宏定义\n";
			if(def.count(str)==0){
				cout<<"ERROR:宏\""<<str<<"\"不存在\n";
				cout<<"OP:操作终止\n";
				continue;
			}
			if(str=="ans"){
				cout<<"ERROR:ans无法取消\n";
				continue;
			}
			cout<<"OP:delat "<<str<<" "<<def[str]<<"\n";
			def.erase(str);
		}else if(op=="open_deal"){
			freopen("history_count_pro.txt","a",stdout);
			out_time();
			cout<<">"<<op<<endl;
			freopen("CON","w",stdout);
			cout<<"open_deal=true\n";
			open_deal=true;
		}else if(op=="close_deal"){
			wt=0;
			freopen("history_count_pro.txt","a",stdout);
			out_time();
			cout<<">"<<op<<endl;
			freopen("CON","w",stdout);
			cout<<"open_deal=false\n";
			open_deal=false;
		}else if(op=="initn"){
			system("count_pro_initn.txt");
			freopen("history_count_pro.txt","a",stdout);
			out_time();
			cout<<">"<<op<<endl;
			freopen("CON","w",stdout);
			freopen("CON","a",stdout);
			cout.clear();
			cout<<"三秒后重启程序以更新";
			Sleep(3000);
			return 0;
		}else if(op=="make_initn"){
			freopen("history_count_pro.txt","a",stdout);
			out_time();
			cout<<">"<<op<<endl;
			freopen("CON","w",stdout);
			freopen("count_pro_initn.txt","w",stdout);
			cout<<"SA_initial_temperature=1000000\n";
			cout<<"SA_min_temperature=0.00000000001\n";
			cout<<"SA_cooling_rate=0.995\n";
			cout<<"Long_double_setprecision=15\n";
			cout<<"SA_times=25\n";
			cout<<"After configuration is completed,please save to update this configuration file and restart the program.\n";
			freopen("CON","w",stdout);
			cin.clear();cout.clear();
			cout<<"三秒后重启程序以更新";
			Sleep(3000);
			return 0;
		}
		else{
			wt++;
			if(wt%5==0)cout<<"你可能需要帮助(输入\"help\")\n";
			string str;getline(cin,str);
			freopen("history_count_pro.txt","a",stdout);
			out_time();
			cout<<"invalid operation>"<<op<<str<<endl;
			freopen("CON","w",stdout);
			cout<<"无法识别\n"; 
		}
	}
	return 0;
}
原文