優點
缺點
點它
點它們
build and run
檢查檔案是否變更/檢查語法是否正確
執行程式
先用學校的gmail登入
和皮卡丘一樣會電人的社師
先點自己
找到加入課程輸入課程代碼
點選現在的課程
按他
就可以點喜歡的題目練習了
點它
點選現在所用的語言
將所寫的code複製貼上到這
#include<iostream>
using namespace std;
int main(){
return 0;
}
控制輸入輸出
設定命名空間
程式的「入口點」
程式正常結束
「設定變數」就是給一個名字,儲存資料或數值,讓你之後可以用這個名字來操作。也就是你給資料取的「暱稱」。
| int | float | double | char | string | bool |
|---|---|---|---|---|---|
| 整數 |
浮點數 (7位以內小數) |
浮點數(16位以內小數) |
字元 (單個字母符號或數字) |
字串 (一個以上的字母符號或數字) |
布林值 (判斷True(1)/ False(0)) |
告訴程式要用甚麼型態儲存變數
_int age =15;
int year = 2025 ;float hight = 180.5 ;
float weight = 50.0 ;char ans = 'A' ;
char grade = 'b' ;bool isFinished = false;
bool isAdult = true;char 序列)cin >> 用來存取輸入內容的變數 ;cin : 從鍵盤讀取(輸入)
int a , b;
cin >> a >> b ;float a , b;
cin >> a >> b ;依照想要輸入內容的類別先宣告欲儲存輸入的變數
整數
浮點數
char a , b;
cin >> a >> b ;字元
string a , b;
cin >> a >> b ;字串
cout << 變數/數字 ;
cout<< " 字串 " ;cout : 在屏幕上印出你想要列印的內容
cout << 520 ; //520
cout << 1314 ; //1314int a=10;
char b='A';
cout<<a<<b; //10A在cout<<後放欲想輸出的內容
整數
變數
cout<<"賺大錢"; //賺大錢文字
cout<<endl;
cout<<520<<endl<<1314;換行
+ |
加法 | 3 + 2 |
5 |
- |
減法 | 5 - 3 |
2 |
* |
乘法 | 4 * 2 |
8 |
/ |
除法(浮點) | 7 / 2 |
3.5 |
/ |
整除(取整) | ||
% |
取餘數 | 7 % 2 |
1 |
** |
(冪次) | 2 3 |
8 |
== |
是否相等 | 5 == 5 |
|
!= |
是否不相等 | 5 != 3 |
|
> |
是否大於 | 7 > 5 |
|
< |
是否小於 | 3 < 5 |
|
>= |
是否大於等於 | 5 >= 5 |
|
<= |
是否小於等於 | 3 <= 5 |
#include<cmath>提供了執行各各種數學運算的函數,包括基本
算術、三角函數、指數、對數和取整函數等
pow ( 底數 , 次方 ) ;#include<iostream>
#include<cmath>
using namespace std;
int main(){
pow(2,3); //8
pow(4,0.5); //2
return 0;
}sqrt ( 底數 ) ;#include<iostream>
#include<cmath>
using namespace std;
int main(){
sqrt(4); //2=pow(4,0.5)
return 0;
}if (條件){
//要執行的程式碼 ;
}
else if (條件){
//要執行的程式碼;
}
如果不符合if的條件但符合這裡的條件,就會執行
else{
//要執行的程式碼 ;
}
其他(不符合上面所有if和elif的條件)
///假設都符合條件
if(true){ //流麻
cout<<"買!";
}
if(true){ //徽章
cout<<"買!";
}
if (true){ //拍立得
cout<<"買!";
}
if(true){ //金屬胸章
cout<<"買!";
}if(true){ ///假設都符合條件
cout<<"you") ;
}
else if(true){ //薄巧
cout<<"薄巧";
}比起薄巧我更喜歡你
if:還有錢
else if:如果前面買了現在這個就沒錢買了
int x=5,y=10;
if (y==10){
cout<<"a";
}
if (x==5){
cout<<"b";
}int x=5,y=10;
if (y==10){
cout<<"a";
}
elif(x==5){
cout<<"b";
}a
b
a
int x=5,y=10;
if (x==5 && y!=10){
cout<<"a";
}int x=5,y=10;
if(x==5 || y!=10){
cout<<"a";
}for (初始化;條件化;變化){
//要重複執行的code;
}for (初始化;條件化;變化){
//要重複執行的code;
}1.
2.
3.
4.
要扣$
#include<iostream>
using namespace std;
int main(){
for (int i=0;i<520;i++){
cout<<"公關姐姐我愛你!"<<endl;
}
return 0;
}當你情人節告白,不用1314行,更不用520行,只要3行,輸出幾遍我愛你都可以
#include<iostream>
using namespace std;
int main(){
for(int i=0;i<6;i++){
cout<<i<<" ";
}
return 0;
}請學妹們來想想,最後的輸出會是怎樣的呢?
#include<iostream>
using namespace std;
int main(){
for(int i=5;i<-1;i--){
cout<<i<<" ";
}
return 0;
}請學妹們來想想,最後的輸出會是怎樣的呢?
while(條件){
//要重複執行的code;
}while(條件){
//要執行的code;
}1
2
#include<iostream>
using namespace std;
int main(){
int i=0;
while(i<=5){
cout<<i<<" ";
i++;
}
return 0;
}請學妹們來想想,最後的輸出會是怎樣的呢?
#include<iostream>
using namespace std;
int main(){
for(int i=0;i<6;i++){
if i==3:
continue;
cout<<i<<" ";
}
return 0;
}#include<iostream>
using namespace std;
int main(){
for(int i=0;i<10;i++){
if i==3:
break;
cout<<i<<" ";
}
return 0;
}定義:一維陣列是一種線性陣列,其中元素的存取是以
行或列索引的單一下標表示。
如果有效的元素索引從0開始,則常數B只是陣列第一個元素的位址。因此C語言指定陣列的索引一定從0開始;許多開發人員會將該元素稱為「第零」而不是「第一」。
Terry
Hoyou
Vivian
Ktlai
#include<iostream>
#include<string>
using namespace std;
int main(){
string team[4]={"Terry","Hoyou","Vivain","Ktlai"};
cout<<team[0]<<endl;
cout<<team[1]<<endl;
cout<<team[2]<<endl;
cout<<team[3];
return 0;}#include<iostream>
using namespace std;
int main(){
宣告 陣列名稱[陣列長度]={陣列裡存放的東東};
return 0;}這個陣列想要放甚麼資料型態的物品
取名規則同變數
櫃子想要有幾格
櫃子也可以空著(默認是0)
陣列名[資料對應位置]==放在這個櫃子的資料
int a[10]={};
a[0]=1;
a[1]=3;
a[2]=12;
a[4]=4;
cout<<a[0]+a[1];Q :輸出是什麼??
A: 4
Why?????????????????????????????????????
1+3=4
有沒有覺得這樣一個一個放東西很麻煩(𖦹.𖦹)
那就用loop解決煩惱٩(๑•̀ω•́๑)۶
a[101]={};
cin>>a[0]>>a[1]>>.......>>a[100];int a[101]={};
for (int i=0;i<101;i++) {
cin>>a[i];
}是不是方便很多?!♥(。→v←。)♥
int a[10]={0,1,2,3,4,5,6,7,8,9};
for (int i=0;i<10;i++) {
cout>>a[i];
}真的很方便!OK(ゝω・´★)
有沒有覺得他跟一維陣列有點像,因為它也是陣列:字元陣列
'\0'表示字串結尾的特殊字符稱為”空字符”它的數值是 0
每個烤肉串最後都有一個籤子
#include<string>char ans = 'A' ;
char grade = 'b' ;string name = "Terry" ;
string grade = "A++" ;#include<iostream>
#include<string>
using namespace std;
int main(){
string ans;
ans='a'+'b'+'c';
cout<<ans; //abc
cout<<ans[2];//c
return 0;}可以用length() || size()知道字串長度🤓👆
#include <iostream>
#include <string>
using namespace std;
int main() {
string a = "hello";
cout << a.length(); //5
cout << a.size(); //5
return 0;}#include<iostream>
#include<string>
using namespace std;
int main(){
string a;
cin>>a;
for(int i=0;i<a.size();i++){
cout<<a[i];
}
cout<<endl;
for(int i=0;i<a.length();i++){
cout<<a[i];
}
cout<<endl;
for(int i=0;a[i]!='\0';i++){
cout<<a[i];
}
return 0;}
#include<iostream>
#include<string>
using namespace std;
int main(){
string a;
cin>>a;
for(int i=0;i<a.size();i++){
cout<<a[i]<<"Happy";
}
return 0;}
其他同理自己照題目推,不懂得來問
電腦沒辦法直接存「字」,所以必須用 數字編號 來代表。
就像是每個字符都有自己的座號,
ex:當我們叫10304,就知道在叫江O慈小朋友
叫04的後11號是叫10315的張O穎小朋友
不用特別背,電腦都記著
char a;
cin>>a;
if ('A'<=a && a<='Z') cout<<a<<"是大寫";
else if('a'<=a && a<='z') cout<<a<<"是小寫";
else cout<<a<<"不是英文字母";原理是利用他的ASCII碼大小來判斷
二維陣列使用陣列名稱與兩個索引值來指定存取陣列元素,這兩個索引值都是由 0 開始。宣告方式與一維陣列類似
何謂二維陣列於記憶體中的配置方式?其實二維陣列存取時的行與列,只是為了便於理解陣列元素索引。如果要大量儲存同一種型態、而且彼此又有密切關係的「表格式」資料,例如數學中的矩陣,這時候就應將其宣告並設定為「二維陣列」。本質是在陣列裡的陣列。
int arr[3][3]={{1,2,3},{4,5,6},{7,8,9}};int arr[3][3]={
{1,2,3},
{4,5,6},
{7,8,9}
};如果換個排版方式
是不是覺得有點像有長x寬的櫃子呢?
你只要會去蝦皮智取店領貨你就會了!
i
j
j
i
宣告 陣列名 [i] [j] = { } ;陣列要儲存什麼樣式的資料型態
取名規則同變數
直排
橫排
放資料的地方
for (int i=0;i<r;i++){
for(int j=0;j<c;j++){
cin>>arr[i][j];
}
}這節要鍛鍊你們自學能力,所以簡報比較短(其實是老人家眼睛太肝最近不能長時間用3c)
vector 是 C++ 中最常用的動態陣列容器。元素可自動擴張(不像一般陣列大小固定)
支援隨機存取
插入、刪除、搜尋都很方便
需要 #include <vector>
vector<int> v;
vector<int> v(5);
// 大小 5,每個元素預設為 0
vector<int> v(5, 10);
// 大小 5,每個值都是 10
| 操作 | 函式 | 說明 |
| ------------|-------------| ---------------|
| 新增到尾端 | push_back(x)| 加在最後 |
| 移除尾端 | pop_back() | 刪除最後一個 |
| 判斷是否為空 | empty() | true/false |
| 清空所有元素 | clear() | 全刪除 |
| 插入 | insert() | 插入到某個位置 |
| 刪除 | erase() | 刪除某位置或某區間|陣列名.函式(你要處理的元素);