백준 1541 c++

magicdrill·2024년 2월 28일

백준 문제풀이

목록 보기
57/673

백준 1541 c++

#include <iostream>
#include <string>

using namespace std;

void input_str(string &str)
{
	//cout << "input_str()\n";

	cin >> str;

	return;
}

int find_result(string str)
{
	//cout << "find_result()\n"
	//'-'뒤에 오는 식을 '-'이 다시 나오기 전까지 모두 괄호 안에 넣는다
	int i;
	bool minus = false;
	int total = 0;
	string temp = "";

	for (i = 0; i < str.length() + 1; i++) //50자면 51까지
	{
		if (str[i] == '-' || str[i] == '+' || str[i] == '\0')
		{
			if (minus)
			{
				total = total - stoi(temp);
			}
			else
			{
				total = total + stoi(temp);
			}
			temp = "";
			if (str[i] == '-')
			{
				minus = true;
			}
			else
			{
				;
			}
		}
		else
		{
			temp = temp + str[i];
		}
	}

	return total;
}

int main(void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	string str;

	input_str(str);
	cout << find_result(str) << "\n";

	return 0;
}

0개의 댓글