第3章 处理数据

C++变量的命名规则

C++内置的整型--unsigned long、long、unsigned int、int、unsigned short、short、char、unsigned char、signed char、bool

C++新增的整型:unsigned long long、long long

表示各种整型的系统限制的climits文件

各种整型的数字字面值(常量)

使用const限定符来创建符号常量

C++内置的浮点类型:float、double、long double

表示各种浮点类型的系统限制的cfloat文件

各种浮点类型的数字字面值

C++的算术运算符

自动类型转换

强制类型转换

 

目录

3.1 简单变量

3.1.1 变量名

3.1.2 整型

3.1.3 整型short、int、long、long long

3.1.4 无符号类型

3.1.5 选择整型类型

3.1.6 整型字面值

3.1.7 C++如何确定常量的类型

3.1.8 char类型:字符和小整数

3.1.9 bool类型

3.2 const限定符

3.3 浮点数

3.3.1 书写浮点数

3.3.2 浮点类型

3.3.3 浮点常量

3.3.4 浮点数的优缺点

3.4 C++算数运算符

3.4.1 运算符的优先级和结合性

3.4.2 除法分支

3.4.3 求模运算符

3.4.4 类型转换

3.4.5 C++11中的auto声明

3.5 总结


3.1 简单变量

信息将存储在哪里
要存储什么值
存储何种类型的信息

3.1.1 变量名

3.1.2 整型

基本整型:
char、short、int、long、long long(C++11新增)
每个版本都有符号版本和无符号版本

3.1.3 整型short、int、long、long long

 

// limits.cpp -- some integer limits
#include <iostream>
#include <climits>              // use limits.h for older systems
int main()
{
    using namespace std;
    int n_int = INT_MAX;        // initialize n_int to max int value
    short n_short = SHRT_MAX;   // symbols defined in climits file
    long n_long = LONG_MAX;
    long long n_llong = LLONG_MAX;

    // sizeof operator yields size of type or of variable
    cout << "int is " << sizeof (int) << " bytes." << endl;
    cout << "short is " << sizeof n_short << " bytes." << endl;
    cout << "long is " << sizeof n_long << " bytes." << endl;
    cout << "long long is " << sizeof n_llong << " bytes." << endl;
    cout << endl;

    cout << "Maximum values:" << endl;
    cout << "int: " << n_int << endl;
    cout << "short: " << n_short << endl;
    cout << "long: " << n_long << endl;
    cout << "long long: " << n_llong << endl << endl;

    cout << "Minimum int value = " << INT_MIN << endl;
    cout << "Bits per byte = " << CHAR_BIT << endl;
	// cin.get();
    return 0;
}
[wlsh@wlsh-MacbookPro] chapter_3$ g++ limits.cpp 
[wlsh@wlsh-MacbookPro] chapter_3$ ./a.out 
int is 4 bytes.
short is 2 bytes.
long is 8 bytes.
long long is 8 bytes.

Maximum values:
int: 2147483647
short: 32767
long: 9223372036854775807
long long: 9223372036854775807

Minimum int value = -2147483648
Bits per byte = 8

C++98 
    int a = {12};
C++11
    int a{12};

3.1.4 无符号类型

// exceed.cpp -- exceeding some integer limits
#include <iostream>
#define ZERO 0      // makes ZERO symbol for 0 value
#include <climits>  // defines INT_MAX as largest int value
int main()
{
    using namespace std;
    short sam = SHRT_MAX;     // initialize a variable to max value
    unsigned short sue = sam;// okay if variable sam already defined

    cout << "Sam has " << sam << " dollars and Sue has " << sue;
    cout << " dollars deposited." << endl
         << "Add $1 to each account." << endl << "Now ";
    sam = sam + 1;
    sue = sue + 1; 
    cout << "Sam has " << sam << " dollars and Sue has " << sue;
    cout << " dollars deposited.\nPoor Sam!" << endl;
    sam = ZERO;
    sue = ZERO;
    cout << "Sam has " << sam << " dollars and Sue has " << sue;
    cout << " dollars deposited." << endl;
    cout << "Take $1 from each account." << endl << "Now ";
    sam = sam - 1;
    sue = sue - 1;
    cout << "Sam has " << sam << " dollars and Sue has " << sue;
    cout << " dollars deposited." << endl << "Lucky Sue!" << endl;
	// cin.get();
    return 0; 
}
[wlsh@wlsh-MacbookPro] chapter_3$ g++ exceed.cpp 
[wlsh@wlsh-MacbookPro] chapter_3$ ./a.out 
Sam has 32767 dollars and Sue has 32767 dollars deposited.

