| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 8666 人关注过本帖
标题:[求助]请教关于cout.setf(ios::fixed)这类的用法等问题 在线等
只看楼主 加入收藏
chlstc
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2007-1-8
收藏
 问题点数:0 回复次数:6 
[求助]请教关于cout.setf(ios::fixed)这类的用法等问题 在线等

请看这段代码,回答问题哦
#include<iostream.h>
#include<math.h>
void main()
{
const double pi=3.1415926;
const int interval=5;
cout<<" 角度x 正弦sin(x)";
cout<<" 余弦cos(x) 正切tan(x)";
float arc;
cout.setf(ios::fixed);
cout.precision(4);
for (int angle=0;angle<=90;angle+=interval)
{
arc=pi*angle/180;
cout<<endl;
cout.width(10);
cout<<angle;
cout.width(16);
cout<<sin(arc);
cout.width(16);
cout<<cos(arc);
cout.width(16);
if(angle==90||angle==-90)
cout<<"-";
else
cout<<tan(arc);
}


cout<<endl;
}

上面代码中cout.setf(ios::fixed); 和cout.precision;这两句还有cout.width();我不是很明白他们的用法和功能还有他们的作用域什么的,希望能帮小弟解释一下,越清楚越好哦,谢谢喽

[此贴子已经被作者于2007-6-25 16:07:21编辑过]

搜索更多相关主题的帖子: ios cout fixed setf 用法 
2007-06-25 16:06
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
fixed : to insert floating-point values in fixed-point format (with no exponent field).

scientific : to insert floating-point values in scientific format (with an exponent field).

precision() : member function stores _Prec in the display precision and returns its previous stored value.

width() : member function stores _Wide in the field width and returns its previous stored value.

摘自MSDN.

Fight  to win  or  die...
2007-06-25 16:19
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 

ios_base::precision
Specifies the number of digits to display in a floating-point number.


streamsize precision( ) const;
streamsize precision(
streamsize _Prec
);
Parameters
_Prec
The number of significant digits to display, or the number of digits after the decimal point in fixed notation.

Return Value
The first member function returns the stored display precision. The second member function stores _Prec in the display precision and returns its previous stored value.

Example
Copy Code
// ios_base_precision.cpp
// compile with: /EHsc
#include <iostream>

int main( )
{
using namespace std;
float i = 31.31234F;

cout.precision( 3 );
cout << i << endl; // display three significant digits
cout << fixed << i << endl; // display three digits after decimal
// point
}

Output

31.3
31.312

///////////////////////////////////////////////////////////////////////////////
ios_base::setf
Sets the specified flags.


void setf(
fmtflags _Mask
);
fmtflags setf(
fmtflags _Mask,
fmtflags _Unset
);
Parameters
_Mask
The flags to turn on.

_Unset
The flags to turn off.

Return Value
The previous format flags
Example
Copy Code
// ios_base_setf.cpp
// compile with: /EHsc
#include <iostream>

int main( )
{
using namespace std;
int i = 10;
cout << i << endl;

cout.unsetf( ios_base::dec );
cout.setf( ios_base::hex );
cout << i << endl;

cout.setf( ios_base::dec );
cout << i << endl;
cout.setf( ios_base::hex, ios_base::dec );
cout << i << endl;
}

Output

10
a
10
a

/////////////////////////////////-----msdn----//////////////////////////////////////////////



Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-06-25 16:26
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
简单来说

cout.setf(ios::fixed) 设置当前流为小数形式输出
cout.precision()设置当前流的小数位数

cout.width()设置输出所占字长

Fight  to win  or  die...
2007-06-25 16:27
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 

查MSDN很容易搞定的了,只是看英文要费点神。
加点相关的;

浮点数按浮点格式输出时,setprecision 指的是有效位数
浮点数按定点(fixed)或科学计算法(scientific)格式输出时,setprecision 指的是小数点的位数
如:
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
double PI = 3.1415926;
cout<<setprecision(6)<<PI<<endl
<<fixed<<PI<<endl
<<scientific<<PI<<endl;
return 0;
}


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-06-25 16:38
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
我都不知道LZ说那种,看了MDSN才知道

好象一般都用setw和setprecision这样用。

Fight  to win  or  die...
2007-06-25 16:46
chlstc
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2007-1-8
收藏
得分:0 

好像懂点了 继续看


我爱这个世界 简简单单 加QQ604804955吧
2007-06-25 17:01
快速回复:[求助]请教关于cout.setf(ios::fixed)这类的用法等问题 在线等
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015411 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved