文章目录
3.1 limits.cpp
代码
#include <iostream>
#include <climits>
using namespace std;
int main()
{
int n_int = INT_MAX;
short n_short = SHRT_MAX;
long n_long = LONG_MAX;
long long n_llong = LLONG_MAX;
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 << "Maximun 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 << "Minimun int value = " << INT_MIN << endl;
cout << "Bits per bytes = " << CHAR_BIT << endl;
return 0;
}
结果
知识点
1.sizeof用法
2.头文件climits
3.变量初始化(声明+赋值)
3.2exceed.cpp
代码
#include <iostream>
#include <climits>
#define ZERO 0;
using namespace std;
int main()
{
short sam = SHRT_MAX;
unsigned short sue = sam;
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;
return 0;
}
结果
知识点
1.无符号类型变量的定义
2.超过变量类型的范围将导致溢出
short类型变量sam最大数值为SHRT_MAX,加上1后超出范围,结果为-32768。
unsigned short类型sue最小数值为0,减去1后超出范围,结果为65535。
3.3 hexoct.cpp
代码
#include <iostream>
using namespace std;
int main()
{
int chest = 42;
int waist = 0x42;
int inseam = 024;
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";
return 0;
}
结果
知识点
- 整数类型的十进制、十六进制、八进制的定义
- \n与endl相同功能(换行)
3.4 hexoct2.cpp
代码
#include <iostream>
using namespace std;
int main()
{
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;
cout << "waist = " << waist << "{hexadecimal for 42}" << endl;
cout << oct;
cout << "inseam = " << inseam << "{octal for 42}" << endl;
return 0;
}
结果
知识点
1.整数类型的十进制、十六进制、八进制的显示
cout << hex;//不显示在屏幕中,但修改了cout显示的方式
3.5 chartype.cpp
代码
#include <iostream>
using namespace std;
int main()
{
char ch;
cout << "Enter a character: " << endl;
cin >> ch;
cout << "Hola! ";
cout << "Thank you for the " << ch << " character." << endl;
return 0;
}
结果
知识点
1.字符型变量的声明,输入和输出。
3.6 morechar.cpp
代码
#include <iostream>
using namespace std;
int main()
{
char ch = 'M';
int i = ch;
cout << "The ASCII code for " << ch << " is " << i << endl;
cout << "Add one to the character code:" << endl;
ch = ch + 1;
i = ch;
cout << "The ASCII code for " << ch << " is " << i << endl;
cout << "Displaying char ch using cout.put(ch): ";
cout.put(ch);
cout.put('!');
cout << endl << "Done" << endl;
return 0;
}
结果
知识点
1.char类型变量的初始化
2.char类型实际上是int类型,可以对其整数的运算,如+
3.cout.put()函数输出字符
3.7 bondini.cpp
代码
#include <iostream>
using namespace std;
int main()
{
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";
return 0;
}
结果
知识点
1.转义字符的使用\a、"、\n等等
3.8 floatnum.cpp
代码
#include<iostream>
using namespace std;
int main()
{
cout.setf(ios_base::fixed, ios_base::floatfield);
float tub = 10.0 / 3.0;
double mint = 10.0 / 3.0;
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;
return 0;
}
结果
知识点
1.cout.setf()函数让cout后面显示出全部内容,一般情况下没有该函数时,多余的0不会显示出来。
2.float、double等浮点型数据的定义。
3.精度限制
3.9 fltadd.cpp
代码
#include <iostream>
using namespace std;
int main()
{
float a = 2.34E+22f;
float b = a + 1.0f;
cout << "a = " << a << endl;
cout << "b - a = " << b - a << endl;
return 0;
}
结果
知识点
1.浮点运算的速度比整型慢,且精度比整型低
b = a + 1.0f;
但是
b - a = 0
说明了精度低。
3.10 arith.cpp
代码
#include <iostream>
using namespace std;
int main()
{
float hats, heads;
cout.setf(ios_base::fixed, ios_base::floatfield);
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;
return 0;
}
结果
知识点
1.基本运算+ - * /
3.11 divide.cpp
代码
#include <iostream>
using namespace std;
int main()
{
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;
return 0;
}
结果
知识点
1.不同类型的数据、混合的数据之间的除法
2.重载:函数相同,输入不同(除法的输入可以是各种类型数据)
3.12 modulus.cpp
代码
#include <iostream>
using namespace std;
int main()
{
const int Lbs_per_stn = 14;
int lbs;
cout << "Enter your weight in pounds: ";
cin >> lbs;
int stone = lbs / Lbs_per_stn;
int pounds = lbs % Lbs_per_stn;
cout << lbs << " pounds are " << stone
<< " stone, " << pounds << " pound(s).\n";
return 0;
}
结果
知识点
1.取余%
3.13 assign.cpp
代码
#include <iostream>
using namespace std;
int main()
{
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;
return 0;
}
结果
知识点
1.类型转换
3.14 typecast.cpp
代码
#include <iostream>
using namespace std;
int main()
{
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;
return 0;
}
结果
知识点
1.强制类型转换,如int(float)
2.static_cast(ch)用法
总结
长路漫漫!!!