C++学习(七)入门篇——C++算数运算符

 如下介绍5种C++基本运算符ios

+、-、×、/、%ide

注意/为第一个数除以第二个数,结果为商的整数部分,小数部分被丢弃函数

%求模,两个操做数必须是整型,它生成第一个数除以第二个数的余数工具

若是其中一个是负数,那么结果的符号知足:(a/b)*b+a%b=aspa

程序清单3.10 arith.cpp //arith.cpp - - some C++ arithmetic
#include<iostream>
int main() { using namespace std; float hats, heads; cout.setf(ios_base::fixed, ios_base::floatfield); cout << "Enter a number: "; cin >> hats; cout << "Enter another number:"; cin >> heads; cin.get(); cout << "hats = " << hats << ";heads = " << heads << endl; cout << "hats + heads = " << hats + heads << endl; cout << "hats - heads = " << hats - heads << endl; cout << "hats * heads = " << hats * heads << endl; cout << "hats / heads = " << hats / heads << endl; cin.get(); }

获得输出:3d

11.17+50.25=61.42,输出为61.41998,这不是运算问题,而是float表示有效位数能力有限。code

它只保证6位有效位,四舍五入成6位就是61.4200是对的,cout打印6个小数,使用double能保证更好的精度。blog

 

1.运算符优先级和结合性ci

先乘除后加减,也能够用括号来执行本身定义的优先级get

而×、/、%优先级相同,这个时候须要考虑结合性,可去附录D查看

如float logs=120/4*5;

乘除都是从左到右的,因此结果为150

仅当两个运算符被用于同一操做数时,优先级和结合性规则才有效

如int dues=20*5+24*6;

程序清单3.11 divide.cpp //divide.cpp - - integer and floating-point division
#include<iostream>
int main() { using namespace std; cout.setf(ios_base::fixed, ios_base::floatfield); cout << "Integer division: 9/5 = " << 9 / 5 << endl; cout << "Floating-point division: 9.0/5.0 = " << 9.0 / 5.0 << endl; cout << "Mixed division: 9.0/5 = " << 9.0 / 5 << endl; cout << "double constants: 1e7/9.0 = " << 1e7f / 9.0 << endl;            //1.e7f也能够
cout << "float constants: 1e7f/9.0f = " << 1e7f/ 9.0f << endl;
cin.get(); }

浮点常量在默认状况下为double类型,两操做数为float则结果为float,一个double,一个float也为double

 

运算符重载是什么意思?

好比你定义一个类型“学生”

如今若是你把两个“学生”类型的变量相加, 那系统怎么知道你要怎么加? 若是是数字的话,系统知道。

那这个时候,你就要为“学生”这个类型重载加号运算符

而后在这个重载的方法里实现相加的意义,好比“学生”类型的年龄相加,或是其它东西相加,这个就你本身定义了

 

2.求模运算符

它返回整数除法的余数,适用于解决要求将一个量分红不一样整数单元的问题

程序清单3.12 modulus.cpp //modulus.cpp - - uses % operator to convert lbs to stone
#include<iostream>
int main() { using namespace std; const int Lbs_per_stn = 14; int lbs; cout << "Enter your weight in pounds:"; cin >> lbs; cin.get(); int stone = lbs / Lbs_per_stn; int pounds = lbs%Lbs_per_stn; cout << lbs << " pounds are " << stone << " stone," << pounds << " pounds\n"; cin.get(); }

获得结果:

 

3.类型转换

C++容许将一种类型的值赋给另外一个类型的变量

so_long = thirty;

程序将thirty的值扩展为long,扩展后获得一个新值,被存储在so_long中,而thirty内容不变。值不会改变,只是占用更多的字节。

潜在的数值转换问题
将较大的浮点类型转换为较小的浮点类型,如将double转换为float 精度(有效位数)下降,值可能超出目标类型的取值范围,在这种状况下,结果将是不肯定的
将浮点类型转换为整型   小数部分丢失,原来的值可能超出目标类型的取值范围,在这种状况下,结果将是不肯定的
将较大的整型转换为较小的整型,如将long转换为short 原来的值可能超出目标类型的取值范围,一般只复制右边的字节

像一个long值,(如2111222333)赋给float精度将降低,float只有6位有效数字,四舍五入为2.11122E9

将浮点转化为整型会将数字截短,其次float相对于int太大了,float至少32位,int至少16位。

程序清单3.13 assign.cpp //init.cpp - - type changes on initialization
#include<iostream>
int main() { using namespace std; cout.setf(ios_base::fixed, ios_base::floatfield); float tree = 3; int guess(3.9832); int debt = 7.2E12; cout << "tree = " << tree << endl; cout << "guess = " << guess << endl; cout << "debt = " << debt << endl; cin.get(); }

获得输出:

int变量没法存储3.0E12,致使C++没有对结果进行定义的状况发生。

使用大括号的初始化称为列表初始化,给复杂的数据类型提供值列表。它对类型的转换要求更加严格,列表初始化不容许缩窄。即变量的类型没法表示赋给它的值。

条件是编译器知道目标变量可以正确的存储赋给它的值。

const int code=66;

int x =66;

char c1 {31325};     缩窄

char c2 = {66};

char c3 = {code};    

char c4 = {x};          x是一个变量,编译器不会跟踪从x被初始化到它被用来初始化c4

x=31325;

char c5 = x;            容许

 

自动转换:

在计算表达式时,C++将bool、char、unsigned char和short值转换为int叫作整型提高,当运算涉及两种类型时,较小的类型被转化为较大的类型。

(1)若是有一个操做数类型为long double,则将另外一个转换为long double

(2)不然,有一个类型为double,则将另外一个转化为double

(3)不然,一个类型为float,则另外一个为float

(4)不然,操做数都是整型,所以执行整型提高

(5)若是两操做数都是有符号或无符号的,且其中一个操做数比另外一个低,则转换为高的级别

(6)若是一个有符号,一个无符号,无符号操做数比有符号的高,则将有符号转化为无符号的类型

(7)若是有符号类型可表示无符号的全部可能取值,则将无符号转化为有符号操做数所属类型

(8)不然,将两个操做数都转化为有符号类型的无符号版本

 

有符号整型按级别从高到底:long long、long、int、short和signed char。无符号整型和有符号整型排列顺序相同。类型char、signed char和unsigned char相同,类型bool级别最低,wchar_t、char16_t、char32_t级别和底层类型相同。

 

强制类型转化(将thorn中的int值转化为long类型)

(long) thorn

long  (thorn)

强制类型通用格式

(typeName) value

  typeName  (value)

第一种格式来自C语言,第二种格式是C++,就像是函数调用。

 

C++还引入了四个强制类型转换符,static cast<>通用格式

static_cast<typeName> (value)

程序清单3.14 typecast.cpp //typecast.cpp - - forcing type changes
#include<iostream>
int main() { using namespace std; int auks, bats, coots; auks = 19.99 + 11.99; bats = (int)19.99 + (int)11.99; coots = int(19.99) + int(11.99); cout << "auks = " << auks << ",bats = " << bats; cout << ",coots = " << coots << endl; char ch = 'Z'; cout << "The code for " << ch << "is "; cout << int(ch) << endl; cout << "Yes,the code is "; cout << static_cast<int>(ch) << endl; cin.get(); }

结果:

 

auto这个新工具可以根据初始值的类型来推断变量的类型

auto n=100;   int

auto x=1.5;    double

auto y=1.3e12L;    long double

但也会致使问题

如要把x、y、z指定为double类型

auto x=0.0;

double y=0;

auto z=0; 可知z是int类型

相关文章
相关标签/搜索