明解C++(3-4章)

第3章 程序流的循环

乘法表

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    for (int i = 1; i <= 9; i++) {
    	for (int j = 1; j <= 9; j++) {
    		cout << setw(3) << i * j; // 需要引入iomanip 使用setw 控制符
    	}
    	cout << endl;
    }

    return 0;
}

打印10行的直角三角形

#include <iostream>

using namespace std;

int main() {
    for (int i = 1; i <= 10; i++) {
    	for (int j = 1; j <= i; j++) {
    		cout << "*"; // 需要引入iomanip 使用setw 控制符
    	}
    	cout << endl;
    }

    return 0;
}

for,while 中的break 是退出循环,continue 是停止执行本次循环的剩余语句

#include <iostream>

using namespace std;

int main() {
    for (int i = 1; i <= 10; i++) {
    	if (i % 3 == 0) continue;
    	if (i % 11 == 0)break;
    	cout << i<<endl;
    }

    return 0;
}
/*输出 
1
2
4
5
7
8
10*/

<p>明解C++(3-4章)</p>

#include <iostream>

using namespace std;

int main() {
    cout << "ABCDEFG"<<"\r123"<<endl;  //123DEFG
    return 0;
}

控制符

: io代表输入输出,manip是manipulator(操纵器)的缩写

<p>明解C++(3-4章)</p>

<p>明解C++(3-4章)</p>

  • internal 在符号位和数值的中间插入需要数量的填充字符以使串两端对齐, 并不是居中

第4章 基本数据类型

整数类型

<p>明解C++(3-4章)</p>

C++标准中整数类型的取值范围,注意char 的值依据系统可能会变化:

<p>明解C++(3-4章)</p>

climits 头文件定义了各种整数的最大值和最小值

#define 指令类似文件编辑器中的替换功能

  • 推荐使用 const 常量对象和枚举替代对象宏

字符型

字符测试函数,需要 头文件:

<p>明解C++(3-4章)</p>

#include <iostream>
#include <cctype>
#include <string>

using namespace std;

int main() {
    char s[] = "Hello!\n";
    char c;
    int len_s = strlen(s);
    for (int i = 0; i < len_s; i++) {
    	c = s[i];
    	if (isalnum(c))cout << c << " is alnum"<<endl;
    	if (isalpha(c))cout << c << " is alpha" << endl;
    	if (isprint(c))cout << c << " is print" << endl;
    	if (islower(c))cout << c << " is lower" << endl;
    	if (iscntrl(c))cout << c << " is cntrl" << endl;
    	cout << endl;
    }

    return 0;
}
/*
H is alnum
H is alpha
H is print

e is alnum
e is alpha
e is print
e is lower

l is alnum
l is alpha
l is print
l is lower

l is alnum
l is alpha
l is print
l is lower

o is alnum
o is alpha
o is print
o is lower

! is print


 is cntrl
*/

sizeof

sizeof 返回值类型为size_t, size_t 是头文件 中定义的类型,根据类型不同可能是是unsigned int的同义词:

// Definitions of common types
#ifdef _WIN64
    typedef unsigned __int64 size_t;
    typedef __int64          ptrdiff_t;
    typedef __int64          intptr_t;
#else
    typedef unsigned int     size_t;
    typedef int              ptrdiff_t;
    typedef int              intptr_t;
#endif
  • typedef 声明用来赋予类型同义词:typedef A B;
  • typeid 用来获取类型相关的各种信息,需要头文件
#include <iostream>
#include <typeinfo>

using namespace std;

int main() {
    cout << typeid('A').name() << endl;
    cout << typeid(123).name() << endl;
    cout << typeid(123U).name() << endl;
    cout << typeid(123UL).name() << endl;
    cout << typeid(123LL).name() << endl;
    return 0;
}
/*
char
int
unsigned int
unsigned long
__int64
*/

浮点型

$1.2345\times 10^9$ 中,1.2345 表示尾数,尾数的位数代表了精度;9代表指数,表示数值的大小

基于显式风格的数据类型转换

  • (类型)表达式,cast风格
  • 类型(表达式),函数风格
  • static_cast<类型> (x):把x转换为指定的类型,主要用于可以把x隐式转换为指定类型的正当类型转换,比如整形和浮点型之间的转换。
#include <iostream>
#include <typeinfo>

using namespace std;

