我编写了这段代码来计算 3 个公式,例如:L c=Lcb-Lca where Lcb and Lca equals to 7915,7*Log10(Tan(X1/2+45)) / G=Y2-Y1 / V=G/Lc
-> 还有一些中间公式可以帮助我将数据放入正确的形式。问题是我有很多数据要处理,如果可能的话,我想找到优化代码的方法。
#include <cstdio>
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <fstream>
#include <sstream>
#include <string>
#include "mysql/mysql.h"
using namespace std;
const string hostMySQL = "localhost";
const string userMySQL = "root";
const string database = "2_Generated_flights";
const string table = "8_airac_6_test";
const string passwordMySQL = "****";
const int port = 0;
const int clientflag = 0;
int main(int argc, char *argv[]){
/* connection à MySQL */
string request;
MYSQL * connection = mysql_init(NULL);
MYSQL_RES * result;
MYSQL_ROW row = NULL;
if(!mysql_real_connect(connection, hostMySQL.c_str(), userMySQL.c_str(),
passwordMySQL.c_str(), database.c_str(),
port, NULL, clientflag)){
fprintf(stderr, "Connection to database failed: %s\n",
mysql_error(connection));
return EXIT_FAILURE;
}
cout << "connected to mysql" << endl;
int sql_min_lat;
sql_min_lat=mysql_query(connection, "SELECT ind, Lat_begin_segment, Lon_begin_segment, Lat_end_segment, Lon_end_segment from 8_airac_6_test");/*on choisit les variables nécessaires*/
if (sql_min_lat !=0)
{
cout <<mysql_error(connection);
return EXIT_FAILURE;
}
result=mysql_store_result(connection);
while((row = mysql_fetch_row(result))){
/*calcul ind*/
int ind, degrees_lat_begin, degrees_lat_end, degrees_lon_begin, degrees_lon_end, G_int;
float lat1, lat2, lon1, lon2, minutes_lat_begin, minutes_lat_end, minutes_lon_begin, minutes_lon_end, G_diff, G_pr;
double lat_beg_deg, lat_end_deg, lon_beg_deg, lon_end_deg, Lca, Lcb, Lc, G, TanV, V, Rv;
ind=atoi(row[0]);
/*calcul lat_begin en degree*/
lat1=atof(row[1]);
degrees_lat_begin = int (lat1);
minutes_lat_begin = (lat1 - degrees_lat_begin) * 60;
minutes_lat_begin=(int)minutes_lat_begin;
if (minutes_lat_begin >-10 && minutes_lat_begin < 10)
{
minutes_lat_begin= minutes_lat_begin/10;
}
else {
minutes_lat_begin= minutes_lat_begin/100;
}
if (minutes_lat_begin ==1 || minutes_lat_begin==-1) {
minutes_lat_begin=minutes_lat_begin/100;
}
lat_beg_deg=degrees_lat_begin + minutes_lat_begin;
/*calcul lat_end en degree*/
lat2=atof(row[3]);
degrees_lat_end = int (lat2);
minutes_lat_end = (lat2 - degrees_lat_end) * 60;
minutes_lat_end=(int)minutes_lat_end;
if (minutes_lat_end >-10 && minutes_lat_end < 10)
{
minutes_lat_end= minutes_lat_end/10;
}
else
{
minutes_lat_end=minutes_lat_end/100;
}
if (minutes_lat_end ==1 || minutes_lat_end==-1) {
minutes_lat_end=minutes_lat_end/100;
}
lat_end_deg=degrees_lat_end + minutes_lat_end;
cout << "Done degrees_lat_end!" << endl;
/*calcul lon_begin en degree*/
lon1=atof(row[2]);
degrees_lon_begin = int (lon1);
minutes_lon_begin = (lon1 - degrees_lon_begin) * 60;
minutes_lon_begin=(int)minutes_lon_begin;
if (minutes_lon_begin >-10 && minutes_lon_begin < 10) {
minutes_lon_begin= minutes_lon_begin/10;
}
else
{
minutes_lon_begin= minutes_lon_begin/100;
}
if (minutes_lon_begin ==1 || minutes_lon_begin==-1) {
minutes_lon_begin=minutes_lon_begin/100;
}
lon_beg_deg=degrees_lon_begin + minutes_lon_begin;
/*calcul lon_begin en degree*/
lon2=atof(row[4]);
degrees_lon_end = int (lon2);
minutes_lon_end = (lon2 - degrees_lon_end) * 60;
minutes_lon_end=(int)minutes_lon_end;
if (minutes_lon_end >-10 && minutes_lon_end < 10) {
minutes_lon_end= minutes_lon_end/10;
}
else
{
minutes_lon_end= minutes_lon_end/100;
}
if (minutes_lon_end ==1 || minutes_lon_end==-1) {
minutes_lon_end=minutes_lon_end/100;
}
lon_end_deg=degrees_lon_end + minutes_lon_end;
cout << "Done degrees_lon_end!" << endl;
/*LCA*/
Lca=tan(45+lat_beg_deg/2);
Lca=abs(Lca);
Lca=7915.7*log10(Lca);
Lca=abs(Lca);
/*LCB*/
Lcb=tan(45+lat_end_deg/2);
Lcb=abs(Lcb);
Lcb=7915.7*log10(Lcb);
Lcb=abs(Lcb);
/*LC*/
Lc=Lcb-Lca;
Lc=abs(Lc);
/*G*/
G_diff=lon_end_deg-lon_beg_deg;
G_diff=abs(G_diff);
G_int=int(G_diff);
G_pr=G_diff-G_int;
if (G_pr==1) {
G_pr=0;
}
G_pr=G_pr*100;
G=G_int*60+int(G_pr);
TanV=G/Lc;
V=atan(TanV)*57.2957795;
if (lat_end_deg>lat_beg_deg && lon_end_deg>lon_beg_deg) {
Rv=V;}
else if (lat_end_deg>lat_beg_deg && lon_end_deg<lon_beg_deg) {
Rv=360-V;}
else if (lat_end_deg<lat_beg_deg && lon_end_deg<lon_beg_deg) {
Rv=180+V;}
else {
Rv=180-V;}
char query[100000];
sprintf(query, "INSERT INTO testRv(ind, Rv) VALUES(%i, %g)", ind, Rv);
mysql_query(connection, query);
}
mysql_close(connection);
cout << "Done!" << endl;
}