c++ 复习
c++ 复习
面向过程编程
顺序结构
c++概述
C++ 是一种静态类型的、编译式的、通用的、大小写敏感的、不规则的编程语言,支持过程化编程、面向对象编程和泛型编程。
#include <iostream> // 头文件,预处理文件 ;输入输出流
using namespace std; // 命名空间
int main(){
cout<<"hello world!";
return 0;
}
头文件
点击查看代码
#include<assert.h>//设定插入点
#include<ctype.h>//字符处理
#include<errno.h>//定义错误码
#include<float.h>//浮点数处理
#include<fstream.h>//文件输入/输出
#include<iomanip.h>//参数化输入/输出
#include<iostream.h>//数据流输入/输出
#include<limits.h>//定义各种数据类型最值常量
#include<locale.h>//定义本地化函数
#include<math.h>//定义数学函数
#include<stdio.h>//定义输入/输出函数
#include<stdlib.h>//定义杂项函数及内存分配函数
#include<string.h>//字符串处理
#include<strstrea.h>//基于数组的输入/输出
#include<time.h>//定义关于时间的函数
#include<wchar.h>//宽字符处理及输入/输出
#include<wctype.h>//宽字符分类
#include<algorithm>//STL通用算法
#include<bitset>//STL位集容器
#include<bits/stdc++.h>//编译器GCC 4.8支持的万能头文件,基本包含所有头文件
#include<cctype> // C字符处理
#include<cerrno>//C的错误报告机制
#include<clocale>
#include<cmath>//兼容C语言数学库
#include<complex>//复数类
#include<cstdio>//C语言输入输出工具
#include<cstdlib>//C语言通用工具
#include<cstring>//C字符串
#include<ctime>//把日期和时间转换为字符串
#include<deque>//STL双端队列容器
#include<exception>//异常处理类
#include<fstream>//文件输入输出流
#include<functional>//STL定义运算函数(代替运算符)
#include<list> // STL线性列表容器
#include<map> // STL映射容器
#include<ios> // 基本输入/输出支持
#include<iosfwd> // 输入/输出系统使用的前置声明
#include<iostream> // 基本输入输出流
#include<queue> // STL队列容器
#include<set> // STL集合容器
#include<sstream> // 基于字符串的流
#include<stack> // STL堆栈容器
#include<stdexcept> // 标准异常类
#include<streambuf> // 底层输入/输出支持
#include<string> // 字符串类
#include<utility> // STL通用模板类
#include<vector> // STL动态数组容器
#include<complex.h> // 复数处理
#include<fenv.h> // 浮点环境
#include<inttypes.h> // 整数格式转换
#include<stdbool.h> // 布尔环境
#include<stdint.h> // 整型环境
#include<tgmath.h>//通用类型数学宏
标识符
C++ 标识符是用来标识变量、函数、类、模块,或任何其他用户自定义项目的名称。一个标识符以字母 A-Z 或 a-z 或下划线 _ 开始,后跟零个或多个字母、下划线和数字(0-9)。
注释
注释也就是解释程序的,提高程序的可读性,再运行的时候,注释不会被执行的。
// 这是一个单行注释
/*
* 这是
* 一个
多行
注释
*/
数据类型
整型(int)、浮点型(float、Double)、字符型(char)、布尔类型(true,false)、空类型(void)、指针类型(*)
变量
变量的定义:<数据类型> <变量名>;。
变量的命名规则:变量名必须由字母(a-z,A-Z),数字(0-9),下划线( _ )组成,且不能以数字开头,不能与关键字重名。
变量的名称形式
驼峰式 :MyName
下划线式:my_name
int a;
char b;
float c;
void d;
计算机存储单位:字节byte,比特位bit
1byte = 8 bit
千字节 KB,兆字节 MB,吉字节 GB 。进制关系 1024
变量的赋值 =
int a = 10;
运算符
算数运算符
+
-
*
/
%
++
--
关系运算符
>
<
==
!=
<=
>=
布尔运算符(逻辑运算符)
与(&&)、或(||)、非(!)
赋值运算符
=
+=
-=
*=
/=
位运算符
对二进制数
与(&), 或 (|), 非 (^), 取反(~),左移(<<) 右移(>>)
运算符的优先级
算数运算符 > 关系运算符 > 逻辑运算符 > 赋值运算符是最低的
同级别运算符,从左往右依次计算;
输入输出流
输入输出流 cin、cout
运算符 << 流插入运算符 cout >> 流提取运算符 cin
输出输出函数,格式化输出
printf()/scanf()
选择结构
条件判断 if
if (表达式) {
// 语句块
}
// 表达式可以为 比较表达式,逻辑表达式
简短式的
if(1==1)cout<<"yes!";
else 否则语句块
if(1==1){
cout<<"yes!";
}else{
cout<<"no!";
}
// 当语句块为一句时,可以简写,可以省略{},如果为多行语句不能省略
if(1==1)cout<<"yes!";
else cout<<"no!";
else if() 分支
#include <iostream>
using namespace std;
int main(){
int a = 6;
if (a == 1){
cout<<"a = 1";
} else if(a == 2){
cout<<"a = 2";
} else if(a == 3){
cout<<"a = 3";
} else{
cout<<"我不知道!";
}
}
条件嵌套
if(){
if(){
if(){
}
}
}else{
if(){
}else{
}
}
switch...case...
switch(计算表达式){
case 1:
case 2:
default : // 可选的
statement(s);
}
三元运算符
? :
#include <iostream>
using namespace std;
int main(){
int a;
a = 1==1?1:2;
cout<<a;
}
循环结构
while
while (true){
// 语句块
}
注意:条件不能永远为真,这样会造成死循环,要有一个趋向于结束的表达式; a++ ,a--
#include <iostream>
using namespace std;
int main(){
int a=1;
while (a < 10){
cout<<a;
a++;
}
}
for
for ( 初始值定义; 判断条件; 表达式 ){
statement(s);
}
#include <iostream>
using namespace std;
int main(){
for(int a = 1 ; a < 10 ;a++) {
cout<<a;
}
}
do ... while()
do
{
statement(s);
}while( condition );
#include <iostream>
using namespace std;
int main(){
int a =1 ;
do{
cout<<a;
a++;
}while(a<10);
}
循环是可以嵌套的;
break 语句
终止 loop 或 switch 语句,程序流将继续执行紧接着 loop 或 switch 的下一条语句。
continue 语句
引起循环跳过主体的剩余部分,立即重新开始测试条件。
goto 语句
将控制转移到被标记的语句。但是不建议在程序中使用 goto 语句。
函数
main()主函数,一个程序只能有一个主函数,程序在执行过程中先执行主函数。
自己定义函数的
函数的用处,将一个功能,封装起来,写成函数。
函数定义
<返回值类型> <函数名>(形式参数(类型 变量名)){
语句块
<return 返回值;>
}
int max(int a;int b){
if(a>b)return a;
else return b;
}
函数的调用
max(1,3);
// 函数名以及参数,实际参数,通过值传递,传参
函数的声明
当自定义的函数在主函数后面时,需要做函数声明;
int max(int a;int b);
当自定义函数在主函数之前,可以不做声明,是因为,程序的加载会从上往下,但是执行先执行主函数,由于写在主函数前面的自定义函数已经被程序加载了,内存已将给自定义函数开辟了一个空间,相当于告知了程序有这个内容。
编程的规范性,最好把主函数放在程序的最前面,
局部作用域与全局作用域
函数里的变量是局部作用域,只能在函数内使用,离开函数便不认识了。
局部作用域中可以调用全局作用域变量,但是局部作用域变量不能调用的;
递归调用
在函数内部,调用函数自身;这会形成死循环
int a(int b){
b+=10;
a(b);
}
二进制
进制转换
二进制、十进制、八进制、十六进制
进制间的转换
二 -> 十 ==> 按权位展开求和
十进制转二进制 除2取余
原码、补码、反码
https://www.cnblogs.com/WangZhaoWei/articles/17921897.html
面向对象编程

浙公网安备 33010602011771号