代码拉取完成,页面将自动刷新
// ConsoleApplication4.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <string>
#include <Windows.h>
#include<atlstr.h>
#include <list>
#include "ConsoleApplication2.h"
class Order {
public:
float price = 100;
float num;
};
class Humen {
public:
Order Buy(float price)
{
Order o;
o.price = (1.f + .2f*rand() / RAND_MAX)*price;
o.num = 2.f - o.price / 100.f;
o.num = pow(o.num, 3);
if (o.num < .1f)
o.num = .1f;
return o;
}
Order Sell(float price)
{
Order o;
o.price = (1.f + .2f*rand() / RAND_MAX)*price;
o.num = o.price / 100.f;
o.num = pow(o.num, 3);
if (o.num < .1f)
o.num = .1f;
return o;
}
};
class TestMarket {
public:
typedef std::list<Order> OrderList;
OrderList BuyList;
OrderList sellList;
float Deal()
{
float res = 0;
m_firstPrice = -1;
while (!BuyList.empty() && !sellList.empty())
{
if (BuyList.back().price >= sellList.front().price)
{
float dealNum = 0;
m_lastPrice = sellList.front().price;
if (m_firstPrice < 0)
m_firstPrice = m_lastPrice;
if (BuyList.back().num > sellList.front().num)
{
BuyList.back().num -= sellList.front().num;
dealNum = sellList.front().num;
sellList.pop_front();
}
else if (BuyList.back().num == sellList.front().num)
{
dealNum = sellList.front().num;
BuyList.pop_back();
sellList.pop_front();
}
else
{
sellList.front().num -= BuyList.back().num;
dealNum = BuyList.back().num;
BuyList.pop_back();
}
res += dealNum;
}
else
{
break;
}
}
BuyTotal -= res;
sellTotal -= res;
return res;
//Buy = 1 * (sellList.empty() ? 1 : 0) + 2 * (BuyList.empty() ? 1 : 0);
}
float GetPrice()
{
float res = m_lastPrice;
float scale = 2.f;
for (auto order : BuyList)
{
res += scale * order.num;
}
for (auto order : sellList)
{
res -= scale * order.num;
}
if (res < 1)
res = 1;
while (BuyList.size() > 100)
{
BuyList.pop_front();
}
while (sellList.size() > 100)
{
sellList.pop_back();
}
BuyTotal = 0;
for (auto iter : BuyList)
{
BuyTotal += iter.num;
}
sellTotal = 0;
for (auto iter : sellList)
{
sellTotal += iter.num;
}
return res;
//return 100 + BuyList.size() - sellList.size();
/*if (0 == Buy)
{
}
else if (1 == Buy)
{
}
else
{
}*/
}
void Buy(Order& order)
{
BuyTotal += order.num;
DealOrder(BuyList, order);
}
void Sell(Order& order)
{
sellTotal += order.num;
DealOrder(sellList, order);
}
private:
void DealOrder(TestMarket::OrderList& list, Order& order)
{
for (auto iter = list.begin(); iter != list.end(); ++iter)
{
if (order.price <= iter->price)
{
list.insert(iter, order);
return;
}
}
list.push_back(order);
}
public:
int Buy;
float m_lastPrice = 100.f;
float m_firstPrice;
float BuyTotal = 0.f;
float sellTotal = 0.f;
};
#define HumenNum 10
int main()
{
TestMarket market;
Humen buyer[HumenNum];
Humen seller[HumenNum];
unsigned int count = 0;
auto fun = [&](TestMarket::OrderList& list, Order& order)
{
for (auto iter = list.begin(); iter != list.end(); ++iter)
{
if (order.price <= iter->price)
{
list.insert(iter, order);
return;
}
}
list.push_back(order);
};
while (true)
{
float price = market.GetPrice();
for (int i = 0; i < HumenNum; ++i)
{
//fun(market.BuyList, buyer[i].Buy(price));
//fun(market.sellList, seller[i].Sell(price));
market.Buy(buyer[i].Buy(price));
market.Sell(seller[i].Sell(price));
//market
}
printf("%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%d\n", market.m_firstPrice, market.m_lastPrice, market.Deal(), market.BuyTotal, market.sellTotal, count++);
}
/*WCHAR wch[100];
wsprintf(wch, L"%f1234",.666);
float lf_PriceAfterDiscount = 12.3;
WCHAR szPriceAfterDiscount[256];
CString str;
str.Format(L"%.2f", lf_PriceAfterDiscount);
wsprintf(szPriceAfterDiscount, L"%s元", str.GetBuffer(str.GetLength()));*/
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。