删除相同字符串

删除相同字符串

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更擅长。

猜你喜欢

转载自blog.csdn.net/fb_941219/article/details/107008867