#include <iostream.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <fstream.h>
#include <sstream.h>
#define R0 (1298039.0046)
#define alfa (1.000597498372)
#define lambda0 (42.500000000000)
#define fi0 (49.500000000000)
#define E2 (0.006674372230622)
#define A (6377397.15508)
int main(void)
{
   ostringstream ostrS, ostrU, ostrn, ostrD, ostrro, ostre, ostrV, ostrl, ostrlh;
   ostringstream ostrx, ostry, ostrz;
   ostrS.setf(ios::fixed, ios::floatfield);
   ostrS.precision(8);
   ostrU.setf(ios::fixed, ios::floatfield);
   ostrU.precision(8);
   ostrV.setf(ios::fixed, ios::floatfield);
   ostrV.precision(8);
   ostrD.setf(ios::fixed, ios::floatfield);
   ostrD.precision(8);
   ostrl.setf(ios::fixed, ios::floatfield);
   ostrl.precision(4);
   ostrlh.setf(ios::fixed, ios::floatfield);
   ostrlh.precision(4);
   ostre.setf(ios::fixed, ios::floatfield);
   ostre.precision(8);
   ostrro.setf(ios::fixed, ios::floatfield);
   ostrro.precision(8);
   ostrx.setf(ios::fixed, ios::floatfield);
   ostrx.precision(4);
   ostry.setf(ios::fixed, ios::floatfield);
   ostry.precision(4);
   ostrz.setf(ios::fixed, ios::floatfield);
   ostrz.precision(4);
   double ro=180/M_PI;
   double S0;
   S0=78.5;
   double ferro;
   double epsilon, R, D, N, H;
   double x;
   double y;
   double arg, S, ps1, pom, pom1;
   double U, deltaV, V, lambda, V0, n, fi, U0, deltaU;
   double a;
   double mira=60;
   int deset=10;
   U0=(49+(27/mira)+35.84625/(pow(mira,2)));
   a=(30+(17/mira)+(17.30311/pow(mira,2)));
   ferro=(17+(40/mira));
   start:
   clrscr();
   cout << "Zadat souradnice :" <<endl;
   cout << "-------------------"<<endl;
   cout << "    X = ";
   cin >>  x;
   cout << "    Y = ";
   cin >> y;
   cout << "h (elipsoidicka) = ";
   cin >> H; 

   /*převod na polární*/

   R=sqrt(x*x+y*y);
   epsilon=atan(y/x);
   cout <<endl<< "Polarni souradnice (ro,epsilon) : "<<endl;
   cout << "-------------------------------- ";
   ostrro<<R;
   ostre<<epsilon*ro;
   cout << endl<<"ro = "<<ostrro.str();
   cout <<"    epsilon = "<<ostre.str();

  /*převod na kartografické*/
   S0=S0/ro;
   n=sin(S0);
   cout <<endl<<endl<< "Kartograficke souradnice (S,D) : "<<endl;
   cout << "-------------------------------- ";
   D=epsilon/n;
   ostrD<<D*ro;
   cout << endl<<"D = "<<ostrD.str();
   pom=R0/R;
   arg=(S0/2)+(M_PI_2/2);
   pom1=tan(arg);
   double ps = pow(pom1,n);
   ps=ps*pom;
   ps1=pow(ps,(1/n));
   S=(atan(ps1)-(M_PI_2/2))*2;
   ostrS << S*ro;
   cout << "    S = " << ostrS.str();

   /*převod na sfericke (U,V)*/
   cout <<endl<<endl<< "Sferické souradnice (U,V) : "<<endl;
   cout <<             "--------------------------- ";
   a=a/ro;
   U=asin(cos(a)*sin(S)-sin(a)*cos(S)*cos(D));
   deltaV=asin(cos(S)*sin(D)*(1/cos(U)));
   V0=alfa*lambda0;
   V=V0-(deltaV*ro);
   ostrU<<U*ro;

   /*převod na Bessela*/
   deltaU=U-(U0/ro);
   double deltafi=(100.1416022789*deltaU/(pow(deset,2))-86.87150417*pow(deltaU,2)/(pow(deset,6))+16.70197*pow(deltaU,3)/(pow(deset,8))+117.5089*pow(deltaU,4)/(pow(deset,10)));
   deltafi=deltafi*ro;
   fi=fi0+deltafi;
   cout <<endl<< "U = " <<ostrU.str();
   lambda=V/alfa;
   ostrV<<V;
   cout << "    V (E-Ferro.) = " <<ostrV.str();
   cout <<endl<<endl<< "Zemepisne souradnice (fi,lambda) : "<<endl;
   cout <<             "---------------------------------- ";
   lambda=(lambda-ferro);
        /*prevod na cele jednotky uhl. miry*/
        int l = lambda;
        double lambda1 = lambda - l;
        lambda1=lambda1*60;
        int lambda2=lambda1;
        int lambda3=lambda1-(lambda1-lambda2);
        double lambda4 = (lambda1-lambda2)*60;
        ostrl << lambda4;

        int f = fi;
        double f1 = fi-f;
        f1=f1*60;
        int f2=f1;
        int f3=f1-(f1-f2);
        double f4 = (f1-f2)*60;
        ostrlh << f4;

   cout << endl<<"lambda = " <<l<<"o " <<lambda3 <<"' "<<ostrl.str()<<"'' " ;
   cout << endl<<"fi = " <<f<<"o " <<f3 <<"' "<<ostrlh.str()<<"'' " ;

   /*převod na geocentrické pravoúhlé*/
   double X,Y,Z;
   fi=fi/ro;
   lambda=lambda/ro;
   double cit = (1-(E2*sin(fi)*sin(fi)));
   cit=sqrt(cit);
   N=A/cit;
   ostry<<N+H;
   cout<<"N= "<<ostry.str();
   X=(N+H)*cos(fi)*cos(lambda);
   Y=(N+H)*cos(fi)*sin(lambda);
   Z=(N*(1-E2)+H)*sin(fi);
   ostrx<<X;
   /*ostry<<Y; */
   ostrz<<Z;
   cout <<endl<<endl<< "Geocentricke souradnice : " <<endl;
   cout << "------------------------- " << endl;
   cout << "X = " <<ostrx.str()<<"   Y = "<<ostry.str()<<"   Z = "<<ostrz.str();
   {
          ofstream vysledek ("fi.txt", ios::app);
	  vysledek << "Kartograficke souradnice S, D : "<< "\n";
	  vysledek << "S = "<<ostrS.str()<<"   D = " <<ostrD.str()<<"\n";
          vysledek << "Sfericke souradncie U, V : " <<"\n";
          vysledek << "U = "<<ostrU.str()<<"   V = "<<ostrV.str()<<"\n";
  }
    cout <<endl<<endl<< "Vymazat obrazovku a pokracovat ? [ano='cokoliv' ne = '0'] ?";
    char dal;
    cin >> dal;
    if (dal == '0') return 0; else goto start;
}
