C++1级课时
一.c++初识
1.1 编写代码
#include
using namespace std;
int main(){
cout << "hello world" << endl;
return 0;
}
1.2 注释
#include
using namespace std;
int main(){
//int a = 6;
//cout << "a = " << a << endl;
return 0;
}
1.3 变量
#include
using namespace std;
int main(){
int a = 6;
cout << "a = " << a << endl;
return 0;
}
1.4 常量
#include
using namespace std;
#define day 7
//#define day = 7错误,不能有等于号
int main(){
//宏常量 #define
//day = 8;错误,常量不能改
cout << "day = " << day << endl;
const int mouth = 12;
// mouth = 13;错误,常量不能改
cout << "mouth = " << mouth << endl;
return 0;
}
变量可以修改,常量不能修改!
1.5 标识符
1. 字母,数字,下划线 组成
2. 不能是数字开头
3. 大小区分 如:ans和ANS不同
4. 不能用关键字
二.数据类型
2.1 整型
#include
using namespace std;
int main(){
short a = 5;//短整型
int b = 6;//整型
long c = 7;//长整型
long long d = 8;//长长整型
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
cout << "d = " << d << endl;
return 0;
}
2.2 sizeof关键字
#include
using namespace std;
int main(){
short a = 5;//短整型
int b = 6;//整型
long c = 7;//长整型
long long d = 8;//长长整型
cout << "short 占用内存空间:" << sizeof(short) << endl;
cout << "int 占用内存空间:" << sizeof(int) << endl;
cout << "long 占用内存空间:" << sizeof(long) << endl;
cout << "long long 占用内存空间:" << sizeof(long long) << endl;
/*
short 占用内存空间:2
int 占用内存空间:4
long 占用内存空间:4
long long 占用内存空间:8
short < int <= long <= long long;
*/
return 0;
}
2.3 实型
#include
using namespace std;
int main(){
float f1 = 3.1415926f;
cout << "f1=" << f1 << endl;
cout << "float 内存空间:" << endl;
double d1 = 1.68;
cout << "d1=" << d1 << endl;
cout << "double 内存空间:" << endl;
float f2 = 3e2;
cout << "f2=" << f1 << endl;
float f3 = 3e-2;
cout << "f3=" << f1 << endl;
return 0;
}
2.4.1 字符型
#include
using namespace std;
int main(){
char ch1 = 'a';
cout << "ch1=" << ch1 << endl;
cout << "char字符在内存当中的占位空间大小:" << sizeof(char) << endl;
// char ch2 = "a";//错误
// char ch3 = 'abc';//错误
char ch4 = 'A';
cout << "A的ASCII码" << (int)ch4 << endl;
char ch5 = 97;
cout << "ch5: " << ch5 << endl;
/*
a = 97
A = 65
0 = 48
*/
return 0;
}
2.4.2 字符型ASCII码
2.4.3 字符型应用
/*1.获取字符的ASCII码
#include
using namespace std;
int main(){
char ch = 'a';
int n1 = ch;
int n2 = (int)ch;
int n3 = int(ch);
cout << "n1 = " << n1 << endl;
cout << "n2 = " << n2 << endl;
cout << "n3 = " << n3 << endl;
return 0;
}
*/
/*2.整数转成字符
#include
using namespace std;
int main(){
int n = 65;
char ch1 = 'A';
char ch2 = (char)n;
char ch3 = char(n);
cout << "n1 = " << n1 << endl;
cout << "n2 = " << n2 << endl;
cout << "n3 = " << n3 << endl;
return 0;
}
*/
/*3.大小写转换/1大写转小写
#include
using namespace std;
int main(){
char ch1 = 'A';
char xx = ch1 + 32;
cout << "xx = " << xx << endl;
return 0;
}*/
/*3.大小写转换/2小写转大写
#include
using namespace std;
int main(){
char ch2 = 'a';
char dx = ch2 + 32;
cout << "dx = " << dx << endl;
return 0;
}
*/
/*4.字符移动
#include
using namespace std;
int main(){
char ch1 = 'f';
char ch2 = ch1 - 2;
cout << "ch2 = " << ch2 << endl;
char ch3 = 'd';
char ch4 = ch3 + 2;
cout << "ch4 = " << ch4 << endl;
return 0;
}
*/
2.5 转义字符
/*
\t(水平制表HT)
下一个TAB
\n(换行LF)
将当前位置移动到下一行开头
\r(回车CR)
将当前位置移动到本行开头
\b(退格BS)
将当前位置移动到前一列
\v(垂直制表VT)
\\
一个反斜线字符\
\'
一个单引号(撇号)字符'
\"
一个双引号字符"
\?()
一个问号?
*/
#include
using namespace std;
int main(){
//换行
// cout << "hello world\ngogogo!" << endl;
cout << "hello world" << endl;
cout << "gogogo!" << endl;
//水平制表符
cout << "aaaaa\thello world" << endl;
cout << "aa\thello world" << endl;
cout << "aaa\thello world" << endl;
return 0;
}
2.6 字符串型
#include
using namespace std;
int main(){
char ch = 'a';
char str1[] = "abc";
cout << str1 << endl;
string str2 = "def";
cout << str2 << endl;
return 0;
}
2.7 布尔类型bool
/*char
true 本质为1,非0即为真
false 本质为0
*/
//bool类型占1字节大小
#include
using namespace std;
int main(){
bool flag = true;//1
cout << flag << endl;
flag = false;//0
cout << flag << endl;
cout << "size of bool = " << sizeof(bool) << endl;//1
return 0;
}
2.8 数据的输入
#include
using namespace std;
int main(){
//1.整型
int a1;
cout << "请输入a的值,a = " << endl;
cin >> a1;
cout << "a = " << a1 << endl;
int a2,b2;
cout << "请输入a和b的值:" << endl;
cin >> a2 >> b2;
cout << "a = " << a2 << endl;
cout << "b = " << b2 << endl;
//2.浮点数
float f;
cout << "请输入f的值,f = " << endl;
cin >> f;
cout << "f = " << f << endl;
//3.字符型
char ch;
cout << "请输入ch的值,ch = " << endl;
cin >> ch;
cout << "ch = " << ch << endl;
//4.字符串
string str;
cout << "请输入str的值,str = " << endl;
cin >> str;
cout << "str = " << str << endl;
//5.布尔类型bool,输入的时候,不要输入true和false,应该输入1和0
bool flag;
cout << "请输入flag的值,flag = " << endl;
cin >> flag;
cout << "flag = " << flag << endl;
return 0;
}
2.9 数据类型的转换
#include
using namespace std;
int main(){
// string str = "abc";
// int a = (int)str; //报错
// cout << a << endl;
// string str = "123";
// int a = (int)str; //报错
// cout << a << endl;
// int a = 123;
// string str = (string)a; //报错
// cout << str << endl;
// float a = 123.33;
// string str = (string)a; //报错
// cout << str << endl;
float a0 = 123.33;
int x0 = (int)a0; //正确
cout << "a = " << a0 << endl;
cout << "x = " << x0 << endl;
bool a = false;
int x = (int)a; //正确
cout << "x = " << x << endl;
int x1 = 5;
bool a1 = (bool)x1; //正确
cout << "a = " << a1 << endl;
int y = 0;
bool b = (bool)y; //正确
cout << "b = " << b << endl;
float f = 3.56;
bool a4 = (bool)f; //正确
cout << "a = " << a4 << endl;
char a2 = 'a';
int x2 = (int)a2; //正确
cout << "x = " << x2 << endl;
int y1 = 65;
char b3 = y1;
cout << "b = " << b3 << endl;
int z = 97;
char c = z;
cout << "c = " << c << endl;
int a5 = 6;
int b5 = 5;
int c0 = a5/b5;
cout << "c = "<< c0 << endl;
return 0;
}
2.10 科学计数法
#include
using namespace std;
int main(){
float f1 = 3.3e-2;
cout << f1 << endl;
float f2 = 3.3e2;
cout << f2 << endl;
float a1 = 123456;
cout << "a1 = " << a1 << endl; //输出a1 = 123456(正常输出)
float a2 = 1234567;
cout << "a2 = " << a2 << endl; //输出a2 = 1.23457e+006(科学计数法输出)
return 0;
}
2.11 unsigned关键字
#include
using namespace std;
int main(){
unsigned a;
a = 1;
cout << "a = " << a << endl;
signed int b;
b = -1;
cout << "b = " << b << endl;
return 0;
}
---------------------------------------------------------------------------------------------------------------------------------
1 |
2 |
3 |
正确 |
正确 |
正确 |
作业
2-1 a+b的和
代码:1.数据结构\1-1 a+b的和.cpp
实现:1.数据结构\1-1 a+b的和.exe
2-2 将int a 转换成 char str
代码:1.数据结构\1-2 将int a 转换成 char str.cpp
实现:1.数据结构\1-2 将int a 转换成 char str.exe
2-3 大小写转换(大转小)
代码:1.数据结构\1-3 大小写转换(大转小).cpp
实现:1.数据结构\1-3 大小写转换(大转小).exe
三.运算符
3.1 算数运算符
3.1.1 示例1-示例2
#include
using namespace std;
int main(){
int a = 10,b = 3;
cout << "a+b = " << a+b << endl;
cout << "a-b = " << a-b << endl;
cout << "a*b = " << a*b << endl;
cout << "a/b = " << a/b << endl;
cout << "a/b = " << (float)a/(float)b << endl;
int a1 = 10,b1 = 3;
cout << "a1 % b1 = " << a1 % b1 << endl;
int a2 = 3;b2 = 10;
cout << "a2 % b2 = " << a2 % b2 << endl;
//只有整型变量可以进行取模
return 0;
}
3.1.2 示例3
#include
using namespace std;
int main(){
//前置递增
int a1 = 10;
++a1;
cout << "a1 = " << a1 << endl;
//后置递增
int a2 = 10;
a2++;
cout << "a2 = " << a2 << endl;
//前置递增与后置递增的区别
int a3 = 10;
int b3 = ++a3 * 10;
cout << "a3 = " << a3 << endl;
cout << "b3 = " << b3 << endl;
int a4 = 10;
int b4 = a4++ * 10;
cout << "a4 = " << a4 << endl;
cout << "b4 = " << b4 << endl;
/*
前置递增:先递增,再参与运算
后置递增:先参与运算,再递增
*/
}
3.2 赋值运算符
#include
using namespace std;
int main(){
//赋值
int a = 10,b = 20;
a = b;
cout << "(赋值)a = " << a << endl;
cout << "(赋值)b = " << b << endl;
//+=
int a1 = 10,b1 = 20;
a1 += b1;
cout << "(+=)a = " << a1 << endl;
cout << "(+=)b = " << b1 << endl;
//-=
int a2 = 20,b2 = 10;
a2 -= b2;
cout << "(-=)a = " << a2 << endl;
cout << "(-=)b = " << b2 << endl;
return 0;
}
3.3 比较运算符
#include
using namespace std;
int main(){
int a = 4,b = 3;
//== 等于
cout << (a == b) << endl;
//!= 不等于
cout << (a != b) << endl;
//> 大于
cout << (a > b) << endl;
//< 小于
cout << (a < b) << endl;
//>= 大于等于
cout << (a >= b) << endl;
//<= 小于等于
cout << (a <= b) << endl;
/*
真 '1'
假 '0'
*/
return 0;
}
3.4 逻辑运算符
#include
using namespace std;
int main(){
//!非
int a = 0;
cout << "(!非)!a = " << (!a) << endl;
//a&&b 都为真
int a1 = 0,b1 = 0;
cout << "(a&&b)a&&b = " << (a1&&b1)<< endl;
// || 或
int a2 = 1,b2 = 1;
cout << "(||)a||b= " << (a2||b2) << endl;
return 0;
}
3.5 三目运算符
#include
using namespace std;
int main(){
int a = 10;
int b = a<5?1:0;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}
3.6 拓展-格式化输入输出
3.6.1 scanf输入
#include
using namespace std;
int main(){
/*
int scanf("%d",&n);
long scanf("%ld",&n);
long long scanf("%lld",&n);
float scanf("%f",&n);
double scanf("%lf",&n);
char scanf("%c",&ch);
char数组 scanf("%s",arr);//arr表示地址,不用&
*/
return 0;
}
数据类型 |
格式化表达式 |
int |
scanf("%d",&n); |
long |
scanf("%ld",&n); |
long long |
scanf("%lld",&n); |
float |
scanf("%f",&n); |
double |
scanf("%lf",&n); |
char |
scanf("%c",&ch); |
char数组 |
scanf("%s",arr);//arr表示地址,不用& |
3.6.2 printf输出
#include
using namespace std;
int main(){
cout << "==============int :" << endl;
int a;
scanf("%d",&a);
printf("%6d",a); //加宽度,左补空格
printf("%06d",a); //加宽度,左补零
printf("%d",a);
cout << "==============long :" << endl;
long b;
scanf("%ld",&b);
printf("%ld\n",b);
cout << "==============long long :" << endl;
long long c;
scanf("%lld",&c);
printf("%lld\n",c);
cout << "==============float 默认小数位6位:" << endl;
float f1;
scanf("%f",&f1);
printf("%.3f\n",f1);
cout << "==============float 保留3位小数位:" << endl;
printf("%f\n",f1);
cout << "==============double 默认小数位6位:" << endl;
double d1;
scanf("%lf",&d1);
printf("%.f\n",d1);
cout << "==============double 保留4位小数位:" << endl;
printf("%.4f\n",d1);
cout << "==============char:" << endl;
char ch;
scanf("%c",&ch);
printf("%c\n",ch);
cout << "==============char[]:" << endl;
char str[10];
scanf("%s",&str);
printf("%s\n",str);
return 0;
}
数据类型 |
格式化表达式 |
int |
printf("%d",a); |
long |
printf("%ld",a); |
long long |
printf("%lld",a); |
float |
printf("%f",a); |
double |
printf("%.f",a); |
char |
printf("%c,ch); |
char数组 |
printf("%s",arr);//arr表示地址,不用& |
3.6.3 设置宽度与对齐
3.6.4 按八进制和十六进制输入输出
3.7 cout保留小数位
3.8 拓展-常用的数字函数
3.9 拓展-数学补充
3.10 拓展-c++万能头文件
万能头:#include |
3.11 拓展-程序示例
1.级数求和
#include
using namespace std;
int main(){
int k,n;
double sn=0.0;
scanf("%d",&k);
for(n=1;;n++){
sn+=1.0/n;
if(sn>k*1.0)
break;
}
printf("%d",n);
return 0;
}