Repository navigation
Expand file tree
/
Copy pathuser.cpp
More file actions
258 lines (227 loc) · 5.12 KB
/
Copy pathuser.cpp
File metadata and controls
258 lines (227 loc) · 5.12 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include"iostream"
#include"fstream"
#include"string"
#include<conio.h>
#include"vector"
#include"user.h"
#include"QDebug"
#include"errors.h"
using namespace std;
int userCount = 0;
vector<User>vecuser;
User CEO;
//保存
void User::save()
{
ofstream ofile;
ofile.open("user.txt", ios::out);
if(!ofile.is_open()){
string error = "无法打开文件user.txt";
throw fileError(error);
}
for (int i = 0; i < vecuser.size(); i++)
{
ofile <<vecuser[i]._phone << endl;
ofile <<CEO.encrypt(vecuser[i]._phone,vecuser[i]._password)<< endl;//保存加密的密码
ofile <<vecuser[i]._studentID<<endl;
ofile <<vecuser[i]._name<<endl;
}
ofile.close();
}
//读取
void User::read()
{
vecuser.clear();
ifstream ifile;
ifile.open("user.txt", ios::in);
User tempUser;
string PASSword;
userCount = 0;
if (!ifile.is_open())
{
string error = "无法打开user.txt";
throw fileError(error);
}
for (int i = 0; ifile>>tempUser._phone; i++)
{
ifile >> PASSword;
tempUser._password=CEO.decrypt(tempUser._phone,PASSword);//解密
ifile >> tempUser._studentID;
ifile >> tempUser._name;
vecuser.push_back(tempUser);
userCount++;
}
userCount--;
ifile.close();
}
//注册
int User::Registers(string setAccount, string setPassword,string setStudentID,string setName)
{
//检测异常继续向上抛出
try {
CEO.read();
} catch (fileError &e) {
throw e;
}
User tempUser;
for (int i = 0; i <vecuser.size(); i++)
{
if (setAccount == vecuser[i]._phone)
{
qDebug("该手机号已注册");
return 0;
}
}
tempUser._phone = setAccount;
tempUser._password = setPassword;
tempUser._studentID=setStudentID;
tempUser._name=setName;
vecuser.push_back(tempUser);
CEO.save();
qDebug("注册成功");
return 1;
}
//登录
int User::Login(string setAccount, string setPassword)
{
//检测异常继续向上抛出
try {
CEO.read();
} catch (fileError &e) {
throw e;
}
User tempUser;
tempUser._phone = setAccount;
tempUser._password = setPassword;
int flag=1;
for (int i = 0; i < vecuser.size(); i++)
{
if (vecuser[i]._phone == tempUser._phone)
{
flag=0;
if (vecuser[i]._password==tempUser._password)
{
//登陆成功
qDebug("登陆成功");
//获取信息
tempUser._studentID=vecuser[i]._studentID;
tempUser._name=vecuser[i]._name;
CEO=tempUser;
//返回1
return 1;
}
else
{
//登录失败
qDebug("密码错误");
//返回0
return 0;
}
}
}
if(flag)
{
//用户名未注册
qDebug("用户未注册");
//返回-1
return -1;
}
}
//修改
void User::edit(string name,string phone,string studentID,string password)
{
try {
CEO.read();
} catch (fileError &e) {
throw e;
}
//修改CEO里的信息
CEO._name = name;
CEO._phone = phone;
CEO._studentID = studentID;
CEO._password = password;
User tempUser;
tempUser=CEO;
for (int i = 0; i < vecuser.size(); i++)
{
if (vecuser[i]._phone == tempUser._phone)
{
vecuser[i]=tempUser;
CEO.save();
}
}
}
//注销
void User::logout()
{
try {
CEO.read();
} catch (fileError &e) {
throw e;
}
User tempUser;
tempUser=CEO;
vector<User>::iterator it;
for (it=vecuser.begin();it!=vecuser.end();it++)
{
if ( it->_phone == tempUser._phone)
{
//删除该元素
vecuser.erase(it);
CEO.save();
}
}
}
//导出类内信息
QString User::qName()
{
QString name = QString::fromStdString(_name);
return name;
}
QString User::qStudentID()
{
QString studentID = QString::fromStdString(_studentID);
return studentID;
}
QString User::qPhone()
{
QString phone = QString::fromStdString(_phone);
return phone;
}
string User::stdPassword()
{
return _password;
}
///加密
/// 用账号的字符串每个字符ASCII码相加之和作为密钥,确保每个账号密钥不同
///
string User::encrypt(string Account,string Password)
{
// string PASSword;
int key=0;
for(int i=0;i<Account.size();i++)
{
key+=Account[i];
}
srand(key);//初始化种子
for(int i=0;i<Password.size();i++)
{
Password[i]=Password[i]^rand();//按每一位进行与随机数的取反
}
return Password;
}
///解密
string User::decrypt(string Account,string PASSword)
{
int key=0;
for(int i=0;i<Account.size();i++)
{
key+=Account[i];
}
srand(key);//初始化种子
for(int i=0;i<PASSword.size();i++)
{
PASSword[i]=PASSword[i]^rand();//按每一位进行与随机数的取反
}
return PASSword;
}