• c++入门之结构体初步


    结构体实际上是一种数据结构的雏形,对结构体的灵活使用很多时候可以带来很多便利.下面给出一个关于结构体的程序:

     1 #include "iostream"
     2 # include "cmath"
     3 struct polar
     4 {
     5     double distance;
     6     double angle;
     7 };
     8 struct rect
     9 {
    10     double x;
    11     double y;
    12 };
    13 void rect_to_polar(rect*pa, polar*pb);
    14 void show_polar(polar*pc);
    15 int main()
    16 {
    17     using namespace std;
    18     rect zhijiao;
    19     polar ji;
    20     cout << "please input the x and the y:" << endl;
    21     while (cin >> zhijiao.x >> zhijiao.y)
    22     {
    23         rect_to_polar(&zhijiao, &ji);
    24         show_polar(&ji);
    25         cout << "next two nbumber(q to quit:):";
    26     }
    27     cout << "Done" << endl;
    28     return 0;
    29 }
    30 
    31 void rect_to_polar(rect*pa, polar*pb)
    32 {
    33     using namespace std;
    34     pb->distance = sqrt(pa->x*pa->x+ pa->y*pa->y);
    35     pb->angle = atan2(pa->y, pa->x);
    36 }
    37 
    38 void show_polar(polar*pc)
    39 {
    40     using namespace std;
    41     const double exchange = 57.29577951;
    42     cout << "distance is:" << pc->distance << endl;
    43     cout << "angle is:" << pc->angle* exchange << endl;
    44     cout << "over" << endl;
    45 
    46 }

    上述代码的作用:将输入的一组x,y直角坐标转换为极坐标.

    关于代码,总结以下:

    如果通过结构体本身,采用.访问结构体元素,比如mystruct.element;但若通过结构体指针来访问元素。如mystruct->element;

    2 很多时候,我们采用指针传递实参的意义在于:普通的参数传递,实际上是值拷贝过程,我们无法改变原来的变量的值,但很多时候,我们需要这么做;其次,一个大的工程,拷贝太多,容易造成内存的浪费.

  • 相关阅读:
    20160227.CCPP体系详解(0037天)
    MinerUtil.java 爬虫工具类
    MinerUrl.java 解析页面后存储URL类
    MinerThreadPool.java 线程池
    MinerStoreThread.java 存储线程
    MinerQueue.java 访问队列
    MinerMonitorThread.java 监控线程
    MinerHtmlThread.java 爬取页面线程
    上班跟打DotA感觉差不多?姑娘你没事儿吧
    7月3日云栖精选夜读:强大的PyTorch:10分钟让你了解深度学习领域新流行的框架
  • 原文地址:https://www.cnblogs.com/shaonianpi/p/9800909.html
Copyright © 2020-2023  润新知