Advertisement
asdfg0998

fdsfas

Sep 15th, 2024
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <sstream>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8.  
  9. // Function to left-align a paragraph
  10. string leftAlign(const vector<string>& paragraph, int width) {
  11.     string result;
  12.     int len = 0;
  13.     for (const string& word : paragraph) {
  14.         if (len + word.length() <= width) {
  15.             if (len > 0) result += " ";
  16.             result += word;
  17.             len += word.length() + 1;
  18.         }
  19.     }
  20.     result += string(width - len + 1, ' '); // Fill remaining space
  21.     return result;
  22. }
  23.  
  24. // Function to right-align a paragraph
  25. string rightAlign(const vector<string>& paragraph, int width) {
  26.     string result;
  27.     int len = 0;
  28.     for (const string& word : paragraph) {
  29.         if (len + word.length() <= width) {
  30.             if (len > 0) result += " ";
  31.             result += word;
  32.             len += word.length() + 1;
  33.         }
  34.     }
  35.     int spacePadding = width - (len - 1);  // Calculate remaining space
  36.     result = string(spacePadding, ' ') + result;
  37.     return result;
  38. }
  39.  
  40. // Function to center-align a paragraph
  41. string centerAlign(const vector<string>& paragraph, int width) {
  42.     string result;
  43.     int len = 0;
  44.     for (const string& word : paragraph) {
  45.         if (len + word.length() <= width) {
  46.             if (len > 0) result += " ";
  47.             result += word;
  48.             len += word.length() + 1;
  49.         }
  50.     }
  51.     int totalSpaces = width - (len - 1);
  52.     int leftPadding = totalSpaces / 2;
  53.     int rightPadding = totalSpaces - leftPadding;
  54.     result = string(leftPadding, ' ') + result + string(rightPadding, ' ');
  55.     return result;
  56. }
  57.  
  58. // Function to format the entire page with borders
  59. void formatPage(const vector<vector<string>>& paragraphs, const vector<string>& aligns, int width) {
  60.     string border = string(width + 2, '*');  // Create top and bottom border
  61.     cout << border << endl;  // Print top border
  62.  
  63.     for (size_t i = 0; i < paragraphs.size(); ++i) {
  64.         string formattedLine;
  65.         if (aligns[i] == "LEFT") {
  66.             formattedLine = leftAlign(paragraphs[i], width);
  67.         } else if (aligns[i] == "RIGHT") {
  68.             formattedLine = rightAlign(paragraphs[i], width);
  69.         } else if (aligns[i] == "CENTER") {
  70.             formattedLine = centerAlign(paragraphs[i], width);
  71.         }
  72.         cout << "*" << formattedLine << "*" << endl;  // Add left and right borders
  73.     }
  74.  
  75.     cout << border << endl;  // Print bottom border
  76. }
  77.  
  78. int main() {
  79.     vector<vector<string>> paragraphs = {
  80.         {"hello", "world"},
  81.         {"How", "areYou", "doing"},
  82.         {"Please look", "and align", "to right"}
  83.     };
  84.    
  85.     vector<string> aligns = {"LEFT", "RIGHT", "RIGHT"};
  86.     int width = 16;
  87.  
  88.     formatPage(paragraphs, aligns, width);
  89.  
  90.     return 0;
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement