Guest User

Untitled

a guest
Jun 8th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. // Implement a CSVReader class that will read a comma-separated-values (CSV)
  7. // file from disk, parse it, and print out the parsed elements with some other
  8. // separator.
  9.  
  10. // * The input delimiter is the comma, ","
  11. // * If that delimiter is contained within an element, then that element must be
  12. // quoted
  13. // * If quotes are contained in an element, then that element must be quoted and
  14. // use double inner quotes (escape character)
  15.  
  16. // input
  17.  
  18. // John,Smith,john.smith@gmail.com,Los Angeles,1
  19. // Jane,Roberts,janer@msn.com,"San Francisco, CA",0
  20. // "Alexandra ""Alex""",Menendez,alex.menendez@gmail.com,Miami,1
  21. // one,two,,four,"five"
  22.  
  23. // output
  24.  
  25. // John|Smith|john.smith@gmail.com|Los Angeles|1
  26. // Jane|Roberts|janer@msn.com|San Francisco, CA|0
  27. // Alexandra "Alex"|Menendez|alex.menendez@gmail.com|Miami|1
  28. // one|two||four|five
  29.  
  30. // To execute C++, please define "int main()"
  31.  
  32. string parseCSV(string str) {
  33. int len = str.size();
  34. bool isInMark = false;
  35. vector<string> res;
  36. string node;
  37. for (int i = 0; i < len; i++) {
  38. //不在引号内
  39. if (!isInMark) {
  40. //遇到引号则进入引号(只要这项元素中含有逗号或者引号则其一定会使用引号包括)
  41. //因此在引号外遇到引号必定标志的新的元素的开始
  42. if (str[i] == '"') {
  43. isInMark = true;
  44. continue;
  45. }
  46. //遇到逗号则完成此列的计算加入到结果数组
  47. if (str[i] == ',') {
  48. res.push_back(node);
  49. node = "";
  50. continue;
  51. }
  52. //常规字符
  53. node += str[i];
  54. continue;
  55. }
  56.  
  57. //在引号内
  58. if (isInMark) {
  59. //连续遇到两个引号则转义(i+1<len保证下一个字符不会溢出)
  60. if (str[i] == '"' && i + 1 < len && str[i + 1] == '"') {
  61. node += '"';
  62. i++;
  63. continue;
  64. }
  65. //再次遇到引号则直接完成该元素
  66. //因为上面已经判断了连续两个引号的情况,因此这里遇到的必定是单独的引号(表示元素的结束)
  67. //完成此计算后若后面有新元素的则会遇到逗号跳转到上面isInMark==false中进入逗号的判断把此次计算push到结果中
  68. //若已经到字符串末尾则会在循环外push这一列元素
  69. if (str[i] == '"') {
  70. isInMark = false;
  71. continue;
  72. }
  73. node += str[i];
  74. continue;
  75. }
  76. }
  77.  
  78. //加入最后计算的一列
  79. if (!node.empty()) res.push_back(node);
  80.  
  81. //构建输出字符串
  82. string resString;
  83. for (int i = 0; i < res.size(); i++) {
  84. if (i == 0)
  85. resString += res[i];
  86. else
  87. resString += "|" + res[i];
  88. }
  89.  
  90. return resString;
  91. }
  92.  
  93. int main() {
  94. string str =
  95. "John,Smith,,john.smith@gmail.com,Los Angeles,1,\"San Francisco, "
  96. "CA\",\"Alexandra \"\"Alex\"\"\"";
  97. string result = parseCSV(str);
  98. cout << result << endl;
  99. return 0;
  100. }
Add Comment
Please, Sign In to add comment