/* AED >> helder.correia@fe.up.pt
 * Data: 25-02-2005
 *
 * 1. Funções, parâmetros, entrada/saida de dados
 *    Exercicio 1
 */

#include <iostream>
#include <cmath>

using namespace std;

void polaresParaRectang(double r, double a, double & x, double & y) {
  y = r * sin(a);
  x = r * cos(a);
}


void rectangParaPolares(double x, double y, double & r, double & a) {
  r = sqrt(x*x + y*y);
  a = atan2(y, x);
}

// duas funções podem ter o mesmo nome desde que tenham argumentos diferentes
void rectangParaPolares(double x, double y, double * rPtr, double * aPtr) {
  *rPtr = sqrt(x*x +  y*y);
  *aPtr = atan2(y, x);
}

int main(int argc, char *argv[]) {
  double x, y, r, a;

  if (argc < 2) {
    cerr << "Sintaxe: " << argv[0] << " rect2pol|pol2rect" << endl;
    return -1;
  }

  if (string(argv[1]) == "pol2rect") {
    cout << "r? ";
    cin >> r;

    cout << "a? ";
    cin >> a;

    polaresParaRectang(r, a, x, y);

    cout << "(x, y) = (" << x << ", " << y << ")" << endl;

  } else if (string(argv[1]) == "rect2pol") {
    cout << "x? ";
    cin >> x;

    cout << "y? ";
    cin >> y;

    rectangParaPolares(x, y, r, a);

    cout << "(r, a) = (" << r << ", " << a << ")" << endl;
  
  } else {
    cerr << "Comando inválido!" << endl;
    return -1;
  }

  return 0;
}


