课本例题:
例题1:
/***********************************************************
**** 5_1定义一个无参函数display,用来显示字符串 ****
************************************************************/
# include<iostream>
using namespace std;
void display()
{
cout<<"This is an example。"<<endl;
}
int main()
{
display();
return 0;
}
例题2:
/*********************************************************
**** 5_2定义一个有参函数min()求两个数最小值 ****
**********************************************************/
# include<iostream>
using namespace std;
double min(double x, double y)
{
return x < y ? x : y;
}
int main()
{
cout<<min(6.0, 5.0)<<endl;
return 0;
}
例题3:
/**********************************************
**** 5_3函数原型示例程序 ****
***********************************************/
# include<iostream>
using namespace std;
double circleArea(double);
int main()
{
double area = circleArea(5.0);
cout<<"area = "<<area<<endl;
return 0;
}
double circleArea(double r) //求圆的面积
{
double pi = 3.14;
double area = pi * r * r;
return area;
}
例题4:
/*************************************************
**** 5_4计算变量a,b之和 ****
**************************************************/
# include<iostream>
using namespace std;
int sum(int x, int y)
{
int temp;
temp = x + y;
return temp;
}
int main()
{
int a, b, c;
a = 10; b = 5;
c = sum(a, b);
cout<<a<<" + "<<b<<" = "<<c<<endl;
return 0;
}
例题5:
/********************************************************
**** 5_5说明实参求值的顺序 ****
*********************************************************/
# include<iostream>
using namespace std;
int ncomp(int i, int j)
{
if(i > j) return 1;
if(i == j) return 0;
return -1;
}
int main()
{
int k = 2;
int n = ncomp(k, ++k);
cout<<n;
return 0;
}
//返回值是0,函数从右向左运
例题6:
/************************************************
**** 5_6输入两个整数输出较大的数函数 ****
*************************************************/
# include<iostream>
using namespace std;
int max(int u, int v)
{
int w;
w = u > v ? u:v;
return w;
}
int main()
{
int a, b, c;
cout<<"please input two numbers : ";
cin>>a>>b;
c = max(a, b);
cout<<" a = "<<a<<" b = "<<b<<endl;
cout<<"Max is"<<c<<endl;
return 0;
}
例题7:
/****************************************************
**** 求整数10的平方 **************************
*****************************************************/
# include<iostream>
using namespace std;
int sqr(int x)
{
x = x * x;
return x;
}
int main()
{
int t = 10;
int s = sqr(t);
cout<<"t = "<<t<<'\t'
<<"sqr("<<t<<") = "<<s<<endl;
return 0;
}
例题8:
/******************************************************
**** 5_8用值调用的方法实现两个数据互换 ****
*******************************************************/
# include<iostream>
using namespace std;
void swap(int u, int v);
int main()
{
int a = 3;
int b = 4;
cout<<"a = "<<a<<"\tb= "<<b<<endl;
swap(a, b);
cout<<"a = "<<a<<"\tb= "<<b<<endl;
return 0;
}
void swap(int u, int v)
{
int temp;
temp = u;
u = v;
v = temp;
}
例题9:
/***************************************************
**** 引用调用的方法实现两个数据互换 ****
****************************************************/
# include<iostream>
using namespace std;
void swap(int &u, int &v);
int main()
{
int a = 3;
int b = 4;
cout<<"a = "<<a<<"\tb = "<<b<<endl;
swap(a, b);
cout<<"a = "<<a<<"\tb = "<<b<<endl;
return 0;
}
void swap(int &u, int &v)
{
int temp = v;
v = u;
u = temp;
}
例题10:
/*************************************************
**** 5_10void类型函数中return语句的作用 ****
**************************************************/
# include<iostream>
using namespace std;
void dispaly(int x, float y)
{
cout<<x<<" "<<y;
}
int main()
{
float a;
int b;
cin>>b>>a;
dispaly(b, a);
return 0;
}
例题11:
/******************************************************************
**** 5_11不同类型参数的示例程序(求一元二次方程的根) ****
*******************************************************************/
# include<iostream>
# include<cmath>
using namespace std;
void GetRoots(/* in */double, /* in */ double, /* in */double,
/* out */double&, /* out */double&);
int main()
{
double a, b, c;
double root1, root2;
cout<<"输入方程的三个系数a, b, c:"<<endl;
cin>>a>>b>>c;
GetRoots(a, b, c, root1, root2);
//a, b, c作为值调用的两个实参,必须要有初始值
//root1, root2作为引用调用的两个实参,可以没有初始值
cout<<"root1 = "<<root1<<" root2 = "<<root2<<endl;
return 0;
}
例题13:
/**************************************************
**** 5_13计算sum = (2*2)! + (3*3)! ****
***************************************************/
# include<iostream>
using namespace std;
long f2(int);
long f1(int);
int main()
{
int i;
long sum = 0;
for(i = 2; i <= 3; i++)
sum += f1(i);
cout<<"sum = "<<sum<<endl;
return 0;
}
long f1(int p)
{
int k;
long r;
k = p * p;
r = f2(k);
return r;
}
long f2(int q)
{
long fact = 1;
for(int i = 1; i <= q; i++)
fact = fact * i;
return fact;
}
例题14:
/************************************************************
**** 5_14用弦解法求一个一元三次方方程的根 ****
*************************************************************/
# include<iostream>
# include<cmath>
using namespace std;
float f(float x); //求函数值
float root(float x1, float x2); //求方程的根
float point(float x1, float x2); //求与x轴的交点
int main()
{
float x1, x2, y1, y2, x;
do //输入x1, x2,直到f(x1)与f(x2)异号
{
cout<<"请输入根所在的范围:";
cin>>x1>>x2;
y1 = f(x1);
y2 = f(x2);
cout<<"两端点的值为["<<y1<<", "<<y2<<"]"<<endl;
}while(y1 * y2 >= 0);
x = root(x1, x2); //求(x1, x2)区间的根
cout<<"在"<<x1<<"与"<<x2<<"之间,方程的解为:"<<x<<endl;
return 0;
}
float f(float x)
{
return(x * x * x -4 * x * x + 6 * x - 10);
}
float root(float x1, float x2)
{
float y1, x, y;
y1 = f(x1);
do
{
x = point(x1, x2);
y = f(x);
if(y * y1 > 0)
{
y1 = y; x1 = x;
}
else x2 = x;
}while(fabs(y) >= 0.0001);
return x;
}
float point(float x1, float x2)
{
float y;
y = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1));
return y;
}
例题15:
/*************************************************
**** 5_15用递归方法求n的阶乘 ****
**************************************************/
# include<iostream>
using namespace std;
float Factorial(int n);
int main()
{
int a;
float f;
cout<<"input an integer number:";
cin>>a;
f = Factorial(a);
cout<<a<<"! ="<<f<<endl;
return 0;
}
float Factorial(int n)
{
float fact;
if(n == 0)
fact = 1;
else
fact = n * Factorial(n - 1); //n*(n-1)*(n-2)*.......*1
return fact; //return 0时结束
}
例题16:
/*******************************************************
**** 5_16编程求出Fibonacci数列第n项 ****
********************************************************/
# include<iostream>
using namespace std;
const N = 8;
long fibo(int n);
int main()
{
long f = fibo(N);
cout<<"Fibonacci数列第8项的值为:"<<f<<endl;
return 0;
}
long fibo(int n)
{
if(n == 1) return 1L;
else if(n == 2) return 1L;
else
return fibo(n - 1) + fibo(n - 2);
}
例题17:
/***************************************
**** 5_17文件作用域 ****
****************************************/
# include<iostream>
using namespace std;
int i; //文件作用域
int main()
{
i = 5; //给文件作用域的变量i赋值
{
int i; //块作用域
i = 7;
cout<<"内层i = "<<i<<endl; //输出7
}
cout<<"外层i = "<<i<<endl; //输出5
return 0;
}
例题18:
/*********************************************
**** 5_18作用域运算符的使用 ****
**********************************************/
# include<iostream>
using namespace std;
int i = 1; //定义具有文件作用域的变量i
int main()
{
cout<<"i = "<<i<<endl; //输出具有文件作用域的变量i的值1
int i = 5; //定义局部变量i,此时覆盖文件作用域变量i
cout<<"i = "<<i<<endl; //此时输出上一条语句定义的局部变量i的值5
{
int i =7; //定义块作用域变量i,此时覆盖前面定义的两个变量i
cout<<"i = "<<i<<endl; //输出作用域变量i的值7
cout<<"i = "<<::i<<endl; //输出文件作用域变量i的值1
}
cout<<"i = "<<i<<endl; //输出局部变量i的值5
cout<<"i = "<<::i<<endl; //输出文件作用域变量i的值1
return 0;
}
例题19:
/*************************************
**** 5_19外部变量的使用 ****
**************************************/
# include<iostream>
using namespace std;
int max(int x, int y)
{
int z;
z = x > y ? x : y;
return z;
}
int main()
{
extern int a, b;
cout <<max(a, b)<<endl; //外部变量说明
return 0;
}
int a = 13, b = -8; //全局变量定义
例题20:
/***********************************************
**** 5_20对一个一维数组进行排序 ****
************************************************/
# include<iostream>
using namespace std;
void sort(int);
void echoa();
int a[5] = {6, 3, 9, 8, 2}; //定义全局数组a
int main()
{
echoa();
sort(5);
echoa();
return 0;
}
void echoa()
{
for(int i = 0; i<5; i++)
cout<<a[i]<<'\t';
cout<<endl;
}
void sort(int n) //选择法排序
{
int i, j, min, temp;
for(i=0; i<n-1; i++)
{
min = i;
for(j=i+1; j<n; j++)
if(a[j] < a[min])
min = j;
temp = a[i]; a[i] = a[min]; a[min] = temp;
}
}
例题21:
/****************************************************
**** 5_21重名的局部变量和全局变量的作用域 ****
*****************************************************/
# include<iostream>
using namespace std;
int a = 3, b = 3; //定义a, b全局变量
int main()
{
int a = 8; //a是局部变量
int c;
c = a > b ? a : b; //此时,a = 8, b = 5
cout<<c<<endl;
return 0;
}
例题22:
/****************************************************
**** 5_22自动变量与静态局部变量的使用举例 ****
*****************************************************/
# include<iostream>
using namespace std;
int f(int a)
{
auto int b = 0; //b为自动局部变量
static int c = 3; //c为静态局部变量
b++;
c++;
return(a + b + c);
}
int main()
{
int a = 2, i;
for(i=0; i<3; i++) //三次调用f函数
cout<<f(a)<<endl;
return 0;
}
例题23:
/**********************************************************
**** 5_23打印连续整数的阶乘(使用静态局部变量) ****
***********************************************************/
# include<iostream>
using namespace std;
int fun(int);
int main()
{
int i, k;
cout<<"请输入一个非负整数:";
cin>>k;
for(i=1; i<=k; i++)
cout<<i<<"! = "<<fun(i)<<endl;
return 0;
}
int fun(int n)
{
static int f = 1; //f为静态局部变量,每一次函数执行后i!的值都会保留
f = f * n;
return f;
}
例题24:
/*********************************************************
**** 5_24打印连续整数的阶乘(使用寄存器变量) ****
**********************************************************/
# include<iostream>
using namespace std;
int fun(int);
int main()
{
int i, k;
cout<<"请输入一个非负整数:";
cin>>k;
for(i=1; i<=k; i++)
cout<<i<<"! = "<<fun(i)<<endl;
return 0;
}
int fun(int n)
{
register int i, f = 1; //变量i和f是寄存器变量
for(i=1; i<=n; i++)
f = f * i;
return f;
}
例题25:
/*******************************************
**** 5_25全局变量使用举例 ****
********************************************/
# include<iostream>
using namespace std;
int a; //全局变量
int power(int n);
int main()
{
int b = 3, c, d, m;
cout<<"enter a and m"<<endl;
cin>>a>>m;
c = a * b;
cout<<"a = "<<a<<"\tb = "<<b<<"\tc = "<<c<<endl;
d = power(m); //调用power函数,求a的n次方
cout<<"a = "<<a<<"\tm = "<<m<<"\td = "<<d<<endl;
return 0;
}
//文件file2.cpp
extern int a;
int power(int n) //该函数的功能是求a的n次方
{
int i, y = 1;
for(i = 1; i <= n; i++)
y *= a;
return y;
}