散列3 QQ帐户的申请与登陆

散列3 QQ帐户的申请与登陆

#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
#define N 100010
#define M 100000
struct QQ
{
    char Acnumber[15];
    char Password[20];
    int  state;
}A[N];

int Hash(char Acnumber[], char Password[], char c )
{
    int address  = 0;
    int s; // L 1:存在且密码正确 0:不存在 -1:存在但密码错误. 
           // N 1:不存在且创建成功 0:存在 
    int l = strlen( Acnumber );
    if( l <= 6)
        for( int i = 0; i < l; i++ )
            address  = address *10 + ( Acnumber[i] - '0');
    else
        for( int i = l-6; i < l; i++ )
            address  = address *10 + ( Acnumber[i] - '0' );

    // Find



    if( c == 'L')
    {
        s = 0;
        for( int i = 0; i < M; i++ ) 
        {
            int j =( address + i ) % M;
            if( A[j].state == 0 )
                break;
            if( strcmp( A[j].Acnumber, Acnumber ) == 0 )
            {
                if( strcmp( A[j].Password, Password ) == 0 )
                    s = 1;
                else
                    s = -1;

                break;
            }

        }

    }
    else if( c == 'N')
    {
        s = 0;
        for( int i = 0; i < M; i++ ) 
        {
            int j =( address + i ) % M;
            if( strcmp( A[j].Acnumber, Acnumber ) == 0 )
                break;
            if( A[j].state == 0 )
            {
                s = 1; 
                A[j].state = 1;
                strcpy( A[j].Acnumber, Acnumber );
                strcpy( A[j].Password, Password );
                break;
            }
        }

    }

    return s; 
}  

int main() 
{
    int n;
    char state;
    char acnumber[15];
    char password[20];
    cin >> n;
    while( n-- )
    {
        cin >> state >> acnumber >> password;
        if( state == 'L')
        {

            int s = Hash( acnumber, password, 'L' );
            if(  s == 1 )
                cout << "Login: OK" << endl;
            else if( s == 0)
                cout << "ERROR: Not Exist" << endl;
            else if( s == -1 )
                cout << "ERROR: Wrong PW" << endl; 
        }
        else if( state == 'N')
        {

            int s = Hash( acnumber, password, 'N' );
            if( s == 1)
                cout << "New: OK" << endl;
            else if( s == 0 )
                cout << "ERROR: Exist" << endl;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值