爆马蜂
#include<bits/stdc++.h>
using namespace std;
#define _ ;
#define __ ,
#define ___ int
#define ____ main
#define _____ {
#define ______ }
#define _______ +
#define ________ (
#define _________ )
#define __________ ::
#define ___________ cin
#define ____________ cout
#define _____________ >>
#define ______________ <<
#define _______________ return 0
___ ________________ __ _________________ _ ___ ____ ________ _________ _____ ___________ _____________ ________________ _____________ _________________ _ ____________ ______________ ________________ _______ _________________ _ _______________ _ ______
开个玩笑,真正的马蜂长这样:
#include<bits/stdc++.h>
using namespace std;
// #define fileio
#define IOS
void ___()
{
#ifdef fileio
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
#ifdef IOS
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define endl '\n'
#endif
}
#define ll long long
string ex;
stack<char> dat,op;
stack<int> s,tmp;
int prio(char c)//优先级
{
switch(c)
{
case '+': return 1;
case '-': return 1;
case '*': return 2;
case '/': return 2;
case '^': return 3;
default: return -1;
}
}
void change(string s)//中缀s转后缀存到dat
{
int ln=s.length();
for(int i=0;i<ln;i++)
{
if(s[i]>='0'&&s[i]<='9')
dat.push(s[i]);
else if(s[i]=='(')
op.push(s[i]);
else if(s[i]==')')
{
while(op.top()!='(')
{
dat.push(op.top());
op.pop();
}
op.pop();
}
else if(s[i]=='^')
op.push(s[i]);
else
{
while(!op.empty()&&prio(op.top())>=prio(s[i]))
{
dat.push(op.top());
op.pop();
}
op.push(s[i]);
}
}
while(!op.empty())
{
dat.push(op.top());
op.pop();
}
while(!dat.empty())
{
op.push(dat.top());
dat.pop();
}
}
int calc(int x,int y,char c)//计算
{
switch(c)
{
case '+': return x+y;
case '-': return x-y;
case '*': return x*y;
case '/': return x/y;
case '^': return pow(x,y);
default: return -1;
}
}
int main()
{
___();
cin>>ex;
change(ex);
char t;
int x,y;
while(!op.empty())
{
cout<<op.top()<<" ";
dat.push(op.top());
op.pop();
}
while(!dat.empty())
{
op.push(dat.top());
dat.pop();
}
cout<<endl;
while(!op.empty())
{
t=op.top();
op.pop();
if(t>='0'&&t<='9')
s.push(t-'0');
if(t=='+'||t=='-'||t=='*'||t=='/'||t=='^')
{
y=s.top(),s.pop();
x=s.top(),s.pop();
s.push(calc(x,y,t));
while(!s.empty())
{
tmp.push(s.top());
s.pop();
}
while(!tmp.empty())
{
cout<<tmp.top()<<" ";
s.push(tmp.top());
tmp.pop();
}
while(!op.empty())
{
cout<<op.top()<<" ";
dat.push(op.top());
op.pop();
}
while(!dat.empty())
{
op.push(dat.top());
dat.pop();
}
cout<<endl;
}
}
return 0;
}
- 头文件:万能头(虽然以前不用)。
- 缺省源:一个含有预编译指令的三个下划线初始化函数,内含去同步和文件输入输出,
十分奇怪;#define ll long long。 - 输入输出:一般用去同步
cin和cout并#define endl '\n',需要格式化的时候会开同步用scanf和printf,不写快读。 - 大括号:换行。
- 空格:运算符和变量或逗号后不打空格,定义 stack 或 queue 等 STL 时尖括号框柱的类型名的右尖括号后面打空格。
- 当
if或for等东西后面只有一行代码时,换行+缩进。 switch:如果是像这个代码里直接 return 的就写在同一行,否则不打大括号,分行+缩进。- 注释:很少打。
- 变量:一般定义在全局,名称一般按题目说明,不喜欢大写变量名。
- 函数:有库函数就用库函数。
差不多就这样。(猜的出来我贴的哪题代码吗?第一个猜出来奖一关