wendy890711

字串判斷

May 20th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. // ConsoleApplication1.cpp : 定義主控台應用程式的進入點。
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <fstream>
  7. #include <string>
  8. #include <cctype>
  9. using namespace std;
  10.  
  11.  
  12. int main()
  13. {
  14. fstream file;
  15. file.open("C:\\nmea_sample.txt", ios_base::in);
  16.  
  17. if (!file)
  18. {
  19. cout << "開起失敗" << endl;
  20. }
  21. string temp, checkstr = "$GPGGA";
  22.  
  23. while (!file.eof())
  24. {
  25. bool isGPGGA = 0;
  26. file >> temp;
  27. if (temp.find(checkstr) != string::npos)
  28. {
  29. cout << temp << endl;
  30. isGPGGA = 1;
  31. }
  32. if (isGPGGA)
  33. {
  34. char checkchar = temp[1];
  35. for (int i = 2; temp[i] != '*'; i++)
  36. {
  37. checkchar ^= temp[i];
  38. }
  39.  
  40. int checknum = 0;
  41.  
  42. if (isalpha(temp[temp.find_first_of('*') + 1]) == 1)
  43. checknum += ((temp[temp.find_first_of('*') + 1] - 'A') + 10) * 16;
  44. else
  45. checknum += (temp[temp.find_first_of('*') + 1] - '0') * 16;
  46.  
  47. if (isalpha(temp[temp.find_first_of('*') + 2]) == 1)
  48. checknum += ((temp[temp.find_first_of('*') + 2] - 'A') + 10);
  49. else
  50. checknum += (temp[temp.find_first_of('*') + 2] - '0');
  51.  
  52.  
  53. if (int(checkchar) == checknum)
  54. {
  55. cout << "PASS" << endl;
  56. unsigned int k = temp.find(',');
  57. while (k!= string::npos)
  58. {
  59. cout << "時間" << temp[k + 1] << temp[k + 2] << ":" << temp[k + 3] << temp[k + 4] << ":" << temp[k + 5] << temp[k + 6] << endl;
  60. k = string::npos;
  61. }
  62. }
  63.  
  64. else
  65. cout << "ERROR" << endl;
  66. }
  67.  
  68.  
  69. }
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment