測試了其他討論裡面的算式也都算得出來,但line6就是過不了,求解QAQ
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cctype>
#include <algorithm>
using namespace std;
float answer(string s);
class digstack {
private:
int top;
int capacity;
float* digit;
public:
digstack() :top(-1), capacity(5) {//運算元建構式
digit = new float[capacity];
}
void push(float val);
float pop();
float cur();
};
void digstack::push(float val) {//運算元push
if (top + 1 == capacity) {
cout << "stack is full!" << endl;
//擴充stack
capacity = capacity * 2;
float* temp = new float[capacity];
copy(digit, digit + top + 1, temp);
delete[] digit;
digit = temp;
}
digit[++top] = val;
}
float digstack::pop() {//運算元pop
if (top == -1) {
cout << "運算元stack is empty!" << endl;
return 0;
}
return digit[top--];
}
float digstack::cur() {//回傳當前最上方的運算元
return digit[top];
}
class operstack {
private:
int top;
int capacity;
char* operators;
public:
operstack() :top(-1), capacity(5) {//運算子建構式
operators = new char[capacity];
}
void push(char oper);
char pop();
bool isEmpty();
char cur();
};
void operstack::push(char oper) {//運算元push
if (top + 1 == capacity) {
cout << "stack is full!" << endl;
//擴充stack
capacity = capacity * 2;
char* temp = new char[capacity];
copy(operators, operators + top + 1, temp);
delete[] operators;
operators = temp;
}
operators[++top] = oper;
}
char operstack::pop() {//運算子pop
if (top == -1) {
cout << "運算子stack is empty!" << endl;
return 0;
}
return operators[top--];
}
bool operstack::isEmpty() {//確認運算子stack是否為空(null)
return (top == -1);
}
char operstack::cur() {//回傳當前最上方的運算子
return operators[top];
}
int main() {
string str;
while (getline(cin, str)) {
str.erase(std::remove(str.begin(), str.end(), ' '), str.end());
int ans = answer(str);
if (ans < 0) {
cout << "- " << abs(ans) << endl;
}
else {
cout << ans << endl;
}
}
return 0;
}
bool isDigit(char c) { //確認是否為數字(運算元)
return (c >= '0' && c <= '9');
}
bool isOp(char c) { //確認是否為運算子
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')' || c == '%');
}
int priority(char c) { // 回傳運算子層級
switch (c) {
case '(': return 0;
case '+': case '-': return 1;
case '*': case '/': case '%': return 2;
default: return 0;
}
}
float calculate(int val1, int val2, char oper) { // 運算
if (oper == '+') return val1 + val2;
if (oper == '-') return val1 - val2;
if (oper == '*') return val1 * val2;
if (oper == '/') return val1 / val2;
if (oper == '%') return val1 % val2;
}
float answer(string s) {
digstack digs; //運算元stack
operstack opers; //運算子stack
int pos = 0;
while (pos < s.length() /*讀輸入字串直到讀完為止*/) {
char spot = s[pos];
if (/*如果是現在讀到的是運算元*/isDigit(spot) == true) {
//將數字push進stack
digs.push(spot - 48);
while (isDigit(s[pos + 1]) == true /*當現在讀到的數的下一位也是數字*/) {
//把stack裡的數字丟出來加上一個位數後,加上下一位再存回去stack
spot = (s[pos + 1] - 48);
digs.push(digs.pop() * 10 + spot);
//換下一個位置讀
pos++;
}
}
else {
if (/* 現在讀到的是減號'-'&&現在位置是在第一位 */spot == '-' && pos == 0) {
//把下一位數字變成負的,存進stack
digs.push(-(s[pos + 1] - 48));
//換下一個位置讀
pos++;
while (isDigit(s[pos + 1]) == true /*當現在讀到的數的下一位也是數字*/) {
//把stack裡的數字丟出來加上一個位數後,,加上「負的」下一位並再存回去stack
spot = (s[pos + 1] - 48);
digs.push(digs.pop() * 10 - spot);
//換下一個位置讀
pos++;
}
}
else if (spot == '-' && isDigit(s[pos - 1]) == false && s[pos - 1] != ')' /*如果是現在讀到的是減號'-'&&前一位不是數字*/) {
//把下一位數字變成負的,存進stack
digs.push(-(s[pos + 1] - 48));
//換下一個位置讀
pos++;
while (isDigit(s[pos + 1])/*當現在讀到的數的下一位也是數字*/) {
//把stack裡的數字丟出來加上一個位數後,,加上「負的」下一位並再存回去stack
spot = s[pos + 1] - 48;
digs.push(digs.pop() - spot);
//換下一個位置讀
pos++;
}
}
else if (spot == '(')
{
opers.push(spot);
}
else if (spot == ')')
{
while (opers.cur() != '(')
{
//Pop出一個運算子
char a = opers.pop();
//Pop出兩個運算元
int b = digs.pop();
int c = digs.pop();
//進行計算(使用calculate)
//將結果存入stack
digs.push(floor(calculate(c, b, a)));
}
opers.pop();
}
else { //執行運算
while (priority(spot) <= priority(opers.cur())/*如果讀到的運算子的層級 <= stack裡的最上層的運算子的層級(使用priority)*/) {
//Pop出一個運算子
char a = opers.pop();
//Pop出兩個運算元
int b = digs.pop();
int c = digs.pop();
//進行計算(使用calculate)
//將結果存入stack
digs.push(floor(calculate(c, b, a)));
}
//將現在讀到的運算子進入stack
opers.push(spot);
}
}
//換下一個位置讀
pos++;
}
while (opers.isEmpty() == false/*當運算子stack不是空的*/) {
//Pop出一個運算子
char a = opers.pop();
//Pop出兩個運算元
int b = digs.pop();
int c = digs.pop();
//進行計算(使用calculate)
//將結果存入stack
digs.push(floor(calculate(c, b, a)));
}
return (digs.cur()/*丟最後結果出來*/);
}