Notice
Recent Posts
Recent Comments
Link
«   2025/08   »
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
Archives
Today
Total
관리 메뉴

코딩로그

[백준/BOJ/C++] 5656번 비교 연산자 본문

YJ/C++

[백준/BOJ/C++] 5656번 비교 연산자

Team DAON 2021. 10. 26. 14:55

[문제]

C언어의 비교 연산자는 아래 표에 나와있다.

연산자뜻

연산자
> 크다
>= 크거나 같다
< 작다
<= 작거나 같다
== 같다
!= 같지 않다

이 연산자는 두 피연산자를 비교하고, (왼쪽 값과 오른쪽 값) true또는 false (1 또는 0)을 리턴한다. 예를 들어, 2 > 3은 "false"를 리턴하고 (2는 3보다 작기 때문), 3 != 4는 "true", 3 >= 3은 "true"를 리턴한다.

C언어의 비교 연산식이 주어졌을 때, 결과를 구하는 프로그램을 작성하시오.

 

[입력]

입력은 최대 12000줄로 이루어져 있다. 각 줄은 두 정수 a, b가 주어지며, 정수 사이에는 연산자 ">", ">=", "<", "<=", "==", "!="중 하나가 주어진다. 연산자와 피연산자 사이에는 공백이 하나 있으며, 연산자로 "E"가 주어진 경우에는 프로그램을 끝낸다. (-10000 ≤ a,b ≤ 10000)

 

입력의 각 줄 마다 입력으로 주어진 식의 결과가 "true"인지 "false"인지 출력한다.

 

[Source Code]

#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main() {
	vector<int> vector;

	while(true){
		int a, b;
		string op;
		cin >> a >> op >> b;
		if(op == "E")
			break;
		else if(op == ">"){
			if(a > b) vector.push_back(1);
			else vector.push_back(0);
		}
		else if(op == ">="){
			if(a >= b) vector.push_back(1);
			else vector.push_back(0);
		}
		else if(op == "<"){
			if(a < b) vector.push_back(1);
			else vector.push_back(0);
		}
		else if(op == "<="){
			if(a <= b) vector.push_back(1);
			else vector.push_back(0);
		}
		else if(op == "=="){
			if(a == b) vector.push_back(1);
			else vector.push_back(0);
		}
		else if(op == "!="){
			if(a != b) vector.push_back(1);
			else vector.push_back(0);
		}
	}

	for(int i = 0; i < vector.size(); i++){
		cout << "Case " << i+1 <<": ";
		if(vector[i] == 1)
			cout << "true\n";
		else
			cout << "false\n";
	}
}

[결과 화면]