PTA 3.11表达式转换
2017-08-23 17:42
405 查看
有一个测试点过不了,希望大家指点。/*
* 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;
}
* 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;
}
相关文章推荐
- PTA数据结构之表达式转换
- PTA-表达式转换
- 数据结构之栈----PTA题目7-20表达式转换(中缀转后缀)
- PTA 表达式转换 算术表达式有前缀表示法、中缀表示法和后缀表示法等形式。日常使用的算术表达式是采用中缀表示法,即二元运算符位于两个运算数中间。请设计程序将中缀表达式转换为后缀表达式。
- pta 5-8 表达式转换 (25分) (栈)
- PTA 7-1 表达式转换——表达式树
- 习题3.11 表达式转换
- 中缀表达式与后缀表达式之间的转换
- 给定任意的一个含数学运算符的字符串,求这个字符串转换成数学表达式的值。
- 【转】中缀表达式转换为后缀表达式
- long l=88;这个表达式是正确的,因为long比int类型大,会发生自动转换
- 数据结构实验之栈二:一般算术表达式转换成后缀式
- 学习kotlin第14天_区间、类型检查与转换、this表达式、相等性
- 中缀表达式到后缀表达式的转换
- 一般算数表达式转换成后缀式问题
- SDUT 2132 数据结构实验之栈二:一般算术表达式转换成后缀式
- 【你不知道】表达式中的隐式类型转换、无名对象作为函数实参
- 第二节 变量 、 基本类型 、 运算符 、 表达式 、 数据 、 类型转换 、 常量
- labview中位正则表达式使用和字符串二进制数值互相转换
- 中序表达式转换为逆波兰表达式