0.引言
一個同學的需求,需要將Excel表格二次排序,當我使用一組數據排序時,另一組數據必然會亂掉,然後需要對另一組數據排序,但是Excel的排序時按照字符串大小來的,而他需要原本的順序。所以只能採用自定義排序。如圖所示:
於是我就講數據列拷貝出來,然後使用代碼刪除重複的字符串然後進行排序。
1.代碼
#include <iostream>
#include <fstream>
#include <cassert>
#include <string>
int IsExist(string tmp, string *output)
{
while (*output !="PASS")
{
if (*output == tmp)
{
return 0;
}
output++;
}
*output = tmp;
}
void test()
{
string Input[1145];
string file = "test1.txt";
ifstream infile;
infile.open(file.data());
assert(infile.is_open());
string s;
int i = 0;
while (getline(infile, s))
{
cout << s << endl;
Input[i] = s;
i++;
}
infile.close();
string Output[400];
for (int i = 0;i<400;i++)//必須將其初始化
{
Output[i] = "PASS";
}
Output[0] = Input[0];
for (int i=0; i<1145;i++)
{
IsExist(Input[i], Output);
}
ofstream outfile;
outfile.open("tets2.txt", ios::app);
int j = 0;
while (Output[j]!="PASS")
{
outfile << Output[j] << "\n";
j++;
}
outfile.close();
}
int main(int argc, char **argv)
{
test();
system("pause");
return 0;
}
實現效果:
然後將結果拷貝進去進行排序:
另外,在這個過程中我發現,這個排序的個數最多隻能輸入254個,他這個表格顯然超出了254個排序了,於是還得分兩個表格進行然後再合併,Excel的Bug?另外,這種小腳本還是Python更擅長。