计算器原理
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<iostream>
#include<stack>
#include<vector>
#include<queue>
#include<string>
#include<algorithm>
using namespace std;
#define MaxSize 1005
stack<char>sta;
void operate(char c)
{
char t;
if(sta.empty())
{
sta.push(c);
}
else
{
t=sta.top();
if(((t=='+'||t=='-')&&(c=='+'||c=='-'))||((t=='*'||t=='/')&&(c=='*'||c=='/')))
{
printf("%c",t);
sta.pop();
operate(c);
}
else if((t=='+'||t=='-')&&(c=='*'||c=='/'))
{
sta.push(c);
}
else if((t=='*'||t=='/')&&(c=='+'||c=='-'))
{
printf("%c",t);
sta.pop();
operate(c);
}
else if(c=='(')
{
sta.push(c);
}
else if(c==')')
{
while(sta.top()!='(')
{
printf("%c",sta.top());
sta.pop();
}
sta.pop();
}
else
{
sta.push(c);
}
}
}
int main()
{
char s[MaxSize];
scanf("%s",s);
int len = strlen(s);
for(int i=0; i<len; i++)
{
if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z'))//可以改成数字字符
printf("%c",s[i]);
else
operate(s[i]);
}
while(!sta.empty())
{
char k=sta.top();
sta.pop();
printf("%c",k);
}
return 0;
}