南阳oj 郁闷的c小加(二) 题目267
2015-08-01 17:28
232 查看
#include<stdio.h>
#include<stack>
#include<string.h>
#define N 1000
#include<stdlib.h>
using namespace std;
stack<char> str;//字符栈 转化为后缀式
stack<double> num;//数字栈 计算值
char s1
,s2
,s3[1000000];
int top;
int com(char x)//比较优先级
{
switch(x)
{
case '+':
case '-':return 1;
case '*':
case '/':return 2;
default:return -1;
}
}
void zhuan()//转化后缀式
{
scanf("%s",s1);//输入字符串
int k=strlen(s1);//获取长度
top=-1;//初始化头
str.push('#');//进入一个字符以防访问过界
for(int i=0;i<k;i++)
{
if(s1[i]>='0'&&s1[i]<='9'||s1[i]=='.')//数字就进入
{
top++;
s2[top]=s1[i];
}
else if(s1[i]=='+'||s1[i]=='-'||s1[i]=='*'||s1[i]=='/')
{
top++;
s2[top]=' ';
if(com(s1[i])>com(str.top())) str.push(s1[i]);//比较优先级,如果大就进入栈
else//否则就输出
{
while(com(str.top())>=com(s1[i]))
{
top++;
s2[top]=str.top();
top++;
s2[top]=' ';
str.pop();
}
str.push(s1[i]);
}
}
else if(s1[i]=='(') str.push(s1[i]);
else if(s1[i]==')')
{
while(str.top()!='(')
{
top++;
s2[top]=' ';
top++;
s2[top]=str.top();
str.pop();
}
str.pop();//删除左括号
}
}
while(str.top()!='#')//将剩余的全部输出
{
top++;
s2[top]=' ';
top++;
s2[top]=str.top();
str.pop();
}
for(int i=0;i<=top;i++)
{
if(s2[i]!=' ') printf("%c",s2[i]);
}
printf("=\n");
}
void ji()
{
while(!num.empty())
num.pop();
double a1,a2;
char x;
int k,t=-1;
for(int i=0;i<=top;i++)
{
k=0;
x=s2[i];
while(s1[i]!=' ')
{
if(s2[i]==' ') break;
s3[k]=s2[i];
k++;
i++;
}
if(x>='0'&&x<='9')
{
s3[k]='\0';
t++;
num.push(atof(s3));
}
else
{
switch(x)
{
case '+':
a1=num.top();
num.pop();
a2=num.top();
num.pop();
num.push(a1+a2);
break;
case '-':
a1=num.top();
num.pop();
a2=num.top();
num.pop();
num.push(a2-a1);
break;
case '*':
a1=num.top();
num.pop();
a2=num.top();
num.pop();
num.push(a2*a1);
break;
case '/':
a1=num.top();
num.pop();
a2=num.top();
num.pop();
num.push(a2/a1);
break;
}
}
}
printf("%.2lf\n\n",num.top());
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
zhuan();
ji();
}
return 0;
}
#include<stdio.h>
#include<stack>
#include<string.h>
#define N 1000
#include<stdlib.h>
using namespace std;
stack<char> str;//字符栈 转化为后缀式
stack<double> num;//数字栈 计算值
char s1
,s2
,s3[1000000];
int top;
int com(char x)//比较优先级
{
switch(x)
{
case '+':
case '-':return 1;
case '*':
case '/':return 2;
default:return -1;
}
}
void zhuan()//转化后缀式
{
scanf("%s",s1);//输入字符串
int k=strlen(s1);//获取长度
top=-1;//初始化头
str.push('#');//进入一个字符以防访问过界
for(int i=0;i<k;i++)
{
if(s1[i]>='0'&&s1[i]<='9'||s1[i]=='.')//数字就进入
{
top++;
s2[top]=s1[i];
}
else if(s1[i]=='+'||s1[i]=='-'||s1[i]=='*'||s1[i]=='/')
{
top++;
s2[top]=' ';
if(com(s1[i])>com(str.top())) str.push(s1[i]);//比较优先级,如果大就进入栈
else//否则就输出
{
while(com(str.top())>=com(s1[i]))
{
top++;
s2[top]=str.top();
top++;
s2[top]=' ';
str.pop();
}
str.push(s1[i]);
}
}
else if(s1[i]=='(') str.push(s1[i]);
else if(s1[i]==')')
{
while(str.top()!='(')
{
top++;
s2[top]=' ';
top++;
s2[top]=str.top();
str.pop();
}
str.pop();//删除左括号
}
}
while(str.top()!='#')//将剩余的全部输出
{
top++;
s2[top]=' ';
top++;
s2[top]=str.top();
str.pop();
}
for(int i=0;i<=top;i++)
{
if(s2[i]!=' ') printf("%c",s2[i]);
}
printf("=\n");
}
void ji()
{
while(!num.empty())
num.pop();
double a1,a2;
char x;
int k,t=-1;
for(int i=0;i<=top;i++)
{
k=0;
x=s2[i];
while(s1[i]!=' ')
{
if(s2[i]==' ') break;
s3[k]=s2[i];
k++;
i++;
}
if(x>='0'&&x<='9')
{
s3[k]='\0';
t++;
num.push(atof(s3));
}
else
{
switch(x)
{
case '+':
a1=num.top();
num.pop();
a2=num.top();
num.pop();
num.push(a1+a2);
break;
case '-':
a1=num.top();
num.pop();
a2=num.top();
num.pop();
num.push(a2-a1);
break;
case '*':
a1=num.top();
num.pop();
a2=num.top();
num.pop();
num.push(a2*a1);
break;
case '/':
a1=num.top();
num.pop();
a2=num.top();
num.pop();
num.push(a2/a1);
break;
}
}
}
printf("%.2lf\n\n",num.top());
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
zhuan();
ji();
}
return 0;
}
相关文章推荐
- int b[m][n]和int **b——关于数组名和指针的关系这件“小事”
- Spring MVC简介
- POJ_1458_Common Subsequence
- Java 去掉字符串中重复的子字符串
- UVA1607 Gates 与非门电路 (二分)
- windows10 正式使用
- http请求原理
- 集群通信组件tribes之通道拦截器
- I2C总线协议学习笔记
- Oracle常见函数的部分解释与示例
- hdu 4704 Sum(费马小定理)解题报告
- 集群通信组件tribes之通道拦截器
- What is an entity system framework for game development?
- CS281: Advanced Machine Learning 第二节 Generative model
- hdoj 1276 士兵队列训练问题
- epoll +++
- 支持向量机:Outliers
- 欢迎使用CSDN-markdown编辑器
- C#之Action和Func的用法
- SDUT2015暑假集训14级周赛1 E - 尤文图斯(NP-博弈)