Advertisement
venik2405

lab2_5

Nov 22nd, 2020
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1. #include <fstream>
  2. #include <string>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. void chooseInput(int& inputType)
  8. {
  9.     bool incorrect;
  10.     string line;
  11.     do
  12.     {
  13.         incorrect = false;
  14.         cout << "Do you want to input from file? (y/n)" << endl;
  15.         cin >> line;
  16.         if (line != "Y" && line != "y" && line != "N" && line != "n")
  17.         {
  18.             incorrect = true;
  19.             cout << "Enter valid answer" << endl;
  20.         }
  21.     } while (incorrect);
  22.     if (line == "N" || line == "n")
  23.         inputType = 1;
  24.     else
  25.         inputType = 0;
  26. }
  27.  
  28. string inputFileLocation()
  29. {
  30.     bool incorrect = false;
  31.     string location = "";
  32.     cout << "Enter file location:" << endl;
  33.     cin >> location;
  34.     ifstream file(location);
  35.     if (file.is_open())
  36.     {
  37.         cout << "File opened successfully\n";
  38.         return location;
  39.     }
  40.     else
  41.     {
  42.         cout << "File with this location is not found" << endl;
  43.     }
  44.     file.close();
  45. }
  46.  
  47. int getSizeFromFile()
  48. {
  49.     int size = 0;
  50.     string line;
  51.     ifstream file(inputFileLocation());
  52.     file.clear();
  53.     file.seekg(0, ios::beg);
  54.     file >> size;
  55.     getline(file, line, '\n');
  56.     file.close();
  57.     return size;
  58. }
  59.  
  60. void outputToFile(int** triangle, int size)
  61. {
  62.     ofstream file(inputFileLocation());
  63.     for (int i = 0; i < size; i++)
  64.     {
  65.         for (int j = 0; j < i; j++)
  66.         {
  67.             file << triangle[i][j] << endl;
  68.         }
  69.         cout << endl;
  70.     }
  71. }
  72.  
  73. void createTriangle(int size, int** triangle) {
  74.     for (int n = 0; n <= size; n++) {
  75.         triangle[n][0] = triangle[n][n] = 1;
  76.         for (int k = 1; k < n; k++) {
  77.             triangle[n][k] = triangle[n - 1][k - 1] + triangle[n - 1][k];
  78.         }
  79.     }
  80. }
  81.  
  82. int getSizeFromConsole()
  83. {
  84.     const int MAX_VALUE = 30;
  85.     const int MIN_VALUE = 1;
  86.     int size = 0;
  87.     bool isIncorrect;
  88.     do
  89.     {
  90.         cin >> size;
  91.         isIncorrect = false;
  92.         if (size < MIN_VALUE || size > MAX_VALUE)
  93.         {
  94.             isIncorrect = true;
  95.             cout << "Please enter a natural value less than six\n";
  96.         }
  97.     } while (isIncorrect);
  98.     return size;
  99. }
  100.  
  101. void printTriangle(int** triangle, int size)
  102. {
  103.     for (int i = 0; i < size + 1; i++)
  104.     {
  105.         for (int j = 0; j < i; j++)
  106.             cout << triangle[i][j] << " ";
  107.         cout << endl;
  108.     }
  109. }
  110.  
  111. int main()
  112. {
  113.     setlocale(LC_ALL, "RUSSIAN");
  114.     cout << "Данная программа строит треугольник Паскаля заданного размера.\n";
  115.     int chosenInput;
  116.     int size = 0;
  117.     chooseInput(chosenInput);
  118.     cout << "Enter the size of Pascal`s triangle\n";
  119.     if (chosenInput == 0)
  120.     {
  121.         size = getSizeFromFile();
  122.     }
  123.     else
  124.     {
  125.         size = getSizeFromConsole();
  126.     }
  127.     cout << endl;
  128.     int** triangle = new int* [size + 1];
  129.     for (int i = 0; i < size + 1; i++)
  130.         triangle[i] = new int [size + 1];
  131.     cout << "Triangle:" << endl;
  132.     createTriangle(size, triangle);
  133.     cout << endl << "Pascal`s triangle" << endl;
  134.     printTriangle(triangle, size);
  135.     outputToFile(triangle, size);
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement