简单实现栈
程序员文章站
2022-07-11 08:26:13
...
首先这是写给我自己的,其他人也可以看,不要吐槽我记录的方式,不然请右上角。
如果有错误欢迎指出。
输入示例
1 2 + 3 4 - *
其实就是(1+2)✖(3-4);(两个乘号就变斜体的操作也是醉了)
输出示例 -3
主要是实现栈- -
#include "stdafx.h"
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
int top,s[10000];
void push(int x){
s[++top]=x;
}
int pop(){
top--;
return s[top+1];
}
int main(){
int a,b,i=0;
top=0;
char S[100];
gets_s(S);
for(int i=0;S[i]!=0;i++)
{
if(S[i]=='+')
{
a=pop();
b=pop();
push(a+b);
}
else if(S[i]=='-')
{b=pop();
a=pop();
push(a-b);
}
else if(S[i]=='*')
{
a=pop();
b=pop();
push(a*b);
}
else {if(S[i]!=' ')
push(S[i]-'0');
}
}
cout<<pop()<<endl;
}
到此结束