C语言 文件读写 数据筛选 源代码
#include
#include
#include
#include
using namespace std;
int p_2, p_3;
//Author:snowman QQ:254934905
void find_p(char* str) //找|
{
int i, num;
num = 0;
for(i = 0; str[i]; i ++)
{
if(str[i] == '|')
{
num ++;
if(num == 2) p_2 = i;
if(num == 3) { p_3 = i; break; }
}
}
}
int str_int(string str)//转化为数字
{
int gd = 0, i = 0;
while(str[i] <&#39;0&#39; || str[i] > &#39;9&#39;) i &#43;&#43;;
gd &#61; str[i] - &#39;0&#39;;
for(i &#61; i&#43;1; str[i]>&#61;&#39;0&#39; && str[i] <&#61; &#39;9&#39;; i &#43;&#43;)
gd &#61; gd*10&#43; str[i] - &#39;0&#39;;
return gd;
}
int main()
{
typedef map M;
map::iterator it;
M m;
char f_name[100], line[400], ch[200]&#61;"处理后";
int grade;
string str, str_p, str_s;
FILE *ftp, *ftp1;
f_name[0] &#61; 0;
bool flag &#61; true;
cout <
cout <
cout <
cout <
cout <
while(flag)
{
scanf("%s",f_name);
/
ftp &#61; fopen(f_name, "r"); //打开文件流 当前目录下的答案文本
if(ftp &#61;&#61; NULL) { cout <
else
flag &#61; false;
}
while(fgets(line,400,ftp) !&#61; NULL)
{
str &#61; line;
find_p(line);
str_p &#61; str.substr(0, p_2&#43;1);
if(p_3-p_2-1 <&#61; 0) str_s[0] &#61; 0;
else
str_s &#61; str.substr(p_2&#43;1, p_3-p_2-1);
grade &#61; str_int(str_s);
it &#61; m.find(str_p);
if(it !&#61; m.end()) // 已经存在
{
if(grade > it->second) it->second &#61; grade;
}
else
m.insert(map::value_type(str_p, grade));
}
fclose(ftp); //关闭文件
strcat(ch, f_name);
cout <
cout <
ftp1 &#61; freopen(ch,"w",stdout); //写入文件
for(it&#61;m.begin(); it !&#61; m.end(); it &#43;&#43;)