0% found this document useful (0 votes)
2 views

C++[第3章] 程式設計工具

Uploaded by

Cabie Wu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C++[第3章] 程式設計工具

Uploaded by

Cabie Wu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

3-3

C++ C Java JavaScript


Visual Basic Scratch Python C++
C C++

cout <<;

程式碼

1 #include <iostream>
2
3 using namespace std;
4
5 int main(){
6 cout << " !";
7
8 return 0;
9 }

執行結果

IT加油站
C++includeheader file

cout<<iostream
cout1 #include <iostream>
coutstandard librarystdnamespace
3 using namespace stdcout
std::cout

TIP
C++5 main ( ) TIP
4-3C++ 8 return 0;
4-3
)
x,y,z y=2x x=10
y=20 x=20 y=40 x

3-12

x=10 10

資料型態 變數名稱 [= 初始值];

(1) int x = 10; x 10


(2) double x = 2.5; x 2.5
(3) string x = " !"; x !

程式碼

1 #include <iostream>
2
3 using namespace std;
4
5 int main(){
6 int x = 10;
7 int y = 20;
8 x = 30;
9 cout << x << " " << y;
10
11 return 0;
12 }

54
CHAPTER 03

執行結果

30 20

IT加油站
coutcout << x;cout << "x";
x x
<<cout << x << " "
<< y;x y

)
3-2

3-2

int x = 10;
double pi = 3.1415;
('')
char c = '!';

(" ") string s = "!";


bool apple_is_red = true
10>5 true 10<5false bool apple_is_blue = false

TIP
/ /

C++short int longfloat double

TIP
C++
#include <string>

55
TIP
ËC++
iostreamcout
Output
Input
iostream cin

Enter
Hello !

程式碼
1 #include <iostream>
2 #include <string>
3
4 using namespace std;
5
6 int main() {
7 string name;
8
9 cout << "" ;
10 cin >> name;
11
12 cout << "Hello " << name << "!";
13
14 return 0;
15 }

執行結果
:
Hello !

)
+,-,×,÷
3-3

56
CHAPTER 03

3-3

- * / % +-

10*3 10/3 10 % 3 3+5


30 3.33 1 8

C++

程式碼

1 #include <iostream>
2
3 using namespace std;
4
5 int main(){
6 int x = 10 + 5; // :x15
7 int y = 20 - x; // :y5
8 int z = x * y; // :z75
9 double q = (double) x / y; // :q3
10 cout << x << " " << y << " " << z << " " << q;
11
12 return 0;
13 }

執行結果

15 5 75 3

IT加油站
Ë
C++//

57
)
3-4

3-4

x=5, y=3
> x > y true
>= x >= y true
< x < y false
<= x <= y false
== x == y false
!= x != y true

IT加油站
x=y x==y
x=y y x
x==y x y

程式碼

1 #include <iostream>
2
3 using namespace std;
4
5 int main(){
6 int x = 10;
7 int y = 20;
8 cout << (x > y) << endl; // 0 false
9 cout << (x < y) << endl; // 1 true
10 cout << (x == y) << endl; // 0 false
11
12 return 0;
13 }

執行結果
TIP
0
1 8~10
0

58
CHAPTER 03

TIP
8~10 endl end of line
coutendl
Microsoft WordEnterEnterendl

)
2

x y

程式碼

1 #include <iostream>
2
3 using namespace std;
4
5 int main(){
6 int x = 10; // x 10
7 int y = 20; // y 20
8 x = y; // yx
9 y = x; // xy
10 cout << x << " " << y << endl;
11
12 return 0;
13 }

執行結果

20 20

x y 20
y x y
y 20 x x 20
y 3-13

59
3-13

x tempx 3-14
x temp y x x
tempy

3-14

程式碼

1 #include <iostream>
2
3 using namespace std;
4
5 int main(){
6 int x = 10; // x 10
7 int y = 20; // y 20
8 int temp = x; // xtemp
9 x = y; // yx
10 y = temp; // tempy
11 cout << x << " " << y << endl;
12
13 return 0;
14 }

執行結果
20 10

60
時況報

a–b
1. a,b,c,d
c+d

a = 50, b = 30, c = 6, d = 2 2.5

2. a,b a2  + b2

a = 4, b = 3 c 5
math.h pow( )
a=4
b = pow(a, 2)
c = pow(a, 0.5) b 16.000000 c 2.000000

61

You might also like