编译型 C++
编译型: 先编译在执行,
解释语言 python 和 R 是逐行解释输出的
.cpp结尾的文件
#include <iostream> // 头文件 相当于 python 的import
using namespace std; // 命名空间 (标准)
// 主函数
int main(){
cout<< "hello hu yu hang" <<endl;
}
c++ 声明变量
变量类型 变量名 = 变量值;
int a = b
float b = 99.9
/ 是整除
int main(){
cout<< "hello hu yu hang" <<endl;
int a = 10;
int b = 4;
float c = a/b;
cout<< c << endl;
}
#include <iostream>
using namespace std;
int main (){
// 前置++
int a = 100;
++a;
cout<< a <<endl;
// 后置++
int b = 100;
b++;
cout<< b <<endl;
int c = ++a * 10 ;
// int c = a++ * 10 ;
cout<< "c = "<< c <<endl;
cout<< "a = "<< a <<endl;
}
三目
#include <iostream>
using namespace std;
int main (){
int a = 100;
int b = 200;
int c = 300;
int max ;
if (a>b){
max = a;
} else{
max = b;
}
int d = a > b ? a: b;
cout<< d <<endl;
cout<< max <<endl;
cout <<"san = "<<((a > b ? a : b) > c? (a > b ? a : b) :c)<<endl;
return 0;
}
while和for循环
#include <iostream>
using namespace std;
int main (){
// 1 ~ 100 奇数累加和
int i = 1;
int sum1 = 0;
int sum2 = 0;
while (i <= 100){
if (i%2==1){
sum1 += i;
}
i++;
}
cout<< "sum1 = "<< sum1 <<endl;
// for
for (int j = 1; j <= 100 ; j++) {
if (j%2==0)
sum2 += j;
}
cout<< "sum2 = "<< sum2 <<endl;
return 0;
}