Skip to content
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

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions 해바라기반/조현창/1541잃어버린 괄호 try2.cpp
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')
Copy link
Collaborator

@alirz-pixel alirz-pixel Apr 10, 2022

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가 발생하지 않더라고요.

덕분에 하나 알아갑니다.

직접 테스트해 본 코드
> 출력
> a
> abc

Copy link
Collaborator

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 하신 점이 굉장히 인상 깊었습니다.

{
tmp *= 10;
tmp += input[i] - '0';
i++;
}
}
res += tmp * sign;
if (i < input.length() && input[i] == '-')
sign = -1;
}
cout << res;
}
53 changes: 53 additions & 0 deletions 해바라기반/조현창/1541잃어버린 괄호.cpp
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;
Copy link
Contributor

@sarah2234 sarah2234 Apr 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

14번째 줄 - 변수명이 (짧더라도) 좀 더 명확하면 좋겠습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
}