STL과 맵,스택
#include <iostream>
#include <string>
#include <map>
#include <stack>
using namespace std;
//실습문제)괄호쌍을입력받아올바른지체크
//예) [{()}] => ok
//예) [{()}]] => error
//예) [{()]} => error
void main()
{
stack<char> st;
string input;
int i;
cout<<"괄호쌍입력:" ;
cin>> input;
for(i = 0; i<input.length(); i++){
if(input[i] == '[' || input[i] == '{' || input[i] == '(') //여는괄호
st.push(input[i]);
else{
//예외처리
if(st.empty())
cout << "입력넣어주세요" << endl; return;
//닫는괄호
if(input[i] == ']' && st.top() == '[')
st.pop();
else if(input[i] == '}' && st.top() =='{')
st.pop();
else if(input[i] == ')' && st.top() =='(')
st.pop();
else {
cout << "error" <<endl;
return;
}
}
}
if(st.empty()) //stack에남아잇는지확인없으면ok 남아있으면error
cout<< "OK " <<endl;
else
cout<< "error" <<endl;
}
/*
//스택과큐
int main()
{
stack<string> st;
string sayings[3] =
{
"The grass is greener on the other side of the fence",
"Even the greatest make mistakes",
"To see is to believe"
};
for(int i=0; i<3; i++)
st.push(sayings[i]);
while(!st.empty())
{
cout << st.top() << endl;
st.pop();
}
return 0;
}
/*
int main()
{
map<string, int> table;
string s;
cout << "문장을입력하시오(종료는컨트롤+ Z) : ";
while(1)
{
cin >> s;
if(cin.eof()) break;
table[s]++;
}
map<string, int>::iterator iter;
for(iter = table.begin(); iter != table.end(); iter++)
{
cout << iter->first << " : " << iter->second << endl;
}
return 0;
}
/*
int main()
{
map<string, string> dic;
dic["boy"] = "소년";
dic["school"] = "학교";
dic["office"] = "직장";
dic["house"] = "집";
dic["morning"] = "아침";
dic["evening"] = "저녁";
cout << "house의의미는" << dic["house"] << endl;
cout << "morning의의미는" << dic["morning"] << endl;
cout << "unknown의의미는" << dic["unknown"] << endl;
return 0;
}
*/