浅谈关于股票的C++ 2
grAphLimits · · 个人记录
前文见此链接:https://www.luogu.me/article/boadkq88
更新:
-
成本说明
成本指的是你当前手里持有的该股票,之前买入时一共花掉的总钱数/当前个数,不是股票现在的市场价。界面在持仓数量那一行的下面,显示一行:成本:微软:xxxx Google:xxxx AAPL:xxxx。买入股票就累加对应股票总成本;只卖出一部分股票,就按卖出股数比例扣减总成本;股票全部卖完,这只股票的成本直接清零。
-
更新规则
进行买入或者卖出操作之后,玩家的现金金额,还有上面的持仓成本数据,都要立刻更新。
-
输入爆炸
现在代码如果输入字母(比如
a、s)给int x,cin 会进入错误状态,后续输入全部失效,程序卡住 修复:检测 cin 失败,清空错误标记、丢弃缓冲区里的垃圾字符。
下面是代码:
#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
}
// 工具函数:处理非法输入(输入字母时调用)
bool readInt(int &num)
{
cin >> num;
if(cin.fail())
{
cin.clear(); // 清除错误标志
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 扔掉缓冲区所有垃圾直到换行
return false;
}
return true;
}
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]/cy[1]);
cout<<" Google:";
printf("%.2lf",costTotal[2]/cy[2]);
cout<<" AAPL:";
printf("%.2lf\n\n",costTotal[3]/cy[3]);
showStock(1, "微软");
showStock(2, "Google");
showStock(3, "AAPL");
// ========== 卖出 ==========
while(true)
{
cout << "\n卖哪只股(0=不卖):";
int x;
if(!readInt(x))
{
cout << "输入不是数字!请重新输入。\n";
continue;
}
if(x == 0) break;
if(x < 1 || x >3)
{
cout << "编号错误!\n";
continue;
}
cout << "卖出数量:";
int y;
if(!readInt(y))
{
cout << "输入不是数字!请重新输入。\n";
continue;
}
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;
if(!readInt(x))
{
cout << "输入不是数字!请重新输入。\n";
continue;
}
if(x == 0) break;
if(x < 1 || x >3)
{
cout << "编号错误\n";
continue;
}
cout << "买入数量:";
int y;
if(!readInt(y))
{
cout << "输入不是数字!请重新输入。\n";
continue;
}
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;
}