目录
四:函数
1.函数的调用
#include<iostream.h>
#include<math.h> //sin函数,标准函数
float f(float x) //自定义函数,求y的值
{
float y;
y = 5 * x * x + 3 * x + 1;
return y;
}
void main(void) //程序从这里主函数开始读
{
float z1, z2, x = 1;
z1 = sin(x); //此时转到定义<math.h>,系统函数
z2 = f(x); //调用自定义函数,转到flaot f(float x),此行的x(实参)给float x(形参)=1,通过return y返回给f(x)
cout << "sin(x)=" << z1 << endl;
cout << "f(x)=" << z2 << endl;
}
#include<iostream.h>
int max(int x,int y) //形参
{
int z;
if(x>y) z=x;
else z=y;
return z;
}
void main(void)
{
int a,b,c,m;
cout<<"Input a,b,c:";
cin>>a>>b>>c;
cout<<"max(b,c)="<<max(b,c)<<endl; //实参,转到形参处赋给形参
m=max(a,max(b,c)); //再调用b,c再用a
cout<<"max(a,b,c)="<<m<<endl;
}
2.函数的原型说明
#include<iostream.h>
void mian(void)
{
int a,b,c,,n;
int max(int x,int y); //函数原型说明
cout<<"Input a,b,c:";
cin>>a>>b>>c;
cout<<"max(b,c)="<<max(b,c)<<endl;
m=max(a.max(b,c));
cout<<"max(a,b,c)="<<m<<endl;
}
int max(int a,int y)
{
int z;
if(x>y) z=x;
else z=y;
return z;
}
3.实参和形参的数据传递
#include<iostream.h>
void swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
}
void main(void)
{
int a=3,b=4;
cout<<"a="<<a<<'\t'<<"b="<<b<<endl;
swap(a,b);
cout<<"a="<<a<<'\t'<<"b="<<b<<endl;
}
4.函数的嵌套调用和递归调用
#include<iostream>
using namespace std;
int fac(int n)
{
int i, t = 1;
for (i = 1; i <= n; i++)
t = t * i;
return t;
}
int cmn(int m, int n)
{
int p = 0;
p = fac(m) / (fac(n) * fac(m - n)); //调用
return p;
}
void main()
{
int s = 0, m, n;
cout << "input m,n:" << endl;
cin >> m >> n;
s = cmn(m, n); //调用
cout << "s=" << s << endl;
}
#include<iostream>
using namespace std;
int fac(int n)
{
int p;
if (n == 0 || n == 1)
p = 1;
else
p = n * fac(n - 1); //p=s*fac(4),p=4*fac(3),...p=2*fac(1)
return p;
}
void main()
{
int n, t;
cout << "input n:" << endl;
cin >> n;
t = fac(n);
cout << "t=" << t << endl;
}
5.数组作为函数参数
5.1值传送
#include<iostream>
using namespace std;
void print(int x)
{ cout << x << '\t';}
void main(void)
{
int a[5] = { 3,7,9,12,92 };
for (int i = 0; i < 5; i++)
print(a[i]);
}
5.2传地址
#include<iostream>
using namespace std;
void swap(int b[2])
{
int t;
cout << "x=" << b[0] << '\t' << "y=" << b[1] << endl;
t = b[0];
b[0] = b[1];
b[1] = t;
cout << "x=" << b[0] << '\t' << "y=" << b[1] << endl;
}
void main()
{
int a[2] = { 3,5 };
cout << "a=" << a[0] << '\t' << "b=" << a[1] << endl;
swap(a);
cout << "a=" << a[0] << '\t' << "b=" << a[1] << endl;
}
6.变量的存储类型
6.1作用域
#include<iostream>
using namespace std;
void main(void)
{
int i = 1,j = 2,k = 3;
cout << i << '\t' << j << '\t' << k << endl;
{
int a = 4, b = 5;
k = a + b;
cout << a << '\t' << b << '\t' << k << endl;
}
cout << i << '\t' << j << '\t' << k << endl;
}
6.2作用域
#include<iostream>
using namespace std;
int a = 10;
void main(void)
{
int a = 20;
a = a + ::a;
{int a = 50; ::a = ::a + a;}
cout << a << '\t' << ::a << endl;
}
7.习题
#include<iostream>
using namespace std;
#define N 10
//定义函数
void print(int b[N])
{
//输出模块
int i;
for (i = 0; i < N; i++)
cout << b[i] << '\t';
cout << endl;
}
//计算模块
void sort2(int b[N])
{
int i, j, t;
for(i=0;i<N-1;i++)
for(j=i+1;j<N;j++)
if (b[i] < b[j])
{
t = b[i];
b[i] = b[j];
b[j] = t;
}
}
void sort1(int b[N])
{
int i, j, t;
for(i=0;i<N-1;i++)
for(j=0;j<N-1;j++)
if (b[j] > b[j + 1])
{
t = b[j];
b[j] = b[j + 1];
b[j + 1] = t;
}
}
float average(int b[N])
{
int i;
float s=0, ave;
for (i = 0; i < N; i++)
s = s + b[i];
ave = s / N;
return ave;
}
int min(int b[N])
{
int m, i;
m = b[0];
for (i = 1; i < N; i++)
if (m > b[i])
m = b[i];
return m;
}
int max(int b[N])
{
int m, i;
m = b[0];
for (i = 1; i < N; i++)
if (m < b[i])
m = b[i];
return m;
}
//
void scan(int b[N])//不需要返回值void
{
//输入模块
int i;
cout << "input "<<N<<"No:" << endl;
for (i = 0; i < N; i++)
cin >> b[i];
}
//程序开始
void main()
{
int a[N], max1, min1;
float ave;
scan(a);
//
max1 = max(a);
min1 = min(a);
ave = average(a);
//排序
//升序排序
sort1(a);
print(a);
//降序排序
sort2(a);
print(a);
cout << "max=" << max1 << endl;
cout << "min=" << min1 << endl;
cout << "ave=" << ave << endl;
}
五:编译预处理
1.文件包含处理
2.宏定义
3.条件编译
六:指针
1.指针变量的引用
#include <iostream>
using namespace std;
void main(void)
{
int a = 100;
int* p, * p1, * q;
p = &a;
p1 = p;
q = 0;
cout << "a=" << a << '\t' << "*p=" << *p << '\t' << "p=" << p << endl;
*p1 = 200;
cout << "a=" << a << '\t' << "*p=" << *p << '\t' << "p=" << p << endl;
cout << '\t' << "*p1=" << *p1 << '\t' << "p1=" << p1 << endl;
}
2.指针的变量赋值运算
微课代码
函数
实参:提供值 给 形参:接收值(有数据类型)
点定义后使用,可以不加函数声明
先使用后定义,必须加上函数声明
int f(int x)
{
return x%10*10+x/10;
}
void main()
{
int a,b;
scanf("%d",&a);
b=f(a); //函数名(实参)
printf("%d",b);
}
递归
int f(int n)
{
if(n>=2) return n*f(n-1);
else return 1;
}
void main()
{
printf("%d",f(3));
}
指针
int a=3,*p;
p=&a;
printf("%d",*p);
*p=*p+5;
二级指针
int a=2,b=3,*p,**t;
t=&p;
*t=&a; //*t=p
.
.
.*t=&b;
通过二级指针修改了一级指针的指向
二级指针:指向指针的指针(指针的值)
结构体
结构体是定义一种新的复杂的数据类型
struct stu;
{
int name;
float score;
};
struct stu stu1,*p;
stu1.name =101;
stu1.score=75.6;
p=&stu1;
printf("%d/t",(*p).name);
printf("%f\n",(*p).score);
}
//
printf("%d\t",p->name);
printf("%d\n",p->score);