Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- void PrintCondition()
- {
- std::cout << "Goal: using a recursive function, find the square root with a given accuracy using Newton's iterative formula.\n";
- }
- void PrintResult(std::ostream& outStream, double y,
- std::string header)
- {
- if (y != 0)
- {
- outStream << header << y;
- }
- }
- double InputNumUnlimited()
- {
- double number;
- bool isCorrect = false;
- do
- {
- std::cin >> number;
- if (std::cin.fail())
- {
- std::cin.clear();
- std::cerr << "\nUnexpected symbol entered\n";
- while (std::cin.get() != '\n');
- }
- else if (std::cin.get() != '\n')
- {
- std::cout << "\nIncorrect value\n";
- while (std::cin.get() != '\n');
- }
- else
- {
- isCorrect = true;
- }
- } while (!isCorrect);
- return number;
- }
- double InputNumber(double min, double max)
- {
- double number;
- bool isCorrect = false;
- do
- {
- number = InputNumUnlimited();
- if (number < min)
- {
- std::cerr << "\nMinimal allowed value is " << min << "\n";
- }
- else if (number > max)
- {
- std::cerr << "\nMaximum allowed value is " << max << "\n";
- }
- else
- {
- isCorrect = true;
- }
- } while (!isCorrect);
- return number;
- }
- char ReadCondition()
- {
- char condition;
- std::cout << "\nWould you like to enter matrix manually [m] or get from file [f] ? \n";
- bool selected = false;
- do
- {
- std::cin >> condition;
- if (condition != 'm' && condition != 'f')
- {
- while (std::cin.get() != '\n');
- std::cout << "\nPlease type [m] or [f]\n";
- }
- else
- {
- if (std::cin.get() != '\n')
- {
- while (std::cin.get() != '\n');
- std::cout << "\nPlease type [m] or [f]\n";
- }
- else
- {
- selected = true;
- }
- }
- } while (!selected);
- return condition;
- }
- double InputValue(std::string text, int min, int max)
- {
- double value;
- std::cout << "\nInput " + text;
- value = InputNumber(min, max);
- return value;
- }
- double CountRoot(double yi1, double x, double eps)
- {
- double yi;
- yi = 0.5 * (yi1 + x / yi1);
- if (abs(yi - yi1) < eps)
- {
- return yi;
- }
- else
- {
- return CountRoot(yi, x, eps);
- }
- }
- double InputSourceValuesManually()
- {
- double y;
- double y0 = 1;
- double x;
- double eps;
- constexpr int imin = std::numeric_limits<int>::min();
- constexpr int imax = std::numeric_limits<int>::max();
- x = InputValue("X (1..10000): ", 1, 10000);
- eps = InputValue("EPS (0<EPS<1): ", 0, 1);
- y = CountRoot(y0, x, eps);
- return y;
- }
- bool IsFileExist(std::string fileName)
- {
- std::ifstream in(fileName);
- return in.good();
- }
- bool HasTxtExtension(std::string fileName)
- {
- int length = fileName.length();
- if (length < 5)
- {
- return false;
- }
- if (fileName[length - 4] != '.')
- {
- return false;
- }
- if (fileName[length - 3] != 't')
- {
- return false;
- }
- if (fileName[length - 2] != 'x')
- {
- return false;
- }
- if (fileName[length - 1] != 't')
- {
- return false;
- }
- }
- std::string InputFilePath(bool checkExistence)
- {
- std::string fileName;
- bool isCorrect = false;
- do
- {
- std::cout << "\nInput file path (*.txt):\n";
- std::getline(std::cin, fileName);
- if (fileName.length() < 5)
- {
- std::cerr << '\n' << "invalid file name\n";
- }
- else if (!HasTxtExtension(fileName))
- {
- std::cerr << '\n' << "invalid file extension \n";
- }
- else if (checkExistence && !IsFileExist(fileName))
- {
- std::cerr << '\n' << fileName << " is not found \n";
- }
- else
- {
- isCorrect = true;
- }
- } while (!isCorrect);
- return fileName;
- }
- double GetValue(std::ifstream& inStream)
- {
- double value = 0;
- try
- {
- inStream >> value;
- if (inStream.fail())
- {
- return 0;
- }
- else
- {
- return value;
- }
- }
- catch (...)
- {
- return 0;
- }
- }
- double GetSourceValuesFromFile()
- {
- double x;
- double eps;
- double y;
- double y0 = 1;
- std::string filename;
- filename = InputFilePath(true);
- try
- {
- std::ifstream in(filename);
- if (in.is_open())
- {
- x = GetValue(in);
- if ((x < 1) || (x > 10000))
- {
- std::cerr << "\nReading X's error. Not correct value.";
- in.close();
- return 0;
- }
- eps = GetValue(in);
- if ((eps < 0.0001) || (eps >= 1))
- {
- std::cerr << "\nReading EPS's error. Not correct value.";
- in.close();
- return 0;
- }
- if (not(in.eof()))
- {
- std::cerr << "\nToo much data.";
- return 0;
- }
- std::cout << "\nX: " << x;
- std::cout << "\nEPS: " << eps;
- y = CountRoot(y0, x, eps);
- return y;
- in.close();
- }
- }
- catch (...)
- {
- std::cerr << "Read error.";
- }
- }
- double GetRealValues()
- {
- char condition = ReadCondition();
- if (condition == 'm')
- {
- return InputSourceValuesManually();
- }
- else
- {
- return GetSourceValuesFromFile();
- }
- }
- bool SaveResult(std::string filename, double y)
- {
- std::ofstream outf(filename);
- if (!outf)
- {
- std::cerr << filename << " could not be opened for writing!\n";
- return false;
- }
- PrintResult(outf, y, "\nRoot of the equation : ");
- outf.close();
- return true;
- }
- void InputFilePathAndSaveResult(double y)
- {
- if (y != 0)
- {
- std::string path;
- bool isCorrect = false;
- do
- {
- path = InputFilePath(false);
- isCorrect = SaveResult(path, y);
- } while (!isCorrect);
- std::cout << "\nCompleted!\n";
- }
- }
- int main()
- {
- double y;
- std::string outFilename;
- PrintCondition();
- y = GetRealValues();
- PrintResult(std::cout, y, "Root of the equation: ");
- InputFilePathAndSaveResult(y);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement