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

質問編集履歴

2

バグを修正

2016/05/11 17:42

投稿

Paalon
Paalon

スコア266

title CHANGED
File without changes
body CHANGED
@@ -78,7 +78,7 @@
78
78
  };
79
79
 
80
80
  int main() {
81
- Direction a = Direction::FRONT;
81
+ Direction a(Direction::FRONT);
82
82
  std::cout << a.get() << std::endl;
83
83
  a.turnLeft();
84
84
  std::cout << a.get() << std::endl;

1

Javaのコードの回転の向きがおかしかったのを修正、そもそもJavaのコードが意図した通りになっていなかったので代わりにC\+\+で記述。

2016/05/11 17:42

投稿

Paalon
Paalon

スコア266

title CHANGED
File without changes
body CHANGED
@@ -14,10 +14,10 @@
14
14
  this.value = value;
15
15
  }
16
16
  public void turnRight() {
17
- this.value = (this.value + 1) % 4;
17
+ this.value = (this.value + 3) % 4;
18
18
  }
19
19
  public void turnLeft() {
20
- this.value = (this.value + 3) % 4;
20
+ this.value = (this.value + 1) % 4;
21
21
  }
22
22
  public void reverse() {
23
23
  this.value = (this.value + 2) % 4;
@@ -34,9 +34,54 @@
34
34
  System.out.println("a: " + a.get()); // a: 0
35
35
  System.out.println("b: " + b.get()); // b: 3
36
36
  b.turnRight();
37
- System.out.println("b: " + b.get()); // b: 1
37
+ System.out.println("b: " + b.get()); // b: 2
38
38
  }
39
39
  }
40
40
  ```
41
41
  JavaScriptとC++とGolangとPythonではそれぞれ、どのようにこのようなクラスを実装すれば良いのでしょうか?(見た目の綺麗さの面や処理速度的な面、妥協点について)
42
- 4つまとめてでなくて構わないのでお聞かせ下さい。
42
+ 4つまとめてでなくて構わないのでお聞かせ下さい。
43
+
44
+ と思っていたのですが、以上のJavaのコードは間違っていたのでC++で書いたものを載せます。すみません。
45
+ ```C++
46
+ #include <iostream>
47
+
48
+ class Direction {
49
+ public:
50
+ static const int FRONT = 0;
51
+ static const int RIGHT = 1;
52
+ static const int BACK = 2;
53
+ static const int LEFT = 3;
54
+ static const int DOWN = 0;
55
+ static const int UP = 2;
56
+ private:
57
+ int value;
58
+
59
+ public:
60
+ Direction() {
61
+ this->value = FRONT;
62
+ }
63
+ Direction(int value) {
64
+ this->value = value;
65
+ }
66
+ void turnRight() {
67
+ this->value = (this->value + 3) % 4;
68
+ }
69
+ void turnLeft() {
70
+ this->value = (this->value + 1) % 4;
71
+ }
72
+ void reverse() {
73
+ this->value = (this->value + 2) % 4;
74
+ }
75
+ int get() {
76
+ return this->value;
77
+ }
78
+ };
79
+
80
+ int main() {
81
+ Direction a = Direction::FRONT;
82
+ std::cout << a.get() << std::endl;
83
+ a.turnLeft();
84
+ std::cout << a.get() << std::endl;
85
+ return 0;
86
+ }
87
+ ```