欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

求绝对值

程序员文章站 2022-06-21 18:40:26
...

使用C++编写程序:输入一个实数,输出它的绝对值,结果保留两位小数。

程序代码如下:

#include<iostream>
#include<iomanip>
#include<cmath>
#define ElemType float

using namespace std;

class ABS
{
public:
	ABS(ElemType Num) :Number(Num) {};
	void GetABS();
private:
	ElemType Number;
};

inline void ABS::GetABS()
{
	cout << fixed << setprecision(2) << fabs(Number);    //求其绝对值并保留两位小数
}

int main()
{
	ElemType num;
	cin >> num;
	ABS A(num);
	A.GetABS();
	return 0;
}

程序运行如下:求绝对值

相关标签: C++ c++