|
15鱼币
有个问题困扰多天了,读取plt文件,读取方式和TXT文件相同,得出很多信息,如何能够把PU和PD后面的坐标提取出来放到一个TXT文件中
似这种
IN;
VS32,1;
VS32,2;
VS32,3;
VS32,4;
VS32,5;
VS32,6;
VS32,7;
VS32,8;
WU0;
PW0.350,1;
PW0.350,2;
PW0.350,3;
PW0.350,4;
PW0.350,5;
PW0.350,6;
PW0.350,7;
PW0.350,8;
LT;
SP1;
PU0 -1200;
PD1600 -1200;
PD1600 1200;
PD0 1200;
PD0 -1200;
SP0;
#include <iostream>
#include <fstream>
#include <string>
#include <cstdint>
#include <vector>
#include <sstream>
union Data
{
struct { int32_t vs_a; int32_t vs_b; };
struct { double pw_a; int32_t pw_b; };
struct { int32_t pu_a; int32_t pu_b; };
struct { int32_t pd_a; int32_t pd_b; };
int32_t wu;
int32_t sp;
};
struct Info
{
std::string id;
Data data;
};
static int32_t StringToInteger(const std::string &num)
{
std::stringstream ss;
ss << num;
int32_t result = 0;
ss >> result;
return result;
}
static size_t ReadInteger(std::string::const_iterator begin, std::string::const_iterator end, int32_t &dest)
{
std::string::const_iterator i = begin;
while(i != end && (!isdigit(*i) && *i != '-'))
++i;
std::string num;
while(i != end && (isdigit(*i) || *i == '-'))
{
num.push_back(*i);
++i;
}
dest = StringToInteger(num);
return i - begin;
}
static double StringToDouble(const std::string &num)
{
std::stringstream ss;
ss << num;
double result = 0;
ss >> result;
return result;
}
static size_t ReadDouble(std::string::const_iterator begin, std::string::const_iterator end, double &dest)
{
std::string::const_iterator i = begin;
while(i != end && (!isdigit(*i) && *i != '-'))
++i;
std::string num;
while(i != end && (isdigit(*i) || *i == '-' || *i == '.'))
{
num.push_back(*i);
++i;
}
dest = StringToDouble(num);
return i - begin;
}
const std::string GetStatement(std::ifstream &file)
{
std::string result;
while(file.peek() != EOF && isspace(file.peek()))
file.get();
while(file.peek() != EOF && file.peek() != ';')
result.push_back(file.get());
if(file.peek() != EOF)
file.get();
return result;
}
const Info ReadStatement(const std::string &statement)
{
Info result = {"", 0};
std::string::const_iterator iter;
for(iter = statement.begin(); iter != statement.end(); ++iter)
{
if(!isalpha(*iter))
break;
result.id.push_back(*iter);
}
if((result.id == "IN") || (result.id == "LT"))
;
else if(result.id == "WU")
ReadInteger(iter, statement.end(), result.data.wu);
else if(result.id == "SP")
ReadInteger(iter, statement.end(), result.data.sp);
else if(result.id == "VS")
{
iter += ReadInteger(iter, statement.end(), result.data.vs_a);
ReadInteger(iter, statement.end(), result.data.vs_b);
}
else if(result.id == "PW")
{
iter += ReadDouble(iter, statement.end(), result.data.pw_a);
ReadInteger(iter, statement.end(), result.data.pw_b);
}
else if(result.id == "PU")
{
iter += ReadInteger(iter, statement.end(), result.data.pu_a);
ReadInteger(iter, statement.end(), result.data.pu_b);
}
else if(result.id == "PD")
{
iter += ReadInteger(iter, statement.end(), result.data.pd_a);
ReadInteger(iter, statement.end(), result.data.pd_b);
}
return result;
}
void WriteFile(std::ofstream &file, const Info &info)
{
if(info.id == "PU")
file << info.data.pu_a << "," << info.data.pu_b << std::endl;
else if(info.id == "PD")
file << info.data.pd_a << "," << info.data.pd_b << std::endl;
}
void PrintInfo(const Info &info)
{
if((info.id == "IN") || (info.id == "LT"))
std::cout << info.id << ":" << std::endl;
else if(info.id == "WU")
std::cout << info.id << ":" << "(" << info.data.wu << ")" << std::endl;
else if(info.id == "SP")
std::cout << info.id << ":" << "(" << info.data.sp << ")" << std::endl;
else if(info.id == "VS")
std::cout << info.id << ":" << "(" << info.data.vs_a << "," << info.data.vs_b << ")" << std::endl;
else if(info.id == "PW")
std::cout << info.id << ":" << "(" << info.data.pw_a << "," << info.data.pw_b << ")" << std::endl;
else if(info.id == "PU")
std::cout << info.id << ":" << "(" << info.data.pu_a << "," << info.data.pu_b << ")" << std::endl;
else if(info.id == "PD")
std::cout << info.id << ":" << "(" << info.data.pd_a << "," << info.data.pd_b << ")" << std::endl;
}
int main()
{
std::ifstream in("1.txt");
std::vector<Info> vi;
std::string statement;
while(statement = GetStatement(in), in)
vi.push_back(ReadStatement(statement));
in.close();
std::ofstream out("2.txt");
for(const auto &i: vi)
{
PrintInfo(i);
WriteFile(out, i);
}
out.close();
return 0;
}
|
-
|