teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

6

コードのバグ修正

2019/06/26 08:42

投稿

kazuma-s
kazuma-s

スコア8222

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, get() :
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

コードの修正

2019/06/26 08:42

投稿

kazuma-s
kazuma-s

スコア8222

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; return !iss ? c = 0 : 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

コードの変更

2019/06/25 00:22

投稿

kazuma-s
kazuma-s

スコア8222

answer CHANGED
@@ -41,6 +41,6 @@
41
41
  int main()
42
42
  {
43
43
  string s;
44
- while (cout << ">> ", getline(cin, s) && s[0] != '.') Stack t(s);
44
+ while (cout << ">> ", getline(cin, s) && s != ".") Stack t(s);
45
45
  }
46
46
  ```

3

コードの追加

2019/06/24 23:40

投稿

kazuma-s
kazuma-s

スコア8222

answer CHANGED
File without changes

2

cout.precision(15); を追加

2019/06/24 23:28

投稿

kazuma-s
kazuma-s

スコア8222

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

コメント訂正

2019/06/24 23:07

投稿

kazuma-s
kazuma-s

スコア8222

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> // isdigitC++
9
+ #include <cctype> // isdigit
10
10
  #include <cmath> // pow
11
11
  using namespace std;
12
12