回答編集履歴
6
コードのバグ修正
answer
CHANGED
@@ -30,7 +30,7 @@
|
|
30
30
|
c == '*' ? v *= expr(b + 2) :
|
31
31
|
c == '/' ? v /= expr(b + 2) : (v = pow(v, expr(b)));
|
32
32
|
else // number, parentheses or unary operators
|
33
|
-
get() == '.' || isdigit(c) ? iss.unget(), iss >> v
|
33
|
+
get() == '.' || isdigit(c) ? iss.unget(), iss >> v ? get() : (c = 3):
|
34
34
|
c == '(' ? v = expr("+-*/^^"), c == ')' ? get() : (c = 2) :
|
35
35
|
c == '+' ? v = expr(b) :
|
36
36
|
c == '-' ? v = -expr(b) : (v = c = 1);
|
@@ -43,4 +43,6 @@
|
|
43
43
|
string s;
|
44
44
|
while (cout << ">> ", getline(cin, s) && s != ".") Stack t(s);
|
45
45
|
}
|
46
|
-
```
|
46
|
+
```
|
47
|
+
### 追記
|
48
|
+
3 + . + 5 がエラーにならないバグがあったの修正しました。
|
5
コードの修正
answer
CHANGED
@@ -20,7 +20,7 @@
|
|
20
20
|
c ? cout << " error\n" : cout << " " << v << "\n";
|
21
21
|
}
|
22
22
|
private:
|
23
|
-
int get() { iss >> c
|
23
|
+
int get() { return iss >> c ? c : (c = 0); }
|
24
24
|
double expr(const char *b) {
|
25
25
|
double v;
|
26
26
|
if (*b) // binary operators. power is right-associative.
|
4
コードの変更
answer
CHANGED
@@ -41,6 +41,6 @@
|
|
41
41
|
int main()
|
42
42
|
{
|
43
43
|
string s;
|
44
|
-
while (cout << ">> ", getline(cin, s) && s
|
44
|
+
while (cout << ">> ", getline(cin, s) && s != ".") Stack t(s);
|
45
45
|
}
|
46
46
|
```
|
3
コードの追加
answer
CHANGED
File without changes
|
2
cout.precision(15); を追加
answer
CHANGED
@@ -16,6 +16,7 @@
|
|
16
16
|
public:
|
17
17
|
Stack(const string& s) : iss(s) {
|
18
18
|
double v = expr("+-*/^^");
|
19
|
+
cout.precision(15);
|
19
20
|
c ? cout << " error\n" : cout << " " << v << "\n";
|
20
21
|
}
|
21
22
|
private:
|
1
コメント訂正
answer
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
#include <iostream> // cin, cout
|
7
7
|
#include <sstream> // istringstream
|
8
8
|
#include <string> // getline
|
9
|
-
#include <cctype> //
|
9
|
+
#include <cctype> // isdigit
|
10
10
|
#include <cmath> // pow
|
11
11
|
using namespace std;
|
12
12
|
|