是我深夜爆炸,不能再去补救C了,真的来不及了,不能再三天打鱼两天晒网了,真的来不及了呜呜呜呜
我实在是不知道看什么课,那黑马吧……MOOC的北邮的C++正在进行呜呜
- #include <iostream>
- using namespace std;
- int main()
- {
- cout << "hallo world" << endl;
- system("pause");
- return 0;
- }
定义变量:数据类型 变量名称 = 变量初始值【格式】
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;
- }
标识符命名规则:非关键字,字母+数字+下划线,首位非数字,大小写
建议,最好能够见名知意
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;
- }
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;
- }
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;
- }
水平制表符——对齐、整齐输出,换页和垂直制表感觉和换行差不多
- #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");
- }
1,C风格字符串:CHAR 变量名【】=“字符串值”;CHAR A='A'字符,CHAR A[ ]="A"字符串
2,C++风格字符串:STRING 变量名=“字符串值”;;包含头文件#include
- #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");
- }
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");
- }
- #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");
- }
+,-,*,/,%【小数和小数不能做取余运算】,++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");
- }
+=,-=,*=,/=,=,%=
==,!=,<,>,<=,>=
- #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");
- }
!非【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撸了不少了,就不仔细打了】
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");
- }
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");
- }
- #include<iostream>
- using namespace std;
- int main()
- {
- int n;
- cin >> n;
- for (int i = 1; i <= n; i++)
- {
- for (int j = 1; j <= i; j++)
- {
- cout << i << "*" << j << "=" << i * j<<"\t";
- }
- cout << endl;
- }
- return 0;
- system("pause");
- }
- #include <iostream>
- #include<ctime>
- using namespace std;
- int main()
- {
- for (int i = 0; i < 10; i++)
- {
- for (int i = 0; i < 10; i++)
- {
- cout << "* ";
- if (i == 5)
- {
- break;
- }
- }
- cout << endl;
- }
- return 0;
- system("pause");
- }
- #include <iostream>
- #include<ctime>
- using namespace std;
- int main()
- {
- for (int i = 1; i < 100; i++)
- {
- if (i % 7 != 0 && i / 10 != 7 && i % 10 != 7)
- {
- continue;
- }
- cout << i << "\t拍桌子" << endl;
- }
- return 0;
- system("pause");
- }
注意:I==7时,输出【7-不是五的倍数 -下一个】
- #include <iostream>
- #include<ctime>
- using namespace std;
- int main()
- {
- cout << "1ooo" << endl;
- cout << "2pppp" << endl;
- goto FLAG;
- cout << "1ooo" << endl;
- cout << "2pppp" << endl;
- FLAG:
- cout << "23432" << endl;
- cout << "2gdfx" << endl;
- for (int i = 1; i < 21; i++)
- {
- cout << i ;
- if (i % 5 == 0)
- {
- goto DA;
- }
- cout << "不是五的倍数" << endl;
- DA:
- cout << "下一个" << endl;
-
- }
- return 0;
- system("pause");
- }