2
已解决
王梓轩
资深光能
资深光能
#include<bits/stdc++.h>
#include<windows.h>
#include<mmsystem.h>
#include<locale.h>
#include<direct.h>
#include<tchar.h>
#include<conio.h>
#pragma comment(lib,"winmm.lib")
using namespace std;
string gjdjia(string a,string b)
{
string gjdjian(string,string);
bool bijiao(string,string);
string c="";
int flag=0;
if(a[0]=='-'&&b[0]=='-')flag=1,a.erase(0,1),b.erase(0,1);
if(b[0]=='-'&&a[0]!='-')swap(a,b);
if(a[0]=='-'&&b[0]!='-')
{
a.erase(0,1);
if(a==b)return "0";
return bijiao(a,b)?'-'+gjdjian(a,b):gjdjian(b,a);
}
reverse(a.begin(),a.end()),reverse(b.begin(),b.end());
int t=0;
for(int i=0; i<a.size()||i<b.size(); i++)
{
if(i<a.size())t+=a[i]-'0';
if(i<b.size())t+=b[i]-'0';
c+=t%10+'0';
t/=10;
}
if(t)c+=t%10+'0';
if(flag)c+='-';
reverse(c.begin(),c.end());
return c;
}
string gjdjian(string a,string b)
{
int flag=0;
if(a==b||a.substr(1,a.size())==b||b.substr(1,b.size())==a)return "0";
if((a[0]=='-'&&b[0]=='-')||(a[0]!='-'&&b[0]=='-'))
{
b.erase(0,1);
return gjdjia(a,b);
}
if(a[0]=='-'&&b[0]!='-')
{
a.erase(0,1);
return '-'+gjdjia(a,b);
}
string ans;
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
if(a.size()<b.size()||b.size()==a.size()&&b>a)swap(a,b),flag=1;
while(b.size()<a.size())b+='0';
int c[10005]= {0},k=a.size();
for(int i=0; i<a.size(); i++)
{
if(a[i]<b[i])a[i]+=10,a[i+1]--;
c[i]=a[i]-b[i];
}
while(c[k]==0&&k>=0)k--;
while(k>=0)
ans+=c[k--]+'0';
if(flag)
return '-'+ans;
return ans;
}
string gjdcheng(string a,string b)
{
string ans;
int flag=0;
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
if(a[a.size()-1]=='-'&&b[b.size()-1]!='-')flag=1,a.erase(a.size()-1,a.size());
if(b[b.size()-1]=='-'&&a[a.size()-1]!='-')flag=1,b.erase(b.size()-1,b.size());
if(a[a.size()-1]=='-')a.erase(a.size()-1,a.size());
if(b[b.size()-1]=='-')b.erase(b.size()-1,b.size());
int c[10005]= {0},k=a.size()+b.size()+1;
for(int i=0; i<a.size(); i++)
for(int j=0; j<b.size(); j++)
{
c[i+j]+=(a[i]-'0')*(b[j]-'0');
c[i+j+1]+=c[i+j]/10;
c[i+j]%=10;
}
while(c[k]==0&&k>=0)k--;
while(k>=0)
ans+=c[k--]+'0';
if(ans=="")return "0";
if(flag)
return '-'+ans;
return ans;
}
bool bijiao(string a,string b)
{
if(a.size()>b.size()||a.size()==b.size()&&a>=b)return 1;
return 0;
}
string gjdchu(string a,string b,int x)
{
if(b=="0")return "error";
int flag=0,flagg=0;
string c;
if(a[0]=='-')a.erase(0,1),flag=1,flagg=1;
if(b[0]=='-')
{
b.erase(0,1);
if(flag==1)flag=0;
else flag=1;
}
int t=b.size(),pos;
for(int i=b.size(); i<a.size(); i++)b+='0';
while(t<=b.size())
{
int s=0;
while(bijiao(a,b))
{
a=gjdjian(a,b);
s++;
}
b=b.substr(0,b.size()-1);
c+=s+'0';
}
string::size_type i=c.find_first_not_of('0');
if(i!=string::npos)c.erase(0,i);
if(x==1)
{
if(c==""||c=="-0")return "0";
if(flag)
{
if(c=="0")return "0";
return '-'+c;
}
return c;
}
else
{
if(a==""||a=="-0")return "0";
if(flagg)
{
if(a=="0")return "0";
return '-'+a;
}
return a;
}
}
bool judge(string a)
{
for(int i=1; i<a.size()-1; i++)
{
if(isdigit(a[i])||((a[i]=='('&&isdigit(a[i+1])&&(a[i-1]=='+'||a[i-1]=='-'||a[i-1]=='*'||a[i-1]=='/'))||(isdigit(a[i+1])&&(a[i]==')'&&(a[i+1]=='+'||a[i+1]=='-'||a[i+1]=='*'||a[i+1]=='/')))))continue;//数字或者正常括号,合理
else if(a[i]=='+'||a[i]=='-'||a[i]=='*'||a[i]=='/')//符号且有数字在两边,合理
if(isdigit(a[i-1])&&isdigit(a[i+1]))continue;
else if((a[i]=='+'||a[i]=='-'||a[i]=='*'||a[i]=='/')&&a[i+1]=='-')//负数,合理
{
i++;
continue;
}
else if(a[i]=='-'&&i==1)continue;//开头负数,合理
return 0;//不合理
}
for(int i=1; i<a.size()-1; i++)
if(a[i]=='(')
{
int flag=0;
for(int j=a.size();j>=i+1;j--)
{
if(a[j]==')')
{
flag=1;
if(!judge(a.substr(i+1,j-1)))return 0;
}
}
if(flag==0)return 0;
}
return 1;
}
int main()
{
string a;
getline(cin,a);
a=' '+a+' ';
if(!judge(a))
{
cout<<"表达式不合法!";
return 0;
}
for(int i=1; i<a.size()-1; i++)
{
if(a[i]=='*')
{
int begin,end;
for(int j=i-1; j>=1; j--)
if(isdigit(a[j])&&(a[j-1]=='+'||a[j-1]=='-'||a[j-1]=='*'||a[j-1]=='/'||a[j-1]==' '))
{
begin=j;
if(a[begin]=='-')begin--;
break;
}
for(int j=i+1; j<=a.size()-1; j++)
if(isdigit(a[j])&&(a[j+1]=='+'||a[j+1]=='-'||a[j+1]=='*'||a[j+1]=='/'||a[j+1]==' '))
{
end=j;
break;
}
string z=a.substr(begin,i-begin),y=a.substr(i+1,end-i);
a.replace(begin,end-begin+1,gjdcheng(z,y));
i=begin;
// cout<<a.substr(1,a.size()-2)<<endl;
}
if(a[i]=='/')
{
int begin,end;
for(int j=i-1; j>=1; j--)
if(isdigit(a[j])&&(a[j-1]=='+'||a[j-1]=='-'||a[j-1]=='*'||a[j-1]=='/'||a[j-1]==' '))
{
begin=j;
if(a[begin]=='-')begin--;
break;
}
for(int j=i+1; j<=a.size()-1; j++)
if(isdigit(a[j])&&(a[j+1]=='+'||a[j+1]=='-'||a[j+1]=='*'||a[j+1]=='/'||a[j+1]==' '))
{
end=j;
break;
}
string z=a.substr(begin,i-begin),y=a.substr(i+1,end-i);
a.replace(begin,end-begin+1,gjdchu(z,y,1));
i=begin;
// cout<<a.substr(1,a.size()-2)<<endl;
}
if(a[1]=='-')
{
int flag=0;
for(int j=2; j<a.size()-1; j++)
if(isdigit(a[j]))continue;
else
{
flag=1;
break;
}
if(flag==0)goto end;
}
}
for(int i=1; i<a.size()-1; i++)
{
if(a[i]=='+')
{
int begin,end;
for(int j=i-1; j>=1; j--)
if(isdigit(a[j])&&(a[j-1]=='+'||a[j-1]=='-'||a[j-1]=='*'||a[j-1]=='/'||a[j-1]==' '))
{
begin=j;
if(a[begin]=='-')begin--;
break;
}
for(int j=i+1; j<=a.size()-1; j++)
if(isdigit(a[j])&&(a[j+1]=='+'||a[j+1]=='-'||a[j+1]=='*'||a[j+1]=='/'||a[j+1]==' '))
{
end=j;
break;
}
string z=a.substr(begin,i-begin),y=a.substr(i+1,end-i);
a.replace(begin,end-begin+1,gjdjia(z,y));
i=begin;
// cout<<a.substr(1,a.size()-2)<<endl;
}
if(a[i]=='-')
{
int begin,end;
for(int j=i-1; j>=1; j--)
if(isdigit(a[j])&&(a[j-1]=='+'||a[j-1]=='-'||a[j-1]=='*'||a[j-1]=='/'||a[j-1]==' '))
{
begin=j;
if(a[begin]=='-')begin--;
break;
}
for(int j=i+1; j<=a.size()-1; j++)
if(isdigit(a[j])&&(a[j+1]=='+'||a[j+1]=='-'||a[j+1]=='*'||a[j+1]=='/'||a[j+1]==' '))
{
end=j;
break;
}
string z=a.substr(begin,i-begin),y=a.substr(i+1,end-i);
a.replace(begin,end-begin+1,gjdjian(z,y));
i=begin;
// cout<<a.substr(1,a.size()-2)<<endl;
}
if(a[1]=='-')
{
int flag=0;
for(int j=2; j<a.size()-1; j++)
if(isdigit(a[j]))continue;
else
{
flag=1;
break;
}
if(flag==0)
{
cout<<a.substr(1,a.size()-2);
return 0;
}
}
}
end:
cout<<a.substr(1,a.size()-2);
}
来人啊!谁能把这个代码改成支持括号的啊!
我就是个蒟蒻