传递指针

代码:

#include <iostream>
#include <cmath>

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(int argc, char const *argv[]) {
  using namespace std;
  rect rplace;
  polar pplace;
  std::cout << "Enter the x and y values:";
  while (cin >> rplace.x >> rplace.y)
  {
    rect_to_polar(&rplace, &pplace);
    show_polar(&pplace);
    std::cout << "Next two numbers (q to quit)" << '\n';
  }
  std::cout << "Done." << '\n';
  return 0;
}

void show_polar(const polar * pda)
{
  using namespace std;
  const double Rad_to_deg = 57.29577951;

  std::cout << "distance = " << pda->distance;
  std::cout << ", angle = " << pda->angle * Rad_to_deg;
  std::cout << " degrees" << '\n';
}

void rect_to_polar(const rect * pxy, polar * pda)
{
  using namespace std;
  pda->distance = sqrt(pxy->x * pxy->x + pxy->y * pxy->y);
  pda->angle = atan2(pxy->y, pxy->x);
}
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-19
猜你喜欢
  • 2021-11-16
  • 2021-12-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-29
  • 2022-12-23
相关资源
相似解决方案