7-15 QQ帐户的申请与登陆 (25分)
实现QQ新帐户申请和老帐户登陆的简化版功能。最大挑战是:据说现在的QQ号码已经有10位数了。
输入格式:
输入首先给出一个正整数N(≤105),随后给出N行指令。每行指令的格式为:“命令符(空格)QQ号码(空格)密码”。其中命令符为“N”(代表New)时表示要新申请一个QQ号,后面是新帐户的号码和密码;命令符为“L”(代表Login)时表示是老帐户登陆,后面是登陆信息。QQ号码为一个不超过10位、但大于1000(据说QQ老总的号码是1001)的整数。密码为不小于6位、不超过16位、且不包含空格的字符串。
输出格式:
针对每条指令,给出相应的信息:
1)若新申请帐户成功,则输出“New: OK”;
2)若新申请的号码已经存在,则输出“ERROR: Exist”;
3)若老帐户登陆成功,则输出“Login: OK”;
4)若老帐户QQ号码不存在,则输出“ERROR: Not Exist”;
5)若老帐户密码错误,则输出“ERROR: Wrong PW”。
思路:将qq账号和密码建立一对一的对应关系,c++可直接利用map,c语言相对复杂一点,就要建立散列函数来一步步创建散列表,具体代码如下:
#include<iostream>
#include<string>
#include<map>
using namespace std;
map<string,string> qq;
int main()
{
int n;
char demand;
string number,password;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>demand>>number>>password;
if(demand=='L')
{
if(qq.find(number)==qq.end())//不存在此qq号
{
cout<<"ERROR: Not Exist"<<endl;
}
else
{
if(qq[number]!=password)//密码不对
{
cout<<"ERROR: Wrong PW"<<endl;
}
else
{
cout<<"Login: OK"<<endl;
}
}
}
else if(demand=='N')
{
if(qq.find(number)!=qq.end())//已经存在此qq
{
cout<<"ERROR: Exist"<<endl;
}
else
{
qq[number]=password;
cout<<"New: OK"<<endl;
}
}
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<math.h>
#include<string.h>
#define KEYLENGTH 16
#define MAXTABLESIZE 10000
typedef char ElementType[KEYLENGTH + 1];
typedef struct LNode* PtrToLNode;
typedef PtrToLNode List;
struct LNode
{
ElementType QNumber;
ElementType QPassword;
PtrToLNode Next;
};
typedef struct HblNode* HashTable;
struct HblNode
{
int TableSize;
List Heads;
};
int NextPrime(int num)
{
int p = (num % 2) ? (num + 2) : (num + 1);
int i = 0;
while (p<MAXTABLESIZE)
{
for (i = (int)sqrt(p); i > 2; i--)
if (p % i == 0)break;
if (i == 2)break;
else
p += 2;
}
return p;
}
int Hash(ElementType QNumber, int TabelSize)
{
return atoi(QNumber + 3) % TabelSize;
}
HashTable BuildHashTable(int TabelSize)
{
HashTable H = (HashTable)malloc(sizeof(struct HblNode));
H->TableSize = NextPrime(TabelSize);
H->Heads = (List)malloc(H->TableSize * sizeof(struct LNode));
for (int i = 0; i < H->TableSize; i++)
{
H->Heads[i].QNumber[0] = '\0';
H->Heads[i].QPassword[0] = '\0';
H->Heads[i].Next = NULL;
}
return H;
}
PtrToLNode Find(ElementType Qnumber, HashTable H)
{
int i = Hash(Qnumber,H->TableSize);
PtrToLNode P = H->Heads[i].Next;
while (P && strcmp(P->QNumber, Qnumber))
P = P->Next;
return P;
}
void Insert(ElementType QNumber,ElementType QPassword,HashTable H)
{
PtrToLNode P, NewCell;
P = Find(QNumber, H);
if (!P)
{
int i = Hash(QNumber, H->TableSize);
NewCell = (PtrToLNode)malloc(sizeof(struct LNode));
strcpy(NewCell->QNumber, QNumber);
strcpy(NewCell->QPassword, QPassword);
NewCell->Next = H->Heads[i].Next;
H->Heads[i].Next = NewCell;
}
}
void Order_L(HashTable H)
{
ElementType QNumber;
ElementType QPassword;
scanf("%s %s", QNumber, QPassword);
PtrToLNode P = Find(QNumber, H);
if (P)
{
if (!strcmp(P->QPassword, QPassword))
printf("Login: OK\n");
else
printf("ERROR: Wrong PW\n");
}
else
printf("ERROR: Not Exist\n");
}
void Order_N(HashTable H)
{
ElementType QNumber;
ElementType QPassword;
scanf("%s %s", QNumber, QPassword);
PtrToLNode P = Find(QNumber, H);
if (P)
printf("ERROR: Exist\n");
else
{
Insert(QNumber, QPassword, H);
printf("New: OK\n");
}
}
void CreateHashTable()
{
int N;
scanf("%d\n", &N);
HashTable H = BuildHashTable(N);
while (N--)
{
char Order[2];
scanf("%s", Order);
switch (Order[0])
{
case 'L':Order_L(H); break;
case 'N':Order_N(H); break;
}
}
}
int main()
{
CreateHashTable();
return 0;
}