Created
April 9, 2014 07:54
-
-
Save autekroy/10238140 to your computer and use it in GitHub Desktop.
NTHU OJ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This program is for NTHU 5710 Stack | |
//題目來源 Problem link: http://140.114.86.38/problem.php?pid=5710 | |
#include<iostream> | |
#include<stdio.h> | |
#include<stack> | |
#include<string.h> | |
using namespace std; | |
stack<int> s; | |
void stackClear() | |
{ | |
while( !s.empty()) | |
s.pop(); | |
} | |
void stackTop() | |
{ | |
if(s.size() == 0) | |
printf("X\n"); | |
else | |
printf("%d\n", s.top()); | |
} | |
void stackPush(int n) | |
{ | |
s.push(n); | |
} | |
void stackPop() | |
{ | |
if( !s.empty()) | |
s.pop(); | |
} | |
void stackAction(char action[]) | |
{ | |
if(strcmp(action, "top") == 0) | |
stackTop(); | |
else if(strcmp(action, "push") == 0) | |
{ | |
int tmp; | |
scanf("%d", &tmp); | |
stackPush(tmp); | |
} | |
else if(strcmp(action, "pop") == 0) | |
stackPop(); | |
} | |
int main() | |
{ | |
char oper[5]; | |
stackClear(); | |
while(scanf("%s", &oper) != EOF) | |
{ | |
stackAction(oper); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment