函数和结构
前言
浮躁的人容易问:我到底该学什么;——别问,学就对了。
一、函数结构和数组结构的异同点
- 相同点:结构变量和数组变量一样,都可以存储多个数据项。
- 不同点:
1.为结构编写函数要比为数组编写函数简单得多,在涉及到函数时,结构变量的行为更接近于基本的单值变量。
2.可以将一个结构体的内容赋给另一个结构,也可以按值传递结构。
3.函数可以返回结构。
4.数组名是数组的首地址,而结构名只是结构的名称,要获得结构的地址,还需要使用地址运算符&。
二、按值传递结构的缺点
如果结构非常大,则复制结构将增加内存要求,降低系统的运行速度。因此,可以倾向于传递结构的地址,使用指针来访问结构内容,C++还可以使用引用的方式传递。
三、传递和返回结构
下面两个代码展示如何在函数中使用结构体:
//该段代码展示了直角坐标系和极坐标系下的距离展示
#include<iostream>
#include<cmath>
using namespace std;
struct polar
{
double distance;
double angle;
};//定义一个结构体,用极坐标系展示方向和距离
struct rect
{
double x;
double y;
};//定义一个结构体,用直角坐标系展示所在位置
polar rect_to_polar(rect xypos);//声明一个函数使直角坐标系转化为极坐标系
void show_polar(polar dapos);//展示直角坐标系中的内容
int main()
{
rect rplace;
polar pplace;
cout << "Enter the x and y values:";
while (cin >> rplace.x >> rplace.y)
{
rect_to_polar(rplace);
show_polar(pplace);
cout << "Next two number(q to quit):";
}
return 0;
}
polar rect_to_polar(rect xypos)
{
polar answer;
answer.distance = sqrt(xypos.x * xypos.x + xypos.y * xypos.y);
answer.angle = atan2(xypos.y, xypos.x);
return answer;
}
void show_polar(polar dapos)
{
const double Rad_to_deg = 57.29577951;
cout << "Distance=" << dapos.distance << endl;
cout << "Angle=" << dapos.angle * Rad_to_deg <<"degree." << endl;
}
//该段代码展示了直角坐标系和极坐标系下的距离展示
#include<iostream>
#include<cmath>
using namespace std;
struct polar
{
double distance;
double angle;
};//定义一个结构体,用极坐标系展示方向和距离
struct rect
{
double x;
double y;
};//定义一个结构体,用直角坐标系展示所在位置
polar rect_to_polar(rect xypos);//声明一个函数使直角坐标系转化为极坐标系
void show_polar(polar dapos);//展示直角坐标系中的内容
int main()
{
rect rplace;
polar pplace;
cout << "Enter the x and y values:";
while (cin >> rplace.x >> rplace.y)
{
pplace=rect_to_polar(rplace);
show_polar(pplace);
cout << "Next two number(q to quit):";
}
return 0;
}
polar rect_to_polar(rect xypos)
{
polar answer;
answer.distance = sqrt(xypos.x * xypos.x + xypos.y * xypos.y);
answer.angle = atan2(xypos.y, xypos.x);
return answer;
}
void show_polar(polar dapos)
{
const double Rad_to_deg = 57.29577951;
cout << "Distance=" << dapos.distance << endl;
cout << "Angle=" << dapos.angle * Rad_to_deg <<"degree." << endl;
}
传递结构的地址
当要传递的地址不是一整个结构体时,我们要重新编写前面的函数使用指向结构的指针。如下代码:
//该段代码展示了直角坐标系和极坐标系下的距离展示
#include<iostream>
#include<cmath>
using namespace std;
struct polar
{
double distance;
double angle;
};//定义一个结构体,用极坐标系展示方向和距离
struct rect
{
double x;
double y;
};//定义一个结构体,用直角坐标系展示所在位置
void rect_to_polar(const rect* pxy, polar* pda);//该函数传递指针,节省了时间和空间
void show_polar(const polar* pda);//展示直角坐标系中的内容
int main()
{
rect rplace;
polar pplace;
cout << "Enter the x and y values:";
while (cin >> rplace.x >> rplace.y)
{
rect_to_polar(&rplace,&pplace);
show_polar(&pplace);
cout << "Next two number(q to quit):";
}
return 0;
}
void rect_to_polar(const rect *pxy,polar *pda)
{
pda->distance = sqrt(pxy->x * pxy->x + pxy->y * pxy->y);
pda->angle = atan2(pxy->y, pxy->x);
}
void show_polar(const polar *pda)
{
const double Rad_to_deg = 57.29577951;
cout << "Distance=" << pda->distance << endl;
cout << "Angle=" << pda->angle * Rad_to_deg <<" degree." << endl;
}