鱼C论坛

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

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

[复制链接]
发表于 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-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
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-1 23:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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