update:根据用户及好友@grAphLimits的建议新增功能
(其实是游戏)
前言
股票是金融圈无论行内还是行外最知名的投资工具,那么洛谷上(据我所知)还没有一款关于股票的游戏介绍
所以我们今天来给神犇们讲解一下股票
正文
直意解释
我们谈到股票时,都会想到这是个赚钱的东西,但他到底是什么呢,我们回归股票根本的意思,来理解它为何那么神奇
股票=股+票,这是一个非常简洁的式子,也是他的字面意思
股是什么呢,股就是公司股份,也就是说,一个公司把他的股份拆成很多份,你买一份就成为了这个公司的股东(虽然股份应该只有不到0.001%)
这个一份东西就叫股票(所以票只是一个物理载体)
那么为什么它能赚钱呢
涨跌含义
取不出好名字了
其实股票的涨跌主要不是由公司价值,公司价值其实影响的很少,更多的是买的人与卖的人人数比较
若买的人>卖的人 则涨
否则 跌
(但你说买的人和卖的人人数是由什么变化,好像也大部分是公司发生的事情,当然也有泡沫这种极端情况,所以)
泡沫为什么会产生
1.一堆抱有疯狂的乐观情绪的人(都觉得股票永远不会跌)
2.几十个用股票赚了钱的人登上了热搜
他们一结合,就让全国人都投上了股票,所以股票疯涨,泡沫产生
同样,如果又有几十个人哪一天脑洞大开卖了股票又登上了热搜
股票就崩盘了
所以我们可以干些什么
真正的正文开始
所以我们就可以用这个逻辑写一个股票游戏
来模拟上面那一大堆
(因为我太懒了,我写了个初版就找AI润色了一下)(成本指的是你当前手里持有的该股票,之前买入时一共花掉的总钱数,不是股票现在的市场价。只卖出一部分股票,就按卖出股数比例扣减总成本;股票全部卖完,这只股票的成本直接清零。):
#include<bits/stdc++.h>
using namespace std;
enum MarketStatus {
NORMAL,
BUBBLE,
CRASH
};
int cy[4]; // cy[1]微软 cy[2]Google cy[3]AAPL 持仓股数
double costTotal[4]; // 每只股票持仓的总成本
double money = 1000.0;
double gp[4][11];
MarketStatus market = NORMAL;
int bubble_timer = 0;
void showStock(int id, string name)
{
cout << name << " 近十日价格:";
for(int i = 1; i <= 9; i++)
{
cout << fixed << setprecision(2) << gp[id][i] << "->";
}
cout << fixed << setprecision(2) << gp[id][10];
double change = (gp[id][10] - gp[id][9]) / gp[id][9] * 100;
cout << " 涨跌幅:" << fixed << setprecision(2) << change << "%\n";
}
double getNewPrice(double oldPrice)
{
double rate;
if (market == BUBBLE)
{
int r = rand() % 100;
if (r < 70)
{
rate = 1.15 + (rand() % 36) / 100.0 + (rand() % 100 - 50) / 1000.0;
}
else
{
rate = 1.02 + (rand() % 8) / 100.0 + (rand() % 100 - 50) / 1000.0;
}
}
else if (market == CRASH)
{
rate = 0.30 + (rand() % 41) / 100.0 + (rand() % 100 - 50) / 1000.0;
market = NORMAL;
}
else
{
int r = rand() % 100;
if (r < 3)
{
rate = 0.60 + (rand() % 21) / 100.0 + (rand() % 100 - 50) / 1000.0;
}
else if(r < 8)
{
rate = 1.10 + (rand() % 16)/100.0 + (rand() % 100 - 50) / 1000.0;
}
else
{
rate = 0.92 + (rand() % 17) / 100.0 + (rand() % 100 - 50) / 1000.0;
}
}
double newp = oldPrice * rate;
if(newp < 0.1) newp = 0.1;
return newp;
}
void clearScreen()
{
#if defined(_WIN32)
system("cls");
#elif defined(__linux__) || defined(__APPLE__)
system("clear");
#endif
}
int main()
{
srand(time(0));
gp[1][10] = 5+rand()%10+(rand()%100)/100.0;
gp[2][10] = 5+rand()%10+(rand()%100)/100.0;
gp[3][10] = 5+rand()%10+(rand()%100)/100.0;
// 初始化持仓、总成本数组清零
memset(cy, 0, sizeof(cy));
memset(costTotal, 0, sizeof(costTotal));
while(money < 2000)
{
cout << "===== 模拟股票市场 =====\n";
cout << "当前现金:" << fixed << setprecision(2) << money << " 元\n";
cout << "持仓:微软:" << cy[1] << " Google:" << cy[2] << " AAPL:" << cy[3] << "\n";
// 打印成本
cout<<"当前成本 微软:";
printf("%.2lf",costTotal[1]);
cout<<" Google:";
printf("%.2lf",costTotal[2]);
cout<<" AAPL:";
printf("%.2lf\n\n",costTotal[3]);
showStock(1, "微软");
showStock(2, "Google");
showStock(3, "AAPL");
// ========== 卖出 ==========
while(true)
{
cout << "\n卖哪只股(0=不卖):";
int x;
cin >> x;
if(x == 0) break;
if(x < 1 || x >3)
{
cout << "编号错误!\n";
continue;
}
cout << "卖出数量:";
int y;
cin >> y;
if(y <= 0)
{
cout << "数量不能为0或负数\n";
continue;
}
if(cy[x] < y)
{
cout << "持仓不足!\n";
}
else
{
double sellMoney = gp[x][10] * y;
money += sellMoney;
// 按比例扣除持仓总成本
double reduceCost = costTotal[x] * y / cy[x];
costTotal[x] -= reduceCost;
cy[x] -= y;
cout << "卖出成功!到手:" << fixed << setprecision(2) << sellMoney << "\n";
// 新增:立刻显示更新后的现金
cout << "现在现金:" << fixed << setprecision(2) << money << " 元\n";
}
}
// ========== 买入 ==========
while(true)
{
cout << "\n买哪只股(0=不买):";
int x;
cin >> x;
if(x == 0) break;
if(x < 1 || x >3)
{
cout << "编号错误\n";
continue;
}
cout << "买入数量:";
int y;
cin >> y;
if(y <= 0)
{
cout << "数量非法\n";
continue;
}
double cost = gp[x][10] * y;
if(cost > money)
{
cout << "资金不足,需要" << fixed << setprecision(2) << cost << ",你只有" << money << "\n";
}
else
{
money -= cost;
cy[x] += y;
costTotal[x] += cost;
cout << "买入成功!花费:" << fixed << setprecision(2) << cost << "\n";
cout << "现在现金:" << fixed << setprecision(2) << money << " 元\n";
}
}
// ========== 更新市场行情 ==========
if(market == BUBBLE)
{
bubble_timer--;
if(bubble_timer <= 0)
{
market = CRASH;
}
}
if(market == NORMAL)
{
if(rand() % 100 < 5)
{
market = BUBBLE;
bubble_timer = 3 + rand() % 5;
}
}
for(int i = 1; i <= 3; i++)
{
double newPrice = getNewPrice(gp[i][10]);
for(int j = 1; j <= 9; j++)
{
gp[i][j] = gp[i][j+1];
}
gp[i][10] = newPrice;
}
clearScreen();
}
cout << "\n恭喜!你的资金超过2000,胜利!\n";
return 0;
}
当然大家可以任意调整参数(你觉得2000小,我试玩了一下还是挺难的,但有一种耍赖方法就是用1000元买某公司所有股票,然后坐等涨价)
当然,有些神犇可能想当上帝而不是买家,所以。。。
#include<bits/stdc++.h>
using namespace std;
enum MarketStatus {
NORMAL,
BUBBLE,
CRASH
};
double gp[4][11];
MarketStatus market = NORMAL;
int bubble_timer = 0;
void showStock(int id, string name)
{
cout << name << " 近十日价格:";
for(int i = 1; i <= 9; i++)
{
cout << fixed << setprecision(2) << gp[id][i] << "->";
}
cout << fixed << setprecision(2) << gp[id][10];
double change = (gp[id][10] - gp[id][9]) / gp[id][9] * 100;
cout << " 涨跌幅:" << fixed << setprecision(2) << change << "%\n";
}
double getNewPrice(double oldPrice)
{
double rate;
if (market == BUBBLE)
{
int r = rand() % 100;
if (r < 70)
{
rate = 1.15 + (rand() % 36) / 100.0 + (rand() % 100 - 50) / 1000.0;
}
else
{
rate = 1.02 + (rand() % 8) / 100.0 + (rand() % 100 - 50) / 1000.0;
}
}
else if (market == CRASH)
{
rate = 0.30 + (rand() % 41) / 100.0 + (rand() % 100 - 50) / 1000.0;
market = NORMAL;
}
else
{
int r = rand() % 100;
if (r < 3)
{
rate = 0.60 + (rand() % 21) / 100.0 + (rand() % 100 - 50) / 1000.0;
}
else if(r < 8)
{
rate = 1.10 + (rand() % 16)/100.0 + (rand() % 100 - 50) / 1000.0;
}
else
{
rate = 0.92 + (rand() % 17) / 100.0 + (rand() % 100 - 50) / 1000.0;
}
}
double newp = oldPrice * rate;
if(newp < 0.1) newp = 0.1;
return newp;
}
void clearScreen()
{
#if defined(_WIN32)
system("cls");
#elif defined(__linux__) || defined(__APPLE__)
system("clear");
#endif
}
void SleepScreen() {
#if defined(_WIN32)
_sleep(100);
#elif defined(__linux__)
sleep(0.1);
#endif
}
int main()
{
srand(time(0));
gp[1][10] = 5+rand()%10+(rand()%100)/100.0;
gp[2][10] = 5+rand()%10+(rand()%100)/100.0;
gp[3][10] = 5+rand()%10+(rand()%100)/100.0;
while(true)
{
cout << "===== 模拟股票市场 =====\n";
showStock(1, "微软");
showStock(2, "Google");
showStock(3, "AAPL");
// ========== 买卖全部结束后,再更新行情、生成新一天股价 ==========
if(market == BUBBLE)
{
bubble_timer--;
if(bubble_timer <= 0)
{
market = CRASH;
}
}
if(market == NORMAL)
{
if(rand() % 100 < 5)
{
market = BUBBLE;
bubble_timer = 3 + rand() % 5;
}
}
for(int i = 1; i <= 3; i++)
{
double newPrice = getNewPrice(gp[i][10]);
for(int j = 1; j <= 9; j++)
{
gp[i][j] = gp[i][j+1];
}
gp[i][10] = newPrice;
}
SleepScreen();
clearScreen();
}
return 0;
}最后
完了,写完这个我觉得我好欠啊
神犇不喜勿喷orz orz
彩蛋
Google帝国万岁!!!
Google帝国万岁!!!
Google帝国万岁!!!
Google帝国万岁!!!
Google帝国万岁!!!
微软帝国发霉!!!
微软帝国发霉!!!
微软帝国发霉!!!
微软帝国发霉!!!
微软帝国发霉!!!