convert.cpp
.
#include
#include
typedef long long LL;
char table[] = "fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF";
int tr[1005];
int pos[] = {11, 10, 3, 8, 4, 6};
#define XORNUM 177451812
#define ADDNUM 8728348608LL
#define MODE 0//0:file_call_mode, 1:input_mode
LL decode(char* BVnumber) {
LL r = 0, Pow58 = 1;
for(int i = 0; i < 6; ++i) {
r += tr[(int)BVnumber[pos[i]]];
Pow58 *= 58;
}
return (r - ADDNUM) ^ XORNUM;
}
char BVtemp[15] = "BV1 4 1 7 ";
char* encode(LL avnumber) {
avnumber = (avnumber ^ XORNUM) + ADDNUM;
static char BVnumber[15] = "BV1 4 1 7 ";
LL Pow58 = 1;
for(int i = 0; i < 6; ++i) {
BVnumber[pos[i]] = table[avnumber / Pow58 % 58];
Pow58 *= 58;
}
return BVnumber;
}
bool checkBV(char* BVnumber) {
for(int i = 0; i < 12; i++)
if(BVtemp[i] != 32 && BVtemp[i] != BVnumber[i])
return false;
return true;
}
#if MODE == 0
int main(int argc, char** argv) {
if(argc == 1) {
puts("EXAMPLE1: \"convert av170001\" (without quotes)");
puts("EXAMPLE2: \"convert BV17x411w7KC\" (without quotes)");
return 0;
}
for(int id = 1; id < argc; id++) {
#else
int main() {
#endif
for(int i = 0; i < 58; ++i) tr[(int)table[i]] = i;
LL avnumber;
#if MODE == 0
char *number = argv[id];
#else
char number[15];
while(scanf("%s", number) != EOF) {
#endif
if(number[0] == 'a' && number[1] == 'v') {
sscanf(number, "av%lld", &avnumber);
printf("%s is converted to %s\n", number, encode(avnumber));
} else if(number[0] == 'B' && number[1] == 'V') {
if(checkBV(number)) printf("%s is converted to av%lld\n", number, decode(number));
else puts("Please check the BV number you entered.");
}
}
return 0;
}
上述代码中包含了两种形式的程序。为防止有些人(我自己)看不懂,下面拆分了一下。
#include
#include
typedef long long LL;
char table[] = "fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF";
int tr[1005];
int pos[] = {11, 10, 3, 8, 4, 6};
#define XORNUM 177451812
#define ADDNUM 8728348608LL
LL decode(char* BVnumber) {
LL r = 0, Pow58 = 1;
for(int i = 0; i < 6; ++i) {
r += tr[(int)BVnumber[pos[i]]];
Pow58 *= 58;
}
return (r - ADDNUM) ^ XORNUM;
}
char BVtemp[15] = "BV1 4 1 7 ";
char* encode(LL avnumber) {
avnumber = (avnumber ^ XORNUM) + ADDNUM;
static char BVnumber[15] = "BV1 4 1 7 ";
LL Pow58 = 1;
for(int i = 0; i < 6; ++i) {
BVnumber[pos[i]] = table[avnumber / Pow58 % 58];
Pow58 *= 58;
}
return BVnumber;
}
bool checkBV(char* BVnumber) {
for(int i = 0; i < 12; i++)
if(BVtemp[i] != 32 && BVtemp[i] != BVnumber[i])
return false;
return true;
}
int main(int argc, char** argv) {
if(argc == 1) {
puts("EXAMPLE1: \"convert av170001\" (without quotes)");
puts("EXAMPLE2: \"convert BV17x411w7KC\" (without quotes)");
return 0;
}
for(int id = 1; id < argc; id++) {
for(int i = 0; i < 58; ++i) tr[(int)table[i]] = i;
LL avnumber;
char *number = argv[id];
if(number[0] == 'a' && number[1] == 'v') {
sscanf(number, "av%lld", &avnumber);
printf("%s is converted to %s\n", number, encode(avnumber));
} else if(number[0] == 'B' && number[1] == 'V') {
if(checkBV(number)) printf("%s is converted to av%lld\n", number, decode(number));
else puts("Please check the BV number you entered.");
}
}
return 0;
}
#include
#include
typedef long long LL;
char table[] = "fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF";
int tr[1005];
int pos[] = {11, 10, 3, 8, 4, 6};
#define XORNUM 177451812
#define ADDNUM 8728348608LL
LL decode(char* BVnumber) {
LL r = 0, Pow58 = 1;
for(int i = 0; i < 6; ++i) {
r += tr[(int)BVnumber[pos[i]]];
Pow58 *= 58;
}
return (r - ADDNUM) ^ XORNUM;
}
char BVtemp[15] = "BV1 4 1 7 ";
char* encode(LL avnumber) {
avnumber = (avnumber ^ XORNUM) + ADDNUM;
static char BVnumber[15] = "BV1 4 1 7 ";
LL Pow58 = 1;
for(int i = 0; i < 6; ++i) {
BVnumber[pos[i]] = table[avnumber / Pow58 % 58];
Pow58 *= 58;
}
return BVnumber;
}
bool checkBV(char* BVnumber) {
for(int i = 0; i < 12; i++)
if(BVtemp[i] != 32 && BVtemp[i] != BVnumber[i])
return false;
return true;
}
int main() {
for(int i = 0; i < 58; ++i) tr[(int)table[i]] = i;
LL avnumber;
char number[15];
while(scanf("%s", number) != EOF) {
if(number[0] == 'a' && number[1] == 'v') {
sscanf(number, "av%lld", &avnumber);
printf("%s is converted to %s\n", number, encode(avnumber));
} else if(number[0] == 'B' && number[1] == 'V') {
if(checkBV(number)) printf("%s is converted to av%lld\n", number, decode(number));
else puts("Please check the BV number you entered.");
}
}
return 0;
}