五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

第1講 C++入門及簡單的順序結構

2023-02-18 20:44 作者:踏月隨舟盡西風  | 我要投稿

編程是一種控制計算機的方式,和我們平時雙擊打開文件、關機、重啟沒有任何區(qū)別

一、軟件環(huán)境

1. 編輯軟件的安裝與使用

?????? DEV C++、VS code(不推薦)

2.作業(yè)的評測與提交

?????? http://ybt.ssoier.cn:8088/

?????? https://www.luogu.com.cn/

?????? https://www.acwing.com/

二、編寫一個簡單的C++程序——手速練習


#include <iostream> // 頭文件
using namespace std; // 名字空間
int main() // 主函數(shù)
{
	cout << "Hello World!" << endl;
 	return 0; // 程序正常結束
} ?

三、語法基礎

1. 變量的定義

常用變量類型及范圍:

(1)布爾型 bool:true == 1?/ false == 0?1B

(2)字符型 char:'a',' ','\n' 1B

(3)整型 int:-2^31 ~ +2^31 - 1? -2147483648 ~ 2147483647?4B

(4)浮點型:

????a.?單精度浮點數(shù) float:1.23,2.5,1.235e2,6 ~ 7 位有效數(shù)字?4B

????b. 雙精度浮點數(shù) double:15 ~ 16 位有效數(shù)字?8B

(5)長整型 long long:-2^63 ~ +2^63 - 1?8B

(6)長浮點型 long double:18 ~ 19 位有效數(shù)字?16B / 12B / 8B

存儲單位:1Byte = 8 bit,500 Mbps

單位進制:B KB GB TB 1024

變量必須先定義,才可以使用。不能重名。

變量定義的方式:

#include <iostream>
using namespace std;
int main()
{
	int a = 2;
	int b, c = a, d = 10 / 2;
	float e = 2.3, f = 1, g = 1.235e2;
	bool h = true, i = false;
	char j = 'a', k = 'b';
 ?
	long long l = 100000000000LL;
	return 0;
}

2. 輸入輸出

整數(shù)的輸入輸出:

#include <iostream>
using namespace std;
int main()
{
 ? ?int a, b;
 ? ?cin >> a >> b; // 輸入
 ? ?cout << a + b << endl; // 輸出
 ? ?return 0;
}

運行結果:

AC:Accepted

WA:Wrong Answer

CE:Compiler Error 編譯錯誤

TLE:Time Limit Error 超時

MLE:Memory Limit Error 超內存

SF:Segmentation Fault 數(shù)組越界

輸入輸出多個不同類型的變量:

#include <iostream>
using namespace std;
int main()
{
 ? ?int a, b;
 ? ?cin >> a >> b;
 ? ?cout << a + b << ' ' << a * b << endl;
 ? ?return 0;
}

scanf printf

#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
 ? ?int a, b;
 ? ?float c, d;
 ? ?char e, f;
 ? ?double g, h;
 ? ?long long i, j;
 ? ?scanf("%d%d", &a, &b);
 ? ?scanf("%f%f", &c, &d);
 ? ?printf("a + b = %d\na * b = %d\n", a + b, a * b);
 ? ?printf("%.2f %.3f", c, d); // 保留n位小數(shù)輸出
 ? ?scanf("%c%c", &e, &f); // 會讀入空格
 ? ?cin >> e >> f; // 不會讀入空格
 ? ?scanf("%lf%lf", &g, &h);
 ? ?printf("%lf %lf", g, h);
 ? ?scanf("%lld%lld", &i, &j);
 ? ?printf("%lld %lld", i, j);
 ? ?
 ? ?/*
 ? ?int:%d
 ? ?float:%f
 ? ?double:%lf
 ? ?char:%c
 ? ?long long:%lld
 ? ?*/
 ? ?return 0;
}

3. 表達式

整數(shù)的加減乘除四則運算:+ - * / %

/:5 / 2 = 2 隱式轉換

%:5 % 2 = 1?

? ? ?-5 % 2 = -1?

? ? ?取余數(shù),和數(shù)學中的余數(shù)定義(0≤余數(shù)≤除數(shù))不同,結果取決于前面數(shù)的正負,只能整數(shù)

#include <iostream>

