
【输入样例】
16 9 4 3 +*-@
【输出样例】
-47
#include<bits/stdc++.h>
using namespace std;
int stak[101];
char s[256];
int comp(char s[256]){
int i = 0,top = 0,x,y;
while(i<=strlen(s)-2){
switch(s[i]){
case '+':stak[--top] += stak[top+1];break;
case '-':stak[--top] -= stak[top+1];break;
case '*':stak[--top] *= stak[top+1];break;
case '/':stak[--top] /= stak[top+1];break;
default:
x = 0;
while(s[i]!=' '){
x = x * 10 + s[i++] - '0';
}
stak[++top] = x;break;
}
i++;
}
return stak[top];
}
main(){
printf("input a string(@_over):");
gets(s);
printf("result=%d",comp(s));
return 0;
}
24588785.cpp: In function ‘int comp(char*)’:
24588785.cpp:7:9: warning: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Wsign-compare]
7 | while(i<=strlen(s)-2){
| ~^~~~~~~~~~~~~
24588785.cpp:9:19: warning: operation on ‘top’ may be undefined [-Wsequence-point]
9 | case '+': stak[--top] += stak[top+1];break;
| ^~~~~
24588785.cpp:9:19: warning: operation on ‘top’ may be undefined [-Wsequence-point]
24588785.cpp:10:19: warning: operation on ‘top’ may be undefined [-Wsequence-point]
10 | case '-': stak[--top] -= stak[top+1];break;
| ^~~~~
24588785.cpp:10:19: warning: operation on ‘top’ may be undefined [-Wsequence-point]
24588785.cpp:11:19: warning: operation on ‘top’ may be undefined [-Wsequence-point]
11 | case '*': stak[--top] *= stak[top+1];break;
| ^~~~~
24588785.cpp:11:19: warning: operation on ‘top’ may be undefined [-Wsequence-point]
24588785.cpp:12:19: warning: operation on ‘top’ may be undefined [-Wsequence-point]
12 | case '/': stak[--top] /= stak[top+1];break;
| ^~~~~
24588785.cpp:12:19: warning: operation on ‘top’ may be undefined [-Wsequence-point]
24588785.cpp:6:22: warning: unused variable ‘y’ [-Wunused-variable]
6 | int i = 0,top = 0,x,y;
| ^
24588785.cpp: At global scope:
24588785.cpp:24:6: warning: ISO C++ forbids declaration of ‘main’ with no type [-Wreturn-type]
24 | main(){
| ^
24588785.cpp: In function ‘int main()’:
24588785.cpp:26:2: error: ‘gets’ was not declared in this scope; did you mean ‘fgets’?
26 | gets(s);
| ^~~~
| fget