Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save autekroy/10238140 to your computer and use it in GitHub Desktop.
Save autekroy/10238140 to your computer and use it in GitHub Desktop.
NTHU OJ
//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