Add $1 to each account.
Now Sam has -32768 dollars and Sue has 32768 dollars deposited.
Poor Sam!
Sam has 0 dollars and Sue has 0 dollars deposited.
Take $1 from each account.
Now Sam has -1 dollars and Sue has 65535 dollars deposited.
Lucky Sue!

short sam     
    32767  +1  -32768
unsighed short sue
        0  -1  65535

3.1.5 选择整型类型

自然长度是计算机处理起来效率最高的长度
int 被设置为自然长度

3.1.6 整型字面值

C++以三种不同的计数方式来书写整数
基数为10、基数为8、基数为16

C++使用前一位来标识数字常量的基数
第一位 1 ~ 9
第一位 0
前两位 0x 0X

仅仅是为了表达上方便,都以相同的方式存储在计算机中--被存储位二进制数(以2位基数)
// hexoct1.cpp -- shows hex and octal literals
#include <iostream>
int main()
{
    using namespace std;
    int chest = 42;     // decimal integer literal
    int waist = 0x42;   // hexadecimal integer literal
    int inseam = 042;   // octal integer literal

    cout << "Monsieur cuts a striking figure!\n";
    cout << "chest = " << chest << " (42 in decimal)\n";
    cout << "waist = " << waist << " (0x42 in hex)\n";
    cout << "inseam = " << inseam << " (042 in octal)\n";
	// cin.get();
    return 0; 
}
[wlsh@wlsh-MacbookPro] chapter_3$ g++ hexoct1.cpp 
[wlsh@wlsh-MacbookPro] chapter_3$ ./a.out 
Monsieur cuts a striking figure!
chest = 42 (42 in decimal)
waist = 66 (0x42 in hex)
inseam = 34 (042 in octal)
// hexoct2.cpp -- display values in hex and octal
#include <iostream>
using namespace std;
int main()
{
    using namespace std;
    int chest = 42;
    int waist = 42; 
    int inseam = 42;

    cout << "Monsieur cuts a striking figure!"  << endl;
    cout << "chest = " << chest << " (decimal for 42)" << endl;
    cout << hex;      // manipulator for changing number base
    cout << "waist = " << waist << " (hexadecimal for 42)" << endl;
    cout << oct;      // manipulator for changing number base
    cout << "inseam = " << inseam << " (octal for 42)" << endl;
    // cin.get();
    return 0; 
}
[wlsh@wlsh-MacbookPro] chapter_3$ g++ hexoct2.cpp 
[wlsh@wlsh-MacbookPro] chapter_3$ ./a.out 
Monsieur cuts a striking figure!
chest = 42 (decimal for 42)
waist = 2a (hexadecimal for 42)
inseam = 52 (octal for 42)
除了控制符endl
还提供控制符 dec、hex、oct
分别指 十进制 十六进制 八进制格式显示整数
不会在屏幕上显示任何内容,只是修改cout显示整数的方式,告诉cout采取何种行为,位于命令看空间std中

3.1.7 C++如何确定常量的类型

cout << "Year = " << 1492 << "\n";
默认是int

后缀用于表示类型
22022 int
22022L long
22022LU 22022 UL unsigned long
ull Ull unsigned long long

3.1.8 char类型:字符和小整数

char专门为存储字符(字母和数字)而设计的
专门的字符编码
表示计算机系统中的所有基本符号,不超过128
常用的是ASCII字符集 A=65
// chartype.cpp -- the char type
#include <iostream>
int main( )
{
    using namespace std;
    char ch;        // declare a char variable

    cout << "Enter a character: " << endl;
    cin >> ch;
    cout << "Hola! ";
    cout << "Thank you for the " << ch << " character." << endl;
    // cin.get();
    // cin.get();
    return 0;
}
[wlsh@wlsh-MacbookPro] chapter_3$ g++ chartype.cpp 
[wlsh@wlsh-MacbookPro] chapter_3$ ./a.out 
Enter a character: 
M
Hola! Thank you for the M character.
cin和cout智能的完成转换工作
// morechar.cpp -- the char type and int type contrasted
#include <iostream>
int main()
{
    using namespace std;
    char ch = 'M';       // assign ASCII code for M to ch
    int i = ch;          // store same code in an int
    cout << "The ASCII code for " << ch << " is " << i << endl;

    cout << "Add one to the character code:" << endl;
    ch = ch + 1;          // change character code in ch
    i = ch;               // save new character code in i
    cout << "The ASCII code for " << ch << " is " << i << endl;

    // using the cout.put() member function to display a char
    cout << "Displaying char ch using cout.put(ch): ";
    cout.put(ch);

    // using cout.put() to display a char constant
    cout.put('!');

    cout << endl << "Done" << endl;
	// cin.get();
    return 0;
}
[wlsh@wlsh-MacbookPro] chapter_3$ g++ morechar.cpp 
[wlsh@wlsh-MacbookPro] chapter_3$ ./a.out 
The ASCII code for M is 77
Add one to the character code:
The ASCII code for N is 78
Displaying char ch using cout.put(ch): N!
Done
C++对字符常量采用单引号、对字符串采用双引号

