//程序控制结构与函数
// switch/case struct
//
#i nclude "stdafx.h"
#i nclude "iostream"
#i nclude "string"
using namespace std;
enum member_type
{
manager, //经理
normal, //普通员工
};
int get_money(member_type type)
{
int ir=0;
switch(type)
{
case manager: //case后必须是整数型常量类型
ir=1000;
cout<<ir<<endl;
break;
case normal:
ir=500;
cout<<ir<<endl;
break;
default:
ir=100;
cout<<ir<<endl;
}
return ir;
}
void main()
{
get_money(normal);
}
=======
// if-else if-else struct
//
#i nclude "stdafx.h"
#i nclude "iostream"
#i nclude "string"
using namespace std;
enum member_type
{
manager, //经理
normal, //普通员工
};
int get_money(member_type type)
{
int ir=0;
if (type==manager)
{
cout<<1000<<endl;
}
else if (type==normal)
{
cout<<500<<endl;
}
else
{
cout<<100<<endl;
}
return ir;
}
void main()
{
get_money(normal);
}
==========
// for struct
//
#i nclude "stdafx.h"
#i nclude "iostream"
#i nclude "string"
using namespace std;
int get()
{
int ir=0;
for(int i=1;i<=100;i++)
{
ir+=i;
//cout<<ir<<endl;
}
return ir;
}
void main()
{
int va_value=get();
cout<<va_value<<endl;
}
===========
// for struct
//
#i nclude "stdafx.h"
#i nclude "iostream"
#i nclude "string"
using namespace std;
int get()
{
int ir=0;
for(int i=1;i<=100;)
{
ir+=i;
i++; //可以把步进移到这里
//cout<<ir<<endl;
}
return ir;
}
void main()
{
int va_value=get();
cout<<va_value<<endl;
}
===========
// for struct
//
#i nclude "stdafx.h"
#i nclude "iostream"
#i nclude "string"
using namespace std;
void get()
{
for(int i=1;i<=100;i++)
{
}
cout<<i<<endl; //i的生命周期在for循环外部
}
void main()
{
get();
}
=========
// while struct
//
#i nclude "stdafx.h"
#i nclude "iostream"
#i nclude "string"
using namespace std;
int get()
{
int ir=0;
int i=1;
while(i<=100)
{
ir+=i;
i++;
//cout<<ir<<endl;
}
return ir;
}
void main()
{
int va_value=get();
cout<<va_value<<endl;
}
=========
// do while struct
//
#i nclude "stdafx.h"
#i nclude "iostream"
#i nclude "string"
using namespace std;
int get()
{
int ir=0;
int i=1;
do
{
ir+=i;
i++;
//cout<<ir<<endl;
}while(i<=100);
return ir;
}
void main()
{
int va_value=get();
cout<<va_value<<endl;
}
=======
// while & break struct
//
#i nclude "stdafx.h"
#i nclude "iostream"
#i nclude "string"
using namespace std;
int get()
{
int ir=0;
int i=1;
while(true)
{
ir+=i;
i++;
if(i>100)
break;
//cout<<ir<<endl;
}
return ir;
}
void main()
{
int va_value=get();
cout<<va_value<<endl;
}
===========
// while continue struct
//
#i nclude "stdafx.h"
#i nclude "iostream"
#i nclude "string"
using namespace std;
int get_count(const char* str)
{
int ir=0;
if(str)
{
while (*str)
{
char ch=*str;
cout<<ch<<endl;
str++; //先跳到下一个字母再continue否则就读b死循环了.
if(ch!='A')
continue;
ir++;
}
}
return ir;
}
void main()
{
cout<<get_count("AbcACAC")<<endl;
}
=============
// 参数传递
//
#i nclude "stdafx.h"
#i nclude "iostream"
#i nclude "string"
using namespace std;
void print(int i)
{
cout<<i<<endl;
i++; //i只在函数体中生存
}
int main()
{
int k=100;
print(k);
cout<<k<<endl;
return 0;
}
===========
// 参数传递
//
#i nclude "stdafx.h"
#i nclude "iostream"
#i nclude "string"
using namespace std;
void print(int& i) //传的是地址
{
cout<<i<<endl;
i++;
}
int main()
{
int k=100;
print(k);
cout<<k<<endl;
return 0;
}
=========
// 参数传递
//
#i nclude "stdafx.h"
#i nclude "iostream"
#i nclude "string"
using namespace std;
void print(int* i) //传指针
{
cout<<*i<<endl;
(*i)++; //指针指向加1而不是指针加1
}
int main()
{
int k=100;
print(&k);
cout<<k<<endl;
return 0;
}
=============
// 参数传递
//
#i nclude "stdafx.h"
#i nclude "iostream"
#i nclude "string"
using namespace std;
void print(int i=100) //参数可以有默认值
{
cout<<i<<endl;
}
int main()
{
int k=200;
print(200);
print();
return 0;
}