1 //C++三大特性:封装,继承,多态
2
3 //C++新增的数据类型:bool型 一个字节 真 true 假 false
4
5 //case 定义变量的问题
6 int nValue = 2;
7 switch(nValue)
8 {
9 case 1:
10 {
11 printf("1\r\n");
12 break;
13 }
14 case 2:
15 {
16 //在case里定义变量要加括号
17 int n = 2;
18 printf("2\r\n");
19 break;
20 }
21 case 3:
22 {
23 printf("3\r\n");
24 break;
25 }
26 }
27
28 cout<<"Hello World"<<endl;
29 //endl = '\n' + flush 即endl的作用是插入换行符并刷新流
30
31 cout<<"Hello World";
32 //若没有加endl或者flush,则只在程序结束的时,才提交数据,并显示Hello World。
33 //"<<"的功能等价于printf函数的功能,可以理解为:"<<"重载了,printf函数的功能。
34
35 streambuf *lpBuff = cout.rdbuf(); //获取缓冲区
36
37 /*格式化输出:
38 C中:
39 %x 十六进制输出 %o 八进制输出
40 C++中:*/
41 cout<<hex<<10<<endl; //十六进制输出,会影响到后面所有的输出
42 cout<<dec<<11<<endl; //十进制输出,会影响到后面所有的输出
43 cout<<oct<<13<<endl; //八进制输出,会影响到后面所有的输出
44
45 //设置输出格式
46 cout.setf(ios::hex); //设置为十六进制格式输出
47 //...................
48 cout.unsetf(ios::hex); //恢复为原来的输出格式
49
50 //设置输出的宽度
51 cout.width(5); //设置宽度,有效一次
52 cout<<"HE"<<endl;
53
54 //setw()设置宽度的函数 在头文件 iomani.h 中
55 cout<<hex<<setw(6)<<"HE"<<endl;
56
57 //设置填充字符
58 cout.width(5);
59 char ch = cout.fill('#'); //设置填充字符,保留原来的填充字符
60 cout<<"HE"<<endl;
61 cout.fill(ch); //恢复为原来的填充字符
62
63 cout<<"0x"<<setfill('0')<<hex<<setw(6)<<234<<endl;
64
65 //设置对齐方式
66 cout.setf(ios::left); //设置为左对齐
67 //....................
68 cout.unsetf(ios::left); //还原对齐方式
69
70 //格式化为科学记数法
71 cout.setf(ios::scientific); //设置为科学记数法格式输出
72 //.....................
73 cout.unsetf(ios::scientific); //还原输出格式
74
75 cout<<setiosflags(ios::scientific)<<313.567<<setiosflags(ios::scientific)<<endl;
76
77 //设置浮点数输出的精度
78 cout.setf(ios::fixed);
79 cout.precision(6);
80 cout<<3.14f<<endl;
81
82 cout<<setiosflags(ios::fixed)<<setprecision(6)<<3.14f<<endl;
83 84
85 //防止输入溢出的方法:
86 char szBuff[5] = {0};
87
88 //1.使用getline函数
89 //getline()函数
90 cin.getline(szBuff, 4,'\n');
91
92 //2.使用read函数
93 //read()函数从输入流中读取指定的数目的字符,并放在指定的地方
94 cin.read(szBuff, 4);
95
96 //清空缓冲区的方法
97 //获取缓冲区的大小
98 int n = cin.rdbuf()->in_avail();
99 //忽略缓冲区
100 cin.ignore(n, '\n');
101
102
103
104