Blame
d84748 | Qwas | 2024-11-17 13:36:49 | 1 | # c++入门 |
2 | ||||
3 | - `endl` 用于向流的末尾部位加入换行符 |
|||
4 | - `cout` 代表显示器 |
|||
5 | - `cout << x` 操作就相当于把x的值输出到显示器 |
|||
6 | ||||
7 | ||||
8 | ## 输入 Hello World |
|||
9 | ||||
10 | ```cpp |
|||
11 | # include<iostream> |
|||
12 | using namespace std; |
|||
13 | int main() { |
|||
14 | cout <<"Hello World!\n"; |
|||
15 | } |
|||
16 | ``` |
|||
17 | ||||
18 | ## 输入输出 |
|||
19 | ||||
20 | ```cpp |
|||
21 | #include <iostream> |
|||
22 | ||||
23 | using namespace std; |
|||
24 | ||||
25 | int main() { |
|||
26 | int a, b, result; |
|||
27 | cout << "please input two number:\n"; |
|||
28 | cin >> a >> b; |
|||
29 | result = 3 * a - 2 * b + 1; |
|||
30 | cout << "result is " << result << endl; |
|||
31 | } |
|||
32 | ``` |
|||
33 |  |
|||
34 | ## 使用函数 |
|||
35 | ||||
36 | ```cpp |
|||
37 | #include<iostream> |
|||
38 | #include<cmath> |
|||
39 | using namespace std; |
|||
40 | ||||
41 | double max(double x, double y); |
|||
42 | ||||
43 | int main() { |
|||
44 | double a, b, c; |
|||
45 | cout << "input two number:\n"; |
|||
46 | cin >> a >> b; |
|||
47 | c = max(a, b); |
|||
48 | cout << "maximum=" << c << "\n"; |
|||
49 | cout << "the squart of maximum=" << sqrt(c); |
|||
50 | } |
|||
51 | ||||
52 | double max(double x, double y) { |
|||
53 | if (x > y) |
|||
54 | return x; |
|||
55 | else |
|||
56 | return y; |
|||
57 | } |
|||
58 | ||||
59 | ``` |
|||
60 |  |
|||
61 | ||||
62 | ## 函数定义包含函数声明 |
|||
63 | ||||
64 | ```cpp |
|||
65 | #include<iostream> |
|||
66 | #include<cmath> |
|||
67 | using namespace std; |
|||
68 | ||||
69 | double max(double x, double y) { // 既是函数定义又是函数声明 |
|||
70 | if (x > y) |
|||
71 | return x; |
|||
72 | else |
|||
73 | return y; |
|||
74 | } |
|||
75 | ||||
76 | int main() { |
|||
77 | double a, b, c; |
|||
78 | cout << "input two numbers:\n"; |
|||
79 | cin >> a >> b; |
|||
80 | c = max(a, b); |
|||
81 | cout << "the squart of maximum=" << sqrt(c); |
|||
82 | } |
|||
83 | ``` |