Text Justification
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Text Justification

Threaded View

  1. #1
    Join Date
    Nov 2013
    Posts
    6

    Question Text Justification

    I need some guidance in this code,how can i start off with. As there is not much material on internet about this type of code so i need some help from you guys. thanks.

    Code:
                         <<Header file>
    
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include "ctype.h"
    
    using namespace std;
    
    // some useful type definitions
    typedef vector<string> PARA;
    typedef unsigned int ERROR;
    
    #define MIN_COL_WIDTH 20
    #define MAX_COL_WIDTH 60
    
    // error codes
    #define ERROR_NONE 0
    #define ERROR_NO_CHARS 1
    #define ERROR_ILLEGAL_WIDTH 2
    
    // function prototype
    ERROR justify(unsigned int width, string in, PARA *out);
    // width is the desired width of line in output (justified) text.
    // "in" is the input string.
    // PARA is a vector of strings, where each element is one line
    // in the output justified text.
    // The first element of the PARA vector is the first line of the 
    // output text, the second element is the second line, and so on.
    // note that output text variable is declared in main function.
    // it is passed by reference to "justify".
    // The function returns one of the above error codes.
    
                             <<Main file>>
    #include "justify.h"
    
    int main() {
    	ERROR result;
    	unsigned int width;
    	string input;
    	PARA output;
    
    	while (1) {
    		// first clear the output string
    		output.clear();
    		cout << endl<< "Welcome to text justification!" << endl << endl;
    		cout << "Enter the output line width (between " << MIN_COL_WIDTH << " and " << MAX_COL_WIDTH << ", or 0 to exit): ";
    		cin >> width;
    		if (width == 0)
    			break;
    		cout << "Enter the string to justify below:" << endl << endl;
    
    		// read in the input string 
    		cin.ignore();
    
    		getline(cin, input);
    
    		// justify the input for given column width
    		result = justify(width, input, &output);
    
    		switch(result) {
    			case ERROR_NONE: 
    				cout << endl << "The justified text is:" << endl << endl;
    				for (unsigned int i = 0; i < output.size(); i++)
    					cout << output[i] << endl;
    				break;
    			case ERROR_NO_CHARS:
    				cout << "ERROR: No characters in input para!" << endl;
    				break;
    			case ERROR_ILLEGAL_WIDTH:
    				cout << "ERROR: Column width out of range!" << endl;
    				break;
    			default:
    				cout << "ERROR: Internal error!" << endl;
    				break;
    		}
    	} 
    }
    
    
                             <<Implementation file>>
    
    #include "justify.h"
    
    // implement the justify function below
    // use push_back method of string class to insert a new character
    // at the end of a string
    // You can terminate a string by calling push_back on it with '\0'.
    // Use the clear method of string class to remove all the
    // characters from the string.
    
    ERROR justify(unsigned int width, string in, PARA *out) {
    
    	
    	//  make sure that column width is valid
    
    	// make sure that the input is not empty string
    
    	// justify the text!
    
    } // end function justify
    Last edited by VictorN; November 28th, 2013 at 02:22 AM. Reason: redundant code tags were removed

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center