C++的一点使用经验
来源:互联网 发布:曾经的网络歌曲 编辑:程序博客网 时间:2024/05/17 07:27
我在使用C++时有一些体会,相信大家都会的。我写出来也是和大家分享一下
1。在C++中数据类型float和double.
同类型为float的变量相比,类型为double的变量可保存的值较大且较为准确,因此我比较喜欢使用double而不是float.这里有一个重要的点要说一下:比如我们定义两个变量
int sum = 1230;
int score = 230;
double avrg = 0.0f;
如果我们用:
avrg = sum/score;
其中(sum/score)这个计算结果是一个整型, 因为sum和score都是整型。在这样的计算中。小数部分会
丢失,因此C++提供了一元强制类型转换
avrg = static_cast< double > ( sum ) / score;
static_cast < double > (), 这位sum创建了一个临时性的浮点数副本,这就是我们说的显式转换。对比显式转换,当然就是隐式转换,例如当score 被提升为double后,开始执行浮点除法运算。
然后我们输出
cout << "aver is " << setprecision(2)
<<setiosflags( ios::fixedm ios::showpoint )
<< avrg <<endl;
setprecision(2) 是被称作参数化操作元的东西,要加一条预编译指令
#include <iomanip>
endl是一个非参数化操纵元,它不需要<iomanip>头文件,如果未指定精度,浮点值会采用6个精度(默认)输出。
注意不要愚蠢的想比较两个浮点数是否相等,应该测试两个浮点数的差值的绝对值是否小于一个指定的小值。这一点在游戏的坐标跟踪中常被用到。
# include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;
# include <iomanip>
using std::setprecision;
using std::setiosflags;
int main()
{
int score = 0;
int sum = 0;
int count = 0;
double avrg;
cout<<"enter score - to end: ";
cin>>score;
while ( score != -1 )
{
sum = sum + score;
count = count + 1;
cout<< "enter score , -1 to end : ";
cin>>score;
}
if( count != 0 )
{
avrg = static_cast< double > (sum ) / count;
cout << "avrg is " << setprecision(2)
<< setiosflags(ios::fixed | ios::showpoint )
<< avrg << endl;
}
else
{ cout << " NO!!!!!";
}
return 0;
}
enter score - to end: 75
enter score , -1 to end : 93
enter score , -1 to end : 23
enter score , -1 to end : 98
enter score , -1 to end : 43
enter score , -1 to end : 54
enter score , -1 to end : 56
enter score , -1 to end : 2334
enter score , -1 to end : 45
enter score , -1 to end : 43
enter score , -1 to end : 454
enter score , -1 to end : 232
enter score , -1 to end : -1
avrg is 295.83
1。在C++中数据类型float和double.
同类型为float的变量相比,类型为double的变量可保存的值较大且较为准确,因此我比较喜欢使用double而不是float.这里有一个重要的点要说一下:比如我们定义两个变量
int sum = 1230;
int score = 230;
double avrg = 0.0f;
如果我们用:
avrg = sum/score;
其中(sum/score)这个计算结果是一个整型, 因为sum和score都是整型。在这样的计算中。小数部分会
丢失,因此C++提供了一元强制类型转换
avrg = static_cast< double > ( sum ) / score;
static_cast < double > (), 这位sum创建了一个临时性的浮点数副本,这就是我们说的显式转换。对比显式转换,当然就是隐式转换,例如当score 被提升为double后,开始执行浮点除法运算。
然后我们输出
cout << "aver is " << setprecision(2)
<<setiosflags( ios::fixedm ios::showpoint )
<< avrg <<endl;
setprecision(2) 是被称作参数化操作元的东西,要加一条预编译指令
#include <iomanip>
endl是一个非参数化操纵元,它不需要<iomanip>头文件,如果未指定精度,浮点值会采用6个精度(默认)输出。
注意不要愚蠢的想比较两个浮点数是否相等,应该测试两个浮点数的差值的绝对值是否小于一个指定的小值。这一点在游戏的坐标跟踪中常被用到。
# include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;
# include <iomanip>
using std::setprecision;
using std::setiosflags;
int main()
{
int score = 0;
int sum = 0;
int count = 0;
double avrg;
cout<<"enter score - to end: ";
cin>>score;
while ( score != -1 )
{
sum = sum + score;
count = count + 1;
cout<< "enter score , -1 to end : ";
cin>>score;
}
if( count != 0 )
{
avrg = static_cast< double > (sum ) / count;
cout << "avrg is " << setprecision(2)
<< setiosflags(ios::fixed | ios::showpoint )
<< avrg << endl;
}
else
{ cout << " NO!!!!!";
}
return 0;
}
enter score - to end: 75
enter score , -1 to end : 93
enter score , -1 to end : 23
enter score , -1 to end : 98
enter score , -1 to end : 43
enter score , -1 to end : 54
enter score , -1 to end : 56
enter score , -1 to end : 2334
enter score , -1 to end : 45
enter score , -1 to end : 43
enter score , -1 to end : 454
enter score , -1 to end : 232
enter score , -1 to end : -1
avrg is 295.83
- css使用的一点经验
- 一点Socket的使用经验
- C++的一点使用经验
- EVC使用的一点经验
- openMP的一点使用经验
- openMP的一点使用经验
- openMP的一点使用经验
- openMP的一点使用经验
- openMP的一点使用经验
- openMP的一点使用经验
- openMP的一点使用经验
- 使用SonarQube的一点经验
- openMP的一点使用经验
- openMP的一点使用经验
- openMP的一点使用经验
- Intellij使用的一点经验
- openMP的一点使用经验
- openMP的一点使用经验
- 利用timestamp实现数据库处理同一记录的时间差异问题
- 测试下 开张了算
- 对等网络(P2P)中主流分布式哈希算法比较分析
- 杂感: vista
- cygwin是一个在windows平台上运行的linux模拟环境
- C++的一点使用经验
- PL/SQL实现Oracle数据库任务调度
- VB网络编程高招
- ZigBee是什么?
- 中国15所名校计算机专业之特色分析
- 网页javascript精华代码集
- 长文章用分页符来分页显示
- How to add OLE drag-and-drop functionality to a pre-existing MFC application
- AVAYA成都研讨会SOA插曲