C++读取Json生成随机名字

/*
 * Read.cpp
 *
 *  Created on: 2018年8月17日
 *      Author: admin
 */
#include<string.h>
#include<json/json.h>
#include<stdlib.h>
#include<iostream>
#include<fstream>
#include<map>
#include<math.h>
#include<time.h>
#include<random>
using namespace std;
map<int,string> first_name_map;
map<int,string> man_second_name_map;
map<int,string> man_thired_name_map;
map<int,string> girl_second_name_map;
map<int,string> girl_thired_name_map;
string Get_Random_Name(int sex)
{
	string name;
	name = first_name_map[rand()%first_name_map.size()];
	if(sex==1)
	{
		if(rand()%2==0)
		name+=man_second_name_map[rand()%man_second_name_map.size()];
		name+=man_thired_name_map[rand()%man_thired_name_map.size()];
	}
	else
	{
		if(rand()%2==0)
		name+=girl_second_name_map[rand()%girl_second_name_map.size()];
		name+=girl_thired_name_map[rand()%girl_thired_name_map.size()];
	}
	return name;
}
void ReadFileJson();
int main()
{
	ReadFileJson();
	int sex;
	time_t now;
	time(&now);
	int starttime = now;
	cin>>sex;
	int Max_num = max(man_second_name_map.size()*man_thired_name_map.size(),girl_second_name_map.size()*girl_thired_name_map.size());
	Max_num*=first_name_map.size();
	for(int i=0;i<Max_num*3;i++)
	{
		string name = Get_Random_Name(i);
		cout<<name<<endl;
		if(name=="**梅")
		{
			time(&now);
			int Cost_Time = (int)now - starttime;
			cout<<Cost_Time+'S'<<endl;
			break;
		}
	}
	return 0;
}
void ReadFileJson()
{
	Json::Value root;
	Json::Reader reader;
	ifstream in("/home/admin/桌面/random_name.json");
	if(!in.is_open())
	{
		cout<<"error to open file!!!"<<endl;
				return ;
	}
	if(reader.parse(in,root))
	{
		first_name_map.clear();
		man_second_name_map.clear();
		man_thired_name_map.clear();
		girl_thired_name_map.clear();
		girl_second_name_map.clear();
		int sz = 0;
		sz = root["first_name"].size();
		for(int i=0;i<sz;i++)
			first_name_map[i] = root["first_name"][i].asString();
		sz = root["man_second"].size();
		for(int i=0;i<sz;i++)
			man_second_name_map[i] = root["man_second"][i].asString();
		sz = root["man_third"].size();
		for(int i=0;i<sz;i++)
			man_thired_name_map[i] = root["man_third"][i].asString();
		sz = root["woman_second"].size();
		for(int i=0;i<sz;i++)
			girl_second_name_map[i] = root["woman_second"][i].asString();
		sz = root["woman_third"].size();
		for(int i=0;i<sz;i++)
			girl_thired_name_map[i] = root["woman_third"][i].asString();
	}
}




七夕快乐,这个算是在公司写的最后一段代码吧,人生有梦,各自精彩。

猜你喜欢

转载自blog.csdn.net/z8110/article/details/81778414