Application of stack --------- input of strings

Topic 1108: Use of stacks             

Time limit: 1 second

Memory Limit: 32 MB

Special Judgment: No

Submitted: 11671

Resolved: 3392

Topic description:

    A stack is a basic data structure. The stack has two basic modes of operation, push and pop. Push pushes a value onto the top of the stack, while pop pops the value from the top of the stack. Now let's verify the use of the stack.

enter:

     For each set of test data, the first line is a positive integer n, 0<n<=10000 (n=0 ends). In the next n lines, the first character of each line may be 'P' or 'O' or 'A'; if it is 'P', it will be followed by an integer, which means to push the data onto the stack; if it is ' O' means pop the value on the top of the stack, if there is no element in the stack, ignore this operation; if it is 'A', it means to ask the value of the current top of the stack, if the stack is empty at that time, output 'E'. The stack starts out empty.

output:

    For each set of test data, process the stack according to the command characters in it; for all 'A' operations, output the value at the top of the stack at that time, each occupying a line, and output 'E' if the stack is empty at that time. When each set of test data is complete, output a blank line.

Sample input:
3
A
P 5
A
4
P 3
P 6
O
A
0
Sample output:
E
5

3
source:
2011 Jilin University Computer Graduate Computer Exam Questions

#include<stdio.h>
#include<stack>
#include<cstring>
using namespace std;
int main(){
int n;
stack<int> ch;
while(scanf("%d",&n)!=EOF&&n!=0){
// getchar();
while(!ch.empty()) ch.pop();
while(n--!=0){
char c[4];
scanf("%s",c);//Gets was originally used to read strings. The difference between it and scanf is that it can accept tabs such as spaces, TAB, etc.
//If you use gets, the numbers that need to be pushed on the stack by the P operation will also be read into a string, which is not as convenient as using scanf directly.
if(c[0]=='A'){
// if(ch.empty()){
// printf("go1\n");}
// if(!ch.empty()){
// printf("go2\n");}
if(!ch.empty()){
// printf("go!\n");
printf("%d\n",ch.top());
}
else
printf("E\n");
}
else if(c[0]=='O'){
if(!ch.empty()){
ch.pop();
}
}
else if(c[0]=='P'){
int a;
scanf("%d",&a);
// if(c[2]=='5')
// printf("ding!\n");
// else
// printf("%c\n",c[2]);
ch.push(a);
}

}
printf("\n");
}
return 0;
}



Topic 1101: Computational Expressions

Time limit: 1 second

Memory Limit: 32 MB

Special Judgment: No

Submitted: 7388

Resolved: 2307

Topic description:

Evaluate an expression without parentheses

enter:

There are many kinds of data, each group of data has one line, and there are no spaces in the expression

output:

output result

Sample input:
6/2+3+3*4
Sample output:
18
source:
2010 Shanghai Jiaotong University Computer Graduate Computer Exam Questions
#include<stack>
#include<cstdio>
#include<iostream>
using namespace std;
stack<int> op;
stack<double> in;
int mat[][5]={
1,0,0,0,0,
1,0,0,0,0,
1,0,0,0,0,
1,1,1,0,0,
1,1,1,0,0,
};
char str[1000];
void getE(double &retn,int &i,bool &reto){
	if(i==0&&op.empty()==true){
		reto = true;
		retn = 0;
		return;
	}
	if(str[i]==0){
		reto = true;
		retn = 0;
		return;
	}
	if(str[i]>='0'&&str[i]<='9'){
		straight=false;
	}
	else{
		reto = true;
		if(str[i]=='+')
		retn = 1;
		if(str[i]=='-')
		retn = 2;
		if(str[i]=='*')
		retn = 3;
		if(str[i]=='/')
		retn = 4;
		i++;
		return;
	}
	retn = 0;
    for(;str[i]<='9'&&str[i]>='0'&&str[i]!=0;i++){
    	retn * = 10;
    	retn + = str [i] - '0';
	}
	double d=1,dd=0;
	if(str[i]=='.'){
		i++;
	   for(;str[i]<='9'&&str[i]>='0';i++){
	   	d/=10;
	   	dd+=(str[i]-'0')*d;
	   }
	}
    retn + = dd;
	return;
}
int main(){
	while(scanf("%s",str)!=EOF){
	while (! op.empty ()) op.pop ();
	while(!in.empty()) in.pop();
	double ren;
	int index=0;
	bool reop;
	while(true){
		getE(ren,index,reop);
	if(reop==false){
		in.push(ren);
	}
	else{
	    if (op.empty () == true || mat [(int) ren] [op.top ()] == 1)
	    op.push(ren);
	    else{
	    	while(mat[(int)ren][op.top()]==0){
	    	int o;
	    	o=op.top();
	    	op.pop ();
			double b=in.top();
			in.pop();
			double a=in.top();
			in.pop();	
			if(o==1)
			a=a+b;
			else if(o==2)
			a=a-b;
			else if(o==3)
			a=a*b;
			else if(o==4)
			a=a/b;
			in.push(a);
			}
	    op.push(ren);
	    
		}
	}
		if(op.size()==2&&op.top()==0)
		break;
		}
	cout<<in.top()<<endl;
	
	}
	
return 0;	
}
//1+3/2-1*3


                           

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325678501&siteId=291194637