#include <iostream>
#include <ctime>
#include <cstring>
//变量有三个属性 值丶名丶存储它的地址 注意数组指针
//指针类型 占4个字节
//'\0' 结尾符
//0为false 非0为rue
//#define NULL 0
using namespace std;
void loopMethods();
void goToMethod();
void switchMethod();
void parametersDefaultMethod(int ,int b=9 );
void lambdaMethod();
void mathMethod();
void arrayMethod();
void stringMethod();
void pointerMethod();
void referenceMethod();
int& getNumber();
void timeMethod();
void outinMethod();
void structMethod();
void classMethod();
int main(){
cout << "hello world" << endl;
//loopMethods();
//goToMethod();
//switchMethod();
//parametersDefaultMethod(100);
//mathMethod();
//arrayMethod();
//stringMethod();
//pointerMethod();
//referenceMethod();
//timeMethod();
//outinMethod();
//structMethod();
classMethod();
return 0;
}
void loopMethods(){
for(short int a=0;a<3;a++){
if(a==0)
continue;
if(a==2)
break;
cout<< "早点睡" <<endl;
}
bool isTrue=true;
while(isTrue){
cout<< "早点睡哦" <<endl;
isTrue=false;
}
bool isFalse=false;
do{
cout<< "早点睡呀" <<endl;
}while(isFalse);
}
void goToMethod(){
bool isTrue=true;
CONTROL: cout<< "在用goto测试"<<endl;
if(isTrue){
isTrue=false;
goto CONTROL;
}
}
void switchMethod(){
enum Color{ BLUE,RED,GREEN};
Color red=RED;
switch(red){
case RED:
cout<<"这是个红色"<< endl;
break;
default:
cout<< "不能识别的颜色"<< endl;
}
red==RED ? red=BLUE : red=GREEN;
if(red==BLUE)
cout<<"这是个蓝色" << endl;
}
void parametersDefaultMethod(int a,int b){
//默认参数可以放在函数声明或者定义中,但只能放在二者之一
cout<< b << endl;
}
void lambdaMethod(){
//[capture] (parameters)->return_type{body}
//在Lambda表达式内可以访问当前作用域的变量,这是Lambda表达式的闭包(Closure)行为。
//与JavaScript闭包不同,C++变量传递有传值和传引用的区别。可以通过前面的[]来指定
// [] // 沒有定义任何变量。使用未定义变量会引发错误。
// [x, &y] // x以传值方式传入(默认),y以引用方式传入。
// [&] // 任何被使用到的外部变量都隐式地以引用方式加以引用。
// [=] // 任何被使用到的外部变量都隐式地以传值方式加以引用。
// [&, x] // x显式地以传值方式加以引用。其余变量以引用方式加以引用。
// [=, &z] // z显式地以引用方式加以引用。其余变量以传值方式加以引用。
//另外有一点需要注意。对于[=]或[&]的形式,lambda 表达式可以直接使用 this 指针。
//但是,对于[]的形式,如果要使用 this 指针,必须显式传入:
//[this]() { this->someFunc(); }();
}
void mathMethod(){
long time1=time(NULL);
cout<< "系统时间秒数 :"<<time1<<endl;
}
void arrayMethod(){
//一维数组名是数组中第一个元素的地址
// 获取字符串首字母的地址 得转化成int* 等非char*(因为cout会将其作为字符串来处理)
int colors[3]={1,2,3};
for(int i=0;i<sizeof(colors)/sizeof(int);++i){
cout<< colors[i] << endl;
}
int fruits[2][2]={0,1,2,3};
int* p=colors;
int o=9;
int (*pp)[2]=fruits;
int* oo =&o;
cout<< fruits <<endl;
cout<< *(fruits[0]) <<endl;
cout<< fruits+1<<endl;
cout<< &fruits <<endl;
cout<< &fruits+1<<endl;
cout<< (0x28fecc-0x28fed4) <<endl;
cout<< oo <<endl;
cout<< oo+1 <<endl;
cout<< (&oo) <<endl;
cout<< &oo +1 <<endl;
}
void stringMethod(){
string strs="hello world ! ";
char* chars="hello today! ";
cout<< strs.size()<< endl;
cout<< strlen(chars) << endl;
}
void pointerMethod(){
//不同数据类型的指针之间唯一的区别就是指针所指向的变量或者常量的数据类型是不同的。
///变量指针是可以递增,递减但是数组不能因为数组是一个常量指针
///数组指针是指向数组的指针,指针数组是一组指针
bool isTrue=true;
if(-2){
cout<< "true" << endl;
}else{
cout<< "false" <<endl;
}
cout << NULL << endl;
int l =100;
int* p=&l;
cout<<p[2]<< endl;
cout<<p<< endl;
cout<<&p[2]<< endl;
cout<<sizeof(p)<< endl;
int ll[3];
p=ll;
cout<<sizeof(ll)<< endl;
cout<<ll<< endl;
void (*pointMthod) (void );
pointMthod=loopMethods;
(*pointMthod)();
}
void referenceMethod(){
///不存在空引用。引用必须连接到一个合法的内存
///一旦引用被初始化为一个对象,就不能指向到另一个对象
///引用必须在创建时初始化
///引用更接近于const指针
///方法不要返回一个局部的引用 局部引用会被销毁当方法结束了 但是可以返回局部静态变量
int i=90;
int o=190;
int& reference_i=i;
int* pointer_i=&i;
cout<<reference_i<<endl;
reference_i=o;
cout<< reference_i<<endl;
int& reference_ii=getNumber();
cout<< reference_ii <<endl;
int* const constPointer=&i;
*constPointer=o;
}
int& getNumber(){
//int p=90;
static int i=90;
return i;
}
void timeMethod(){
time_t now = time(0);
char* nowStr=ctime(&now); /// 星期几 月 日 时:分:秒 年
cout<< nowStr <<endl;
tm* tmStruct=gmtime(&now);
char* tmStructStr = asctime(tmStruct);
cout<< tmStructStr << endl;
time_t nowTime = time(0);
cout<<"从1970年到目前经过的秒数:" << nowTime <<endl;
long long a=0;
cout<< sizeof(a) << " " << sizeof(long)<<endl;
time_t timeNow = time(0);
tm* tm_now = localtime(&timeNow);
cout<< " 年:" << 1900+tm_now->tm_year <<endl;
cout<< " 月:" << 1+tm_now->tm_mon <<endl;
cout<< " 日:" << tm_now->tm_mday <<endl;
cout<< " 小时:" << tm_now->tm_hour <<endl;
cout<< " 分钟:" << tm_now->tm_min <<endl;
cout<< " 秒数:" << tm_now->tm_sec <<endl;
char l = '大';
//cout<< l << endl;
time_t fromNow = time(0);
char tmp[20];
strftime(tmp,20,"%y-%m-%d %H:%M:%S",localtime(&fromNow)); /// 格式化日期和时间为指定的格式
cout<< tmp<<endl;
}
void outinMethod(){
char* name;
cout<< "请输入你的名字:" << endl;
cin>>name;
cout<<"你的名字是: "<< name <<endl;
cerr<< "无解。。" <<endl;
clog << "厉害了约" <<endl;
}
void structMethod(){
struct Money{
int oneRMB;
int fiveRMB;
int tenRMB;
int fiftyRMB;
}myMoney;
myMoney.oneRMB=1;
clog<< "myMoney.oneRMB : "<< myMoney.oneRMB<<endl;
struct Money* moneyPointer;
Money jkl;
jkl.oneRMB=10;
clog<< jkl.oneRMB<<endl;
moneyPointer = &myMoney;
cout<<moneyPointer->oneRMB<<endl;
}
void classMethod(){
}
#include <iostream>
#include <limits>
//宏定义
#define WESTALGO_DUALCAMERA
//命名空间的使用
using namespace std;
//其他
extern int p=0;
int getInt();
int main(){
#ifdef WESTALGO_DUALCAMERA
for (int i=0;i<3;++i){
cout << "22m"<< endl;
}
#endif // WESTALGO_DUALCAMERA
cout << numeric_limits<float>::min() << endl;
cout << numeric_limits<float>::max() << endl;
char l=78;
double j= -0.69;
float g =-0.8f;
cout << j<< endl;
cout << g<< endl;
typedef int zint;
zint aa=89;
enum color {red , green , blue };
color mColor=red;
cout << mColor << endl;
mColor=green;
cout << mColor << endl;
mColor=blue;
cout << mColor << endl;
// 单精度 一位符号位,8位指数位,23小数位 1+8+23=32=4*8
//双精度 一位符号位,11 位指数位,52位小数位 1+11+52=64=8*8
int ab,bb,vv;
char ll; //初始化默认是‘\0’ pointer 是NULL
int p=90;
getInt();
wchar_t i =L'U';
//常量变量 有#define 和 const
signed short int a=32769;
unsigned short int b=32769;
cout << a<< endl;
cout << b<< endl;
register int qwe=90;
cout<< sizeof(aa)<<endl;
//printf("%u",10);
//mutable
//thread_local int yy=90;
// & 与,同时为1才是1,其余为0
// | 或,只要有个为1就是1,其余为0
// ^异或,两次异或同一个数等于他本身,不同为1,其余为0
// << >> 左乘右除(2的基数) 左移是在最低位真加要移动的位数,右移是去掉从最低位开始的要移动的位数
int ty=60;
cout << (ty>>3)<< endl;
cout << &ty << endl;
bool isTrue=true;
// a : cout << &ty << endl;
if(isTrue){
isTrue=false;
// goto 1;
}
for(;;){} // 当条件没有的时候默认为rue
return 0;
}
int getInt(){
return p;
}
class X{
//static string a;
};
void play(){
//X::a="tt";
}