可以做外鏈的圖片網(wǎng)站成都網(wǎng)絡(luò)營銷推廣公司
是我深夜爆炸,不能再去補(bǔ)救C了,真的來不及了,不能再三天打魚兩天曬網(wǎng)了,真的來不及了嗚嗚嗚嗚
我實(shí)在是不知道看什么課,那黑馬吧……MOOC的北郵的C++正在進(jìn)行嗚嗚
#include <iostream>
using namespace std;
int main()
{cout << "hallo world" << endl;system("pause");return 0;
}
1.1? 變量
定義變量:數(shù)據(jù)類型? 變量名稱 = 變量初始值【格式】
1.2? 常量? 不可修改
1,#define 宏常量,#define 常量名? 常量值
2,const? 修飾的變量? ,const? 數(shù)據(jù)類型 常量名=常量值
#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??關(guān)鍵字
標(biāo)識(shí)符命名規(guī)則:非關(guān)鍵字,字母+數(shù)字+下劃線,首位非數(shù)字,大小寫
建議,最好能夠見名知意
二,數(shù)據(jù)類型
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? 實(shí)型(浮點(diǎn)型
float 單精度,7有效數(shù)字,數(shù)值后面加上F表示類型;double,雙。。,15-16位
默認(rèn)輸出6位小數(shù),科學(xué)計(jì)數(shù)法
#include <iostream>
using namespace std;
int main()
{float fnum1 = 3.15344534f;//加后綴自動(dòng)識(shí)別FLOAT,否則DOUBLEdouble 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? 變量名 =‘單個(gè)字符’,大小1字節(jié),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 轉(zhuǎn)義字符
水平制表符——對(duì)齊、整齊輸出,換頁和垂直制表感覺和換行差不多
#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風(fēng)格字符串:CHAR 變量名【】=“字符串值”;CHAR A='A'字符,CHAR A[ ]="A"字符串
2,C++風(fēng)格字符串: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;賦值給數(shù)字,除了0都代表真
#include <iostream>
using namespace std;
int main()
{bool flag = true;cout << flag << endl;flag = false;cout << flag << endl;cout << sizeof(bool) << endl;//1return 0;system("pause");
}
2.7 數(shù)據(jù)輸入
#include <iostream>
#include<string>
using namespace std;
int main()
{//intint ant = 23;cout << ant << endl;cin >> ant;cout << ant << endl;//floatfloat ff = 5.8900f;cout << ff << endl;//輸出抹零了cin >> ff;cout << ff << endl;//charchar ch = 'a';cout << ch << endl;cin >> ch;cout << ch << endl;//stringstring b= "qunidsefw";cout << b << endl;cin >> b;cout << b << endl;//boolbool flag = false;cout << flag << endl;//除了0,輸入啥都是1cin >> flag;cout << flag << endl;return 0;system("pause");
}
三,運(yùn)算符
3.1 算數(shù)運(yùn)算符
+,-,*,/,%【小數(shù)和小數(shù)不能做取余運(yùn)算】,++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 賦值運(yùn)算符
+=,-=,*=,/=,=,%=
3.3 比較運(yùn)算符
==,!=,<,>,<=,>=
#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 邏輯運(yùn)算符
!非【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");
}
四,程序流程結(jié)構(gòu)
順序,選擇,循環(huán)?
【C擼了不少了,就不仔細(xì)打了】
4.1 選擇結(jié)構(gòu)
1.0? IF——同C
2.0 三目運(yùn)算符:表達(dá)式?A:B,如果表達(dá)式為真,返回A,假返回B【返回的是變量,可以繼續(xù)賦值】
3.0 SWITCH——同C【結(jié)構(gòu)清晰,效率高,只能整型和字符型,BREAK】
#include <iostream>
using namespace std;
int main()
{int a = 9, b = 90;cout << (a > b ? a : b) << endl;(a > b ? a : b) = 78;//==78cout << a << endl;cout << b << endl;(a < b ? a : b) = 78;cout << a << endl;cout << b << endl;return 0;system("pause");
}
4.2 循環(huán)結(jié)構(gòu)
1.0 WHILE循環(huán)
【RAND()%100,%100表示生成隨機(jī)數(shù)的區(qū)間,0~99,0+1~99+1,rand()%100+1】
可以用BREAK退出當(dāng)前循環(huán)
2.0 DO……WHILE循環(huán)
#include <iostream>
#include<ctime>
using namespace std;
int main()
{srand((unsigned int)time(NULL));//添加隨機(jī)數(shù)種子,作用:利用當(dāng)前系統(tǒng)時(shí)間生成隨機(jī)數(shù),防止每次隨機(jī)數(shù)都一樣int num=rand() % 100 + 1;cout << "請(qǐng)猜數(shù)字" << endl;int val;cin >> val;while(num!=val){if (num > val){cout << "猜小了" << endl;}else if (num < val){cout << "猜大了" << endl;}cin >> val;}cout << "恭喜猜對(duì)了,數(shù)字就是" <<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循環(huán)
???????4.0 嵌套循環(huán)——外層執(zhí)行一次,內(nèi)層執(zhí)行一周
#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 跳轉(zhuǎn)語句
1.0 break語句,跳出循環(huán),嵌套循環(huán)中可以是跳出內(nèi)循環(huán)
2.0 continue語句,結(jié)束本次,繼續(xù)下一次循環(huán)