質問編集履歴

1

コードの書き忘れ

2015/10/11 06:44

投稿

akamakku
akamakku

スコア191

test CHANGED
File without changes
test CHANGED
@@ -11,3 +11,183 @@
11
11
 
12
12
 
13
13
  どんな理由が考えられますか?
14
+
15
+
16
+
17
+ ```C
18
+
19
+ #include <stdio.h>
20
+
21
+ #include <string.h>
22
+
23
+ #include <ctype.h>
24
+
25
+
26
+
27
+ void shift(char str[], int n){
28
+
29
+ int i = 0, j;
30
+
31
+ for(j = 0; j < n; j++){
32
+
33
+ while(str[i] != '\0'){
34
+
35
+ str[i] = str[i+1];
36
+
37
+ i++;
38
+
39
+ }
40
+
41
+ i = 0;
42
+
43
+ }
44
+
45
+ }
46
+
47
+
48
+
49
+
50
+
51
+ void devideStr(char str[], char ret[], int s, int e){
52
+
53
+ int i;
54
+
55
+ for(i = s; i <= e; i++){
56
+
57
+ ret[i - s] = str[i];
58
+
59
+ }
60
+
61
+ ret[i] = '\0';
62
+
63
+ }
64
+
65
+
66
+
67
+ int chemsymToMolWeight(char chemsym[]){
68
+
69
+ int ret;
70
+
71
+ if(strcmp(chemsym,"A") == 0){
72
+
73
+ ret = 2;
74
+
75
+ }
76
+
77
+ if(strcmp(chemsym,"Bc") == 0){
78
+
79
+ ret = 3;
80
+
81
+ }
82
+
83
+ if(strcmp(chemsym,"De") == 0){
84
+
85
+ ret = 4;
86
+
87
+ }
88
+
89
+ if(strcmp(chemsym,"F") == 0){
90
+
91
+ ret = 5;
92
+
93
+ }
94
+
95
+ return ret;
96
+
97
+ }
98
+
99
+
100
+
101
+ int strToMolWeight(char chemform[]){
102
+
103
+ int MW=0, tmp, p = 0, c, n = 1;
104
+
105
+ char chemsym[3], ret[50], num[50];
106
+
107
+ while(chemform[0] != '\0'){
108
+
109
+ if(isupper(chemform[0])){
110
+
111
+ chemsym[p++] = chemform[0];
112
+
113
+ shift(chemform, 1);
114
+
115
+ if(islower(chemform[0])){
116
+
117
+ chemsym[p++] = chemform[0];
118
+
119
+ shift(chemform, 1);
120
+
121
+ }
122
+
123
+ chemsym[p] = '\0';
124
+
125
+ p = 0;
126
+
127
+ tmp = chemsymToMolWeight(chemsym);
128
+
129
+ }else if(chemform[0] == '('){
130
+
131
+ for(c = 1; chemform[c] != ')'; c++);
132
+
133
+ devideStr(chemform, ret, 1, c-1);
134
+
135
+ tmp = strToMolWeight(ret);
136
+
137
+ shift(chemform, c+1);
138
+
139
+ }
140
+
141
+ if(isdigit(chemform[0])){
142
+
143
+ while(isdigit(chemform[0])){
144
+
145
+ num[p++] = chemform[0];
146
+
147
+ shift(chemform, 1);
148
+
149
+ }
150
+
151
+ num[p] = '\0';
152
+
153
+ n = atoi(num);
154
+
155
+ p = 0;
156
+
157
+ }
158
+
159
+ tmp *= n;
160
+
161
+ MW += tmp;
162
+
163
+ n = 1;
164
+
165
+
166
+
167
+
168
+
169
+ }
170
+
171
+ return MW;
172
+
173
+ }
174
+
175
+
176
+
177
+ int main(){
178
+
179
+ char chemform[] = "A2(A23Bc)2";
180
+
181
+ printf("\n"); //ここです!
182
+
183
+ int MW;
184
+
185
+ MW = strToMolWeight(chemform);
186
+
187
+ printf("MolWeight = %d\n",MW);
188
+
189
+ return 0;
190
+
191
+ }
192
+
193
+ ```