Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string.h>
- #include <ctime>
- using namespace std;
- int main()
- {
- char ignoreSpaces, saveTextInFile, text[256];
- int spaceCounter = 0, lineCounter = 0;
- ofstream file("TriangleText.txt");
- cout << "Enter the text from which you want to make a triangle!" << endl << "> ";
- cin.getline(text, sizeof(text));
- for (unsigned int i = 0; i < strlen(text); ++i)
- if (text[i] == ' ')
- ++spaceCounter;
- if (spaceCounter == 1)
- cout << endl << "The text contains a space character. Do you want to ignore it? (Y/N): ";
- if (spaceCounter > 1)
- cout << endl << "The text contains " << spaceCounter << " space characters. Do you want to ignore them? (Y/N): ";
- while ((spaceCounter >= 1) && (ignoreSpaces != 'y' && ignoreSpaces != 'Y' && ignoreSpaces != 'n' && ignoreSpaces != 'N'))
- cin >> ignoreSpaces;
- cout << endl << "-------------------------------------------------" << endl;;
- clock_t begin = clock();
- for (unsigned int i = 0; i < strlen(text); ++i)
- {
- if (ignoreSpaces == 'y' || ignoreSpaces == 'Y')
- while (text[i] == ' ')
- ++i;
- for (unsigned int j = 0; j <= i; ++j)
- {
- cout << text[j];
- file << text[j];
- }
- cout << endl;
- file << endl;
- ++lineCounter;
- }
- for (int i = strlen(text) - 2; i >= 0; --i)
- {
- if (ignoreSpaces == 'y' || ignoreSpaces == 'Y')
- while (text[i] == ' ')
- --i;
- for (int j = 0; j <= i; ++j)
- {
- cout << text[j];
- file << text[j];
- }
- cout << endl;
- file << endl;
- ++lineCounter;
- }
- clock_t end = clock();
- double elapsedSeconds = double(end - begin) / CLOCKS_PER_SEC;
- cout << "-------------------------------------------------" << endl;
- cout << lineCounter<<" line(s) created in " << elapsedSeconds << " seconds." << endl;
- cout << "To copy the text to clipboard highlight it and press CTRL+C buttons." << endl << endl;
- cout << "Do you want to save the triangle in a text file? (Y/N): ";
- while (saveTextInFile != 'y' && saveTextInFile != 'Y' && saveTextInFile != 'n' && saveTextInFile != 'N')
- {
- cin >> saveTextInFile;
- switch (saveTextInFile)
- {
- case 'Y':
- case 'y': cout << "File saved." << endl << endl; break;
- case 'N':
- case 'n': file.close(); remove("TriangleText.txt"); cout << "File deleted." << endl << endl; break;
- }
- }
- cout << "Press [Enter] to continue . . . ";
- getchar(); getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement