- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
Latest commit
121 lines (107 loc) · 2.74 KB
/
Copy pathmain.cpp
File metadata and controls
121 lines (107 loc) · 2.74 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
#include<iostream>
#include<string>
#include<fstream>
#include<cassert>
usingnamespacestd;
#defineMAX_NUM1000
#defineMAX_LEN10
intgetSinLoc(char);//获取字母对应数字
intgetDv(int*, int, int);//获取差值间的字母数
intgetIndex(int*);//获取单词索引
intmain()
{
//打开input.txt与output.txt文件
ifstream srcFile("X:\\0931Xu\\Study\\input.txt");
assert(srcFile.is_open()); //若失败,则输出错误消息,并终止程序运行
ofstream destFile("X:\\0931Xu\\Study\\output.txt");
assert(destFile.is_open()); //若失败,则输出错误消息,并终止程序运行
string word;//存储要查询的字符串
int wordNum[MAX_LEN] = { 0 };//可以改变来适应不同长度word
int times=MAX_NUM;//存储查询次数及行数
int mlength = MAX_LEN;//单词长度
int location = 0;//对应数最高位
int tmp = 0;//暂存index
for (int t = 0; t < MAX_NUM&&t <= times; t++) {
getline(srcFile, word);
if (t == 0) {//获取行数
times = atoi(word.c_str());
cout <<"We have "<< word <<" words to be transited."<< endl;
continue;
}
int length = word.length();
int start = mlength - length;
for (int t1 = 0, location = mlength - length; t1 < length; t1++,location++) {//获得word对应的数字序列
wordNum[location] = getSinLoc(word[t1]);
}
//输出检查
cout << word << '\t';
do {//输出数组内对应数字
cout << wordNum[start] << '\t';
} while (start++ +1 < mlength);
tmp = getIndex(wordNum);
cout<<"\nIndex is "<<tmp<<".\n\n";
destFile << tmp << endl;
}
//关闭input.txt与output.txt文件
srcFile.close();
destFile.close();
system ("pause");
return0;
}
//获取字母对应数字
intgetSinLoc(char letter)
{
int number = letter;
if (64 < number&&number < 91) {
return number - 64;
}
elseif (96 < letter&&letter < 123) {
return number - 96;
}
elsereturn0;
};
//得到word索引
intgetIndex(int* nums)
{
int index = 0;
while (nums[MAX_LEN - 1] != 1) {
index += getDv(nums, MAX_LEN - 2, MAX_LEN - 1);
}
if (index == 0) return1;
elsereturn index;
};
//各位归位
voidguiwei(int* nums, int left, int right)
{
if (nums[left] == 0) {
return;
}
elseif (left == 0 || nums[left - 1] == 0 || nums[left - 1] < nums[left] - 1) {
nums[left]--;
for (; right < MAX_LEN; right++) {
nums[right] = 27 - MAX_LEN + right;
}
}
elseif(nums[left-1]==nums[left]-1) {
guiwei(nums, left-1, left);
}
};
//计末位的次数
intgetDv(int* nums, int left, int right)
{
int dv = nums[right] - nums[left];
if (nums[left] == 0 && nums[right] == 0) {
return0;
}
elseif (nums[right] <= nums[left]) {
return -20000;
}
elseif ( right != 0) {
nums[right] = nums[left] + 1;
guiwei(nums, left, right);
return dv;
}
elsereturn -10000;
};
//TODO:getDv后的数据恢复
// :MAX_LEN的微调