【C】栈的应用

问题 A: 简单计算器

时间限制: 1 Sec   内存限制: 32 MB
提交: 178   解决: 113
[ 提交][ 状态][ TK题库][命题人: ]

题目描述

读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。

输入

测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。

输出

对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。

样例输入

30 / 90 - 26 + 97 - 5 - 6 - 13 / 88 * 6 + 51 / 29 + 79 * 87 + 57 * 92
0

样例输出

12178.21

#include<stdio.h>
#include<stack>
#include<string.h>
#include<math.h>
using namespace std;
stack<double> s1;
stack<double> s3;
stack<char> s2;
stack<char> 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;
}

注:(1)涉及到字符串处理,这里使用gets(str)录入字符串,再查看是否新录入的字符串是“0\0"以判断是否退出程序;
 (2)自右向左将数字求出并且放入s1数字栈,将符号筛选出并且放入s2符号栈,然后再通过操作s1和s2进行计算;
 (3)涉及到计算符号的优先级,这里+、-优先级相同,*、/优先级相同,从s2入栈到s4时,需要考虑当前入栈与当前栈顶符号的优先级,若栈顶的优先级比我高或者和我相同,则栈顶出栈,进行关于栈顶符号的计算;
 (4)如果有(、),遇到左括号一律进运算符栈,右括号一律不进运算符栈,在遇到对应的右括号前,括号内运算按规则(3)进行,当遇到右括号时,将括号内运算结果进操作数栈,并取出左括号。

问题 B: Problem E

时间限制: 1 Sec   内存限制: 32 MB
提交: 135   解决: 54
[ 提交][ 状态][ TK题库][命题人: ]

题目描述

请写一个程序,判断给定表达式中的括号是否匹配,表达式中的合法括号为”(“, “)”, “[", "]“, “{“, ”}”,这三个括号可以按照任意的次序嵌套使用。

输入

有多个表达式,输入数据的第一行是表达式的数目,每个表达式占一行。

输出

对每个表达式,若其中的括号是匹配的,则输出”yes”,否则输出”no”。

样例输入

4
[(d+f)*{}]
[(2+3))
()}
[4(6]7)9

样例输出

yes
no
no
no

#include<stack>
#include<stdio.h>
#include<string.h>
using namespace std;
stack<char> s1;
int main(){
	int i,j;
	int n;
	scanf("%d",&n);
	int buff[1000]={0};
	for(i=0;i<n;i++){
		char buf[1000];
		scanf("%s",buf);
		int len=strlen(buf);
		j=0;
		while(buff[i]==0&&j<=len){
			switch(buf[j]){
				case '(':s1.push(buf[j]);break;
				case ')':
					if(!s1.empty()){
						char a=s1.top();
						if(a!='(') buff[i]=1;
						else s1.pop();
					}
					else buff[i]=1;
					break;
				case '[':s1.push(buf[j]);break;
				case ']':
					if(!s1.empty()){
						char a=s1.top();
						if(a!='[') buff[i]=1;
						else s1.pop();
					}
					else buff[i]=1;
					break;
				case '{':s1.push(buf[j]);break;
				case '}':
					if(!s1.empty()){
						char a=s1.top();
						if(a!='{') buff[i]=1;
						else s1.pop();
					}
					else buff[i]=1;
					break;
				default:break;
			}
			j++;
		}
		if(buff[i]==1) printf("no\n");
		else{
			if(!s1.empty()){
				printf("no\n");
			}
			else{
				printf("yes\n");
			}
		}
		while(!s1.empty()) s1.pop();
	}
	return 0;
}

注:这里需要注意的是,每道表达式判断结束后,需要将栈清空。

PAT A1051. Pop Sequence (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 2
Sample Output:
YES
NO
NO
YES
NO
#include<stdio.h>
#include<stack>
using namespace std;
stack<int> 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;i<n;i++){
			scanf("%d",&a[i]);
		}
		i=0;
		while(num>0&&flag==1){
			if(!s.empty()){
				t=s.top();
				while(a[i]>t&&b<n&&s.size()<m){//说明a可能还没有入栈
					s.push(++b);t=b;
				}
				if(a[i]<t){//目标出栈小于当前栈顶,出错
					flag=0;
				}
				else if(a[i]==t) s.pop();//目标出栈就是当前栈顶
				else flag=0;
			}
			else{
				while(a[i]>t&&b<n&&s.size()<m){
					s.push(++b);t=b;
				}
				if(a[i]==t) s.pop();
				else flag=0;
			}
			num--;i++;
		}
		if(flag==1) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}
注:直接模拟入栈过程判断序列是否合格即可。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值