Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<cmath>
- #include<fstream>
- #include<string>
- #include<vector>
- using namespace std;
- class Quadratic {
- public:
- Quadratic(double a, double b, double c);
- ~Quadratic();
- void Solution();
- private:
- double *x;
- double a;
- double b;
- double c;
- };
- Quadratic::Quadratic(double a, double b, double c) {
- x = new double[2];
- this->a = a;
- this->b = b;
- this->c = c;
- };
- Quadratic::~Quadratic() {
- delete[]x;
- };
- void Quadratic::Solution() {
- if (a == 0) {
- if (b == 0) {
- if (c == 0) {
- cout << "х - любой число!" << endl;
- }
- else {
- cout << "c!=0\n" << "Нет решений!" << endl;
- };
- }
- else {
- if (c == 0) {
- x[0] = (c / b);
- cout << "x=" << x[0] << endl;
- }
- else {
- x[0] = -(c / b);
- cout << "x=" << x[0] << endl;
- }
- };
- }
- else {
- double D;
- D = (b*b) - 4 * a*c;
- if (D >= 0) {
- if (b == 0) {
- if (c == 0) {
- cout << "x1=x2=0" << endl;
- }
- else {
- x[0] = (sqrt(D)) / (2 * a);
- x[1] = (-sqrt(D)) / (2 * a);
- cout << "x1=" << x[0] << endl << "x2=" << x[1] << endl;
- };
- }
- else {
- x[0] = (-b + sqrt(D)) / (2 * a);
- x[1] = (-b - sqrt(D)) / (2 * a);
- cout << "x1=" << x[0] << endl << "x2=" << x[1] << endl;
- }
- }
- else {
- if (b == 0) {
- cout << "x1=" << "+i*" << sqrt(abs(D)) / (2 * a) << endl;
- cout << "x2=" << "-i*" << sqrt(abs(D)) / (2 * a) << endl;
- }
- else {
- cout << "x1=" << (-b) / (2 * a) << "+i*" << sqrt(abs(D)) / (2 * a) << endl;
- cout << "x2=" << (-b) / (2 * a) << "-i*" << sqrt(abs(D)) / (2 * a) << endl;
- };
- };
- };
- };
- int main()
- {
- double a;
- double b;
- double c;
- setlocale(LC_ALL, "ru");
- int menu;
- bool t = true;
- while (t) {
- cout << "ax^2+bx+c=0" << endl;
- cout << "1.Ввести коэффициенты с клавиатуры" << endl;
- cout << "2.Взять коэффициенты из файла" << endl;
- cout << "0.Выход" << endl;
- cin >> menu;
- switch (menu)
- {
- case 1: {
- system("cls");
- for (int i = 0; i < 3; i++)
- {
- if (i == 0) {
- cout << "Введите коэффициент а" << endl;
- cin >> a;
- }
- if (i == 1) {
- cout << "Введите коэффициент b" << endl;
- cin >> b;
- }
- if (i == 2) {
- cout << "Введите коэффициент c" << endl;
- cin >> c;
- };
- }
- Quadratic equation(a, b, c);
- equation.Solution();
- break;
- }
- case 2: {
- system("cls");
- fstream fs;
- fs.open("Text.txt", fstream::in);
- if (!fs.is_open()) {
- cout << "Файл не найден" << endl;
- }
- else {
- /*string str="";
- while (!fs.eof()) {
- getline(fs, str);
- if (str.find('a') != string::npos&&str.find(';') != string::npos) {
- a = stod(str.substr(str.find('=')+1, str.find(';') - (str.find('=')+1)));
- }
- if (str.find('b') != string::npos&&str.find(';') != string::npos) {
- b = stod(str.substr(str.find('=') + 1, str.find(';') - (str.find('=') + 1)));
- }
- if (str.find('c') != string::npos&&str.find(';') != string::npos) {
- c = stod(str.substr(str.find('=') + 1, str.find(';') - (str.find('=') + 1)));
- }
- cout << str << endl;
- }*/
- string str = "";
- char ch;
- vector<int> values;
- fstream fs;
- fs.open("Text.txt", fstream::in);
- while (!fs.eof()) {
- fs.get(ch);
- str = str + ch;
- if (ch == ' ') {
- if (str == " " || str == "" || str == "\0") {
- }
- else {
- values.push_back(stoi(str));
- str = "";
- }
- };
- }
- auto it = values.begin();
- if (values.size() < 3) {
- cout << "Недостаточно коэффициентов" << endl;
- }
- else {
- a = *it;
- ++it;
- b = *it;
- ++it;
- c = *it;
- values.erase(values.begin(),it);
- }
- Quadratic equation(a, b, c);
- equation.Solution();
- }
- break;
- }
- default:
- t = false;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement