第一节课
#include <iostream>using namespace std;int main(){cout << "hello world" << endl;//输出system("pause");return 0;}
第二节课:注释
1.单行注释 //
2.多行注释/* */
第三节课:变量
1.变量存在意义:方便我们管理内存空间
#include <iostream>using namespace std;int main(){int a = 10;cout << "a=" << a << endl;system("pause");return 0;}
第四节课:常量
1.
#include <iostream>using namespace std;#define Day 7int main(){//Day = 14; //错误,Day是常量,一旦修改就错cout << "一周有几天:" << Day << "天" << endl;const int month = 12;//month = 24;//错误,const修饰的变量也称为常量cout << "一年有几月:" << month << "月" << endl;system("pause");return 0;}
第五节课:关键字
1.定义变量和常量时不要用关键字
第六节课:标识符起名规则
第七节课:数据类型
1.数据类型的意义是:
给变量分配一个适合的内存空间
2.语法:
数据类型 变量名 = 变量初始值
3.整型:
#include <iostream>using namespace std;int main(){//短整型(-32768 ~ 32767)short num1 = 10;//整型int num2 = 100;//长整型long num3 = 1900;// 长长整型long long num4 = 1000;cout << "num1=" << num1 << endl;cout << "num2=" << num2 << endl;cout << "num3=" << num3 << endl;cout << "num4=" << num4 << endl;system("pause");return 0;}
4. sizeof关键字
#include <iostream>using namespace std;int main(){//整型:short(2) int(4) long(4) long long(4)//可以利用sizeof求数据类型占用的内存大小//语法:sizeof(数据类型/变量)short num1 = 10;cout << "short占用内存空间多大" << sizeof(short) << endl;cout << "short占用内存空间多大" << sizeof(num1) << endl;int num2 = 10;cout << "int占用内存空间多大" << sizeof(long) << endl;long num3 = 10;cout << "long占用内存空间多大" << sizeof(long) << endl;long long num4 = 10;cout << "long long占用内存空间多大" << sizeof(long long) << endl;//整型大小比较//short《int《=long 《=long longsystem("pause");return 0;}
5.实型(浮点型)
作用:用于表示小数。
#include <iostream>using namespace std;int main(){//单精度 float//双精度 double//默认情况下,输出一个小数,会显示6位小数//float后面定义数字记得加f,不然默认为doublefloat f1 = 3.14f;cout << "f1=" << f1<<endl;double d1 = 3.14;cout << "d1=" << d1 << endl;//统计float和double占用内存空间cout << "float占用多少空间= " << sizeof(float) << endl;//4字节cout << "double占用多少空间= " << sizeof(double) << endl;//8字节//科学计数法float f2 = 3e2f;//3*10^2cout << "f2=" << f2 << endl;float f3 = 3e-2f;//3*10^-2cout << "f3= " << f3 << endl;system("pause");return 0;}
6.字符型
#include <iostream>using namespace std;int main(){//字符型变量创建方式char ch = 'a';cout << ch << endl;//字符型变量所占内存大小cout << sizeof(char) << endl;//字符型变量错误//char ch2 = "b";//要用单引号//char ch3 = 'abcd'//只能一个字符//字符型变量对应的ASCII编码cout << (int)ch << endl;//a--97 A--65system("pause");return 0;}
7.转义字符
#include <iostream>using namespace std;int main(){//转义字符//换行符\ncout << "hello world \n";//等于<<endl;//反斜杠 \\cout << "\\" << endl;//水平制表\t 8个位置 作用可以整齐输出数据cout << "aaa\thello" << endl;cout << "aaaaa\thello" << endl;cout << "aaaaaaa\thello" << endl;system("pause");return 0;}
8.字符串
作用:用于表示字符
两种风格
#include <iostream>#include <string> //字符串头文件using namespace std;int main(){//C风格字符串//注意1:char 字符串名 []//注意2:等号后面要用双引号 来包含字符串char str[] = "hello";cout << str << endl;//C++风格字符串//C++要包含一个头文件string str2 = "hello";cout << str2 << endl;system("pause");return 0;}
9.布尔类型 bool
#include <iostream>using namespace std;int main(){//创建bool数据类型bool flag = true;cout << flag << endl;flag = false;cout << flag << endl;//true 1//false 0cout << sizeof(flag) << endl;system("pause");return 0;}
10.数据的输入
关键词:cin
语法:cin >> 变量
#include <iostream>#include <string>using namespace std;int main(){//整型int a = 0;cout << "请给a赋值" << endl;cin >> a;cout << "a=" <<a<< endl;//浮点型float f = 3.14f;cout << "请给f赋值" << endl;cin >> f;cout << "f=" << f << endl;//字符型char d = 'b';cout << "请给d赋值" << endl;cin >> d;cout << "d=" << d << endl;//字符串型string str = "abc";cout << "请给str赋值" << endl;cin >> str;cout << "str=" <<str<< endl;//布尔类型bool flag = true;cout << "请给flag赋值" << endl;cin >> flag; //bool类型只要不是0都是真cout << "flag=" << flag << endl;system("pause");return 0;}
第八节课 运算符
1.类型
2.算数运算符--加减乘除
#include <iostream>using namespace std;int main(){//加减乘除int a = 2;int b = 3;cout << a + b << endl;cout << a - b << endl;cout << a * b << endl;cout << a / b << endl;//两个整数相除依然是整数,小数部分自动去除//两个数相除,除数不能为零double c = 2.3;double d = 4.7;cout << c / d << endl;//两个小数可以相除system("pause");return 0;}
3.算数运算符--取余
#include <iostream>using namespace std;int main(){//取模运算的本质就是取余数int a = 10;int b = 9;cout << a % b << endl;int a1 = 2;int b1 = 5;cout << a1 % b1 << endl;int a2 = 19;int b2 = 0;//两个数相除,除数不能为零//cout << a2 % b2 << endl;//两个小数不能取余;C++硬性规定/*double c = 2.11;double d = 3.11;cout << c % d << endl;*/system("pause");return 0;}
4.算数运算符--前置递增与后置递增
#include <iostream>using namespace std;int main(){//1.前置递增int a = 3;++a;cout << "a=" << a << endl;//2.后置递增int b = 4;b++;cout << "b=" << b << endl;//3.前置和后置区别//前置是让变量先+1,然后进行表达式运算int c = 4;int d = ++c*5;cout << "c=" << c << endl;cout << "d=" << d << endl;//后置是让变量进行表达式运算,然后+1int e = 4;int f = e++*5;cout << "e=" << e << endl;cout << "f=" << f << endl;system("pause");return 0;}
4.算数运算符--前置递减与后置递减
与递增类比
5.赋值运算符
#include <iostream>using namespace std;int main(){//赋值运算符// = int a = 10;a = 100;cout << "a=" << a << endl;// +=a = 10;a += 2;//a=a+2cout << "a=" << a << endl;// -=a = 10;a -= 2;//a=a-2cout << "a=" << a << endl;// *=a = 10;a *= 2;//a=a*2cout << "a=" << a << endl;// /=a = 10;a /= 2;//a=a/2cout << "a=" << a << endl;// %=a = 10;a %= 2;//a=a%2cout << "a=" << a << endl;system("pause");return 0;}
6.比较运算符
#include <iostream>using namespace std;int main(){//比较运算符// ==int a = 100;int b = 78;cout << (a == b) << endl;// !=cout << (a != b) << endl;// >cout << (a > b) << endl;// <cout << (a < b) << endl;// <=cout << (a <= b) << endl;// >=cout << (a >= b) << endl;system("pause");return 0;}
7.逻辑运算符
#include <iostream>using namespace std;int main(){//逻辑运算符 非!int a = 10;cout << !a << endl;//在C++中除了0都为真//0cout << !!a << endl;//又为真//1//逻辑运算符 与 &&int b = 10;int c = 10;cout << (c && b) << endl;//1b = 0;c = 10;cout << (c && b) << endl;//0//逻辑运算符 或 ||int e = 1;int f = 2;cout << (e || f) << endl;//1e = 0;f = 1;cout << (e || f) << endl;//1e = 0;f = 0;cout << (e || f) << endl;//0system("pause");return 0;}
如果觉得《[黑马程序员课程记录]C++基础部分1》对你有帮助,请点赞、收藏,并留下你的观点哦!