鱼C论坛

 找回密码
 立即注册
查看: 1621|回复: 7

[已解决]如何把含有字母数字和符号的文件,提取出数字坐标,并保存到TXT文件中

[复制链接]
发表于 2019-1-17 22:17:23 | 显示全部楼层 |阅读模式
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;
最佳答案
2019-1-17 22:17:24
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstdint>
  5. #include <vector>
  6. #include <sstream>

  7. union Data
  8. {
  9.         struct { int32_t vs_a; int32_t vs_b; };
  10.         struct { double pw_a; int32_t pw_b; };
  11.         struct { int32_t pu_a; int32_t pu_b; };
  12.         struct { int32_t pd_a; int32_t pd_b; };
  13.         int32_t wu;
  14.         int32_t sp;
  15. };

  16. struct Info
  17. {
  18.         std::string id;
  19.         Data data;
  20. };


  21. static int32_t StringToInteger(const std::string &num)
  22. {
  23.         std::stringstream ss;
  24.         ss << num;
  25.         int32_t result = 0;
  26.         ss >> result;
  27.         return result;
  28. }

  29. static size_t ReadInteger(std::string::const_iterator begin, std::string::const_iterator end, int32_t &dest)
  30. {
  31.         std::string::const_iterator i = begin;
  32.         while(i != end && (!isdigit(*i) && *i != '-'))
  33.                 ++i;

  34.         std::string num;
  35.         while(i != end && (isdigit(*i) || *i == '-'))
  36.         {
  37.                 num.push_back(*i);
  38.                 ++i;
  39.         }

  40.         dest = StringToInteger(num);
  41.         return i - begin;
  42. }

  43. static double StringToDouble(const std::string &num)
  44. {
  45.         std::stringstream ss;
  46.         ss << num;
  47.         double result = 0;
  48.         ss >> result;
  49.         return result;
  50. }

  51. static size_t ReadDouble(std::string::const_iterator begin, std::string::const_iterator end, double &dest)
  52. {
  53.         std::string::const_iterator i = begin;
  54.         while(i != end && (!isdigit(*i) && *i != '-'))
  55.                 ++i;

  56.         std::string num;
  57.         while(i != end && (isdigit(*i) || *i == '-' || *i == '.'))
  58.         {
  59.                 num.push_back(*i);
  60.                 ++i;
  61.         }

  62.         dest = StringToDouble(num);
  63.         return i - begin;
  64. }

  65. const std::string GetStatement(std::ifstream &file)
  66. {
  67.         std::string result;

  68.         while(file.peek() != EOF && isspace(file.peek()))
  69.                 file.get();
  70.         while(file.peek() != EOF && file.peek() != ';')
  71.                 result.push_back(file.get());
  72.         if(file.peek() != EOF)
  73.                 file.get();

  74.         return result;
  75. }

  76. const Info ReadStatement(const std::string &statement)
  77. {
  78.         Info result = {"", 0};
  79.         std::string::const_iterator iter;
  80.         for(iter = statement.begin(); iter != statement.end(); ++iter)
  81.         {
  82.                 if(!isalpha(*iter))
  83.                         break;
  84.                 result.id.push_back(*iter);
  85.         }

  86.         if((result.id == "IN") || (result.id == "LT"))
  87.                 ;
  88.         else if(result.id == "WU")
  89.                 ReadInteger(iter, statement.end(), result.data.wu);
  90.         else if(result.id == "SP")
  91.                 ReadInteger(iter, statement.end(), result.data.sp);
  92.         else if(result.id == "VS")
  93.         {
  94.                 iter += ReadInteger(iter, statement.end(), result.data.vs_a);
  95.                 ReadInteger(iter, statement.end(), result.data.vs_b);
  96.         }
  97.         else if(result.id == "PW")
  98.         {
  99.                 iter += ReadDouble(iter, statement.end(), result.data.pw_a);
  100.                 ReadInteger(iter, statement.end(), result.data.pw_b);
  101.         }
  102.         else if(result.id == "PU")
  103.         {
  104.                 iter += ReadInteger(iter, statement.end(), result.data.pu_a);
  105.                 ReadInteger(iter, statement.end(), result.data.pu_b);
  106.         }
  107.         else if(result.id == "PD")
  108.         {
  109.                 iter += ReadInteger(iter, statement.end(), result.data.pd_a);
  110.                 ReadInteger(iter, statement.end(), result.data.pd_b);
  111.         }

  112.         return result;
  113. }

  114. void WriteFile(std::ofstream &file, const Info &info)
  115. {
  116.         if(info.id == "PU")
  117.                 file << info.data.pu_a << "," << info.data.pu_b << std::endl;
  118.         else if(info.id == "PD")
  119.                 file << info.data.pd_a << "," << info.data.pd_b << std::endl;
  120. }

  121. void PrintInfo(const Info &info)
  122. {
  123.         if((info.id == "IN") || (info.id == "LT"))
  124.                 std::cout << info.id << ":" << std::endl;
  125.         else if(info.id == "WU")
  126.                 std::cout << info.id << ":" << "(" << info.data.wu << ")" << std::endl;
  127.         else if(info.id == "SP")
  128.                 std::cout << info.id << ":" << "(" << info.data.sp << ")" << std::endl;
  129.         else if(info.id == "VS")
  130.                 std::cout << info.id << ":" << "(" << info.data.vs_a << "," << info.data.vs_b << ")" << std::endl;
  131.         else if(info.id == "PW")
  132.                 std::cout << info.id << ":" << "(" << info.data.pw_a << "," << info.data.pw_b << ")" << std::endl;
  133.         else if(info.id == "PU")
  134.                 std::cout << info.id << ":" << "(" << info.data.pu_a << "," << info.data.pu_b << ")" << std::endl;
  135.         else if(info.id == "PD")
  136.                 std::cout << info.id << ":" << "(" << info.data.pd_a << "," << info.data.pd_b << ")" << std::endl;
  137. }

  138. int main()
  139. {
  140.         std::ifstream in("1.txt");
  141.         std::vector<Info> vi;
  142.         std::string statement;
  143.         while(statement = GetStatement(in), in)
  144.                 vi.push_back(ReadStatement(statement));
  145.         in.close();

  146.         std::ofstream out("2.txt");
  147.         for(const auto &i: vi)
  148.         {
  149.                 PrintInfo(i);
  150.                 WriteFile(out, i);
  151.         }
  152.         out.close();
  153.         return 0;
  154. }
