基本函数
#if 0
/*基本函数*/
#include<iostream>
using namespace std;
//add()函数的定义,其有返回值
double add(double x,double y)
{
double z;
z=x+y;
cout<<x<<"+"<<y<<"="<<z<<endl;
return(z);
}
main()
{
double a=0.5,b=1.0;
//以不同参数形式调用函数add()
cout<<"add(1.5,2.5)="<<add(1.5,2.5)<<endl;
cout<<"add(a,b)="<<add(a,b)<<endl;
cout<<"add(2*a,a+b)="<<add(2*a,a+b)<<endl;
cout<<"----------------------"<<endl;
//以表达式方式调用函数add()
double c=2*add(a,b);
cout<<"c="<<c<<endl;
cout<<"----------------------"<<endl;
//以语句式方式调用函数add()
add(2*a,b);
cout<<"----------------------"<<endl;
//用其他类型参数调用函数add()
int n=1,m=2;
cout<<"add("<<n<<","<<m<<")="<<add(n,m)<<endl;
}
#endif
符号函数
#if 0
/*符号函数*/
#include<iostream>
using namespace std;
//定义符号函数sgn(),其返回值为int类型
int sgn(double x)
{
if (x>0) return(1); //返回出口1
if (x<0) return(-1); //返回出口2
return(0); //返回出口3
}
//main()函数定义
main()
{
double x;
int i;
for (i=0;i<3;i++) {
cout<<"x=";
cin>>x;
cout<<"sgn("<<x<<")="<<sgn(x)<<endl;
}
}
#endif
函数声明于内部
#if 0
#include<iostream>
using namespace std;
//float max(float,float);函数原型语句可以在这里
//定义main()函数
main()
{
//max()函数原型声明语句
float max(float,float);
//变量声明语句
float a,b,Max;
//输入参数并计算
cout<<"a=";
cin>>a;
cout<<"b=";
cin>>b;
Max=max(a,b); //调用max()函数
cout<<"max("<<a<<","<<b<<")="<<Max<<endl;
}
//定义max()函数
float max(float x,float y) //max()返回值类型为浮点型
{
float z;
z=(x>y)?x:y;
return(z);
}
#endif
值传递方式
#if 0
/*值传递方式*/
#include<iostream>
using namespace std;
//定义f()函数
f(int x,int y) //f()的参数以值方式传递
{
++x;
--y;
cout<<"x="<<x<<",y="<<y<<endl;
}
main() {
int a,b;
//设置实际参数的值
a=b=10;
//以变量为参数调用f()函数
f(a,b);
//验证实际参数的值
cout<<"a="<<a<<",b="<<b<<endl;
//以表达式参数形式调用f()函数
f(2*a,a+b);
}
#endif
结构体相关函数
#if 0
/*结构体相关函数*/
#include<iostream>
using namespace std;
//定义公共结构类型
struct student {
int num;
char name[10];
float maths;
float physics;
float chemistry;
double total;
};
//定义结构输入函数
input_Rec(struct student *p) //参数为student类型的结构指针变量
{
cin>>p->num;
cin>>p->name;
cin>>p->maths;
cin>>p->physics;
cin>>p->chemistry;
}
//定义结构数据交换函数
swap_Rec(struct student *p1,struct student *p2)
{
struct student x;
//交换两个记录的数据
x=*p1;
*p1=*p2;
*p2=x;
}
//输出结构的值
put_Rec(struct student *p)
{
cout<<p->num<<'\t';
cout<<p->name<<'\t';
cout<<p->maths<<'\t';
cout<<p->physics<<'\t';
cout<<p->chemistry<<'\t';
cout<<p->total<<endl;
}
//定义main()函数
main()
{
int i,j;
// 声明结构指针变量和结构数组
struct student *p1,a[3];
//输入3个学生的数据并计算总成绩
cout<<"num\tname\tmaths\tphysics\tchemistry"<<endl;
for (p1=a;p1<=a+2;p1++) {
input_Rec(p1);
p1->total=p1->maths+p1->physics+p1->chemistry;
}
//对3个学生的数据排序
for (i=0;i<=2;i++)
for (j=i+1;j<=2;j++)
if (a[i].total<a[j].total)
swap_Rec(&a[i],&a[j]); //交换两个结构变量中的数据
cout<<"-------------------"<<endl; //输出一分界线
//输出排序后的结构数组
cout<<"num\tname\tmaths\tphysics\tchemistry\ttotal"<<endl;
for (p1=a;p1<=a+2;p1++)
put_Rec(p1);
}
#endif
结构体函数的参数传递
#if 0
#include<iostream>
using namespace std;
//定义结构
struct student {
char name[10];
float grade;
};
//交换student类型的数据
void swap(student &x,student &y) //swap的参数为引用传递方式
{
student temp;
temp=x;
x=y;
y=temp;
}
//返回student类型的引用,求优者
student& max(student &x,student &y) //swap的参数为引用传递方式
{
return (>?x:y);
}
//显示student类型的数据
void show(student &x) //show的参数为引用传递方式
{
cout<<<<" "<<<<endl;
}
void main()
{
student a={"XieCh",351.5},b={"TangX",385};
//显示a和b的数据
cout<<"a:";
show(a);
cout<<"b:";
show(b);
cout<<"------------------"<<endl;
//交换a和b的数据,并显示
swap(a,b);
cout<<"a:";
show(a);
cout<<"b:";
show(b);
cout<<"------------------"<<endl;
//计算和显示成绩高者
student t=max(a,b);
cout<<"Max:";
show(t);
}
#endif
参数带有默认值得函数
#if 0
#include <iostream>
using namespace std;
//参数带有默认值的函数
disp(int x=1,int y=1,int z=1)
{
cout<<"参数1: "<<x<<endl;
cout<<"参数2: "<<y<<endl;
cout<<"参数3: "<<z<<endl;
cout<<"------------------"<<endl;
}
//main()函数中测试参数带有默认值的函数disp()
void main()
{
disp();
disp(10);
disp(10,20);
disp(10,20,30);
int a=1,b=2,c=3;
disp(a,b,c);
}
#endif
strlen(char)
#if 0
#include <iostream>
using namespace std;
//计算字符串长度的函数
int str_len(const char *string)
{
//char *temp=string; 编译报错!
//*string='x'; 编译报错!
int i=0;
while (*(string+i)!=NULL)
i++;
return i;
}
//main()函数中测试str_len()
void main()
{
char a[]="ABCDE";
cout<<a<<"\t"<<str_len(a)<<endl;
char *str="Hello!";
cout<<str<<"\t"<<str_len(str)<<endl;
cout<<"This is a test."<<"\t"<<str_len("This is a test.")<<endl;
}
#endif
函数声明
#if 0
#include<iostream>
using namespace std;
void show(void); //这个函数声明语句不能少
//定义main()函数的参数和返回值类型是void类型
void main(void)
{
//调用void类型函数
show();
}
//以下定义disp()函数
void show(void) {
cout<<" This is a test."<<endl;
}
#endif
函数重载
#if 0
/*函数重载*/
#include<iostream>
using namespace std;
//函数原型语句
int abs(int x);
long abs(long x);
float abs(float x);
//main()函数的定义
void main(void)
{
//声明变量
int i1=32767,i2=-32767;
long l1=456789,l2=-456789;
float x1=1.1234,x2=-1.1234;
//直接在cout输出中调用函数
cout<<abs(i1)<<","<<abs(i2)<<endl;
cout<<abs(l1)<<","<<abs(l2)<<endl;
cout<<abs(x1)<<","<<abs(x2)<<endl;
}
//定义int型的abs()函数
int abs(int x) {
if (x<0)
return(-x);
else
return(x);
}
//定义long型的abs()函数
long abs(long x) {
if (x<0)
return(-x);
else
return(x);
}
//定义float型 abs函数
float abs(float x) {
if (x<0.0)
return(-x);
else
return(x);
}
#endif
内联函数
#if 0
/*内联函数*/
#include<iostream>
using namespace std;
//max()为内联函数
inline int max(int x,int y) //注意inline关键字
{
return x>y?x:y;
}
//定义main()函数
main()
{
int a=3,b=5,c;
c=max(a,b);
cout<<"max("<<a<<","<<b<<")="<<c<<endl;
cout<<"max("<<15<<","<<11<<")="<<max(15,11)<<endl;
}
#endif
递归方式函数
#if 0
/*函数声明于内部*/
#include<iostream>
using namespace std;
main()
{
//函数原型声明
int fact(int x);
int n,sn;
//依次从键盘上输入3个正整型数据计算它们的阶乘
for (int i=1;i<=3;i++)
{
cout<<i<<" n=";
cin>>n;
sn=fact(n);
cout<<n<<"!="<<sn<<endl;
}
}
//以下是采用递归方法定义的fact()函数
int fact(int x)
{
if (x==0) return(1);
return(x*fact(x-1)); //此处又调用了它自身
}
#endif
#if 0
/*显示出当前路径*/
#include<iostream>
using namespace std;
//带参数的main()函数
int main(int argc,char *argv[])
{
int i;
for(i=0;i<argc;i++)
cout<<i<<":"<<argv[i]<<endl;
return 0;
}
#endif
数组与函数
#if 0
/*数组与函数*/
#include<iostream>
using namespace std;
//用函数原型声明要使用的函数
void show_array1(int*,int);
void show_array2(int a[],int);
void sort(int*,int);
main()
{
//声明数组并初始化
int a[]={2,4,6,1,3,5};
int b[3][3]={{2,4,6},{1,3,5},{0,1,2}};
//显示数组的值
cout<<"show_array1(int*,int):"<<endl;
show_array1(a,6);
show_array1(&b[0][0],3*3);
//用sort1排序并显示
cout<<"sort(int*,int) and show_array1(int*,int): "<<endl;
sort(a,6);
show_array1(a,6);
sort(&b[0][0],3*3);
show_array1(&b[0][0],9);
//显示数组的值
cout<<"show_array2(int a[],int):"<<endl;
show_array2(a,6);
show_array2(&b[0][0],3*3);
}
//显示数组,用指针当参数
void show_array1(int *p,int size) {
for(int i=0;i<size;i++)
cout<<*(p+i)<<" ";
cout<<endl;
}
//显示数组,用数组当参数
void show_array2(int a[],int size) {
for(int i=0;i<size;i++)
cout<<a[i]<<" ";
cout<<endl;
}
//对数组按从大到小顺序排序
void sort(int *p,int size) {
int t;
for (int i=0;i<size-1;i++)
for (int j=i+1;j<size;j++)
if (*(p+i)<=*(p+j))
{
t=*(p+i);
*(p+i)=*(p+j);
*(p+j)=t;
}
}
#endif
struct函数
#if 0
/*struct与函数*/
#include<iostream>
using namespace std;
//定义结构
struct student {
char name[10];
float grade;
};
//更改student数据的grade成员,参数形式为引用
void change(student &x,float grade)
{
=grade;
}
//更改student数据的grade成员,参数形式为指针
void change1(student *p,float grade)
{
p->grade=grade;
}
//更改student类型的数据,普通参数形式
void change2(student x,float grade)
{
=grade;
}
//显示student类型的数据,参数形式为引用
void show(student &x)
{
cout<<<<" "<<<<endl;
}
//在main()函数中,测试对结构的处理函数
void main()
{
student a={"XieCh",351.5};
//显示a的数据
show(a);
//用change修改分数,并显示
cout<<"change(student &x,float grade):"<<endl;
change(a,360);
show(a);
//用change1修改分数,并显示
cout<<"change1(student *p,float grade):"<<endl;
change1(&a,375);
show(a);
//用change2修改分数,并显示
cout<<"change2(student x,float grade):"<<endl;
change2(a,380.5);
show(a);
}
#endif
#if 0
#include<iostream>
using namespace std;
//定义函数计算数组的和和平均值
void calculate(int a[],int size,int& sum,float& average)
{
sum=0;
for (int i=0;i<size;i++) {
sum+=a[i];
}
average=sum/size;
}
//定义显示数组的函数
void put_arr(int a[],int size)
{
for(int i=0;i<size;i++)
cout<<a[i]<<" ";
cout<<endl;
}
main()
{
//声明数组并初始化
int asize,bsize;
int a[]={2,4,6,8,10,12};
int b[]={1,3,5,7,9,11,13,15};
//显示数组的值
asize=sizeof(a)/sizeof(int);
cout<<"put_arr(a,asize):"<<endl;
put_arr(a,asize);
bsize=sizeof(b)/sizeof(int);
cout<<"put_arr(b,bsize):"<<endl;
put_arr(b,bsize);
//计算数组的和和平均值
float a_ave,b_ave;
int a_sum,b_sum;
cout<<"calculate(a,asize,a_sum,a_ave):"<<endl;
calculate(a,asize,a_sum,a_ave);
cout<<"a_sum="<<a_sum;
cout<<" a_ave="<<a_ave<<endl;
cout<<"calculate(b,bsize,b_sum,b_ave):"<<endl;
calculate(b,bsize,b_sum,b_ave);
cout<<"b_sum="<<b_sum;
cout<<" b_ave="<<b_ave<<endl;
}
#endif
参数为函数指针的函数(函数嵌套)
#if 0
#include<iostream>
using namespace std;
//参数为函数指针的函数
int get_result(int a, int b, int (*sub)(int,int))
{
int r;
r=sub(a,b);
return r;
}
//计算最大值
int max(int a, int b)
{
cout<<"In max"<<endl;
return((a > b) ? a: b);
}
//计算最小值
int min(int a, int b)
{
cout<<"In min"<<endl;
return((a < b) ? a: b);
}
//求和
int sum(int a, int b)
{
cout<<"In sum"<<endl;
return(a+b);
}
//测试指向函数的指针
void main(void)
{
int a,b,result;
//测试3次
for (int i=1;i<=3;i++) {
cout<<"Input a and b :";
cin>>a>>b;
cout<<i<<"\tget_result("<<a<<","<<b<<", &max):"<<endl;
result =get_result(a, b, &max);
cout<<"Max of "<<a<<" and "<<b<<" is "<<result<<endl;
result = get_result(a, b, &min);
cout<<"Min of "<<a<<" and "<<b<<" is "<<result<<endl;
result = get_result(a, b, &sum);
cout<<"Sum of "<<a<<" and "<<b<<" is "<<result<<endl;
}
}
#endif
#if 0
#include<iostream>
#include<>
using namespace std;
#define size 3
//定义book结构类型
struct book
{
char title[20];
char author[15];
int pages;
float price;
};
//book结构的输入函数
input_book(book& bk,char *name)
{
cout<<name<<":"<<endl;
cout<<"title:";
cin>>;
cout<<"author:";
cin>>;
cout<<"pages:";
cin>>;
cout<<"price:";
cin>>;
}
//book结构的输出函数
output_book(book& bk,char *name)
{
cout<<name<<": ";
cout<<<<" ";
cout<<<<" ";
cout<<<<" ";
cout<<<<endl;
}
void main(void)
{
//声明变量和结构数组
int i;
char str[20];
book bk[size];
//输入结构数组
for(i=0;i<size;i++) {
sprintf(str,"bk[%d]",i+1);
input_book(bk[i],str);
}
//显示结构数组
for(i=0;i<size;i++) {
sprintf(str,"bk[%d]",i+1);
output_book(bk[i],str);
}
}
#endif
extern声明变量
#if 0
#include<iostream>
using namespace std;
//声明全局变量并初始化
extern int a[]={1,2,3};
extern float p=3.14;
//在show()函数中使用外部变量
show()
{
int i;
cout<<"In show():"<<endl;
cout<<"p="<<p<<endl;
cout<<"a[]: ";
for (i=0;i<=2;i++)
cout<<a[i]<<" ";
cout<<endl;
//cout<<"y="<<y<<endl; 编译出错!
}
//声明外部变量并初始化
int y=5678;
//在main()函数中使用外部变量
main()
{
//声明局部变量
int i,p=100;
//显示重名变量
cout<<"In main():"<<endl;
cout<<"p="<<p<<endl;
//显示全局变量
cout<<"::p="<<::p<<endl;
cout<<"a[]: ";
for (i=0;i<=2;i++)
cout<<a[i]<<" ";
cout<<endl;
cout<<"y="<<y<<endl; //编译正确!
show(); //调用函数
}
#endif
static变量
#if 0
/*static*/
#include <iostream>
using namespace std;
//使用静态变量的计数器函数
count1()
{
//声明静态变量i,并置初值为0。i在count()中局部可见
static int i=0;
return(++i);
}
//使用局部变量的计数器函数
count2()
{
int i=0;
return(++i);
}
//在main()函数中调用count()函数
main()
{
int i;
//调用count1()10次
cout<<"count1():"<<endl;
for (i=1;i<=12;i++)
cout<<count1()<<" ";
cout<<endl;
//调用count2()10次
cout<<"count2():"<<endl;
for (i=1;i<=12;i++)
cout<<count2()<<" ";
cout<<endl;
}
#endif
extern函数
#if 0
#include<iostream>
using namespace std;
main()
{
int i,s=0;
extern int fact(int x);
for (i=2;i<=6;i=i+2)
s+=fact(i);
cout<<"s="<<s<<endl;
}
// 计算阶乘函数文件
//定义fact()函数为外部(extern)函数
extern int fact(int x)
{
int i,t=1;
if(x==0) return(1);
for(i=1;i<=x;i++)
t*=i;
return(t);
}
#endif