image.png
    有这样个数的数据,要对数据进行一些处理,比如,根据GDP,计算相应的加盟指导价格

    1. // 思路:
    2. // 1.从文件读入数据
    3. // 2.利用C++语言,处理数据,生成想要的数据
    4. // 3.文件写入到文件里

    in.csv

    1. #include <bits/stdc++.h>
    2. using namespace std;
    3. typedef long long ll;
    4. const int N = 1e6 + 10;
    5. string s;
    6. // 核心是这个函数
    7. void Stringsplit(const string& str, const string& split, vector<string>& res)
    8. {
    9. //std::regex ws_re("\\s+"); // 正则表达式,匹配空格
    10. std::regex reg(split); // 匹配split
    11. std::sregex_token_iterator pos(str.begin(), str.end(), reg, -1);
    12. decltype(pos) end; // 自动推导类型
    13. for (; pos != end; ++pos)
    14. {
    15. res.push_back(pos->str());
    16. }
    17. }
    18. ll check(string s){
    19. ll t = 0;
    20. for (int i = 0, len = s.size(); i < len; i++){
    21. t = t * 10 + (s[i] - '0');
    22. }
    23. return t;
    24. }
    25. int caldandian(ll x){
    26. if (x > 1000) return 15;
    27. if (x > 900) return 14;
    28. if (x > 800) return 13;
    29. if (x > 700) return 12;
    30. if (x > 600) return 11;
    31. if (x > 500) return 10;
    32. if (x > 400) return 9;
    33. if (x > 300) return 8;
    34. if (x > 200) return 7;
    35. if (x > 100) return 6;
    36. if (x > 0) return 5;
    37. return -1;
    38. }
    39. double calbaozheng(ll x){
    40. if (x < 0) return -1;
    41. if (x < 100) return 0.5;
    42. return 1.0;
    43. }
    44. double calguanli(ll x){
    45. if (x < 0) return -1;
    46. if (x < 100) return 1.5;
    47. return 2.0;
    48. }
    49. int main(){
    50. freopen("in.csv", "r", stdin);
    51. freopen("out.csv", "w", stdout);
    52. while (getline(cin, s)){
    53. vector<string> S;
    54. Stringsplit(s, ",", S); // 整行读入的字符串,拆分完存入vector里
    55. cout << S[0] << ',';
    56. cout << S[1] << ',';
    57. cout << S[2] << ',';
    58. cout << check(S[3]) << ','; // 计算人口,把字符串转成数字
    59. ll GDP = check(S[4]);
    60. if (GDP < 0) GDP = -1;
    61. cout << GDP << ','; // GDP
    62. ll dandian = caldandian(GDP);
    63. cout << dandian << ','; // 计算单店加盟费用
    64. double quyu = dandian * 1.5;
    65. if (quyu < 0) printf("-1,");
    66. else printf("%.1lf,", quyu);
    67. double baozheng = calbaozheng(GDP);
    68. if (baozheng < 0) printf("-1,");
    69. else if (baozheng < 1) printf("0.5,");
    70. else printf("1,");
    71. double guanli = calguanli(GDP);
    72. if (guanli < 0) printf("-1\n");
    73. else if (guanli < 2) printf("1.5\n");
    74. else printf("2\n");
    75. }
    76. // for (auto x : S[0]) cout << x << ' ';
    77. // puts("");
    78. return 0;
    79. }
    1. // 正则表达式(regular expression)描述了一种字符串匹配的模式(pattern),可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。
    2. // 而在C++的正则中,把这种操作称为Tokenize分词(或者叫切割)。这种操作刚好可以满足我们的需求,用模板类regex_token_iterator<>提供分词迭代器,可以完成字符串的分割。

    输出文件示例:
    out.csv