int main() {
    int a = 7, b = 8;
    double ave;
    ave = (7 + 8) / 2;
    cout << ave << endl; //7

    ave = (double)(7 + 8) / 2;
    cout << ave << endl; //7.5

    ave = double(7 + 8) / 2;
    cout << ave << endl; //7.5
    
    ave = static_cast<double>(7 + 8) / 2;
    cout << ave << endl; //7.5

    return 0;
}

计算机并不能精确的表示小数,从0,步长为0.001计算和的结果, 按等差数列求和公式$S_n= (a_1+a_n)\times n/2=(0+1)\times 1001\2=500.5$

float:精度不够

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    float sum = 0;
    cout << fixed << setprecision(6);
    for (int i = 0; i <= 1000; i++) {
    	float x = static_cast<double> (i) / 1000;
    	cout << "x=" << x << endl;
    	sum += x;
    	cout << "sum=" << sum << endl;
    }
    return 0;
}
/*
x=0.994000
sum=494.514984
x=0.995000
sum=495.509979
x=0.996000
sum=496.505981
x=0.997000
sum=497.502991
x=0.998000
sum=498.500977
x=0.999000
sum=499.499969
x=1.000000
sum=500.499969
*/

double:

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    double sum = 0;
    cout << fixed << setprecision(6);
    for (int i = 0; i <= 1000; i++) {
    	double x = static_cast<double> (i) / 1000;
    	cout << "x=" << x << endl;
    	sum += x;
    	cout << "sum=" << sum << endl;
    }
    return 0;
}
/*
x=0.997000
sum=497.503000
x=0.998000
sum=498.501000
x=0.999000
sum=499.500000
x=1.000000
sum=500.500000
*/

枚举类型

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    //定义枚举类型, 相应的字符串不需要引号
    enum animal {Dog,Cat,Bird};
    int animal_type;

    //获取动物编号输入,保证必须为枚举类型中的值
    do {
    	cout << "请输入对应动物的编号:0 Dog, 1 Cat, 2  Bird:"<<endl;
    	cin >> animal_type;
    } while (animal_type<Dog || animal_type >Bird);
    cout << "animal_type=" << animal_type << endl;
    
    animal selected = static_cast<animal>(animal_type);

    switch (selected)
    {
    case Dog:cout << "汪汪";
    	break;
    case Cat:cout << "喵喵";
    	break;
    case Bird:cout << "啾啾";
    	break;
    default:
    	break;
    }
    

    return 0;
}
  • animal 为枚举体名字, Dog,Cat,Bird 为枚举成员,各枚举体成员被自动分配数值0,1,2,各成员都是 animal 类型而不是整数类型
  • 枚举体成员是常量,不能修改,但可以在声明时指定,如果设置了第一个成员的值,后续的值自动加1
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    //定义枚举类型, 相应的字符串不需要引号
    enum animal {Dog=1,Cat,Bird};
    int animal_type;

    //获取动物编号输入,保证必须为枚举类型中的值
    do {
    	cout << "请输入对应动物的编号:1 Dog, 2 Cat, 3  Bird:"<<endl;
    	cin >> animal_type;
    } while (animal_type<Dog || animal_type >Bird);
    cout << "animal_type=" << animal_type << endl;
    
    animal selected = static_cast<animal>(animal_type);

    switch (selected)
    {
    case Dog:cout << "汪汪";
    	break;
    case Cat:cout << "喵喵";
    	break;
    case Bird:cout << "啾啾";
    	break;
    default:
    	break;
    }	

    return 0;
}
/*
请输入对应动物的编号:1 Dog, 2 Cat, 3  Bird:
3
animal_type=3
啾啾
*/

可以随意指定值:

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    //定义枚举类型, 相应的字符串不需要引号
    enum animal {Dog=1,Cat=3,Bird=5};
    int animal_type;

    //获取动物编号输入,保证必须为枚举类型中的值
    do {
    	cout << "请输入对应动物的编号:1 Dog, 3 Cat, 5  Bird:"<<endl;
    	cin >> animal_type;
    } while (animal_type<Dog || animal_type >Bird);
    cout << "animal_type=" << animal_type << endl;
    
    animal selected = static_cast<animal>(animal_type);

    switch (selected)
    {
    case Dog:cout << "汪汪";
    	break;
    case Cat:cout << "喵喵";
    	break;
    case Bird:cout << "啾啾";
    	break;
    default:
    	break;
    }	

    return 0;
}
/*
请输入对应动物的编号:1 Dog, 3 Cat, 5  Bird:
3
animal_type=3
喵喵
*/

正文完
 0
评论(没有评论)