Emacs号称神的编辑器,它自带了一个计算器。与其他计算器不同,它是基于后缀表达式的,即运算符在操作数的后面。例如“2 3
+”等价于中缀表达式的“2 + 3”。
请你实现一个后缀表达式的计算器。
输入包含多组数据。
每组数据包括两行:第一行是一个正整数n (3≤n≤50);紧接着第二行包含n个由数值和运算符组成的列表。
“+-*/”分别为加减乘除四则运算,其中除法为整除,即“5/3=1”。
对应每一组数据,输出它们的运算结果。
3 2 3 + 5 2 2 + 3 * 5 2 2 3 + *
5 12 10
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<functional>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <exception>
#include <iomanip>
#include <memory>
#include <sstream>
#include <list>
using namespace std;
int main(int argc, char** argv)
{
//freopen("in.txt", "r", stdin);
int n;
while (cin >> n)
{
vector<string> pre;
string s;
for (int i = 0; i < n; ++i)
{
cin >> s;
pre.emplace_back(s);
}
while (pre.size() > 1)
{
for (int i = 0; i < pre.size(); ++i)
{
string op = pre[i];
if (op == "+" || op == "-" || op == "*" || op == "/")
{
int x = stoi(pre[i - 2]), y = stoi(pre[i - 1]);
switch (op[0])
{
case '+': x += y; break;
case '-': x -= y; break;
case '*': x *= y; break;
case '/': x /= y; break;
default:break;
}
pre[i] = to_string(x);
pre.erase(pre.begin() + i - 2, pre.begin() + i);
break;
}
}
}
cout << pre[0] << endl;
}
return 0;
}
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int main(){
stack<int> s1;
int n;
while(cin>>n)
{
for(int i=0;i<n;i++){
string s;
cin>>s;
if(s[0]>='0'&&s[0]<='9'){
int num=0;
for(int i=0;i<s.length();i++)
num=num*10+s[i]-'0';
s1.push(num);
}
else{
int x=s1.top();s1.pop();
int y=s1.top();s1.pop();
if(s=="+") s1.push(x+y);
else if(s=="-") s1.push(y-x);
else if(s=="*") s1.push(x*y);
else if(s=="/") s1.push(y/x);
}
}
cout<<s1.top()<<endl;
}
return 0;
} import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()){
int n=in.nextInt();
Stack<String> sk=new Stack<String>();
for (int i = 0; i < n; i++) {
String s=in.next();
switch (s) {
case "+":
sk.push(String.valueOf(Integer.valueOf(sk.pop())+Integer.valueOf(sk.pop())));
break;
case "-":
sk.push(String.valueOf(-(Integer.valueOf(sk.pop())-Integer.valueOf(sk.pop()))));
break;
case "*":
sk.push(String.valueOf(Integer.valueOf(sk.pop())*Integer.valueOf(sk.pop())));
break;
case "/":
sk.push(String.valueOf(Integer.valueOf(sk.pop())/Integer.valueOf(sk.pop())));
break;
default:
sk.push(s);
break;
}
}
System.out.println(sk.pop());
}
}
}
#include<iostream>
#include<string>
#include<stack>
#include<queue>
using namespace std;
int atoi(string c);
int main()
{
//cout<<string::npos<<endl;
int n;
//int res=0;
string c[50];
string sign="+-*/";
/*if(sign.find("+")!=string::npos)
{
cout<<"+ is in the sign"<<endl;
}*/
while(cin>>n)
{
queue<string> ss1;
stack<int> ss;
ss.push(1);
for(int i=0;i<n;i++)
{
cin>>c[i];
ss1.push(c[i]);
}
//cout<<ss1.front()<<endl;
int a,b;
while(!ss1.empty())
{
string cc=ss1.front();
//cout<<cc<<endl;
ss1.pop();
if(sign.find(cc)==string::npos)
{
ss.push(atoi(cc));
//cout<<ss.top()<<endl;
continue;
}
else
{
b=ss.top();
//cout<<a<<endl;
ss.pop();
a=ss.top();
//cout<<b<<endl;
ss.pop();
//cout<<cc<<endl;
if(cc=="+")
{
//cout<<1<<endl;
ss.push(a+b);
//cout<<ss.top()<<endl;
}
else if(cc=="-")
{
ss.push(a-b);
}
else if(cc=="*")
{
ss.push(a*b);
}
else if(cc=="/")
{
if(b==0)
{
return 0;
}
ss.push(a/b);
}
}
}
//cout<<n<<endl;
int res=ss.top();
ss.pop();
cout<<res<<endl;
}
return 0;
}
int atoi(string c)
{
int len=c.length();
int res=0;
int i;
for(i=0;i<len;i++)
{
res=res*10+(c[i]-'0');
}
//cout<<res<<endl;
return res;
}
#include <iostream>
#include <string>
#include <vector>
#include <stack>
using namespace std;
int main()
{
int n;
while (cin >> n)
{
vector<string> calc(n);
for (int i = 0; i < n; ++i)
{
cin >> calc[i];
}
string ops("+-*/");
stack<int> st; //用int会溢出 long long不会 但int能过 long long不能过
//不知道为啥溢出才是正确的
for (int i = 0; i < n; ++i)
{
if (ops.find(calc[i]) != string::npos)
{
char op = calc[i][0];
int right = st.top();
st.pop();
int left = st.top();
st.pop();
int res = 0;
switch (op)
{
case '+':
res = left + right;
break;
case '-':
res = left - right;
break;
case '*':
res = left * right;
break;
case '/':
res = left / right;
break;
default:
cout << "bug" << endl;
break;
}
st.push(res);
}
else
{
int num = stoi(calc[i]);
st.push(num);
}
}
cout << st.top() << endl;
}
} import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
String str = sc.nextLine();
int[] nums = new int[n];
char[] arr = sc.nextLine().toCharArray();
int i = 0 , size = 0;
for(char ch : arr){
if(ch == '+'){
add(nums,--size);
--size;
}else if(ch == '-'){
sub(nums,--size);
--size;
}else if(ch == '*'){
mul(nums,--size);
--size;
}else if(ch == '/'){
div(nums,--size);
--size;
}else if(ch != ' '){
nums[size] = nums[size] * 10 + ch - '0';
}else{
size++;
}
}
System.out.println(nums[0]);
}
}
private static void add(int[] nums,int index){
nums[index-1] = nums[index-1] + nums[index];
nums[index] = 0;
}
private static void sub(int[] nums,int index){
nums[index-1] = nums[index-1] - nums[index];
nums[index] = 0;
}
private static void mul(int[] nums,int index){
nums[index-1] = nums[index-1] * nums[index];
nums[index] = 0;
}
private static void div(int[] nums,int index){
nums[index-1] = nums[index-1] / nums[index];
nums[index] = 0;
}
} #include <iostream>
#include <stack>
#include <string>
using namespace std;
int main()
{
int n;
while(cin >> n)
{
stack<int> s;
for(int i = 0; i < n; i++)
{
string num;
cin >> num;
if(num[0] >= '0' && num[0] <= '9')
s.push(atoi(num.c_str()));
else
{
int back = s.top();
s.pop();
int front = s.top();
s.pop();
if(num == "+")
s.push(front + back);
if(num == "-")
s.push(front - back);
if(num == "*")
s.push(front * back);
if(num == "/")
s.push(front / back);
}
}
cout << s.top() << endl;
}
return 0;
} import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = Integer.parseInt(sc.nextLine());
String[] str = sc.nextLine().split(" ");
Stack<String> stack = new Stack<>();
int sum = 0;
for(int i = 0; i < n; i++){
if(!str[i].equals("+") && !str[i].equals("-")
&& !str[i].equals("*") && !str[i].equals("/")){
stack.push(str[i]);
}
else{
int a = Integer.parseInt(stack.pop());
int b = Integer.parseInt(stack.pop());
if(str[i].equals("+")){
sum = a + b;
}
if(str[i].equals("-")){
sum = b - a;
}
if(str[i].equals("*")){
sum = a * b;
}
if(str[i].equals("/")){
sum = b / a;
}
stack.push(String.valueOf(sum));
}
}
System.out.println(stack.pop());
}
}
} // 利用vector实现 原理和 stack一样;
// write your code here cpp
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
// 把 n个字符先 放进 s (vector) 里
vector s(n);
for(int i =0; i<n; i++)
{
cin>>s[i];
}
//从s里取数据 先判断 再 操作;
vector v;
for(auto e:s) // &e
{
if(e=="+"|| e=="-" || e=="*" || e=="/") //'+'
{
int second = v.back();
v.pop_back();
int first = v.back();
v.pop_back();
if(e =="+")
v.push_back(first+second);
if(e =="-")
v.push_back(first-second);
if(e =="*")
v.push_back(first*second);
if(e =="/")
v.push_back(first/second);
}
else
{
v.push_back(stoi(e));
}
}
cout<<v.back()<<endl;
}
return 0;
} // write your code here cpp
#include <iostream>
#include <vector>
#include <stack>
#include <sstream>
using namespace std;
int main()
{
int n = 0;
while (cin >> n)
{
vector<char> num(n, '0');
stack<int> st;
for (int i = 0; i < n; i++)
{
cin >> num[i];
}
for (int i = 0; i < n; i++)
{
if (num[i] == '+' || num[i] == '-' || num[i] == '*' || num[i] == '/')
{
if (num[i] == '+')
{
int num1 = st.top();
st.pop();
int num2 = st.top();
st.pop();
st.push(num1 + num2);
}
if (num[i] == '*')
{
int num1 = st.top();
st.pop();
int num2 = st.top();
st.pop();
st.push(num1 * num2);
}
if (num[i] == '-')
{
int num1 = st.top();
st.pop();
int num2 = st.top();
st.pop();
st.push(num2 - num1);
}
if (num[i] == '/')
{
int num1 = st.top();
st.pop();
int num2 = st.top();
st.pop();
st.push(num2 / num1);
}
}
else
{
stringstream ss;
int tmp = 0;
ss << num[i];
ss >> tmp;
st.push(tmp);
}
}
int result = st.top();
cout << result << endl;
}
} // write your code here cpp
#include<cstdio>
(802)#include<stack>
#include<cstring>
(803)#include<cctype>
using namespace std;
int main(){
char str[60][20];
int n;
while(scanf("%d",&n)!=EOF){
stack<int> s;
//scanf("%s",str);
//int len=strlen(str);
for(int i=0;i<n;++i){
scanf("%s",str[i]);
}
for(int i=0;i<=n-1;++i){
if(isdigit(str[i][0])){
int sum=0;
for(int j=0;j<strlen(str[i]);++j){
sum=sum*10+str[i][j]-'0';
}
s.push(sum);
}else{
int a=s.top();
s.pop();
int b=s.top();
s.pop();
int sum;
if(str[i][0]=='+')
sum=a+b;
else if(str[i][0]=='-')
sum=b-a;
else if(str[i][0]=='*')
sum=a*b;
else
sum=b/a;
s.push(sum);
}
}
printf("%d\n",s.top());
}
} #include<iostream>
(720)#include<string>
#include<stack>
using namespace std;
int main()
{
int n = 0;
stack<int> arr;
while (cin >> n)
{
string str;
getline(cin, str);
getline(cin, str);
for (int i = 0; i < str.size(); ++i)
{
if(str[i] >= '0' && str[i] <= '9')
{
int num = 0;
while(str[i] != ' ')
{
num = num*10 + str[i] - '0';
++i;
}
arr.push(num);
}
else if (str[i] == '+' || str[i] == '-'
|| str[i] == '*' || str[i] == '/')
{
int x = arr.top();
arr.pop();
int y = arr.top();
arr.pop();
if (str[i] == '*')
arr.push(x * y);
else if(str[i] == '/')
arr.push(y / x);
else if (str[i] == '-')
arr.push(y - x);
else
arr.push(x + y);
}
}
cout << arr.top() << endl;
}
return 0;
} 真不知道这是哪错了#include<iostream> (720)#include<vector> #include<stack> (850)#include <string> using namespace std; int main() { int n = 0; while (cin >> n) { getchar(); string str; getline(cin, str); stack<int> si; for (int i = 0; i < str.size(); i++) { if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/') { int a = si.top(); si.pop(); int b = si.top(); si.pop(); if (str[i] == '+') { si.push(a + b); } else if (str[i] == '-') { si.push(b - a); } else if (str[i] == '*') { si.push(b*a); } else { if (a) si.push(b / a); } } else if (str[i] != ' ') si.push(str[i] - '0'); } cout << si.top() << endl; } }
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main()
{
int n;
while (cin >> n)
{
getchar();
stack<int> st;
for (size_t i = 0; i<n; i++)
{
int sum = 0;
string ch;//运用string而不是用char的原因是,char只能存10以下的字符,当输入的字符
//大于10,就会出现错误
cin >> ch;
if (ch[0] == '+' ||ch[0] == '-' || ch[0] == '*' ||ch[0] == '/')
{
int right = st.top();//栈结构,自底向上,所以取值时,需要先取右操作数
st.pop();
int left = st.top();
st.pop();
if (ch[0] == '+')
sum = left + right;
if (ch[0] == '-')
sum = left - right;
if (ch[0] == '*')
sum = left*right;
if (ch[0] == '/')
{
if (right != 0)
sum = left / right;
else
return 0;
}
st.push(sum);
}
else
st.push(atoi(ch.c_str()));
}
cout << (int)st.top() << endl;
}
return 0;
}
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
#include <stack>
using namespace std;
int main()
{
int n;
while(cin >> n)
{
vector<string> v;
v.resize(n);
stack<int> s;
string str;
for(int i = 0; i < n; i++)
{
cin >> v[i];
}
for(int i = 0; i < n; i++)
{
str = v[i];
if(!(str == "+" || str == "-" || str == "*" || str == "/"))
//字符串转整数,注意是c语言的函数,要加上c_str
s.push(atoi(str.c_str()));
else
{
int right = s.top();
s.pop();
int left = s.top();
s.pop();
//把每次运算完的和入栈
if(str == "+")
s.push(right+left);
else if(str == "-")
s.push(left-right);
else if(str == "*")
s.push(left*right);
else if(str == "/")
{
if(right != 0)
s.push(left/right);
else
return 0;
}
}
str = "";
}
cout << s.top() << endl;
}
return 0;
}
import java.util.Scanner; import java.util.Stack; public class EmacsCal { public static void main(String[] args) {想不通为啥过不了,但本地IDEA能过Scanner in = new Scanner(System.in); while (in.hasNext()) { String nn = in.nextLine(); int n = Integer.parseInt(nn); String[] str = new String[n]; for(int i=0; i<n; i++){ str[i] = in.nextLine(); } Stack<Integer> stk = new Stack<Integer>(); for(int i=0; i<n; i++) { String[] res = str[i].split(" "); int len = res.length; for(int j=0; j<len; j++) { if(!res[j].equals("+") && !res[j].equals("-") && !res[j].equals("*")&& !res[j].equals("/")) { stk.push(Integer.parseInt(res[j])); }else if (res[j].equals("+")) { stk.push(stk.pop()+stk.pop()); }else if (res[j].equals("-")) { stk.push((stk.pop()-stk.pop())*(-1)); }else if (res[j].equals("*")) { stk.push(stk.pop()*stk.pop()); }else if (res[j].equals("/")) { int a = stk.pop(); int b = stk.pop(); stk.push(b/a); } } System.out.println(stk.peek()); } } } }
#include<iostream> //for cout endl
#include<stack> //for stack
#include<string>// for string
#include<vector>//for vector
#include<algorithm>//for pow
using namespace std;
int computeSuffix(vector<string>);//计算后缀表达式 int整数多于1位 + - * /
int str2dec(string);
int main()
{
int n = 0;
while (cin >> n)
{
vector<string> str;
for (int i = 0; i < n; i++)
{
string temp;
cin >> temp;
str.push_back(temp);
}
int res = computeSuffix(str);
cout << res << endl;
}
return 0;
}
int computeSuffix(vector<string> str)
{
int size = str.size();
stack<int> st;
for (int i = 0; i < size; i++)
{
if (str[i] != "+"&&str[i] != "-"&&str[i] != "*"&&str[i] != "/")
st.push(str2dec(str[i]));
if (str[i] == "+")
{
int a = st.top();
st.pop();
int b = st.top();
st.pop();
st.push(a + b);
}
if (str[i] == "-")
{
int a = st.top();
st.pop();
int b = st.top();
st.pop();
st.push(b - a);
}
if (str[i] == "*")
{
int a = st.top();
st.pop();
int b = st.top();
st.pop();
st.push(a * b);
}
if (str[i] == "/")
{
int a = st.top();
st.pop();
int b = st.top();
st.pop();
st.push(b / a);
}
}
return st.top();
}
int str2dec(string str)
{
int size = str.size();
int res = 0;
for (int i = 0; i < size; i++)
res += (str[i] - '0')*pow(10, size-i-1);
return res;
} 一开始将数据设置成long long型,只能说我考虑多了,反而错了!
#include <bits/stdc++.h>
using namespace std;
int atoi(string s) {
int res = 0;
for(int i = 0; i < s.size(); ++i) {
res *= 10;
res += s[i] - '0';
}
return res;
}
int main()
{
int n;
while(cin >> n) {
vector<string> v(n);
stack<int> st;
for(int i = 0; i < n; ++i) {
cin >> v[i];
if(v[i] != "+" && v[i] != "-" && v[i] != "*" && v[i] != "/") {
st.push(atoi(v[i]));
}
else {
int right = st.top();
st.pop();
int left = st.top();
st.pop();
if (v[i] == "+")
st.push(left + right);
else if (v[i] == "-")
st.push(left - right);
else if (v[i] == "*")
st.push(left * right);
else if (v[i] == "/") {
if (right != 0)
st.push(left / right);
else
return 0;
}
}
}
cout << st.top() << endl;
}
return 0;
}
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int atoi(string c)
{
int len = c.length();
int res = 0;
int i = 0;
for(i=0;i<len;i++){
res = res*10 + (c[i]-'0');
}
return res;
}
int main()
{
int n;
string c;
string sign = "+-*/";
while(cin >> n){
stack<int> ss;
int a,b;
while(n--){
cin >> c;
if(sign.find(c) == string::npos){
ss.push(atoi(c));
continue;
}
else{
a = ss.top();
ss.pop();
b = ss.top();
ss.pop();
if(c == "+")
ss.push(a+b);
else if(c == "-")
ss.push(b-a);
else if(c == "*")
ss.push(a*b);
else
ss.push(b/a);
}
}
int res = ss.top();
ss.pop();
cout << res << endl;
}
return 0;
}
// write your code here cpp#include<stdio.h>#include<stdlib.h>#include<string.h>int atoi(char s[]) {int res=0;int i=0;if(s[i]=='-') i++;while(s[i]) {res = res*10+ s[i]-'0';i++;}if(s[0]=='-') res *= -1;return res;}int cal(int a, int b, char op[]){switch(op[0]) {case '+': return a+b;case '-': return a-b;case '*': return a*b;case '\/': return b!=0? a/b:-1;default:return -1;}}int main(int argc, char* argv[]){int n;while(scanf("%d",&n)!=EOF) {char a[n][32];int i;for(i=0;i<n;i++) {scanf("%s",a[i]);}int res=0;int num=0;int ops=n/2;while(num<ops) {for(i=0;i<n;i++) {if((a[i][0]<'0' || a[i][0]>'9')&& a[i][1]==0) {res = cal(atoi(a[i-2]),atoi(a[i-1]), a[i]);sprintf(a[i-2],"%d",res);for(int j=i+1;j<n;j++) {strcpy(a[j-2],a[j]);}num++;n-=2;break;}}}printf("%s\n",a[0]);}return 0;}/*input:4937 66 66 39 8 - * - 74 41 - 98 - 79 + * 38 / 10 - 60 58 + 14 99 64 * / 46 - - 24 * + 23 / 88 46 + 8 * 46 / 59 / + 22 + +491 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + + + + + + + + + + + + + + + + + + + + + + + +output:19825*/