Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
59 lines (50 loc) · 2.12 KB

File metadata and controls

59 lines (50 loc) · 2.12 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stack>
#include<string.h>
using namespace std;
int main()
{
int N;// 表示数据的组数;
char ch[1003]; // 用来存取表达式;
char resultStr[1003]; // 用来存放输出结果的字符串;
cin>>N;
while(N--)
{
stack<char> chStack; // 用来存放字符的栈;
int f=0;// 用来标识当前 ResultStr 中的实际字符数;
memset(ch,'\0',sizeof(ch));
memset(resultStr,'\0',sizeof(resultStr)); // 由于是多组数据的测试,所以每次用之前必须要对字符数组做 置空 处理,否则会出现意想不到的错误;
cin>>ch;
for(int i=0;i<(int)strlen(ch);i++)
{
if(ch[i]>='0'&&ch[i]<='9') // 如果是数据,则用字符数组存起来;
{
resultStr[f]=ch[i];f++;
}
else
{
// 可以把小面的分情况讨论写成一个用于判断的函数,那样的话可能会清晰一点,很简单,在这里就不写啦;
if(chStack.empty()) {chStack.push(ch[i]);continue;}
if((chStack.top()=='*'||chStack.top()=='/')&&(ch[i]=='*'||ch[i]=='/'))
{ resultStr[f]=chStack.top();f++;chStack.pop(); i--;continue; }
if((chStack.top()=='+'||chStack.top()=='-')&&(ch[i]=='+'||ch[i]=='-'))
{ resultStr[f]=chStack.top();f++;chStack.pop(); i--;continue; }
if((chStack.top()=='*'||chStack.top()=='/')&&(ch[i]=='+'||ch[i]=='-'))
{ resultStr[f]=chStack.top();f++;chStack.pop(); i--;continue; }
if((chStack.top()=='+'||chStack.top()=='-')&&(ch[i]=='*'||ch[i]=='/'))
{ chStack.push(ch[i]); continue; }
if(chStack.top()=='('&&ch[i]!=')') { chStack.push(ch[i]); continue; }
if(chStack.top()=='('&&ch[i]==')') {chStack.pop();continue;}
if(chStack.top()!='('&&ch[i]==')') {resultStr[f]=chStack.top();f++;chStack.pop(); i--;continue;}
if(ch[i]=='(') { chStack.push(ch[i]); continue; }
}
}
cout<<resultStr;
while(!chStack.empty()) // 如果栈中还有元素,那么就一次弹出所有的元素;
{
cout<<chStack.top();
chStack.pop();
}
cout<<endl;
}
return 0;
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.