#include<iostream>
#include<string>
using namespace std;
class node{
public:
string name;
string keyword;
node* left;
node* right;
node(string a = "0", string b = "0", node* c = 0, node* d = 0) :
name(a), keyword(b), left(c), right(d){}
~node(){
left = 0; right = 0;
}
};
void search(string name2, string keyword2, bool& nameb, bool& keywordb,node* root){
node* p = root;
nameb = keywordb = false;
while (p){
if (name2 < p->name)
p = p->left;
else if (name2>p->name)
p= p->right;
else {
nameb = true;
if (keyword2 == p->keyword)
keywordb = true;
else
keywordb = false;
}
}
}
int main(){
node a, b, c, d, e;
a.name = "cang"; a.keyword = "12345";
b.name = "123"; b.keyword = "123";
c.name = "onepiece"; c.keyword = "54321";
d.name = "fan"; d.keyword = "fan123";
e.name = "wang"; e.keyword = "@@110";
a.left = &b;
a.right = &c;
c.left = &d;
c.right = &e;
node* root = &a;
cout << "請輸入要登錄的用戶名和密碼" << endl;
string aa, bb;
cin >> aa >> bb;
bool nameb, keywordb;//判斷用戶名和密碼正確與否
search(aa,bb, nameb, keywordb, root);
if (nameb&&keywordb)cout << "登錄成功!" << endl;
else if (nameb == true && keywordb == false){
while (keywordb == false){
cout << "您輸入的密碼有誤,請重新輸入密碼" << endl;
cin >> bb;
search(aa,bb, nameb, keywordb, root);
if (keywordb == true)
cout << "登錄成功!" << endl;
}
}
return 0;
}
可以通過編譯,輸入完後,程序沒有反應,我是在做一個二叉樹的實驗,所以把問題精簡成這樣來測試,
我百思不得其解啊
void search(string name2, string keyword2, bool& nameb, bool& keywordb,node* root){
node* p = root;
nameb = keywordb = false;
while (p){
if (name2 < p->name)
p = p->left;
else if (name2>p->name)
p= p->right;
else {
nameb = true;
if (keyword2 == p->keyword){
keywordb = true;
return;
}
else{
keywordb = false;
return;
}
}
}
}
這樣改似乎更好些,我還是太粗心了啊,基礎不牢固啊