質問編集履歴

6

コードを訂正しました

2018/02/01 08:09

投稿

Momomo.
Momomo.

スコア22

test CHANGED
File without changes
test CHANGED
@@ -34,7 +34,9 @@
34
34
 
35
35
 
36
36
 
37
- void toInteger(string num, int n, int x, int numInInteger){
37
+ int toInteger(string num, int n, int x, int numInInteger){
38
+
39
+ int count=num.length()-1;
38
40
 
39
41
 
40
42
 
@@ -42,25 +44,25 @@
42
44
 
43
45
  if(num.length()==0)
44
46
 
45
- return;
47
+ return 0;
46
48
 
47
49
 
48
50
 
49
51
  //keep doing if the length is not zero
50
52
 
51
- while(num.length()>=1){
53
+ while(count>=0){
52
54
 
53
55
  numInInteger = numInInteger*n+(num.at(x)-'0');
54
56
 
57
+ count--;
55
58
 
59
+ //move position from right to left, and increase n by 10 times
60
+
61
+ toInteger(num, n=n*10, x-1, numInInteger);
56
62
 
57
63
  }
58
64
 
59
-
60
-
61
- //move position from right to left, and increase n by 10 times
62
-
63
- toInteger(num, n=n*10, x-1, numInInteger);
65
+ return numInInteger;
64
66
 
65
67
 
66
68
 
@@ -68,7 +70,7 @@
68
70
 
69
71
 
70
72
 
71
- void toIntegerIterative(string num, int numInInteger){
73
+ int toIntegerIterative(string num, int numInInteger){
72
74
 
73
75
 
74
76
 
@@ -78,11 +80,7 @@
78
80
 
79
81
  numInInteger = numInInteger*10+(num.at(i)-'0');
80
82
 
81
-
82
-
83
- //print result
83
+ return numInInteger;
84
-
85
- cout << "The result is " << numInInteger <<endl;
86
84
 
87
85
  }
88
86
 
@@ -116,7 +114,17 @@
116
114
 
117
115
  toInteger(num,n,x,numInInteger);
118
116
 
117
+ //print result
118
+
119
+ cout << "The result is " << numInInteger <<endl;
120
+
121
+
122
+
119
123
  toIntegerIterative(num,numInInteger);
124
+
125
+ //print result
126
+
127
+ cout << "The result is " << numInInteger <<endl;
120
128
 
121
129
 
122
130
 

5

誤字

2018/02/01 08:09

投稿

Momomo.
Momomo.

スコア22

test CHANGED
File without changes
test CHANGED
@@ -58,7 +58,7 @@
58
58
 
59
59
 
60
60
 
61
- //move index from right to left, and increase n by 10 times
61
+ //move position from right to left, and increase n by 10 times
62
62
 
63
63
  toInteger(num, n=n*10, x-1, numInInteger);
64
64
 

4

ご指摘いただいた箇所を修正しました

2018/02/01 07:43

投稿

Momomo.
Momomo.

スコア22

test CHANGED
File without changes
test CHANGED
@@ -24,7 +24,7 @@
24
24
 
25
25
  ```c++
26
26
 
27
- include <iostream>
27
+ #include <iostream>
28
28
 
29
29
  #include <ctime>
30
30
 
@@ -34,9 +34,11 @@
34
34
 
35
35
 
36
36
 
37
- void toInteger(string num, int n, int index, int numInInteger){
37
+ void toInteger(string num, int n, int x, int numInInteger){
38
38
 
39
39
 
40
+
41
+ //base case
40
42
 
41
43
  if(num.length()==0)
42
44
 
@@ -44,9 +46,11 @@
44
46
 
45
47
 
46
48
 
49
+ //keep doing if the length is not zero
50
+
47
51
  while(num.length()>=1){
48
52
 
49
- numInInteger = numInInteger*10+(num.at(index)-'0');
53
+ numInInteger = numInInteger*n+(num.at(x)-'0');
50
54
 
51
55
 
52
56
 
@@ -54,7 +58,9 @@
54
58
 
55
59
 
56
60
 
61
+ //move index from right to left, and increase n by 10 times
62
+
57
- toInteger(num, n*10, index-1, numInInteger);
63
+ toInteger(num, n=n*10, x-1, numInInteger);
58
64
 
59
65
 
60
66
 
@@ -70,11 +76,11 @@
70
76
 
71
77
  for(int i=0; i<num.length(); i++)
72
78
 
73
- //
74
-
75
79
  numInInteger = numInInteger*10+(num.at(i)-'0');
76
80
 
77
81
 
82
+
83
+ //print result
78
84
 
79
85
  cout << "The result is " << numInInteger <<endl;
80
86
 
@@ -84,13 +90,9 @@
84
90
 
85
91
  int main(){
86
92
 
93
+ //define variables
94
+
87
95
  string num;
88
-
89
- int n=1;
90
-
91
- int index=num.at(num.length());
92
-
93
- int numInInteger=0;
94
96
 
95
97
 
96
98
 
@@ -102,9 +104,17 @@
102
104
 
103
105
 
104
106
 
107
+ int n=1;
108
+
109
+ int x=num.at(num.length()-1);
110
+
111
+ int numInInteger=0;
112
+
113
+
114
+
105
115
  //call functions
106
116
 
107
- toInteger(num,n,index,numInInteger);
117
+ toInteger(num,n,x,numInInteger);
108
118
 
109
119
  toIntegerIterative(num,numInInteger);
110
120
 

3

実際に実行した際の状況を追加いたしました

2018/02/01 07:42

投稿

Momomo.
Momomo.

スコア22

test CHANGED
File without changes
test CHANGED
@@ -6,7 +6,17 @@
6
6
 
7
7
 
8
8
 
9
+ 実際に実行すると
10
+
11
+ libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string
12
+
13
+ このように表示されてしまいます。
14
+
15
+
16
+
9
- 自分なりに通常のループを使ったパターンと回帰のパターンを書きました。よろしければアドバイスをください。
17
+ このコードは自分なりに通常のループを使ったパターンと回帰のパターンを書きました。よろしければアドバイスをください。
18
+
19
+
10
20
 
11
21
 
12
22
 

2

期待する結果を追加しました

2018/02/01 07:25

投稿

Momomo.
Momomo.

スコア22

test CHANGED
File without changes
test CHANGED
@@ -2,13 +2,11 @@
2
2
 
3
3
 
4
4
 
5
- 例えばユーザーがStringで8967と入力したintの8967に変換したいです。
5
+ 期待する結果:例えばユーザーがStringで8967と入力した場合にintの8967をプリントしたいです。
6
-
7
- atoiやstoiを使ってできることをatoiやstoiを使わずにしたいということです。
8
6
 
9
7
 
10
8
 
11
- 自分なりに通常のループを使ったパターンと回帰と、両方のパターンでプログラムを書いてみたのですがどうやらコードにどこかおかしいところがあるようで、思うようにできません。よろしければアドバイスをください。
9
+ 自分なりに通常のループを使ったパターンと回帰のパターンを書きました。よろしければアドバイスをください。
12
10
 
13
11
 
14
12
 

1

質問が分かりにくかったようなので例えを追加いたしました。もしまだ分かりにくかったら教えてください!

2018/02/01 07:06

投稿

Momomo.
Momomo.

スコア22

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,15 @@
2
2
 
3
3
 
4
4
 
5
+ 例えば、ユーザーがStringで8967と入力したらintの8967に変換したいです。
6
+
7
+ atoiやstoiを使ってできることをatoiやstoiを使わずにしたいということです。
8
+
9
+
10
+
5
11
  自分なりに通常のループを使ったパターンと回帰と、両方のパターンでプログラムを書いてみたのですがどうやらコードにどこかおかしいところがあるようで、思うようにできません。よろしければアドバイスをください。
12
+
13
+
6
14
 
7
15
 
8
16