#include <iostream> #include <stdio.h> #include <cstdlib> #include <fstream> #include <windows.h> #include <math.h> // Все ограничение вынес в define что бы проще было регулировать. #define ALPHA_RESTRICTION_START 0 #define E_RESTRICTION_START 1E-9 #define V_RESTRICTION_START 1000 #define B_RESTRICTION_START 0.01 #define ALPHA_RESTRICTION_END M_PI/2 #define E_RESTRICTION_END 3E-9 #define V_RESTRICTION_END 2000 #define B_RESTRICTION_END 0.025 using namespace std; class ComputeObject{ protected: double F; virtual void input()=0; virtual void output()=0; virtual void parameterCalculation()=0; }; class Charge:public ComputeObject{ double temp,temp1,temp2,temp3; // временные переменные bool flag; bool flag1; bool flag2; bool flag3; bool flag4; protected: double e,V,B,alpha; void rangeCheck(double alpha, double e, double V, double B){ // проверка на ввод , если хоть 1 данное ведено не верно выводиться сообщение об ошибке , // и поле которое не правильно введено.Если всё верно данные с // временной переменной // переносятся в нужные flag = false; // устанавливаем все флаги как false flag1 = false; flag2 = false; flag3 = false; flag4 = false; if (alpha>= ALPHA_RESTRICTION_START && alpha <= ALPHA_RESTRICTION_END) { this->alpha= alpha; } else { flag = true; flag1 = true; } if (e>= E_RESTRICTION_START && e<= E_RESTRICTION_END) { this->e = e; } else { flag = true; flag2 = true; } if (V >= V_RESTRICTION_START && V <= V_RESTRICTION_END) { this->V = V; } else { flag = true; flag3 = true; } if (B >= B_RESTRICTION_START && B<= B_RESTRICTION_END) { this->B = B; } else { flag = true; flag4 = true; } if (flag) { errorMessege(); exit(1); // что бы после ошибки программа завершалась } } void errorMessege() { cout << "Ошибка! Данные не соответсвуют заданым диапазонам:" << endl; if (flag1) { cout << "\tПоле \"alpha\" Введено не верно.Введите данные в диапазоне от " << ALPHA_RESTRICTION_START << " до " << ALPHA_RESTRICTION_END << endl; } if (flag2) { cout << "\tПоле \"e\" Введено не верно.Введите данные в диапазоне от "<< E_RESTRICTION_START <<" до " << E_RESTRICTION_END << endl; } if (flag3) { cout << "\tПоле \"V\" Введено не верно.Введите данные в диапазоне от "<< V_RESTRICTION_START << " до " << V_RESTRICTION_END << endl; } if (flag4) { cout << "\tПоле \"B\" Введено не верно.Введите данные в диапазоне от "<< B_RESTRICTION_START <<" до " << B_RESTRICTION_END << endl; } }; public: void output()override{ ofstream fout("./../result.txt",ios_base::app); if (!fout.is_open()) { cout << "Файл не может быть открыт для записи!\n"; exit(1); // если файл не открился закрываем программу. }else{ /* fout.setf(ios_base::fixed); Эксопненциальную в обычную форму fout.precision(10);*/ fout << "Начальные данные: "<<endl; fout << "Alpha = " <<alpha <<endl; fout << "e = " << e <<endl; fout << "V = " << V <<endl; fout << "B = " << B <<endl; fout << "Результат: " << endl; fout << "F = " << F << endl; fout << "----------------------------------------------------------------------------------------------"<<endl; fout.close(); } } void parameterCalculation()override{ F= alpha*e*V*B; } void input ()override{ ifstream fin(R"(C:\Users\User\CLionProjects\Kursach_Max_Krugvovykh\hello.txt)",ios_base::in); if (!fin.is_open()) { cout << "Файл не может быть открыт!\n"; exit(1); // если файл не открылся закрываем программу. } else{ fin >> temp; fin >> temp1; fin >> temp2; fin >> temp3; rangeCheck(temp, temp1, temp2, temp3); } } Charge(double e, double V, double B, double alpha); Charge() {}; Charge(const Charge &other){ // конструктор копирования this->e = other.e; this->B = other.B; this->V = other.V; this->alpha = other.alpha; } }; Charge::Charge(double alpha, double e, double V, double B) { rangeCheck(alpha, e, V, B); }; int main() { cin.get(); return 0; } 

Help render the functions rangeCheck() and errorMessege() into a separate class MyError so as not to violate the functional, for I have already tried everything that I know :(

  • And inheritance too? - Adokenai
  • I tried with inheritance but I get lost in the assignment area then (this-> alpha, for example) because it doesn't work like that - Max Krugovykh
  • Protected to the data and put into one of the parent classes. For example, all flag * should obviously be in MyError. There is one thing: the constructor does not return the result, so the selection in a separate class may be unjustified. It is better to look towards exceptions (throw). - Adokenai
  • with exceptions not familiar. decided to bring into a separate class as logically these methods have no place in the class change - Max Krugovykh
  • Now there is a reason to study. There you can cook up your class. But with regard to exceptions, I will not help anymore, because I myself use the "ostrich" method or in the exclusion environment are not available and every sneeze must be foreseen. - Adokenai

0