727运算符前缀变后缀
来源:互联网 发布:编程员招聘 编辑:程序博客网 时间:2024/06/13 12:03
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <stack>
using namespace std;
stack<char>operat;
int main()
{
//freopen("in.in","r",stdin);
int test_case;
cin>>test_case;
cin.ignore(1);
char c;
int flage = 0;
while(test_case--)
{
while((c = getchar()) != EOF)
{
if(c == '\n')
{
flage++;
if(flage == 2) break;
continue;
}
flage = 0;
if(isdigit(c))
cout<<c;
else{
if(c == '+' || c == '-'){
while(!operat.empty())
{
if(operat.top() != '(' )
{
cout<<operat.top();
operat.pop();
}
else break;
}
operat.push(c);
}
else if(c == '*' || c == '/')
{
while(!operat.empty())
{
if(operat.top() == '*' || operat.top() == '/')
{
cout<<operat.top();
operat.pop();
}
else break;
}
operat.push(c);
}
else if(c == '(')
operat.push(c);
else if(c == ')')
{
while(!operat.empty())
{
if(operat.top() != '(')
{
cout<<operat.top();
operat.pop();
}
else
{
operat.pop();
break;
}
}
}
}
}
while(!operat.empty())
{
cout<<operat.top();
operat.pop();
}
if(test_case != 0) cout<<endl;
cout<<endl;
}
return 0;
}
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <stack>
#include <cmath>
#define M 500
using namespace std;
long long deal(char *str,char a,char b)
{
stack<char>operat;
stack<long long>num;
long long len = strlen(str);
long long flage = 0;
long long n;
char ope;
for(long long i = 0;i < len;i++)
{
if(!flage){
sscanf(&str[i],"%lld",&n);
num.push(n);
if(n >= 10) i++;
flage = 1;
}
else{
ope = str[i];
flage = 0;
if(!operat.empty())
{
char cal = operat.top();
long long x,y;
if(ope == b || cal == a){
operat.pop();
x = num.top();num.pop();
y = num.top();num.pop();
long long r;
if(cal == '*')
r = x * y;
else r = x + y;
num.push(r);
}
operat.push(ope);
}
else operat.push(ope);
}
}
while(!operat.empty())
{
char cal = operat.top();
operat.pop();
long long x,y;
x = num.top(); num.pop();
y = num.top(); num.pop();
long long r;
if(cal == '*')
r = x * y;
else r = x + y;
num.push(r);
}
return num.top();
}
int main()
{
//freopen("in.in","r",stdin);
long long T;
long long minv,maxv;
char str[M];
scanf("%lld",&T); getchar();
while(T--)
{
gets(str);
minv = deal(str,'*','+');
maxv = deal(str,'+','*');
printf("The maximum and minimum are %lld and %lld.\n",maxv,minv);
}
return 0;
}
- 727运算符前缀变后缀
- 运算符*, 前缀++,后缀++
- 前缀++ 后缀++ 运算符重载
- 前缀++ 后缀++ 运算符重载
- 前缀++ 后缀++ 运算符重载
- 运算符重载 前缀++ 后缀++
- 前缀变后缀
- c++运算符重载++前缀后缀
- 前缀运算和后缀运算
- 定义类的前缀运算符和后缀运算符
- 简述前缀运算符与后缀运算符
- 运算符重载(重点++前缀和后缀)
- 自增、自减运算符的前缀和后缀
- 哑元与运算符重载:前缀++与后缀++
- 【stack 实现前缀、后缀、中缀表达式运算】
- 关于前缀自增运算符与后缀自增运算符的区别,以及利弊
- C++: 重载前缀运算符与重载后缀运算符(以++为例)
- 前缀和后缀运算符在效率上的小区别
- 《STL源码剖析》-- defalloc.h
- google系统结构图
- 理解PHP数据类型
- wpa_supplicant 初始化流程分析
- C#编程:多线程与UI操作
- 727运算符前缀变后缀
- 自定义ViewGroup 实现拖动跟快速滚动的效果
- hostapd
- 拓扑排序
- iPhone Wax:用Lua生成自然的UIKit程序
- 也试MySQL数据库分割与跨库查询
- wpa supplicant
- 素数打表
- linux WIFI P2P