항상 프로그래머 게시판에서 많은 정보와 도움을 얻고 가는 프로그래밍 공부하는 학생입니다 :-) <div>얼마전에 같은 질문으로 글을 올렸는데 아직도 해결이 되지 않아서 이렇게 다시 염치없게 질문 합니다 ㅠㅠ</div> <div><br></div> <div>kg, lbs, feet, inch, m, cm 으로 입력시 결과를 도출하는 건데 (예: 60kg and 1.7m) </div> <div><br></div> <div>feet과 inch를 '(single quotation) 와 "(double quotation)으로 입력시 결과가 다오게 하려면 어떻게 해야 하는지 아무리 머리를</div> <div>쥐어짜도 답이 안나오네요 ㅠㅠ</div> <div><br></div> <div>요약: 5 ft6 in 를 5'6"로도 입력 가능하게 해서 같은 값을 나오게 하고 싶음 </div> <div><br></div> <div><div style="font-family:gulim;line-height:14.4000005722046px;"><span style="font-size:9pt;line-height:1.5;">#include <iostream></span></div> <div style="font-family:gulim;line-height:14.4000005722046px;">#include <string></div> <div style="font-family:gulim;line-height:14.4000005722046px;">#include <iomanip></div> <div style="font-family:gulim;line-height:14.4000005722046px;">#include <cmath></div> <div style="font-family:gulim;line-height:14.4000005722046px;"><br></div> <div style="font-family:gulim;line-height:14.4000005722046px;">using namespace std;</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><br></div> <div style="font-family:gulim;line-height:14.4000005722046px;">double weight_convert(double, string);</div> <div style="font-family:gulim;line-height:14.4000005722046px;">double height_convert(double, string);</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><br></div> <div style="font-family:gulim;line-height:14.4000005722046px;">int main()</div> <div style="font-family:gulim;line-height:14.4000005722046px;">{</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>double weight;</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>double height;</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>// double height_add;</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>string weight_unit;</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>string height_unit;</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>// string height_unit_add;</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>string and;</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span></div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>cout << "Welcome to BMI Calculator!" << endl;</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>cout << "--------------------------" << endl;</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>cout << "Enter your weight in kg or lb and height in m, cm, ft, or in: ";</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>cin >> weight;</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>cin >> weight_unit;</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>cin >> and;</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>cin >> height;</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>cin >> height_unit;</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>// bmi formular using double, pow</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>double bmi = weight_convert(weight, weight_unit) / pow(height_convert(height, height_unit), 2);</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>cout << "\nBMI: " << fixed << setprecision(3) << bmi << endl;</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><br></div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>// name: category</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>// return: if</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>// purpose: to determine the category of the person depends upon the BMI calculated</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>// accept: if, else if</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>if (bmi < 15)</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>{</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>cout << "You are very severely underweight.\n";</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>}</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>else if (bmi >= 15.0 && bmi < 16.0)</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>{</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>cout << "You are severely underweight.\n";</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>}</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>else if (bmi >= 16.0 && bmi < 18.5)</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>{</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>cout << "You are underweight.\n";</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>}</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>else if (bmi >= 18.5 && bmi < 25)</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>{</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>cout << "You are normal(Healthy weight!)\n";</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>}</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>else if (bmi >= 30 && bmi < 35)</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>{</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>cout << "You are obese class 1 (Moderately obese!)\n";</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>}</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>else if (bmi >= 35 && bmi < 40)</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>{</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>cout << "You are obese class 2 (Severely obese!)\n";</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>}</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>else if (bmi >= 40)</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>{</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>cout << "You are obese class 3 (Very severely obese!)\n";</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>}</div> <div style="font-family:gulim;line-height:14.4000005722046px;">}</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><br></div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>// name: weight_convert</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>// return: double</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>// purpose: to convert other units into 'kg'</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>// accept: double, string</div> <div style="font-family:gulim;line-height:14.4000005722046px;">double weight_convert(double weight, string unit)</div> <div style="font-family:gulim;line-height:14.4000005722046px;">{</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>if (unit == "lb")</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>return weight * 0.453592;</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>else if (unit == "kg")</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>return weight;</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>cout << "Error: Invalid input. Please try again!" << endl;</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>exit(0);</div> <div style="font-family:gulim;line-height:14.4000005722046px;">}</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><br></div> <div style="font-family:gulim;line-height:14.4000005722046px;">double height_convert(double height, string unit)</div> <div style="font-family:gulim;line-height:14.4000005722046px;">{</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>if (unit == "m")</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>return height;</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>else if (unit == "cm")</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>return height * 0.01;</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>else if (unit == "ft")</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>return height * 0.3048;</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>else if (unit == "in")</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>return height * 0.0254;</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>cout << "Error: Invalid input. Please try again!" << endl;</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span>exit(0);</div> <div style="font-family:gulim;line-height:14.4000005722046px;"><span class="Apple-tab-span" style="white-space:pre;"> </span></div> <div style="font-family:gulim;line-height:14.4000005722046px;">}</div></div>
댓글 분란 또는 분쟁 때문에 전체 댓글이 블라인드 처리되었습니다.