定义一个类Stock,记录一支股票交易的基本信息,信息包括交易日序号(表示本月的第几个交易日,用整数表示)、当日最高价、当日最低价、当日开盘价和当日收盘价。尽量发挥想象力。为该类设计成员函数。
在主函数中建立两个股票对象,分别存储该股票昨天和今天两天的信息,以当日收盘价计算该股票今天的涨幅。
定义一个类Stock,记录一支股票交易的基本信息,信息包括交易日序号(表示本月的第几个交易日,用整数表示)、当日最高价、当日最低价、当日开盘价和当日收盘价。尽量发挥想象力。为该类设计成员函数。
在主函数中建立两个股票对象,分别存储该股票昨天和今天两天的信息,以当日收盘价计算该股票今天的涨幅。
增加三个属性:股票编号、股票名称、当前价格
支持操作:获得交易日/股票编号/股票名称/当前价格/最高价/最低价/开盘价/收盘价/涨幅/是否涨/跌/显示股票信息/开盘/收盘/改变价格。。
#include <iostream>
#include <string>
using namespace std;
// 定义浮点数最大值
#ifndef FLT_MAX
#define FLT_MAX 3.402823466e+38F
#endif
class Stock
{
public:
Stock(const Stock& stock)
{
this->m_strStockID = stock.GetStockID();
this->m_strStockName = stock.GetStockName();
this->m_fPriceDayHigh = FLT_MAX;
this->m_fPriceDayLow = 0.0f;
}
Stock(const Stock stock, int nTradeDataNo)
{
this->m_strStockID = stock.GetStockID();
this->m_strStockName = stock.GetStockName();
this->m_nTradeDataNo = nTradeDataNo;
this->m_fPriceDayHigh = FLT_MAX;
this->m_fPriceDayLow = 0.0f;
}
Stock(const Stock stock, int nTradeDataNo, float fPriceDayOpen)
{
this->m_strStockID = stock.GetStockID();
this->m_strStockName = stock.GetStockName();
this->m_nTradeDataNo = nTradeDataNo;
this->m_fPriceNow = this->m_fPriceDayHigh = this->m_fPriceDayLow = this->m_fPriceDayOpen = fPriceDayOpen;
}
Stock(string strStockID, string strStockName)
{
this->m_strStockID = strStockID;
this->m_strStockName = strStockName;
this->m_fPriceDayHigh = FLT_MAX;
this->m_fPriceDayLow = 0.0f;
}
Stock(string strStockID, string strStockName, int nTradeDataNo)
{
this->m_strStockID = strStockID;
this->m_strStockName = strStockName;
this->m_nTradeDataNo = nTradeDataNo;
this->m_fPriceDayHigh = FLT_MAX;
this->m_fPriceDayLow = 0.0f;
}
Stock(string strStockID, string strStockName, int nTradeDataNo, float fPriceDayOpen)
{
this->m_strStockID = strStockID;
this->m_strStockName = strStockName;
this->m_nTradeDataNo = nTradeDataNo;
this->m_fPriceNow = this->m_fPriceDayHigh = this->m_fPriceDayLow = this->m_fPriceDayOpen = fPriceDayOpen;
}
public:
// 设置交易日
void SetTradeDataNo(int nTradeDataNo)
{
this->m_nTradeDataNo = nTradeDataNo;
}
// 开盘
void Open(float fPriceDayOpen)
{
this->m_fPriceNow = this->m_fPriceDayHigh = this->m_fPriceDayLow = this->m_fPriceDayOpen = fPriceDayOpen;
}
// 收盘
void Close()
{
this->m_fPriceDayClose = m_fPriceNow;
}
// 获得当前价格
int GetPriceNow() const
{
return this->m_fPriceNow;
}
// 获得交易日序号
int GetTradeDateNo() const
{
return this->m_nTradeDataNo;
}
// 获得最高价
float GetDayHigh() const
{
return this->m_fPriceDayHigh;
}
// 获得最低价
float GetDayLow() const
{
return this->m_fPriceDayLow;
}
// 获得开盘价
float GetDayOpen() const
{
return this->m_fPriceDayOpen;
}
// 获得收盘价
float GetDayClose() const
{
return this->m_fPriceDayClose;
}
// 获得股票名
string GetStockName() const
{
return this->m_strStockName;
}
// 获得股票编号
string GetStockID() const
{
return this->m_strStockID;
}
// 获得涨幅
float GetRaises() const
{
return this->m_fPriceDayClose / this->m_fPriceDayOpen;
}
// 涨还是跌
bool IsRaise() const
{
return GetRaises() > 1;
}
// 改变价格
void ChangePrice(float fPriceChanged)
{
if (fPriceChanged > m_fPriceDayHigh)
{
this->m_fPriceDayHigh = fPriceChanged;
}
if (fPriceChanged < m_fPriceDayLow)
{
this->m_fPriceDayLow = fPriceChanged;
}
m_fPriceNow = fPriceChanged;
}
// 显示信息
void PrintStockInfo() const
{
cout << m_nTradeDataNo << "日 ";
cout << "[" << m_strStockID << "] " << m_strStockName << "交易信息" << endl;
cout << "涨幅:" << GetRaises() << "【" << (IsRaise()?"涨":"跌") << "】" <<endl;
cout << "开盘价:" << m_fPriceDayOpen << endl;
cout << "收盘价:" << m_fPriceDayClose << endl;
cout << "最高价:" << m_fPriceDayHigh << endl;
cout << "最低价:" << m_fPriceDayLow << endl;
cout << "=========================================================" << endl;
}
private:
int m_nTradeDataNo; // 交易日序号
float m_fPriceDayHigh; // 当日最高价
float m_fPriceDayLow; // 当日最低价
float m_fPriceDayOpen; // 当日开盘价
float m_fPriceDayClose; // 当日收盘价
float m_fPriceNow; // 当前价格
string m_strStockID; // 股票编号
string m_strStockName; // 股票名
};
int main(void)
{
Stock yesterday("DK3301", "某某股票", 1, 3.5f);
yesterday.ChangePrice(3.7f);
yesterday.ChangePrice(3.1f);
yesterday.ChangePrice(2.7f);
yesterday.Close();
yesterday.PrintStockInfo();
Stock today(yesterday, 2);
today.Open(2.5f);
today.ChangePrice(2.8f);
today.ChangePrice(2.7f);
today.ChangePrice(2.9f);
today.ChangePrice(3.1f);
today.Close();
today.PrintStockInfo();
return 0;
}