#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){}
};
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;
}
}
}
}
void login(){
node a;
a.name = "f";
a.keyword = "f";
a.left = new node("c", "c");
a.left->left = new node("123", "123");
a.right = new node("one", "one");
a.right->right = new node("wang", "@@110");
node* root = &a;
cout << "請輸入要登錄的用戶名和密碼" << endl;
string c, d;
cin >> c >> d;
bool nameb, keywordb;//判斷用戶名和密碼正確與否
search(c, d, nameb, keywordb,root);
if (nameb&&keywordb)cout << "登錄成功!" << endl;
else if (nameb == true && keywordb == false){
while (keywordb == false){
cout << "您輸入的密碼有誤,請重新輸入密碼" << endl;
cin >> d;
search(c,d, nameb, keywordb,root);
if (keywordb == true){
cout << "登錄成功!" << endl;
return;
}
}
}
else {
cout << "您輸入的用戶名不存在!請重新輸入用戶名和密碼!" << endl;
login();
}
delete a.left->left;
a.left->left = 0;
delete a.left;
a.left = 0;
delete a.right->right;
a.right->right = 0;
delete a.right;
a.right = 0;
}
int main(){
login();
}
我是在做一個二叉平衡樹的實驗,出現了一個問題,所以簡化代碼測試如上,但是運行不能通過,若是通過了,在連續兩次輸對用戶名,輸錯密碼之後,會有一個小錯誤,求解啊
若是通過了,在連續兩次輸對用戶名,輸錯密碼之後,會連續兩次cout << "您輸入的用戶名不存在!請重新輸入用戶名和密碼!" << endl;