zoukankan      html  css  js  c++  java
  • P10.3 usestock0.cpp

    stock.h

    #ifndef STOCK_H
    #define STOCK_H
    
    #include <string>
    
    class Stock  //类声明
    {
    private:
        std::string company;
        long shares;  //
        double share_val;
        double total_val;
        void set_tot(){total_val=share_val*shares;}
    public:
        void acquire(const std::string &co,long n,double pr);  //获得股票
        void buy(long num,double price);  //买入股票
        void sell(long num,double price);  //卖出股票
        void update(double price);  //更新股票价格
        void show();  //显示关于所持股票的信息
    };
    
    #endif // STOCK_H
    
    

    main.cpp

    #include <iostream>
    #include "stock.h"
    
    using namespace std;
    //对某个公司股票的首次购买
    void Stock::acquire(const string &co, long n, double pr)
    {
        company=co;
        if(n<0)
        {
            cout<<"Number of shares can't be negative;"
               <<company<<"shares set to 0.
    ";
        }
        else
        {
            shares=n;
        }
        share_val=pr;
        set_tot();
    }
    //购买股票
    void Stock::buy(long num, double price)
    {
        if(num<0)
        {
            cout<<"Number of shares purchased can't be negative."
               <<"Transaction is aborted.
    ";
        }
        else
        {
            shares+=num;
            share_val=price;
            set_tot();
        }
    }
    //减少持有的股票
    void Stock::sell(long num, double price)
    {
        if(num<0)
        {
            cout<<"Number of shares sold cna't be negative."
               <<"Transaction is aborted.
    ";
        }
        else if(num>shares)
        {
            cout<<"You can't sell more than you have!"
               <<"Transaction is aborted.
    ";
        }
        else
        {
            shares-=num;
            share_val=price;
            set_tot();
        }
    }
    //
    void Stock::update(double price)
    {
       share_val=price;
       set_tot();
    }
    void Stock::show()
    {
        ios_base::fmtflags orig=
                cout.setf(ios_base::fixed,ios_base::floatfield);
        std::streamsize prec=cout.precision(3);
        cout<<"Company:"<<company
           <<" Shares:"<<shares<<'
    ';
        cout<<" Shares Price:$"<<share_val;
           cout.precision(3);
         cout<<" Total Worth:$"<<total_val<<'
    ';
    
         //show()应重置格式信息,使其恢复到自己被调用前的状态
         cout.setf(orig,ios_base::floatfield);
         cout.precision(prec);
    }
    int main(int argc, char *argv[])
    {
        cout << "Hello World!" << endl;
        Stock fluffy_the_cat;
        fluffy_the_cat.acquire("NanoSmart",20,12.50);
        fluffy_the_cat.show();
        fluffy_the_cat.buy(15,18.125);
        fluffy_the_cat.show();
        fluffy_the_cat.sell(400,20.00);
        fluffy_the_cat.show();
        fluffy_the_cat.buy(300000,40.125);
        fluffy_the_cat.show();
        fluffy_the_cat.sell(300000,0.125);
        fluffy_the_cat.show();
        return 0;
    }
    
    

    运行结果如下

  • 相关阅读:
    IntelliJ Idea和IntelliJ webstrm 常用快捷键
    mybatis基础学习2---(resultType和resultMap的用法和区别)和setting的用法
    使用观察者模式观察线程的生命周期
    观察者设计模式介绍
    java的内存模型及缓存问题
    一个解释volatile关键字作用的最好的例子
    多线程的waitset详细介绍
    浅谈单例模式
    SimpleThreadPool给线程池增加自动扩充线程数量,以及闲时自动回收的功能
    SimpleThreadPool给线程池增加拒绝策略和停止方法
  • 原文地址:https://www.cnblogs.com/Manual-Linux/p/9733246.html
Copyright © 2011-2022 走看看