“值的类型将引导cout选择如何显示值”--智能对象

char ch;
cin >> ch;
//字符‘5’,转换为对应的字符编码53存储到ch中
char n;
cin >> n;
//字符‘5’,转换为对应的数字值5存储到n中

函数cout.put()是成员函数,用来输出字符,替代<<运算符
为什么?
如何把双引号表示为字符串字面值?
转义序列
\"将双引号作为常规字符,而不是字符串分隔符

cout << "Ben \"Buggsie\" Hacker\n was here";

// bondini.cpp -- using escape sequences
#include <iostream>
int main()
{
    using namespace std;
    cout << "\aOperation \"HyperHype\" is now activated!\n";
    cout << "Enter your agent code:________\b\b\b\b\b\b\b\b";
    long code;
    cin >> code;
    cout << "\aYou entered " << code << "...\n";
    cout << "\aCode verified! Proceed with Plan Z3!\n";
    // cin.get();
    // cin.get();
    return 0; 
}
[wlsh@wlsh-MacbookPro] chapter_3$ g++ bondini.cpp 
[wlsh@wlsh-MacbookPro] chapter_3$ ./a.out 
Operation "HyperHype" is now activated!
Enter your agent code:42007007
You entered 42007007...
Code verified! Proceed with Plan Z3!

3.1.9 bool类型

int ans = true;  //1
int promise = false; //0

bool start = -100;
bool stop = 0;

3.2 const限定符

处理符号常量的方法:
#define Months 12;
const int Months = 12;

const好处:
(1)明确指定类型
(2)将定义限制在特定的函数或文件中
(3)将const用于更复杂的类型

3.3 浮点数

3.3.1 书写浮点数

方法:
(1)标准小数点表示法
    8.0
(2)E表示法
    2.52e+8

d.dddE+n表示小数点向右移n位
d.dddE~n表示小数点向左移2位

3.3.2 浮点类型

float-32、double-64、long double

ostream方法self()迫使输出使用定点表示法
cout默认打印6位小数
// floatnum.cpp -- floating-point types
#include <iostream>
int main()
{
    using namespace std; 
    cout.setf(ios_base::fixed, ios_base::floatfield); // fixed-point
    float tub = 10.0 / 3.0;     // good to about 6 places
    double mint = 10.0 / 3.0;   // good to about 15 places
    const float million = 1.0e6;

    cout << "tub = " << tub;
    cout << ", a million tubs = " << million * tub;
    cout << ",\nand ten million tubs = ";
    cout << 10 * million * tub << endl;

    cout << "mint = " << mint << " and a million mints = ";
    cout << million * mint << endl;
    // cin.get();
    return 0;
}
[wlsh@wlsh-MacbookPro] chapter_3$ g++ floatnum.cpp 
[wlsh@wlsh-MacbookPro] chapter_3$ ./a.out 
tub = 3.333333, a million tubs = 3333333.250000,
and ten million tubs = 33333332.000000
mint = 3.333333 and a million mints = 3333333.333333

3.3.3 浮点常量

默认是double类型

1.234f  //float constant
1.234F  //float constant
1.234   //double constant
1.234L  //long double constant

3.3.4 浮点数的优缺点

// fltadd.cpp -- precision problems with float
#include <iostream>
int main()
{
    using namespace std;
    float a = 2.34E+22f;
    float b = a + 1.0f;

    cout << "a = " << a << endl;
    cout << "b - a = " << b - a << endl;
    // cin.get();
    return 0; 
}
[wlsh@wlsh-MacbookPro] chapter_3$ ./a.out 
a = 2.34e+22
b - a = 0
优点:整数之间的值,有缩放因子,表示的范围大
缺点:浮点运算的速度通常比整数运算慢,且精度降低

应为1确实0
小数点左边是23位数字,但只能表示前6位,修改第23位没有影响

3.4 C++算数运算符

// arith.cpp -- some C++ arithmetic
#include <iostream>
int main()
{
    using namespace std;
    float hats, heads;

    cout.setf(ios_base::fixed, ios_base::floatfield); // fixed-point
    cout << "Enter a number: ";
    cin >> hats;
    cout << "Enter another number: ";
    cin >> heads;

    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();
    // cin.get();
    return 0;
}
[wlsh@wlsh-MacbookPro] chapter_3$ g++ arith.cpp 
[wlsh@wlsh-MacbookPro] chapter_3$ ./a.out 
Enter a number: 50.25
Enter another number: 11.17
hats = 50.250000; heads = 11.170000
hats + heads = 61.419998
//C++只保证6位 61.4200
hats - heads = 39.080002
hats * heads = 561.292480
hats / heads = 4.498657

