PTA 3.11表达式转换
来源:互联网 发布:linux 自启动 编辑:程序博客网 时间:2024/06/05 10:12
有一个测试点过不了,希望大家指点。
/* * 1.cpp * * Created on: 2017年8月23日 * Author: Administrator */#include <bits/stdc++.h>using namespace std ;int main(){string s;cin>>s;stack<char> st;bool flag = false;for( int i = 0; i < s.length()&&s.length()<=20; i++){//负数情况if( i == 0 && s[i] == '-'){cout<<s[i];}//多位数及小数else if(isdigit(s[i])||s[i]=='.'){if(i>0&&(isdigit(s[i-1])||s[i-1]=='.')){cout<<s[i];}else if(!flag){flag = true;cout<<s[i];}else{cout<<" "<<s[i];}}//左括号else if(s[i]=='('){st.push(s[i]);}//右括号else if(s[i]==')'){while(st.top()!='('){if(!flag){flag = true;cout<<st.top();}else{cout<<" "<<st.top();}st.pop();}st.pop();}//+,— 号else if(s[i]=='+'||s[i]=='-'){//括号内有负数,直接输出if(s[i-1]=='('){if(s[i]=='-'){if(!flag){flag = true;cout<<s[i]<<s[i+1];i++;}else{cout<<" "<<s[i]<<s[i+1];i++;}}}//优先级问题else{if(!st.empty()){while(!st.empty()&&(st.top()=='*'||st.top()=='/')){if(!flag){flag = true;cout<<st.top();}else{cout<<" "<<st.top();}st.pop();}while(!st.empty()&&(st.top() == '+' || st.top() == '-')){if(!flag){flag = true;cout<<st.top();}else{cout<<" "<<st.top();}st.pop();}}st.push(s[i]);}}//*、/号else if(s[i]=='*'||s[i]=='/'){if(!st.empty()){while(!st.empty()&&(st.top()=='*'||st.top()=='/')){if(!flag){flag = true;cout<<st.top();}else{cout<<" "<<st.top();}st.pop();}}st.push(s[i]);}}while(!st.empty()){if(!flag){flag = true;cout<<st.top();}else{cout<<" "<<st.top();}st.pop();}return 0;}
阅读全文
0 0
- PTA 3.11表达式转换
- PTA-表达式转换
- PTA 7-1 表达式转换——表达式树
- pta 5-8 表达式转换 (25分) (栈)
- 数据结构之栈----PTA题目表达式转换(中缀转后缀)
- [数据结构]--PTA求前缀表达式的值
- PTA
- PTA
- pta
- pta
- 【pta】实验2-1-2 温度转换
- pta中 字符串转换成十进制整数
- PTA 求前缀表达式的值 (25分)
- PTA 切分表达式——写个tokenizer吧
- pta 切分表达式——写个tokenizer吧
- [PTA] 7-23 币值转换(20 分)
- 表达式转换
- 表达式转换
- Redis学习(二)redis配置
- EL表达式处理字符串 是否 包含 某字符串 截取 拆分...............
- solidThinking.Click2Form.2017.1.0.Win64 1DVD
- C#中的委托与事件
- Android 获取控件的宽和高
- PTA 3.11表达式转换
- 【非技术面试】31道,不准备就会掉进坑的问题
- HDU-1398 Square Coins
- WebView的基本使用
- 正则表达式
- Oracle connect by的使用
- 一些博客
- oracle 重启与关闭
- Redis 学习(一)redis的安装