第一部分#include<iostream> #include<stack> #include<string.h> using namespace std; stack<int> stk; char temp[1005] = {}; int A() { int sum = 0; for (int i = 0; i < strlen(temp); ++i) { if (temp[i] == '&') break; sum = sum * 10 + temp[i] - '0'; temp[i] = '&'; } return sum; } int main() { int tmp = 0; char fuhao; int a1, a2; int idx = 0; while (fuhao != '@') { cin >> fuhao; if (fuhao >= '0' && fuhao <= '9') { temp[idx] = fuhao; idx++; } if (fuhao == '.') { idx = 0; tmp = A(); stk.push(tmp); } if (fuhao == '+') { a1 = stk.top(); stk.pop(); a2 = stk.top(); stk.pop(); stk.push(a1 + a2); } if (fuhao == '-') { a1 = stk.top(); stk.pop(); a2 = stk.top(); stk.pop(); stk.push(a2 - a1); } if (fuhao == '*') { a1 = stk.top(); stk.pop(); a2 = stk.top(); stk.pop(); stk.push(a1 * a2); } if (fuhao == '/') { a1 = stk.top(); stk.pop(); a2 = stk.top(); stk.pop(); stk.push(a2 / a1); } } cout << stk.top() << endl; return 0; }不使用string和stack的STL容器实现这个代码
时间: 2024-01-19 22:03:24 浏览: 193
好的,我已经将第一部分代码转化为不使用STL容器的版本。请查看下面的代码:
```
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1005;
int stk[maxn];
char temp[maxn];
int top = -1;
int A() {
int sum = 0;
for (int i = 0; i < strlen(temp); ++i) {
if (temp[i] == '&') break;
sum = sum * 10 + temp[i] - '0';
temp[i] = '&';
}
return sum;
}
int main() {
int tmp = 0;
char fuhao;
int a1, a2;
int idx = 0;
while (fuhao != '@') {
cin >> fuhao;
if (fuhao >= '0' && fuhao <= '9') {
temp[idx] = fuhao;
idx++;
}
if (fuhao == '.') {
idx = 0;
tmp = A();
stk[++top] = tmp;
}
if (fuhao == '+') {
a1 = stk[top];
top--;
a2 = stk[top];
top--;
stk[++top] = a1 + a2;
}
if (fuhao == '-') {
a1 = stk[top];
top--;
a2 = stk[top];
top--;
stk[++top] = a2 - a1;
}
if (fuhao == '*') {
a1 = stk[top];
top--;
a2 = stk[top];
top--;
stk[++top] = a1 * a2;
}
if (fuhao == '/') {
a1 = stk[top];
top--;
a2 = stk[top];
top--;
stk[++top] = a2 / a1;
}
}
cout << stk[top] << endl;
return 0;
}
```
接下来请发给我第二部分代码。
阅读全文