#include <stdio.h>
int top=-1;
int stack[200];
void push(int a)
{
stack[++top]=a;
}
int pop()
{
return stack[top--];
}
int main()
{
int a,i,j,sum;
char arr[200];
scanf("%d ",&a);
for(i=1; i<=a; i++)
{
gets(arr);//만능
if(arr[0]=='p' &&arr[1]=='u')
{
sum=0;
for(j=6; arr[j]!=' '; j++)
{
sum=sum*10+arr[j]-'0';
}
push(sum);
}
else if(arr[0]=='t'&&arr[1]=='o')
{
if(top==-1)
{
printf("-1\n");
}
else
{
printf("%d\n",stack[top]);
}
}
else if(arr[0]=='p'&&arr[1]=='o')
{
pop();
}
else if(arr[0]=='s'&&arr[1]=='i')
{
printf("%d\n",top+1);
}
else if(arr[0]=='e'&&arr[1]=='m')
{
if(top==-1)
{
printf("true\n");
}
else
{
printf("false\n");
}
}
}
}
___________________________________________________________________________________________________
#include <stdio.h>
int top=-1;
int stack[200];
void push(int a)
{
stack[++top]=a;
}
int pop()
{
return stack[top--];
}
int main()
{
int a,i,j,sum;
char arr[200];
scanf("%d ",&a);
for(i=1; i<=a; i++)
{
gets(arr);//만능
if(arr[0]=='p' &&arr[1]=='u')
{
sum=0;
for(j=6; arr[j]!=' '; j++)
{
sum=sum*10+arr[j]-'0';
}
push(sum);
}
else if(arr[0]=='t'&&arr[1]=='o')
{
if(top==-1)
{
printf("-1\n");
}
else
{
printf("%d\n",stack[top]);
}
}
else if(arr[0]=='p'&&arr[1]=='o')
{
if(top!=-1) pop();
}
else if(arr[0]=='s'&&arr[1]=='i')
{
printf("%d\n",top+1);
}
else if(arr[0]=='e'&&arr[1]=='m')
{
if(top==-1)
{
printf("true\n");
}
else
{
printf("false\n");
}
}
}
}
_____________________________________________________________________________________________________
숙제
출력형식
입력:1 3 5 4 0 0 7 0 0 6
출력;7
#include <stdio.h>
int top=-1;
int stack[100000];
void push(int a)
{
stack[++top]=a;
}
int pop()
{
return stack[top--];
}
int main()
{
int i,j,sum,k;
int arr[10000];
scanf("%d",&k);
for(i=0; i<k; i++)
{
scanf("%d",arr[i]);
}
for(i=0; i<k; i++)
{
if(arr[i]==0)
{
pop();
}
else if(arr[i]==arr[k])
{
sum=sum+pop();
}
else
{
push(i);
}
}
printf("%d",sum);
}
----------------------------------------------------
#include <stdio.h>
int top=-1;
int stack[100000];
void push(int a)
{
stack[++top]=a;
}
int pop()
{
if(top!=-1)
return stack[top--];
}
int main()
{
char str[201]= {};
int i,sum,num=0, a, b;
gets(str);
for(i=0; str[i]!=0; i++)
{
if('0'<=str[i] && str[i]<='9') //숫자이면
{
num=num*10+str[i]-'0';
if(str[i+1]==' ')
{
push(num);
num=0;
}
}
else if(str[i]!=' ') //연산자이면
{
a = pop();
b = pop();
if(str[i]=='+')
{
push(a+b);
}
else if(str[i]=='*')
{
push(a*b);
}
else if(str[i]=='-')
{
push(b-a);
}
}
}
printf("%d",pop());
}



