有这样个数的数据,要对数据进行一些处理,比如,根据GDP,计算相应的加盟指导价格
// 思路:
// 1.从文件读入数据
// 2.利用C++语言,处理数据,生成想要的数据
// 3.文件写入到文件里
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
string s;
// 核心是这个函数
void Stringsplit(const string& str, const string& split, vector<string>& res)
{
//std::regex ws_re("\\s+"); // 正则表达式,匹配空格
std::regex reg(split); // 匹配split
std::sregex_token_iterator pos(str.begin(), str.end(), reg, -1);
decltype(pos) end; // 自动推导类型
for (; pos != end; ++pos)
{
res.push_back(pos->str());
}
}
ll check(string s){
ll t = 0;
for (int i = 0, len = s.size(); i < len; i++){
t = t * 10 + (s[i] - '0');
}
return t;
}
int caldandian(ll x){
if (x > 1000) return 15;
if (x > 900) return 14;
if (x > 800) return 13;
if (x > 700) return 12;
if (x > 600) return 11;
if (x > 500) return 10;
if (x > 400) return 9;
if (x > 300) return 8;
if (x > 200) return 7;
if (x > 100) return 6;
if (x > 0) return 5;
return -1;
}
double calbaozheng(ll x){
if (x < 0) return -1;
if (x < 100) return 0.5;
return 1.0;
}
double calguanli(ll x){
if (x < 0) return -1;
if (x < 100) return 1.5;
return 2.0;
}
int main(){
freopen("in.csv", "r", stdin);
freopen("out.csv", "w", stdout);
while (getline(cin, s)){
vector<string> S;
Stringsplit(s, ",", S); // 整行读入的字符串,拆分完存入vector里
cout << S[0] << ',';
cout << S[1] << ',';
cout << S[2] << ',';
cout << check(S[3]) << ','; // 计算人口,把字符串转成数字
ll GDP = check(S[4]);
if (GDP < 0) GDP = -1;
cout << GDP << ','; // GDP
ll dandian = caldandian(GDP);
cout << dandian << ','; // 计算单店加盟费用
double quyu = dandian * 1.5;
if (quyu < 0) printf("-1,");
else printf("%.1lf,", quyu);
double baozheng = calbaozheng(GDP);
if (baozheng < 0) printf("-1,");
else if (baozheng < 1) printf("0.5,");
else printf("1,");
double guanli = calguanli(GDP);
if (guanli < 0) printf("-1\n");
else if (guanli < 2) printf("1.5\n");
else printf("2\n");
}
// for (auto x : S[0]) cout << x << ' ';
// puts("");
return 0;
}
// 正则表达式(regular expression)描述了一种字符串匹配的模式(pattern),可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。
// 而在C++的正则中,把这种操作称为Tokenize分词(或者叫切割)。这种操作刚好可以满足我们的需求,用模板类regex_token_iterator<>提供分词迭代器,可以完成字符串的分割。
输出文件示例:
out.csv