Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string.h>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     char ignoreSpaces, saveTextInFile, text[256];
  11.     int spaceCounter = 0, lineCounter = 0;
  12.     ofstream file("TriangleText.txt");
  13.  
  14.     cout << "Enter the text from which you want to make a triangle!" << endl << "> ";
  15.     cin.getline(text, sizeof(text));
  16.  
  17.     for (unsigned int i = 0; i < strlen(text); ++i)
  18.         if (text[i] == ' ')
  19.             ++spaceCounter;
  20.  
  21.     if (spaceCounter == 1)
  22.         cout << endl << "The text contains a space character. Do you want to ignore it? (Y/N): ";
  23.  
  24.     if (spaceCounter > 1)
  25.         cout << endl << "The text contains " << spaceCounter << " space characters. Do you want to ignore them? (Y/N): ";
  26.  
  27.     while ((spaceCounter >= 1) && (ignoreSpaces != 'y' && ignoreSpaces != 'Y' && ignoreSpaces != 'n' && ignoreSpaces != 'N'))
  28.         cin >> ignoreSpaces;
  29.  
  30.     cout << endl << "-------------------------------------------------" << endl;;
  31.  
  32.     clock_t begin = clock();
  33.  
  34.     for (unsigned int i = 0; i < strlen(text); ++i)
  35.     {
  36.         if (ignoreSpaces == 'y' || ignoreSpaces == 'Y')
  37.             while (text[i] == ' ')
  38.                 ++i;
  39.  
  40.         for (unsigned int j = 0; j <= i; ++j)
  41.         {
  42.             cout << text[j];
  43.             file << text[j];
  44.         }
  45.  
  46.         cout << endl;
  47.         file << endl;
  48.  
  49.         ++lineCounter;
  50.     }
  51.  
  52.     for (int i = strlen(text) - 2; i >= 0; --i)
  53.     {
  54.         if (ignoreSpaces == 'y' || ignoreSpaces == 'Y')
  55.             while (text[i] == ' ')
  56.                 --i;
  57.  
  58.         for (int j = 0; j <= i; ++j)
  59.         {
  60.             cout << text[j];
  61.             file << text[j];
  62.         }
  63.  
  64.         cout << endl;
  65.         file << endl;
  66.  
  67.         ++lineCounter;
  68.     }
  69.  
  70.     clock_t end = clock();
  71.     double elapsedSeconds = double(end - begin) / CLOCKS_PER_SEC;
  72.  
  73.     cout << "-------------------------------------------------" << endl;
  74.     cout << lineCounter<<" line(s) created in " << elapsedSeconds << " seconds." << endl;
  75.     cout << "To copy the text to clipboard highlight it and press CTRL+C buttons." << endl << endl;
  76.  
  77.     cout << "Do you want to save the triangle in a text file? (Y/N): ";
  78.     while (saveTextInFile != 'y' && saveTextInFile != 'Y' && saveTextInFile != 'n' && saveTextInFile != 'N')
  79.     {
  80.         cin >> saveTextInFile;
  81.         switch (saveTextInFile)
  82.         {
  83.             case 'Y':
  84.             case 'y': cout << "File saved." << endl << endl; break;
  85.             case 'N':
  86.             case 'n': file.close(); remove("TriangleText.txt"); cout << "File deleted." << endl << endl; break;
  87.         }
  88.     }
  89.  
  90.     cout << "Press [Enter] to continue . . . ";
  91.     getchar(); getchar();
  92.  
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement