读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
30 / 90 - 26 + 97 - 5 - 6 - 13 / 88 * 6 + 51 / 29 + 79 * 87 + 57 * 92
0
12178.21
#include
#include
#include
#include
using namespace std;
stack s1;
stack s3;
stack s2;
stack s4;
int youxianji(char a){
switch(a){
case '+':return 1;
case '-':return 1;
case '*':return 2;
case '/':return 3;
}
}
double jisuan(double a1,double a2,char b){
switch(b){
case '+':return a1+a2;
case '-':return a1-a2;
case '*':return a1*a2;
case '/':return a1/a2;
}
}
int main(){
char str[210];
while(gets(str)){
if(str[0]=='0'&&str[1]==0) return 0;
int len=strlen(str);
//printf("%d\n",len);
int i=len-1;
double a=0,wei=0;
while(i>=0){//先从右至左遍历字符串,计算出每个数字存入数字栈,每个符号存入符号栈
if(str[i]!='+'&&str[i]!='-'&&str[i]!='*'&&str[i]!='/'&&str[i]!=' '){
a=a+(double)(str[i]-'0')*pow(10.0,wei);
wei++;
//printf("i=%d shu zia=%f\n",i,a);
}
else if(str[i]==' '){
//printf("i=%d kong gea=%f\n",i,a);
}
else{
//printf("fu hao=%c i=%d a=%f\n",str[i],i,a);
s1.push(a);
a=0;wei=0;
s2.push(str[i]);
}
i--;
}
s1.push(a);
while(!s1.empty()&&!s2.empty()){//分别从先前的数字串和符号串中取出进行运算
double a1=s1.top();
s3.push(a1);s1.pop();
//printf("a1=%f\n",a1);
char k=s2.top();//新的符号
s2.pop();
if(!s4.empty()){
char b=s4.top();
while(!s4.empty()&&youxianji(b)>=youxianji(k)){
s4.pop();//将这个高优先级的符号弹出,即将进行运算
double a2=s3.top();//a2;
s3.pop();
//printf("a2=%f\n",a2);
double a3=s3.top();
s3.pop();
//printf("a3=%f\n",a3);
double a4=jisuan(a3,a2,b);
//printf("a4=%f%c%f=%f\n",a3,b,a2,a4);
s3.push(a4);
if(!s4.empty()) b=s4.top();
}
s4.push(k);
}
else s4.push(k);
}
double a1=s1.top();
s1.pop();
s3.push(a1);
while(!s3.empty()&&!s4.empty()){
double a2=s3.top();s3.pop();
double a3=s3.top();s3.pop();
char b=s4.top();s4.pop();
double a4=jisuan(a3,a2,b);
s3.push(a4);
}
double m=s3.top();
printf("%.2f\n",m);
}
return 0;
}
请写一个程序,判断给定表达式中的括号是否匹配,表达式中的合法括号为”(“, “)”, “[", "]“, “{“, ”}”,这三个括号可以按照任意的次序嵌套使用。
有多个表达式,输入数据的第一行是表达式的数目,每个表达式占一行。
4
[(d+f)*{}]
[(2+3))
()}
[4(6]7)9
yes
no
no
no
#include
#include
#include
using namespace std;
stack s1;
int main(){
int i,j;
int n;
scanf("%d",&n);
int buff[1000]={0};
for(i=0;i
Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if M is 5 and N is 7, we can obtain 1, 2, 3, 4, 5, 6, 7 from the stack, but not 3, 2, 1, 7, 5, 6, 4.
Input Specification:
Each input file contains one test case. For each case, the first line contains 3 numbers (all no more than 1000): M (the maximum capacity of the stack), N (the length of push sequence), and K (the number of pop sequences to be checked). Then K lines follow, each contains a pop sequence of N numbers. All the numbers in a line are separated by a space.
Output Specification:
For each pop sequence, print in one line "YES" if it is indeed a possible pop sequence of the stack, or "NO" if not.
Sample Input:5 7 5 1 2 3 4 5 6 7 3 2 1 7 5 6 4 7 6 5 4 3 2 1 5 6 4 3 7 2 1 1 7 6 5 4 3 2Sample Output:
YES NO NO YES NO
#include
#include
using namespace std;
stack s;
int main(){
int m,n,k,i,j;
scanf("%d %d %d",&m,&n,&k);
while(k--){
while(!s.empty()) s.pop();
int num=n;
int a[1000],b=1,t=1,flag=1;
s.push(1);//先将1入栈
for(i=0;i0&&flag==1){
if(!s.empty()){
t=s.top();
while(a[i]>t&&bt&&b
注:直接模拟入栈过程判断序列是否合格即可。