版本比较
浅谈关于股票的C++
正文
增加 171 行删除 14 行
12345637 行未改动444546474648495051523 行未改动565758595860616263646562 行未改动12812913013112913013113213313413513613713813914014114214314413814514614714814915015115215315415514314415615715815916017 行未改动17817918018117018218318418518618717218818919019119219317619419519619719820 行未改动21922022122222322422522622722820922923023123223321 行未改动25525625725823925926026126226326426524526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640721 行未改动update:根据用户及好友@[grAphLimits](https://www.luogu.com.cn/user/1811566)的建议新增功能
~~(其实是游戏)~~# 前言股票是金融圈**无论行内还是行外最知名**的投资工具,那么洛谷上(据我所知)还没有一款关于股票的~~游戏~~介绍
37 行未改动所以我们就可以用这个逻辑写一个股票游戏
来模拟上面那一大堆
(因为我太懒了,我写了个初版就找AI润色了一下):(因为我太懒了,我写了个初版就找AI润色了一下)(成本指的是你当前手里持有的该股票,之前买入时一共花掉的总钱数,不是股票现在的市场价。只卖出一部分股票,就按卖出股数比例扣减总成本;股票全部卖完,这只股票的成本直接清零。):
```cpp#include<bits/stdc++.h>using namespace std;3 行未改动 BUBBLE, CRASH};
int cy[4];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;62 行未改动
int main(){ srand(time(0)); gp[1][10] = 5+rand()%10+(rand()%100)/100; gp[2][10] = 5+rand()%10+(rand()%100)/100;; gp[3][10] = 5+rand()%10+(rand()%100)/100;; 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;17 行未改动 cout << "持仓不足!\n"; } else { money += gp[x][10] * y; 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) << gp[x][10]*y << "\n"; cout << "卖出成功!到手:" << fixed << setprecision(2) << sellMoney << "\n"; // 新增:立刻显示更新后的现金 cout << "现在现金:" << fixed << setprecision(2) << money << " 元\n"; } }
// 买入 // ========== 买入 ========== while(true) { cout << "\n买哪只股(0=不买):"; int x;20 行未改动 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)21 行未改动 }
clearScreen(); } cout << "\n🎉恭喜!你的资金超过2000,胜利!\n"; cout << "\n恭喜!你的资金超过2000,胜利!\n"; return 0;}
```
当然大家可以任意调整参数(你觉得2000小,我试玩了一下还是挺难的)
当然大家可以任意调整参数(你觉得2000小,我试玩了一下还是挺难的,但有一种耍赖方法就是用1000元买某公司所有股票,然后坐等涨价)
当然,有些神犇可能想当上帝而不是买家,所以。。。```cpp#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 orz21 行未改动