C++ 基本语言
// 引入 in and out stream
#include
这个语句可以引入cout 或 cin;
如:
#include <iostream>
using namespace std;
int main() {
cout << "please input your name" << endl;
cin >> name;
return 0;
}
其中 cout 指的是 c++ 语言中的 output;
cin 指的是 c++ 语言中的 input;
endl 指的是 end line;
是不是好记很多。
// 定义标准命名空间
using namespace std;
// 定义主函数
#include <iostream>
using namespace std;
int main() {
//please input your codes here
return 0;
}
此时记住初学者需要打两个花括号,不然容易忘记。
// 定义变量类型, 其中bites 指空间字节数。
整型:int (4 bites); long (8 bites); short (8 bites); long long (16 bites)
浮点型: float (4 bites); double (8 bites); long double (16 bites)
非字符: string (32 bites);
bool (1 bite) 用于定义True 或 False;
char (1 bite);
void (1 bite) void 没有类型;
// 命名规则:
开头: _ 或 a-zA-Z;
中间可以有数字,下划线;
一般用意思表达:
如: my father’s name: FatherName 或者 father_name;
// 定义常量
const int PI = 3.1415;
其中const 是 constant, 原来一般用 #define 来定义。
// 运算符(条件运算符,逻辑运算符,算数运算符)
- 条件运算符
Exp1 ? Exp2 : Exp3;
如果Exp1 为真,则执行Exp2,否则执行Exp3;
如:
#include <iostream>
using namespace std;
int main() {
// identify pass line according to gender
string gender;
float PassLine;
cout << "please input your gender:" << endl;
cin >> gender;
PassLine = (gender == "female" ? 12.5 : 9.5);
cout << "your pass line is" << PassLine << endl;
return 0;
}
-
逻辑运算符
与或非: &&; ||; !; -
算数运算符
加减:+, - ;
乘除余:*, /, % ;
次方: ^ ;
… …
今天总结就到这里了,明天继续。