Problem:
๋ง์
(+), ๋บ์
(-), ๊ณฑ์
(*), ๋๋์
(/), ๋๋จธ์ง(%)์ ์ ์ 5์ฒ ์ฐ์ฐ์ ํ ์ ์๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ๋ผ. ์์ ๋ค์๊ณผ ๊ฐ์ ํ์์ผ๋ก ์
๋ ฅ๋๋ค. ์ ์์ ์ฐ์ฐ์๋ ํ๋์ ๋น์นธ์ผ๋ก ๋ถ๋ฆฌ๋๋ค.
Objective & Hints:
๊ณต๋ฐฑ์ ํฌํจํ๋ ๋ฌธ์์ด ์ฝ๊ธฐ, C++ ํ๋ก๊ทธ๋จ ์ข
ํฉ ์์ฉ
ํ ์ค์ ๋ฌธ์์ด๋ก ์ฝ๊ณ , ๊ณต๋ฐฑ ๋ฌธ์๋ฅผ ์ฐพ์ ์ฐ์ฐ์์ ๋ ๊ฐ์ ํผ์ฐ์ฐ์๋ฅผ ๊ตฌ๋ถํ ํ, ๊ณ์ฐํ๋ฉด ๋ฉ๋๋ค.
๋ฌธ์์ด์ ์ ์๋ก ๋ฐ๊ฟ ๋ atoi() ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
์๋ฅผ ๋ค๋ฉด atoi("34") = 34 ์
๋๋ค.
Execution Result:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include #include #include using namespace std; int main(){ char sic[100]; char *symbol; int front,rear; while(true){ cout << "? "; cin.getline(sic,100); front = atoi(strtok(sic, " ")); symbol = strtok(NULL, " "); rear = atoi(strtok(NULL, " ")); if(*symbol == '+'){ cout << front << " + " << rear << " = " << front+rear << endl; } else if(*symbol == '-'){ cout << front << " - " << rear << " = " << front-rear << endl; } else if(*symbol == '*'){ cout << front << " * " << rear << " = " << front*rear << endl; } else if(*symbol == '/'){ cout << front << " / " << rear << " = " << front/rear << endl; } else if(*symbol == '%'){ cout << front << " % " << rear << " = " << front%rear << endl; } } return 0; } Explanation:
...