复制代码


1.png
%%ESL(@REI1@6YGQG9H6MV3.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-1-17 22:17:24 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstdint>
  5. #include <vector>
  6. #include <sstream>

  7. union Data
  8. {
  9.         struct { int32_t vs_a; int32_t vs_b; };
  10.         struct { double pw_a; int32_t pw_b; };
  11.         struct { int32_t pu_a; int32_t pu_b; };
  12.         struct { int32_t pd_a; int32_t pd_b; };
  13.         int32_t wu;
  14.         int32_t sp;
  15. };

  16. struct Info
  17. {
  18.         std::string id;
  19.         Data data;
  20. };


  21. static int32_t StringToInteger(const std::string &num)
  22. {
  23.         std::stringstream ss;
  24.         ss << num;
  25.         int32_t result = 0;
  26.         ss >> result;
  27.         return result;
  28. }

  29. static size_t ReadInteger(std::string::const_iterator begin, std::string::const_iterator end, int32_t &dest)
  30. {
  31.         std::string::const_iterator i = begin;
  32.         while(i != end && (!isdigit(*i) && *i != '-'))
  33.                 ++i;

  34.         std::string num;
  35.         while(i != end && (isdigit(*i) || *i == '-'))
  36.         {
  37.                 num.push_back(*i);
  38.                 ++i;
  39.         }

  40.         dest = StringToInteger(num);
  41.         return i - begin;
  42. }

  43. static double StringToDouble(const std::string &num)
  44. {
  45.         std::stringstream ss;
  46.         ss << num;
  47.         double result = 0;
  48.         ss >> result;
  49.         return result;
  50. }

  51. static size_t ReadDouble(std::string::const_iterator begin, std::string::const_iterator end, double &dest)
  52. {
  53.         std::string::const_iterator i = begin;
  54.         while(i != end && (!isdigit(*i) && *i != '-'))
  55.                 ++i;

  56.         std::string num;
  57.         while(i != end && (isdigit(*i) || *i == '-' || *i == '.'))
  58.         {
  59.                 num.push_back(*i);
  60.                 ++i;
  61.         }

  62.         dest = StringToDouble(num);
  63.         return i - begin;
  64. }

  65. const std::string GetStatement(std::ifstream &file)
  66. {
  67.         std::string result;

  68.         while(file.peek() != EOF && isspace(file.peek()))
  69.                 file.get();
  70.         while(file.peek() != EOF && file.peek() != ';')
  71.                 result.push_back(file.get());
  72.         if(file.peek() != EOF)
  73.                 file.get();

  74.         return result;
  75. }

  76. const Info ReadStatement(const std::string &statement)
  77. {
  78.         Info result = {"", 0};
  79.         std::string::const_iterator iter;
  80.         for(iter = statement.begin(); iter != statement.end(); ++iter)
  81.         {
  82.                 if(!isalpha(*iter))
  83.                         break;
  84.                 result.id.push_back(*iter);
  85.         }

  86.         if((result.id == "IN") || (result.id == "LT"))
  87.                 ;
  88.         else if(result.id == "WU")
  89.                 ReadInteger(iter, statement.end(), result.data.wu);
  90.         else if(result.id == "SP")
  91.                 ReadInteger(iter, statement.end(), result.data.sp);
  92.         else if(result.id == "VS")
  93.         {
  94.                 iter += ReadInteger(iter, statement.end(), result.data.vs_a);
  95.                 ReadInteger(iter, statement.end(), result.data.vs_b);
  96.         }
  97.         else if(result.id == "PW")
  98.         {
  99.                 iter += ReadDouble(iter, statement.end(), result.data.pw_a);
  100.                 ReadInteger(iter, statement.end(), result.data.pw_b);
  101.         }
  102.         else if(result.id == "PU")
  103.         {
  104.                 iter += ReadInteger(iter, statement.end(), result.data.pu_a);
  105.                 ReadInteger(iter, statement.end(), result.data.pu_b);
  106.         }
  107.         else if(result.id == "PD")
  108.         {
  109.                 iter += ReadInteger(iter, statement.end(), result.data.pd_a);
  110.                 ReadInteger(iter, statement.end(), result.data.pd_b);
  111.         }

  112.         return result;
  113. }

  114. void WriteFile(std::ofstream &file, const Info &info)
  115. {
  116.         if(info.id == "PU")
  117.                 file << info.data.pu_a << "," << info.data.pu_b << std::endl;
  118.         else if(info.id == "PD")
  119.                 file << info.data.pd_a << "," << info.data.pd_b << std::endl;
  120. }

  121. void PrintInfo(const Info &info)
  122. {
  123.         if((info.id == "IN") || (info.id == "LT"))
  124.                 std::cout << info.id << ":" << std::endl;
  125.         else if(info.id == "WU")
  126.                 std::cout << info.id << ":" << "(" << info.data.wu << ")" << std::endl;
  127.         else if(info.id == "SP")
  128.                 std::cout << info.id << ":" << "(" << info.data.sp << ")" << std::endl;
  129.         else if(info.id == "VS")
  130.                 std::cout << info.id << ":" << "(" << info.data.vs_a << "," << info.data.vs_b << ")" << std::endl;
  131.         else if(info.id == "PW")
  132.                 std::cout << info.id << ":" << "(" << info.data.pw_a << "," << info.data.pw_b << ")" << std::endl;
  133.         else if(info.id == "PU")
  134.                 std::cout << info.id << ":" << "(" << info.data.pu_a << "," << info.data.pu_b << ")" << std::endl;
  135.         else if(info.id == "PD")
  136.                 std::cout << info.id << ":" << "(" << info.data.pd_a << "," << info.data.pd_b << ")" << std::endl;
  137. }

  138. int main()
  139. {
  140.         std::ifstream in("1.txt");
  141.         std::vector<Info> vi;
  142.         std::string statement;
  143.         while(statement = GetStatement(in), in)
  144.                 vi.push_back(ReadStatement(statement));
  145.         in.close();

  146.         std::ofstream out("2.txt");
  147.         for(const auto &i: vi)
  148.         {
  149.                 PrintInfo(i);
  150.                 WriteFile(out, i);
  151.         }
  152.         out.close();
  153.         return 0;
  154. }
复制代码


1.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-1-17 22:33:26 | 显示全部楼层
得到的txt要什么样?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-1-17 22:39:27 | 显示全部楼层
ba21 发表于 2019-1-17 22:33
得到的txt要什么样?

类似这种,只去PU和PD之后的坐标的值
0 -1200
1600 -1200
1600 1200
0 1200
0 -1200
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-1-18 11:00:00 | 显示全部楼层

谢谢,功能实现了,就是C++我只了解一点,所以看起来还是比较吃力的,我用c语言写的提取出来了,这些信息,但是如何把坐标值提取出来,去除前面的PD和PU和后面的分号,就有些迷惑了
PD-1109 1207;
PD-1633 1207;
PD-1621 1249;
PD-1595 1282;
PD-1557 1315;
PD-1430 1389;
PD-1322 1448;
PD-1256 1493;
PD-1221 1531;
PD-1210 1566;
PD-1220 1599;
PD-1251 1627;
PD-1299 1645;
PD-1359 1651;
PD-1423 1645;
PD-1472 1625;
PD-1504 1594;
PD-1515 1553;
PD-1614 1559;
PD-1590 1621;
PD-1537 1665;
PD-1459 1693;
PD-1357 1702;
PD-1255 1692;
PD-1177 1662;
PD-1127 1618;
PD-1115 1593;
PD-1111 1565;
PD-1115 1536;
PD-1130 1507;
PD-1194 1446;
PD-1344 1360;
PD-1457 1298;
PD-1498 1265;
PD-1109 1265;
PU-1004 1450;
PD-997 1529;
PD-976 1591;
PD-940 1638;
PD-890 1673;
PD-826 1695;
PD-747 1702;
PD-688 1698;
PD-635 1686;
PD-592 1666;
PD-556 1640;
PD-529 1606;
PD-508 1567;
PD-494 1516;
PD-490 1450;
PD-497 1372;
PD-518 1310;
PD-554 1263;
PD-604 1228;
PD-668 1206;
PD-747 1199;
PD-849 1212;
PD-926 1250;
PD-961 1285;
PD-985 1331;
PD-1000 1386;
PD-1004 1450;
PU-905 1450;
PD-894 1349;
PD-880 1314;
PD-860 1289;
PD-809 1258;
PD-747 1249;
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-1-18 14:39:26 | 显示全部楼层
C++  666
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-1-18 15:54:14 | 显示全部楼层
lwd2wl 发表于 2019-1-18 11:00
谢谢,功能实现了,就是C++我只了解一点,所以看起来还是比较吃力的,我用c语言写的提取出来了,这些信息 ...
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <stdint.h>

  5. typedef struct
  6. {
  7.         int32_t x, y;
  8. } POINT;

  9. static int32_t StringToInteger(const char *num)
  10. {
  11.         int32_t result = 0;
  12.         sscanf(num, "%d", &result);
  13.         return result;
  14. }

  15. static size_t ReadInteger(const char *statement, int32_t *dest)
  16. {
  17.         const char *begin = statement;
  18.         for(; *statement; ++statement)
  19.         {
  20.                 if(!isdigit(*statement))
  21.                         continue;
  22.                 break;
  23.         }

  24.         char num[512];
  25.         int32_t index = 0;
  26.         while(1)
  27.         {
  28.                 if(isdigit(*statement) || *statement == '-')
  29.                 {
  30.                         num[index++] = *statement;
  31.                         ++statement;
  32.                         continue;
  33.                 }
  34.                 num[index++] = '\0';
  35.                 break;
  36.         }

  37.         *dest = StringToInteger(num);
  38.         return statement - begin;
  39. }

  40. char GetStatement(FILE *file, char *buf)
  41. {
  42.         char ch;
  43.         while(1)
  44.         {
  45.                 ch = fgetc(file);
  46.                 if(ch != EOF && isspace(ch))
  47.                         continue;
  48.                 if(ch != EOF)
  49.                         ungetc(ch, file);
  50.                 break;
  51.         }

  52.         while(1)
  53.         {
  54.                 ch = fgetc(file);
  55.                 if(ch != EOF && ch != ';')
  56.                 {
  57.                         *buf++ = ch;
  58.                         continue;
  59.                 }
  60.                 *buf++ = '\0';
  61.                 break;
  62.         }

  63.         return ch;
  64. }

  65. const POINT ReadStatement(const char *statement)
  66. {
  67.         for(; *statement; ++statement)
  68.         {
  69.                 if(!isdigit(*statement))
  70.                         continue;
  71.                 break;
  72.         }

  73.         POINT result = {0, 0};
  74.         statement += ReadInteger(statement, &result.x);
  75.         ReadInteger(statement, &result.y);
  76.         return result;
  77. }

  78. int main(void)
  79. {
  80.         char buf[512];
  81.         FILE *file = fopen("1.txt", "r");
  82.         while(GetStatement(file, buf) != EOF)
  83.         {
  84.                 POINT point = ReadStatement(buf);
  85.                 printf("(%d, %d)\n", point.x, point.y);
  86.         }
  87.         fclose(file);

  88.         return 0;
  89. }
复制代码


1.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-1-18 16:06:55 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-17 11:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表