函数和结构

本文探讨了在C++中如何处理结构,包括按值传递结构和通过地址传递结构。按值传递适用于小结构,如直角坐标到极坐标的转换。而传递结构地址能节省资源,但需要管理内存,确保正确释放。文中提供了具体示例和注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为结构编写函数比为数组编写函数更简单,虽然,结构变量和数组一样,都可以存储多个数据项,但在涉及到函数时,结构变量的行为更接近普通基本变量,也就是说,与数组不同,结构将其数据组合成单个实体或数据对象,该实体被视为一个整体,可以将结构赋给另一个结构,也可以按值传递结构,就像普通变量一样。在这种情况下,结构使用的是原始数据的副本。另外,函数也可以返回结构,与数组不同,数组名表示为数组第一个元素的地址,而结构名就是一个名称,要获得结构地址,需要使用地址运算符&。

下面介绍两种,结构在函数中的传递方式

1.按值传递

当结构比较小时,按值传递结构最合理。下面来看技术示例,假设要描述屏幕上某点的位置,或地图上相对于原点的位置,则一种方法是指出该点相对于原点的水平位移量和垂直位移量即直角坐标系中的x,y坐标值。可以用结构来表示,

struct rect
{
    double x;
    double y;
}

另一种描述点的位置的方法是,指出它偏离原点的距离和方向(例如,东北方向40度)。距离和角度构成了传统意义上的极坐标系。

struct polar
{
    double distance;
    double angle;
}

此例中,我们构建一个转换函数,将直角坐标转换成极坐标,并返回此极坐标,再创建一个显示函数,显示此极坐标。

转换函数:

polar rec_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;
}

此处,atan2()函数可以根据x和y的值计算角度,此角度是与x轴的夹角,返回值的单位为弧度,取值范围为[-π,π]。引用头文件为cmath。

显示函数:这里为了便于查看,需要把弧度值转换为角度值,意味着将弧度值乘以180/π,即57.29577951

void show_polar(polar dapos)
{
    using namespace std;
    const double red_to_deg=57.29577951;
    cout<<"distance="<<dapos.distance;
    cout<<",angle="<<dapos.angle*red_to_deg;
    cout<<"degrees\n";
}

全部程序代码;

#include <iostream>
#include <cmath>
struct rect 
{
    double x;
    double y;
};
struct polar 
{
    double distance;
    double angle;
};
polar rec_to_polar(rect xypos);
void show_polar(polar dapos);
int main()
{
    using namespace std;
    rect rdot;
    polar pdot;
    cout<<"Enter the x and y value: ";
    while ( cin >> rdot.x >> rdot.y)
    {
        pdot=rec_to_polar(rdot);
        show_polar(pdot);
        cout<<"Next two numbers (q to quit):";
    }
    cout<<"Done.\n";

    return 0;
}
//转换函数
polar rec_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)
{
    using namespace std;
    const double red_to_deg=57.29577951;
    cout<<"distance="<<dapos.distance;
    cout<<",angle="<<dapos.angle*red_to_deg;
    cout<<"degrees\n";
}

输出结果:

2.传递结构的地址

假设要传递的是结构地址而不是整个结构以节省空间和时间,则需要重新编写前述函数,使用指向结构的指针。
程序如下:

#include <iostream>
#include <cmath>
struct rect 
{
    double x;
    double y;
};
struct polar 
{
    double distance;
    double angle;
};
polar* rec_to_polar(const rect* xypos);
void show_polar( const polar* dapos);
int main()
{
    using namespace std;
    rect rdot;
    cout<<"Enter the x and y value: ";
    while ( cin >> rdot.x >> rdot.y)
    {
        polar* pdot=rec_to_polar(&rdot);
        show_polar(pdot);
        delete pdot;
        cout<<"Next two numbers (q to quit):";
    }
    cout<<"Done.\n";

    return 0;
}
//转换函数
polar* rec_to_polar(const rect* xypos)
{
    polar* answer=new polar ;
    answer->distance=sqrt(xypos->x * xypos->x + xypos->y * xypos->y);
    answer->angle=atan2(xypos->y,xypos->x);
    return answer;
}
//显示函数
void show_polar( const polar* dapos)
{
    using namespace std;
    const double red_to_deg=57.29577951;
    cout<<"distance="<<dapos->distance;
    cout<<",angle="<<dapos->angle*red_to_deg;
    cout<<"degrees\n";
}

输出结果:
输出结果

程序需要注意的问题是:
当函数返回一个指针,该指针指向new分配的内存,切记,切记,切记,使用delete释放掉它们。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值