3.4.1 运算符的优先级和结合性

3.4.2 除法分支

// 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 = ";
    cout << 9.0 / 5.0 << endl;
    cout << "Mixed division: 9.0/5 = " << 9.0 / 5  << endl;
    cout << "double constants: 1e7/9.0 = ";
    cout << 1.e7 / 9.0 <<  endl;
    cout << "float constants: 1e7f/9.0f = ";
    cout << 1.e7f / 9.0f <<  endl;
    // cin.get();
    return 0;
}
[wlsh@wlsh-MacbookPro] chapter_3$ g++ divide.cpp 
[wlsh@wlsh-MacbookPro] chapter_3$ ./a.out 
Integer division: 9/5 = 1
Floating-point division: 9.0/5.0 = 1.800000
Mixed division: 9.0/5 = 1.800000
double constants: 1e7/9.0 = 1111111.111111
//默认是double,结果位double
float constants: 1e7f/9.0f = 1111111.125000
//两个都是浮点,结果为浮点

3.4.3 求模运算符

// 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;
    int stone = lbs / Lbs_per_stn;      // whole stone
    int pounds = lbs % Lbs_per_stn;     // remainder in pounds
    cout << lbs << " pounds are " << stone
         << " stone, " << pounds << " pound(s).\n";
    // cin.get();
    // cin.get();
    return 0; 
}
[wlsh@wlsh-MacbookPro] chapter_3$ g++ modulus.cpp 
[wlsh@wlsh-MacbookPro] chapter_3$ ./a.out 
Enter your weight in pounds: 181
181 pounds are 12 stone, 13 pound(s).

3.4.4 类型转换

(1)将一种算术类型的值赋给另一种算术类型的变量时,C++将对值进行转换。
(2)表达式中包含不同的类型时,C++将对值进行转换
(3)将参数传递给函数时,C++将对值进行转换

// assign.cpp -- type changes on assignment
#include <iostream>
int main()
{
    using namespace std;
    cout.setf(ios_base::fixed, ios_base::floatfield);
    float tree = 3;     // int converted to float
    int guess = 3.9832; // float converted to int
    int debt = 7.2E12;  // result not defined in C++
    cout << "tree = " << tree << endl;
    cout << "guess = " << guess << endl;
    cout << "debt = " << debt << endl;
    // cin.get();
    return 0;
}
[wlsh@wlsh-MacbookPro] chapter_3$ g++ assign.cpp 
assign.cpp:8:17: warning: implicit conversion from 'double' to 'int' changes value from 3.9832 to 3 [-Wliteral-conversion]
    int guess = 3.9832; // float converted to int
        ~~~~~   ^~~~~~
assign.cpp:9:16: warning: implicit conversion of out of range value from 'double' to 'int' is undefined [-Wliteral-conversion]
    int debt = 7.2E12;  // result not defined in C++
        ~~~~   ^~~~~~
2 warnings generated.

tree = 3.000000
guess = 3;
debt = 1634811904
在计算表达式时,C++将bool、char、unsigned char、signed char、short转换为int。这些转换称为整型提升。

传递参数时的转换C++将float提升为double

C:(long) thorn
C++:long (thorn)
想法:要让强制类型转换像是函数调用

C++的4个强制类型转换符:
static_cast<long> (thorn)
// typecast.cpp -- forcing type changes
#include <iostream>
int main()
{
    using namespace std;
    int auks, bats, coots;

    // the following statement adds the values as double,
    // then converts the result to int
    auks = 19.99 + 11.99;

    // these statements add values as int
    bats = (int) 19.99 + (int) 11.99;   // old C syntax
    coots = int (19.99) + int (11.99);  // new C++ syntax
    cout << "auks = " << auks << ", bats = " << bats;
    cout << ", coots = " << coots << endl;

    char ch = 'Z';
    cout << "The code for " << ch << " is ";    // print as char
    cout << int(ch) << endl;                    // print as int
    cout << "Yes, the code is ";
    cout << static_cast<int>(ch) << endl;       // using static_cast
   // cin.get();
    return 0; 
}
[wlsh@wlsh-MacbookPro] chapter_3$ g++ typecast.cpp 
[wlsh@wlsh-MacbookPro] chapter_3$ ./a.out 
auks = 31, bats = 30, coots = 30
The code for Z is 90
Yes, the code is 90

3.4.5 C++11中的auto声明

编译器根据初始值判断变量的类型
auto n = 100;
auto x = 1.5;
auto y = 1.3e12L;

3.5 总结

 

 

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值