是我深夜爆炸,不能再去补救C了,真的来不及了,不能再三天打鱼两天晒网了,真的来不及了呜呜呜呜
我实在是不知道看什么课,那黑马吧……MOOC的北邮的C++正在进行呜呜
#include <iostream>
using namespace std;
int main()
{
cout << "hallo world" << endl;
system("pause");
return 0;
}
1.1 变量
定义变量:数据类型 变量名称 = 变量初始值【格式】
1.2 常量 不可修改
1,#define 宏常量,#define 常量名 常量值
2,const 修饰的变量 ,const 数据类型 常量名=常量值
#include <iostream>
#define DAY 7
using namespace std;
int main()
{
const int mouth = 31;
cout << "hallo world,一周"<<DAY<<"天,大月"<<mouth << endl;
system("pause");
return 0;
}
1.3 关键字
标识符命名规则:非关键字,字母+数字+下划线,首位非数字,大小写
建议,最好能够见名知意
二,数据类型
2.1 整型
short ==2 【-32768~32767】;int ==4;long==4(分那啥);long long==8;
sizeof( )
#include <iostream>
using namespace std;
int main()
{
short num1 = 32768;
int num2 = 32768;
long num3 = 0;
long long num4 = 0;
cout << num1 << "——"<<sizeof(short)<<endl;
cout << num2 << "——" << sizeof(int) << endl;
cout << num3 << "——" << sizeof(long) << endl;
cout << num4 << "——" << sizeof(long long) << endl;
system("pause");
return 0;
}
2.2 实型(浮点型
float 单精度,7有效数字,数值后面加上F表示类型;double,双。。,15-16位
默认输出6位小数,科学计数法
#include <iostream>
using namespace std;
int main()
{
float fnum1 = 3.15344534f;//加后缀自动识别FLOAT,否则DOUBLE
double dnum2 = 4.4335363748456345234232;
float num3 = 3e2;
float num4 = 3e-2;
cout << fnum1 << "——"<<sizeof(float)<<endl;
cout << dnum2 << "——" << sizeof(double) << endl;
cout << num3 << endl;
cout << num4 << endl;
system("pause");
return 0;
}
2.3 字符型
CHAR 变量名 =‘单个字符’,大小1字节,ASCII码,a-97,A-67,0-31控制字符,32-126打印字符
#include <iostream>
using namespace std;
int main()
{
char a = 'a';
cout << a << "——"<<sizeof(char)<<endl;
cout << a << "——" << (int)a << endl;
system("pause");
return 0;
}
2.4 转义字符
水平制表符——对齐、整齐输出,换页和垂直制表感觉和换行差不多
#include <iostream>
using namespace std;
int main()
{
cout << "aaa\abbb" << endl;
cout << "aaa\bbb" << endl;
cout << "aaa\f换页bbb" << endl;
cout << "aaa\nbbb" << endl;
cout << "aaa\tbbb" << endl;
cout << "a\tbbb" << endl;
cout << "aaa\vbbb\v垂直制表" << endl;
cout << "aaa\vbbb\v垂直制表" << endl;
cout << "aaa\\bbb" << endl;
cout << "aaa\'bbb" << endl;
cout << "aaa\"bbb" << endl;
cout << "aaa\?bbb" << endl;
return 0;
system("pause");
}
2.5 字符串型
1,C风格字符串:CHAR 变量名【】=“字符串值”;CHAR A='A'字符,CHAR A[ ]="A"字符串
2,C++风格字符串:STRING 变量名=“字符串值”;;包含头文件#include <string>
#include <iostream>
#include <string>
using namespace std;
int main()
{
char a[] = "hallo word?";
string b = "ni hao,xiexie";
cout << a << endl;
cout << b << endl;
return 0;
system("pause");
}
2.6 布尔类型BOOL
true--1,false--0,sizeof(bool)==1;赋值给数字,除了0都代表真
#include <iostream>
using namespace std;
int main()
{
bool flag = true;
cout << flag << endl;
flag = false;
cout << flag << endl;
cout << sizeof(bool) << endl;//1
return 0;
system("pause");
}
2.7 数据输入
#include <iostream>
#include<string>
using namespace std;
int main()
{
//int
int ant = 23;
cout << ant << endl;
cin >> ant;
cout << ant << endl;
//float
float ff = 5.8900f;
cout << ff << endl;//输出抹零了
cin >> ff;
cout << ff << endl;
//char
char ch = 'a';
cout << ch << endl;
cin >> ch;
cout << ch << endl;
//string
string b= "qunidsefw";
cout << b << endl;
cin >> b;
cout << b << endl;
//bool
bool flag = false;
cout << flag << endl;//除了0,输入啥都是1
cin >> flag;
cout << flag << endl;
return 0;
system("pause");
}
三,运算符
3.1 算数运算符
+,-,*,/,%【小数和小数不能做取余运算】,++A,A++,--A,A--,同C
#include <iostream>
using namespace std;
int main()
{
int a = 2;
int b = a++;
int c = ++a;
cout << a << "\t" << b << "\t" << c << endl;
cout << c % a << endl;
cout << a++ * 100 << endl;
cout << ++a * 100 << endl;
return 0;
system("pause");
}
3.2 赋值运算符
+=,-=,*=,/=,=,%=
3.3 比较运算符
==,!=,<,>,<=,>=
#include <iostream>
using namespace std;
int main()
{
int a = 2;
int b = ++a;
cout << (a>b)<< endl;
cout << (a < b) << endl;
cout << (a != b) << endl;
cout << (a==b)<< endl;
cout << (a <= b) << endl;
cout << (a >= b) << endl;
return 0;
system("pause");
}
3.4 逻辑运算符
!非【BOOL里面,不是0都是真】,&&与,||或
#include <iostream>
using namespace std;
int main()
{
int a = 2; int b = 10;
cout << !a << endl;
cout << !!a << endl;
a = 2; b = 2;
cout << (a&&b)<< endl;
cout << (a || b) << endl;
a = 0; b = 3;
cout << (a && b) << endl;
cout << (a || b) << endl;
a = 0; b = 0;
cout << (a && b) << endl;
cout << (a || b) << endl;
return 0;
system("pause");
}
四,程序流程结构
顺序,选择,循环
【C撸了不少了,就不仔细打了】
4.1 选择结构
1.0 IF——同C
2.0 三目运算符:表达式?A:B,如果表达式为真,返回A,假返回B【返回的是变量,可以继续赋值】
3.0 SWITCH——同C【结构清晰,效率高,只能整型和字符型,BREAK】
#include <iostream>
using namespace std;
int main()
{
int a = 9, b = 90;
cout << (a > b ? a : b) << endl;
(a > b ? a : b) = 78;//==78
cout << a << endl;
cout << b << endl;
(a < b ? a : b) = 78;
cout << a << endl;
cout << b << endl;
return 0;
system("pause");
}
4.2 循环结构
1.0 WHILE循环
【RAND()%100,%100表示生成随机数的区间,0~99,0+1~99+1,rand()%100+1】
可以用BREAK退出当前循环
2.0 DO……WHILE循环
#include <iostream>
#include<ctime>
using namespace std;
int main()
{
srand((unsigned int)time(NULL));
//添加随机数种子,作用:利用当前系统时间生成随机数,防止每次随机数都一样
int num=rand() % 100 + 1;
cout << "请猜数字" << endl;
int val;
cin >> val;
while(num!=val)
{
if (num > val)
{
cout << "猜小了" << endl;
}
else if (num < val)
{
cout << "猜大了" << endl;
}
cin >> val;
}
cout << "恭喜猜对了,数字就是" <<val<< endl;
return 0;
system("pause");
}
#include <iostream>
#include<ctime>
using namespace std;
int main()
{
int a = 0;
do
{
cout << a << endl;
a++;
if (a == 10)break;
} while (a);
a = 0;
while (a)
{
cout << a << endl;
a++;
if (a == 10)
{
break;
}
}
return 0;
system("pause");
}
3.0 FOR循环
4.0 嵌套循环——外层执行一次,内层执行一周
#include <iostream>
#include<ctime>
using namespace std;
int main()
{
for (int i = 0; i < 10; i++)
{
for (int i = 0; i < 10; i++)
{
cout << "* ";
}
cout << endl;
}
return 0;
system("pause");
}
4.3 跳转语句
1.0 break语句,跳出循环,嵌套循环中可以是跳出内循环
2.0 continue语句,结束本次,继续下一次循环