【C++】如何使用类?C++如何设定输出小数位数?C++如何不让输出科学计数法?C++如何设定数字输出格式?
【摘要】 目录
以一个股票为例
如何使用类?注意看usestock00.cpp代码
usestock00.cpp
stock00.h
stock00.cpp
C++如何设定数字输出格式?看stock00.cpp这个show_pre()函数
函数解读
程序运行结果
以一个股票为例
stock00.cpp 是stock00.h类函数实现usestock00.cp...
目录
C++如何设定数字输出格式?看stock00.cpp这个show_pre()函数
以一个股票为例
- stock00.cpp 是stock00.h类函数实现
- usestock00.cpp就是主函数
如何使用类?注意看usestock00.cpp代码
usestock00.cpp
-
//uesstock00.cpp -- 客户端程序
-
-
//记得和stock00.cpp stock00.h编译
-
-
#include<iostream>
-
#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<string>
-
-
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<iostream>
-
#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位显示
然后最后恢复默认设置
程序运行结果
文章来源: kings.blog.csdn.net,作者:人工智能博士,版权归原作者所有,如需转载,请联系作者。
原文链接:kings.blog.csdn.net/article/details/98063650
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)