大家好,这里是国中之林!
❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←
问题:
解答:
main.cpp
#include <iostream>
#include "stock20.h"
using namespace std;
const int STKS = 4;
int main()
{
Stock stocks[STKS] =
{
Stock("NanoSmart",12,20.0),
Stock("Boffo Objects",200,2.0),
Stock("Monnlithic Obelisks",130,3.25),
Stock("Fleep Enterpriser",60,6.5)
};
cout << "股票持有:" << endl;
int st;
for (st = 0; st < STKS; st++)
{
//stocks[st].show();
cout << stocks[st] << endl;
}
const Stock* top = &stocks[0];
for (st = 1; st < STKS; st++)
{
top = &top->topval(stocks[st]);
}
cout << "\n最有价值的持股:\n";
//top->show();
cout << *top << endl;
return 0;
}
stock20.h
#pragma once
#include <iostream>
using namespace std;
class Stock
{
private:
//string company;
char* company;
int shares;
double share_val;
double total_val;
void set_tot() { total_val = shares * share_val; }
public:
Stock();
//Stock(const string& co, long n = 0, double pr = 0.0);
Stock(const char* co, long n = 0, double pr = 0.0);
Stock(const Stock& s);
Stock& operator=(const Stock& s);
~Stock();
void buy(long num, double price);
void sell(long num, double price);
void update(double price);
void show()const;
const Stock& topval(const Stock& s)const;
friend ostream& operator<<(ostream& os, const Stock& s);
};
stock20.cpp
#include "stock20.h"
Stock::Stock()
{
company = NULL;
shares = 0;
share_val = 0.0;
total_val = 0.0;
}
Stock::Stock(const Stock& s)
{
int len = strlen(s.company);
company = new char[len + 1];
strcpy_s(company, len + 1, s.company);
shares = s.shares;
share_val = s.share_val;
total_val = s.total_val;
}
Stock& Stock::operator=(const Stock& s)
{
if (this == &s)return *this;
if (company)delete[] company;
int len = strlen(s.company);
company = new char[len + 1];
strcpy_s(company, len + 1, s.company);
shares = s.shares;
share_val = s.share_val;
total_val = s.total_val;
return *this;
}
//Stock::Stock(const string& co, long n, double pr)
//{
// company = co;
// if (n < 0)
// {
// cout << "股份不能为负;" << company << " 的股份设置为0" << endl;
// shares = 0;
// }
// else
// {
// shares = n;
// }
// share_val = pr;
// set_tot();
//}
Stock::Stock(const char* co, long n, double pr)
{
company = new char[strlen(co) + 1];
strcpy_s(company, strlen(co) + 1, co);
if (n < 0)
{
cout << "股份不能为负;" << company << " 的股份设置为0" << endl;
shares = 0;
}
else
{
shares = n;
}
share_val = pr;
set_tot();
}
Stock::~Stock()
{
if (company)
{
delete[] company;
}
}
void Stock::buy(long num, double price)
{
if (num < 0)
{
cout << "购买股份的数量不能为负." << endl;
cout << "事物终止." << endl;
}
else
{
shares += num;
share_val = price;
set_tot();
}
}
void Stock::sell(long num, double price)
{
if (num < 0)
{
cout << "出售股份的数量不能为负." << endl;
cout << "事物终止." << endl;
}
else if(num>shares)
{
cout << "您不能售卖股份超过您所拥有的." << endl;
cout << "事物终止." << endl;
}
else
{
shares -= num;
share_val = price;
set_tot();
}
}
void Stock::update(double price)
{
share_val = price;
set_tot();
}
void Stock::show()const
{
ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield);
streamsize prec = cout.precision(3);
cout << "Company: " << company << " Shares:" << shares << endl;
cout << " Share Price: $" << share_val;
cout.precision(2);
cout << " Total Worth: $" << total_val << endl;
cout.setf(orig, ios_base::floatfield);
cout.precision(prec);
}
const Stock& Stock::topval(const Stock& s)const
{
if (s.total_val > total_val)
{
return s;
}
else
{
return *this;
}
}
ostream& operator<<(ostream& os, const Stock& s)
{
ios_base::fmtflags orig = os.setf(ios_base::fixed, ios_base::floatfield);
streamsize prec = os.precision(3);
os << "Company: " << s.company << " Shares:" << s.shares << endl;
os << " Share Price: $" << s.share_val;
os.precision(2);
os << " Total Worth: $" << s.total_val << endl;
os.setf(orig, ios_base::floatfield);
os.precision(prec);
return os;
}
运行结果:
考查点:
- 构造函数
- 动态内存分配
- 运算符重载
- 友元
- 析构
- 浮点数的精度表示
注意:
- 浮点数的精度表示和还原
2024年9月8日10:21:22