#pragma once
#include "stdafx.h"
#include "json/json.h"
// #include "json\writer.h"
// #include "json\reader.h"
using namespace std;
typedef struct
{
int Id;
int Rmb;
string Name;
string Explain;
}Item;
class Config
{
private:
int mId;
int mRmb;
string mName;
string mExplain;
map<int, Item>m_ItemType;
public:
//单例模式;
void readJson();
void saveItem();
map<int, Item>getItemType();
static Config * getInstance();
/*void update();*/
Config(void);
~Config(void);
};#include "stdafx.h"
#include "Config.h"
#include <fstream>
static Config *game = NULL;
Config::Config(void)
{
readJson();
}
Config::~Config(void)
{
}
Config * Config::getInstance()
{
if (game == NULL)
{
game = new Config();
}
return game;
}
map<int, Item> Config::getItemType()
{
return m_ItemType;
}
void Config::readJson()
{
Json::Value root;
Json::Value itemdatas;
Json::Reader reader;
//读取文件;
ifstream is;
is.open("jiangping.json",ios::binary);
if (reader.parse(is,root))
{
itemdatas = root["datas"];
for (int i=0;i<itemdatas.size();++i)
{
Item item;
int id = itemdatas[i]["id"].asInt();
item.Id = id;
item.Rmb = itemdatas[i]["rmb"].asInt();
item.Name =itemdatas[i]["name"].asString();
item.Explain = itemdatas[i]["explain"].asString();
m_ItemType.insert(map<int,Item>::value_type(id,item));
}
}
is.close();
}
// TheLuckyRoller.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "Config.h"
#include <map>
int _tmain(int argc, _TCHAR* argv[])
{
map<int,Item>Items=Config::getInstance()->getItemType();
// for (map<int,Item>::iterator it = Items.begin();it !=Items.end();++it)
// {
// it->first;
//
// }
cout<<"1等奖是苹果哦,4等奖是神秘物品祝你好运;"<<endl;
cout<<"摇奖1开始!!!其他结束;"<<endl;
while (1)
{
int index1,cindex;
cin>>cindex;
index1=cindex;
if(index1<=1)
{
srand((int)time(NULL));
int index = rand()%10+1;
map<int,Item>::iterator it = Items.find(index);
if (it !=Items.end())
{
cout<<"恭喜您中奖了奖品为;"<<endl;
cout<<"ID"<<Items[index].Id<<endl;
cout<<"名称;"<<Items[index].Name<<endl;
cout<<"价格;"<<Items[index].Rmb<<endl;
cout<<"说明;"<<Items[index].Explain<<endl;
break;
}
else if(index>=4)
{
cout<<"你没用好中奖;"<<index<<endl;
break;
}
}
}
system("pause");
return 0;
}
#include "stdafx.h"
#include "json/json.h"
// #include "json\writer.h"
// #include "json\reader.h"
using namespace std;
typedef struct
{
int Id;
int Rmb;
string Name;
string Explain;
}Item;
class Config
{
private:
int mId;
int mRmb;
string mName;
string mExplain;
map<int, Item>m_ItemType;
public:
//单例模式;
void readJson();
void saveItem();
map<int, Item>getItemType();
static Config * getInstance();
/*void update();*/
Config(void);
~Config(void);
};#include "stdafx.h"
#include "Config.h"
#include <fstream>
static Config *game = NULL;
Config::Config(void)
{
readJson();
}
Config::~Config(void)
{
}
Config * Config::getInstance()
{
if (game == NULL)
{
game = new Config();
}
return game;
}
map<int, Item> Config::getItemType()
{
return m_ItemType;
}
void Config::readJson()
{
Json::Value root;
Json::Value itemdatas;
Json::Reader reader;
//读取文件;
ifstream is;
is.open("jiangping.json",ios::binary);
if (reader.parse(is,root))
{
itemdatas = root["datas"];
for (int i=0;i<itemdatas.size();++i)
{
Item item;
int id = itemdatas[i]["id"].asInt();
item.Id = id;
item.Rmb = itemdatas[i]["rmb"].asInt();
item.Name =itemdatas[i]["name"].asString();
item.Explain = itemdatas[i]["explain"].asString();
m_ItemType.insert(map<int,Item>::value_type(id,item));
}
}
is.close();
}
// TheLuckyRoller.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "Config.h"
#include <map>
int _tmain(int argc, _TCHAR* argv[])
{
map<int,Item>Items=Config::getInstance()->getItemType();
// for (map<int,Item>::iterator it = Items.begin();it !=Items.end();++it)
// {
// it->first;
//
// }
cout<<"1等奖是苹果哦,4等奖是神秘物品祝你好运;"<<endl;
cout<<"摇奖1开始!!!其他结束;"<<endl;
while (1)
{
int index1,cindex;
cin>>cindex;
index1=cindex;
if(index1<=1)
{
srand((int)time(NULL));
int index = rand()%10+1;
map<int,Item>::iterator it = Items.find(index);
if (it !=Items.end())
{
cout<<"恭喜您中奖了奖品为;"<<endl;
cout<<"ID"<<Items[index].Id<<endl;
cout<<"名称;"<<Items[index].Name<<endl;
cout<<"价格;"<<Items[index].Rmb<<endl;
cout<<"说明;"<<Items[index].Explain<<endl;
break;
}
else if(index>=4)
{
cout<<"你没用好中奖;"<<index<<endl;
break;
}
}
}
system("pause");
return 0;
}