не создается файл и не записываются значения. в чем проблема?
#include <iostream>
#include <fstream>
#include <cmath>
long double eccentricity = 0.5220019945150835;
const long double PI = 3.14159265358979323846;
int period = 28080; //c
double apoсenter_radius_vector = 12389.5; //km
double periapsis_radius_vector = 3654.5; //km
long double calculateEccentricAnomaly(long double eccentricity, long double mean_anomaly, long double precision) {
long double E0 = mean_anomaly;
long double diff = 0.001;
int max_iterations = 10000;
int iteration = 0;
while (diff > precision && iteration < max_iterations) {
long double E1 = mean_anomaly + eccentricity * sin(E0);
diff = std::abs(E1 - E0);
E0 = E1;
iteration++;
}
return E0;
}
long double calculateTrueAnomaly(long double eccentricity, long double eccentric_anomaly) {
long double true_anomaly = 2 * atan(sqrt((1 + eccentricity) / (1 - eccentricity)) * tan(eccentric_anomaly / 2));
return true_anomaly;
}
int main() {
// расчет массы марса
double mass = exp(24 * log(0.642) + log(10));
double gravitational_constant = exp(-11 * log(6.67) + log(10));
return 0;
// расчет гравитационного параметра
long double gravitational_parametr;
gravitational_parametr = mass * gravitational_constant;
long double major_axis;
//расчет главной полуоси
major_axis = (apoсenter_radius_vector + periapsis_radius_vector) / 2;
long double eccentricity;
// расчет эксцентриситета
eccentricity = (apoсenter_radius_vector / major_axis) - 1;
long double focal_parameter;
// Расчет фокального параметра
focal_parameter = major_axis * (1 - eccentricity * eccentricity);
// Вывод результата
std::cout << "а: " << major_axis << std::endl;
std::cout << "e: " << eccentricity << std::endl;
std::cout << "p: " << focal_parameter << std::endl;
return 0;
long double precision = 1e-10;
std::ofstream fout("result.txt");
fout.open("result.txt", std::fstream::in | std::fstream::out);
//std::ofstream out("results.txt"); // Open the file for writing
for (int time = 0; time <= 28080; time++) {
long double mean_anomaly = 2 * PI * time / 28080;
long double eccentric_anomaly = calculateEccentricAnomaly(eccentricity, mean_anomaly, precision);
long double true_anomaly = calculateTrueAnomaly(eccentricity, eccentric_anomaly);
fout << time << " " << eccentric_anomaly << " " << true_anomaly << std::endl; // Write the results to the file
}
fout.close(); // Close the file
std::cout << "File has been written" << std::endl;
system("pause")
return 0;
}
Ответы (1 шт):
Автор решения: NunOfIt
→ Ссылка
У вас в main() три return 0;, зачем? Вы ведь по сути отсекаете любой код, который после них записан.
В строке std::ofstream fout("result.txt"); вы уже открыли файл для записи, вам его не нужно повторно открывать...
И лучше переменную time в файл не записывать, ибо номер строки в которой расположжен ваш вывод и является переменной time. Просто, когда будете считывать результат, создайте какую-нибудь переменную i и она будет вашим счётчиком, а так вы только дополнительное место в файле занимаете.
#include <cmath>
#include <fstream>
#include <iostream>
/* Это куда??? */
// long double eccentricity = 0.5220019945150835;
/* ??? */
const int PERIOD = 28080;
const double PI = 3.14159265358979323846;
const double PERIAPSIS_RADIUS_VECTOR = 3654.5; //km
const double APOCENTER_RADIUS_VECTOR = 12389.5; //km
double calculateEccentricAnomaly(double eccentricity, double mean_anomaly, double precision) {
static const int MAX_ITERATIONS = 10000;
double diff = 0.001;
double e1, e0 = mean_anomaly;
for(int i = 0; diff > precision && i < MAX_ITERATIONS; ++i) {
e1 = mean_anomaly + eccentricity * sin(e0);
diff = e1 - e0;
if(diff < 0) { diff = -diff; }
e0 = e1;
}
return e0;
}
double calculateTrueAnomaly(double eccentricity, double eccentric_anomaly) {
eccentric_anomaly = tan(eccentric_anomaly / 2);
eccentricity = sqrt((1 + eccentricity) / (1 - eccentricity));
return 2 * atan(eccentricity * eccentric_anomaly);
}
int main() {
// расчет массы марса
const double MASS = exp(24 * log(0.642) + log(10));
const double GRAVITATIONAL_CONSTANT = exp(-11 * log(6.67) + log(10));
// расчет гравитационного параметра
double gravitational_parametr;
gravitational_parametr = MASS * GRAVITATIONAL_CONSTANT;
//расчет главной полуоси
double major_axis;
major_axis = (APOCENTER_RADIUS_VECTOR + PERIAPSIS_RADIUS_VECTOR) / 2;
// расчет эксцентриситета
double eccentricity;
eccentricity = (APOCENTER_RADIUS_VECTOR / major_axis) - 1;
// Расчет фокального параметра
double focal_parameter;
focal_parameter = major_axis * (1 - eccentricity * eccentricity);
// Вывод результата
std::cout << "а: " << major_axis << '\n';
std::cout << "e: " << eccentricity << '\n';
std::cout << "p: " << focal_parameter << '\n';
std::ofstream fout("result2.txt");
if(!fout.is_open()) {
std::cout << "File has not been written!\n";
return 0;
}
const double PRECISION = 1e-10;
double ma_i = 2 * PI / PERIOD;
double mean_anomaly = 0, eccentric_anomaly, true_anomaly;
fout << "0 0\n";
for (int i = 0; i < PERIOD; ++i) {
mean_anomaly += ma_i;
eccentric_anomaly = calculateEccentricAnomaly(eccentricity, mean_anomaly, PRECISION);
true_anomaly = calculateTrueAnomaly(eccentricity, eccentric_anomaly);
fout << eccentric_anomaly << " " << true_anomaly << '\n';
}
fout.close();
std::cout << "File has been written!\n";
return 0;
}