【C++】如何使用类?C++如何设定输出小数位数?C++如何不让输出科学计数法?C++如何设定数字输出格式?

目录

以一个股票为例

如何使用类?注意看usestock00.cpp代码

usestock00.cpp

stock00.h

stock00.cpp

C++如何设定数字输出格式?看stock00.cpp这个show_pre()函数

函数解读

程序运行结果


 

以一个股票为例

  • stock00.cpp 是stock00.h类函数实现
  • usestock00.cpp就是主函数

如何使用类?注意看usestock00.cpp代码

usestock00.cpp

//uesstock00.cpp -- 客户端程序

//记得和stock00.cpp  stock00.h编译

#include
#include"stock00.h"
int main()
{
	Stock one;
	one.acquire("BWM", 12, 25.12);
	one.show();
	one.show_pre();

	one.buy(15, 18.125);
	one.show();
	one.show_pre();

	one.sell(400, 20.00);
	one.show();
	one.show_pre();

	one.buy(300000, 40.125);
	one.show();
	one.show_pre();

	one.sell(300000, 0.125);
	one.show();
	one.show_pre();
}

stock00.h

// stock00.h -- Stock class interface
// 版本 00 类的声明
#ifndef STOCK00_H_
#define STOCK00_H_

#include

class Stock
{
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();
	void show_pre();

private:
	std::string company;
	long shares;  //股票数量
	double share_val;  // 每股的价格
	double total_val;   //股票的总价格
	void set_tot()
	{
		total_val = shares*share_val;
	}

};

#endif

stock00.cpp

// stock00.cpp -- 对类stock00.h进行函数描述
// 版本还是00
#include
#include"stock00.h"

void Stock::acquire(const std::string & co, long n, double pr)
{
	company = co;
	if (n < 0)
	{
		std::cout << "股票数量不能设置为0; "
			<< company << " shares set to 0. \n";
		shares = 0;
	}
	else
	{
		shares = n;
	}
	share_val = pr;
	set_tot();
}

void Stock::buy(long num, double price)
{
	if (num < 0)
	{
		std::cout << "股票购买数量不能是负数。" << "交易被终止了!!\n";
	}
	else
	{
		shares += num;
		share_val = price;
		set_tot();
	}
}

void Stock::sell(long num, double price)
{
	using std::cout;
	if (num < 0)
	{
		cout << "股票不能卖负数,你脑筋有问题?" << "中断交易了";
	}
	else if (num>shares)
	{
		cout << "你自己有几个股票不清楚吗?你能卖比自己持有还多的股票吗?!" << "交易终止!!!";
	}
	else
	{
		shares -= num;
		share_val = price;
		set_tot();
	}
}

void Stock::update(double price)
{
	share_val = price;
	set_tot();
}

void Stock::show()
{
	std::cout << "Company: " << company
		<< "  Shares: " << shares << "\n"
		<< " Share Price: $ " << share_val
		<< "Total Worth: $" << total_val << "\n";
}

// 设置输出的数字格式
void Stock::show_pre()
{
	using std::cout;
	using std::ios_base;

	//设置格式为#.###
	ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield);
	std::streamsize prec = cout.precision(3);

	cout << "Company: " << company
		<< " Shares: " << shares << '\n';
	cout << " Share Price: $ " << share_val;

	//设置格式为#.##
	cout.precision(2);
	cout << "   Total Worth: $" << total_val << '\n';

	//重新恢复原来的格式
	cout.setf(orig, ios_base::floatfield);
	cout.precision(prec);
		
}

C++如何设定数字输出格式?看stock00.cpp这个show_pre()函数

// 设置输出的数字格式
void Stock::show_pre()
{
	using std::cout;
	using std::ios_base;

	//设置格式为#.###
	ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield);
	std::streamsize prec = cout.precision(3);

	cout << "Company: " << company
		<< " Shares: " << shares << '\n';
	cout << " Share Price: $ " << share_val;

	//设置格式为#.##
	cout.precision(2);
	cout << "   Total Worth: $" << total_val << '\n';

	//重新恢复原来的格式
	cout.setf(orig, ios_base::floatfield);
	cout.precision(prec);
		
}

函数解读

使用ios_base设定了小数点后三位显示

然后又设定小数点后2位显示

然后最后恢复默认设置

程序运行结果

【C++】如何使用类?C++如何设定输出小数位数?C++如何不让输出科学计数法?C++如何设定数字输出格式?_第1张图片

 

你可能感兴趣的:(C++,Primer,Plus,C++,Primer,Plus)