zoukankan      html  css  js  c++  java
  • 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 很多时候,我们采用指针传递实参的意义在于:普通的参数传递,实际上是值拷贝过程,我们无法改变原来的变量的值,但很多时候,我们需要这么做;其次,一个大的工程,拷贝太多,容易造成内存的浪费.

  • 相关阅读:
    sentinel.conf样例
    哨兵模式java实例
    哨兵模式启动redis
    华为笔试:直角三角形个数
    leetcode 337. 打家劫舍iii
    leetcode 494. 目标数
    leetcode 394. 字符串解码
    leetcode 128. 最长连续子序列
    链表快排
    leetcode 877. 石子游戏
  • 原文地址:https://www.cnblogs.com/shaonianpi/p/9800909.html
Copyright © 2011-2022 走看看