您的位置:首页 > 编程语言 > C语言/C++

【C++ 学习笔记小程序05】 const限定符 浮点数 算数运算符

2014-11-26 22:37 387 查看
#include "MyFirstDemo.h"
#include <iostream>

#include <climits>

// 引入资源包
using namespace std;

MyFirstDemo::MyFirstDemo(void)
{
}

MyFirstDemo::~MyFirstDemo(void)
{
}

// 声明函数原型

void my_const();
void my_float();
void my_arithmetic();

int main()
{

// 调用自定义函数
<pre name="code" class="cpp">
<span style="white-space:pre">	</span>my_const();
<span style="white-space:pre">	</span>my_float();
<span style="white-space:pre">	</span>my_arithmetic();
// 在windows平台,某些编译器会在程序执行完之后关闭窗口,这样就看不到输出// 以下代码读取下一次键击,这样就可以看到输出,按“enter” 程序继续执行cin.get();cin.get();cin.get();return 0;} // 20141126 const限定符void my_const(){// 0 、 个人觉得和Java中的final语句差不多// 1 、 常常用来定义常量,声明后需要立即初始化。编译器将不允许修改该值const int MOUTH =
10;} // 浮点数void my_float(){// 0 、 浮点数的由来// 浮点数被分为两个部分存储;一部分是数据,另一部分是对值得缩放因子。例如10.01;被分为0.1001和10两部分;// 因子控制小数点的位移,浮点数由此得名// 浮点数类型// floatfloat f = 2.2555f;// doubledouble d = 2.3333333333333;// long doublelong double ld = 2.33333333335555; // 浮点数的优缺点:可以表示证书之间的值;可以表示更大的范围;运算数度比正数慢,并且计算精度会降低;float
a = 2.34E+22f;float b = a + 1.0f;cout << "a is : " << a << endl;cout << "b is : " << b << endl;cout << "b - a is : " << b - a << endl; // 输出0}// 算数运算符void my_arithmetic(){// 0 、 + 、 - 、 * 、 / 、 % 五种// 加int int_a = 5 + 6;int int_b = 5;int int_c = 6;int int_d
= int_b + int_c;// 减float f_a = 5 - 2.01f;float f_b = 5.0;float f_c = 6.0;float f_d = f_b - f_c;// 乘int int_e = 5 * 3;float f_e = 5 * 3.0;int int_f = int_b * int_c;// 除int int_g = 5/3;float f_g = 5.0/3.0;// 模(取余)int int_h = 5%3;// 1 、 优先级*/%大于+-// 2 、 操作符重载的例子(先前提到<<的重载)//
除法符号/的重载:除法到底输出整型还是浮点型,由除数和被除数决定;两者都是整形是,输出整型,其他情况都是输出浮点数;}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ 面向对象