using namespace std;

int main()
{
 ? ?int a = 6 + 3 * 4 / 2 - 2;

 ? ?cout << a << endl;

 ? ?int b = a * 10 + 5 / 2;

 ? ?cout << b << endl;

 ? ?cout << 23 * 56 - 78 / 3 << endl;

 ? ?return 0;
}

浮點數(shù)(小數(shù))的運算:

#include <iostream>

using namespace std;

int main()
{
 ? ?float x = 1.5, y = 3.2;

 ? ?cout << x * y << ' ' << x + y << endl;

 ? ?cout << x - y << ' ' << x / y << endl;

 ? ?return 0;
}

整型變量的自增、自減:a++ ++a a-- --a

#include <iostream>

using namespace std;

int main()
{
 ? ?int a = 1;
 ? ?int b = a ++ ;

 ? ?cout << a << ' ' << b << endl;

 ? ?int c = ++ a;

 ? ?cout << a << ' ' << c << endl;

 ? ?return 0;
}

簡寫運算:+= -= *= /= %=

變量的強制類型轉換:

#include <iostream>

using namespace std;

int main()
{
 ? ?float x = 123.12;
 ? ?int y = (int)x; // 下取整
 ? ?int a = 97;
 ? ?char c = (char)a;
 ? ?cout << x << ' ' << y << endl;
 ? ?cout << c << endl;

 ? ?char b = 'A';
 ? ?cout << (char)(b + 32) << endl;
 ? ?return 0;
}

不同變量做運算時發(fā)生隱式類型轉換:朝向精度高的類型轉換

4. 順序語句

(1) 輸出第二個整數(shù)

#include <iostream>

using namespace std;

int main()
{
 ? ?int a, b, c;
 ? ?cin >> a >> b >> c;
 ? ?cout << b << endl;
 ? ?return 0;
}

(2) 計算 (a + b) * c的值

#include <iostream>

using namespace std;

int main()
{
 ? ?int a, b, c;

 ? ?cin >> a >> b >> c;

 ? ?cout << (a + b) * c << endl;

 ? ?return 0;
}

(3) 帶余除法

#include <iostream>

using namespace std;

int main()
{
 ? ?int a, b;

 ? ?cin >> a >> b;

 ? ?int c = a / b, d = a % b;

 ? ?cout << c << ' ' << d << endl;

 ? ?return 0;
}

(4) 求反三位數(shù)

#include <iostream>

using namespace std;

int main()
{
 ? ?int n;
 ? ?cin >> n;

 ? ?int a = n % 10;
 ? ?n = n / 10;
 ? ?int b = n % 10;
 ? ?n = n / 10;
 ? ?int c = n;

 ? ?cout << a << b << c << endl;

 ? ?return 0;
}

(5) 交換兩個整數(shù)

#include <iostream>

using namespace std;

int main()
{
 ? ?int a = 3, b = 4;

 ? ?int t = a;
 ? ?a = b;
 ? ?b = t;

 ? ?cout << a << ' ' << b << endl;

 ? ?return 0;
}

(6) 輸出菱形

#include <iostream>

using namespace std;

int main()
{
 ? ?char c;

 ? ?cin >> c;

 ? ?cout << " ?" << c << endl;
 ? ?cout << " " << c << c << c << endl;
 ? ?cout << c << c << c << c << c << endl;
 ? ?cout << " " << c << c << c << endl;
 ? ?cout << " ?" << c << endl;

 ? ?return 0;
}


第1講 C++入門及簡單的順序結構的評論 (共 條)

分享到微博請遵守國家法律
三原县| 香格里拉县| 响水县| 辽宁省| 北碚区| 房产| 郓城县| 宁化县| 南汇区| 盐津县| 沁阳市| 邵阳市| 舒城县| 冷水江市| 洛阳市| 吉首市| 云霄县| 长垣县| 西畴县| 浏阳市| 海林市| 广德县| 无棣县| 饶阳县| 梓潼县| 定边县| 孝感市| 通州区| 遂宁市| 前郭尔| 平乐县| 和林格尔县| 晴隆县| 宜黄县| 长春市| 扎赉特旗| 全州县| 炎陵县| 都兰县| 溆浦县| 兰考县|