코딩테스트/프로그래머스 - LV 0

[프로그래머스] 문자열 계산하기

웅드 2024. 1. 12. 20:54
#include <string>
#include <vector>
#include <sstream>

using namespace std;

int solution(string my_string) {
    int answer = 0;
    stringstream s(my_string);
    s >> answer; // 첫 번째 숫자를 answer에 저장
    int n;
    char c;
    while(s >> c >> n){ // 문자 숫자가 반복해서 나오기 때문에 s>>c>>n 으로 실행
        if(c=='+') answer += n;
        else answer -= n;
    }
    return answer;
}
반응형