输入输出问题

APIandMFC 2008-10-22 08:57:20
#include<iostream>
using namespace std;
#define PI 3.14
class Circle
{
float pool_radius;//水池的半径
float poolrim_radius;//栅栏的半径
public:
Circle(const float r)
{
if(r>=0)
{
pool_radius=r;
poolrim_radius=r+3;
}
else
{
cout<<"请输入一个大于0的数"<<endl;
exit(1);
}
}
void display1()
{
float value=3.5*poolrim_radius*2*PI;
cout<<value<<endl;
}
void display2()
{
float area=PI*poolrim_radius*poolrim_radius-PI*pool_radius*pool_radius;
float value=0.5*area;
cout<<value<<endl;
}
};
int main()
{
float r;
cin>>r;
Circle cir(r);
cir.display1();//栅栏的造价
cir.display2();//过道的造价,注意应设置结果的显示方式。
return 0;
}


1 如果用户输入一个很大的数,怎么显示正确的结果?
2 怎么保证结果只保留小数点后两位?
...全文
106 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuliyang 2008-10-23
  • 打赏
  • 举报
回复
void display1()
{
float value=3.5*poolrim_radius*2*PI;
cout.setf(ios_base::fixed,ios_base::floatfield);
cout.precision(2);
cout<<value<<endl;
}
void display2()
{
float area=PI*poolrim_radius*poolrim_radius-PI*pool_radius*pool_radius;
float value=0.5*area;
cout.setf(ios_base::fixed,ios_base::floatfield);
cout.precision(2);
cout<<value<<endl;
}
deerwin1986 2008-10-23
  • 打赏
  • 举报
回复
用字符串存储大数
然后用adjustfield 和 setprecisio 来调整输出精度
jakqigle 2008-10-22
  • 打赏
  • 举报
回复
看个例子:
double n = 0;
n = 1.0 / 3.0;
cout<<"---计算 n = 1 / 3---"<<endl;
cout<<n<<setw(8)<<"系统默认的小数点保留位数是6"<<endl<<endl;
cout<<"使用setprecision函数设定小数点位数"<<endl;
for(int i = 1;i < 10;i++)
{
cout<<"保留小数点"<<i<<"位的结果:";
cout<<setprecision(i)<<n<<endl;
}
cout<<endl;

jakqigle 2008-10-22
  • 打赏
  • 举报
回复
多大的数呢?很大的话,是不应该用大整数乘法或用字符串乘法,用字符串存数字,就不存在数范围限制。
三文鱼也会飞 2008-10-22
  • 打赏
  • 举报
回复
尽量一起除,减少误差,
保留后两位用:
FormatFloat("0.00",float变量)
三文鱼也会飞 2008-10-22
  • 打赏
  • 举报
回复
void display1()
{
float value = (poolrim_radius*2198)/100;
cout<<value<<endl;
}
void display2()
{
float area = 314*poolrim_radius*(100*poolrim_radius-314)*pool_radius*pool_radius)/10000;
float value = area/2;
cout<<value<<endl;
}

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