求绝对值
程序员文章站
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;
}
程序运行如下:
上一篇: case语句与for - while - until 循环语句
下一篇: GitLab实现持续集成