안녕하세요 엘체프 GG 임돠
DB대신 map에 데이터를 넣어 이용할 수 있는 login 문제입니다.
실행/출력 결과 예시)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 |
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main(void) {
string id;
string pw;
// 문1) id/pw 입력 부분 완성
// 시작
// 끝
// 문2) map 객체 변수를 생성하고, 주어진 데이터를
// map 객체에 대입.
// ID/PW는 각각 아래와 같이 2가지 세트를 대입합니다.
// ID : abcd, PW : 1234
// ID : a1234, PW : 5678
// 대입한 map 객체의 데이터를 연속적으로 화면 인쇄 !
// 시작
// 끝
// 입력한 id/pw를 map 객체의 id/pw와 비교하여
// 메시징하는 부분 -> 이중 if !
// id 존재여부 점검 -> pw 존재 여부 점검
map<string, string>::iterator it
= mapLogin.find(id);
if (it == mapLogin.end()) {
cout << "아이디가 존재하지 않습니다." << endl;
} else {
if (id == (*it).first) {
if (pw == (*it).second) {
cout << "패쓰워드가 일치합니다." << endl;
} else {
cout << "패쓰워드가 일치하지 않습니다." << endl;
} // if
}
}
system("PAUSE");
return 0;
} |
-답안
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 |
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main(void) {
string id;
string pw;
// 문1) id/pw 입력 부분 완성
cout << "아이디를 입력하십시오 : ";
cin >> id;
// cout << endl;
cout << "패쓰워드를 입력하십시오 : ";
cin >> pw;
cout << endl;
cout << id << "," << pw << endl;
// 문2) map 객체 변수를 생성하고, 주어진 데이터를
// map 객체에 대입.
// ID/PW는 각각 아래와 같이 2가지 세트를 대입합니다.
// ID : abcd, PW : 1234
// ID : a1234, PW : 5678
// 대입한 map 객체의 데이터를 연속적으로 화면 인쇄 !
map<string, string> mapLogin;
mapLogin.insert(make_pair("abcd", "1234"));
mapLogin.insert(make_pair("a1234", "5678"));
/*
map<string, string>::reverse_iterator it
= mapLogin.rbegin();
while (it !=mapLogin.rend()) {
cout << it->first << "," << it->second << endl;
it++;
} // for
*/
// 입력한 id/pw를 map 객체의 id/pw와 비교하여
// 메시징하는 부분 -> 이중 if !
// id 존재여부 점검 -> pw 존재 여부 점검
map<string, string>::iterator it
= mapLogin.find(id);
if (it == mapLogin.end()) {
cout << "아이디가 존재하지 않습니다."
<< endl;
}
else {
if (id == (*it).first) {
if (pw == (*it).second) {
cout << "패쓰워드가 일치합니다." << endl;
}
else {
cout << "패쓰워드가 일치하지 않습니다."
<< endl;
} // if
}
}
system("PAUSE");
return 0;
} |
cs |
감사합니다.
'스터디 > C,C++' 카테고리의 다른 글
비속어 필터링(trash talk filtering) 문제 (6) | 2018.03.05 |
---|---|
map 정렬 문제 (6) | 2018.03.05 |
C 문자열(string) 분리 (6) | 2018.02.28 |
C 문자열(string) 바꾸기 (6) | 2018.02.28 |
마지막 C 시험문제 공백 사각찍기 (7) | 2018.02.28 |