-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[1주차] 해바라기반 - 조현창 #3
The head ref may contain hidden characters: "1\uC8FC\uCC28/\uD574\uBC14\uB77C\uAE30\uBC18/\uC870\uD604\uCC3D"
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <deque> | ||
#include <string> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
ios_base::sync_with_stdio(0); | ||
cin.tie(NULL); | ||
cout.tie(NULL); | ||
string input; | ||
int res = 0, sign = 1; | ||
cin >> input; | ||
for (int i = 0; i < input.length(); i++) | ||
{ | ||
int tmp = 0; | ||
if (input[i] >= '0' && input[i] <= '9') | ||
{ | ||
while (input[i] >= '0' && input[i] <= '9') | ||
{ | ||
tmp *= 10; | ||
tmp += input[i] - '0'; | ||
i++; | ||
} | ||
} | ||
res += tmp * sign; | ||
if (i < input.length() && input[i] == '-') | ||
sign = -1; | ||
} | ||
cout << res; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <deque> | ||
#include <string> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
ios_base::sync_with_stdio(0); | ||
cin.tie(NULL); | ||
cout.tie(NULL); | ||
string input; | ||
int a, res = 0, tmp = 0, minus = 0, sum = 0, sub = 0; | ||
vector<int> A; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 14번째 줄 - 변수명이 (짧더라도) 좀 더 명확하면 좋겠습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아하! 2번째 커밋에 반영했습니다! |
||
vector<char> op; | ||
|
||
cin >> input; | ||
for (int i = 0; i < input.length(); i++) | ||
{ | ||
if (input[i] >= '0' && input[i] <= '9') | ||
{ | ||
while (input[i] >= '0' && input[i] <= '9') | ||
{ | ||
tmp *= 10; | ||
tmp += input[i] - '0'; | ||
i++; | ||
} | ||
} | ||
A.push_back(tmp); | ||
tmp = 0; | ||
if (i < input.length()) | ||
op.push_back(input[i]); | ||
} | ||
for (int i = 0; i < A.size(); i++) | ||
{ | ||
if (minus) | ||
{ | ||
sub += A[i]; | ||
} | ||
else | ||
{ | ||
res += A[i]; | ||
} | ||
if (i < op.size()) | ||
{ | ||
if (op[i] == '-') | ||
{ | ||
minus = 1; | ||
} | ||
} | ||
} | ||
cout << res - sub; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
현창님이 작성하신 코드를 보다가 이 부분에서
out of range
가 발생하지 않은 것을 보고 신기하여 좀 찾아보았습니다.C++의
string
의 경우,string
의 length에 해당하는 index를 참조해도 exception을 발생않는다고 합니다.참고한 사이트
out of range가 발생하는 경우
그래서
visual studio 2019
환경에서 문제가 발생할 만한 코드를 작성한 후에 디버깅을 해보았는데,정말로
string
의 length에 해당하는 index에 대해선out of range
가 발생하지 않더라고요.덕분에 하나 알아갑니다.
직접 테스트해 본 코드
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sarah2234 님의 풀이(#4) 처럼 저도 한 글자마다 숫자를 parsing 했었는데,
while
문으로 숫자를 parsing 하신 점이 굉장히 인상 깊었습니다.